SitePoint Sponsor |
|
User Tag List
Results 1 to 19 of 19
-
Feb 2, 2009, 09:00 #1
- Join Date
- Feb 2007
- Posts
- 69
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP/MySQL Limit The Length of the Result
Hi Guys,
Im pretty new to using MySQL within PHP and have come across a problem. I wondered if it is possible to limit the number of characters or words displayed from a result.
What I am trying to do is have a description which only displays the first say 50 words, the user can then click onto the item to view the full description!
I am just not sure of what SQL code I need to use!
I have tried searching the net, but the only results I get are to do with limiting the number of results returned, not limiting the length of the results themselves.
Any help would be greatly appreciated!
Regards
Shaun
-
Feb 2, 2009, 09:02 #2
- Join Date
- Apr 2006
- Location
- Maryland
- Posts
- 1,838
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Feb 2, 2009, 09:02 #3
- Join Date
- Dec 2008
- Location
- Australia
- Posts
- 389
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I have exactly what you want, view an example on my site:
http://www.30.com.au/portfolio (like this page?)
Let me grab my code for you, two secs:-)
-
Feb 2, 2009, 09:04 #4
- Join Date
- Dec 2008
- Location
- Australia
- Posts
- 389
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Heres my code:
PHP Code:<?echo substr($pf['description'],0,50);?>....(<a href="/portfolio/<?echo$pf['id'];?>/<?echo$pf['thumb'];?>" title="<?echo$pf['name'];?>" class="navy">more</a>)
This is a PHP function, not mySQL - for future reference, most things you do on your website that involve your database can probably be done with PHP functions.:-)
-
Feb 2, 2009, 09:07 #5
- Join Date
- Dec 2008
- Posts
- 120
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
LEFT mysql function
-
Feb 2, 2009, 09:08 #6
- Join Date
- Jul 2002
- Location
- Toronto, Canada
- Posts
- 39,347
- Mentioned
- 63 Post(s)
- Tagged
- 3 Thread(s)
i prefer to use SUBSTRING_INDEX to pull, say, the first 20 full words (so as not to break off in mid-word)
SELECT SUBSTRING_INDEX(somecolumn, ' ', 20) ...
-
Feb 2, 2009, 09:08 #7
If you don't want to cut words in half, take a look at wordwrap.
Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Feb 2, 2009, 09:10 #8
- Join Date
- Jul 2002
- Location
- Toronto, Canada
- Posts
- 39,347
- Mentioned
- 63 Post(s)
- Tagged
- 3 Thread(s)
except for those things which require sending a lot of data to the php client and then using only some of it
like returning only the latest entry in a blog (instead of all entries), computing the number of entries in each category (instead of counting them with SQL), sending an entire TEXT column value when you only want the first 20 words, etc.
-
Feb 2, 2009, 09:12 #9
-
Feb 2, 2009, 09:13 #10
- Join Date
- Apr 2006
- Location
- Maryland
- Posts
- 1,838
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Feb 2, 2009, 09:14 #11
-
Feb 2, 2009, 09:18 #12
- Join Date
- Jul 2002
- Location
- Toronto, Canada
- Posts
- 39,347
- Mentioned
- 63 Post(s)
- Tagged
- 3 Thread(s)
-
Feb 2, 2009, 09:19 #13
- Join Date
- Jul 2002
- Location
- Toronto, Canada
- Posts
- 39,347
- Mentioned
- 63 Post(s)
- Tagged
- 3 Thread(s)
-
Feb 2, 2009, 09:23 #14
- Join Date
- Dec 2008
- Location
- Australia
- Posts
- 389
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm just saying, say someone wanted:
Description Part 1
Show first 50 of description
Description Part 2
Show second 50 of description
Description Part 3
Show third 50 of description
Say they wanted to split text up into 3 tables, instead of using 3 queries, they could use 3 functions, eliminated lines of code and query time.:-)
-
Feb 2, 2009, 09:26 #15
- Join Date
- Apr 2006
- Location
- Maryland
- Posts
- 1,838
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Feb 2, 2009, 09:27 #16
- Join Date
- Feb 2007
- Posts
- 69
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thank you for your fast responses!
I have implemented the 'substr' option and it works great!
I knew it would be something rather simple!
You can see it in action here:
http://www.glenlucecaravansales.co.uk/dev
(Excuse the styling on the pop-up windows, I ain't got that far yet)
Again, Thank you very much!
Shaun
-
Feb 2, 2009, 09:29 #17
- Join Date
- Jul 2002
- Location
- Toronto, Canada
- Posts
- 39,347
- Mentioned
- 63 Post(s)
- Tagged
- 3 Thread(s)
-
Feb 2, 2009, 09:33 #18Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Feb 2, 2009, 09:36 #19
- Join Date
- Dec 2008
- Location
- Australia
- Posts
- 389
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
A lot of people that would read the description would get to the end and click more anyway, never mind if the last word has completed or not.
:-)
Bookmarks