Jump to content
Sign in to follow this  
rootKID

how to make (...) after (x) words outputted?...

Recommended Posts

hello again,just a fast question... if i have somfthing like a little bit over 500 Words in a DB... and i am outputtin them with a normal SELECT in the query, is there not somfthing i can do in the query so that it can only show like 100 words, and if more, then in the end of the word(s), there will be added this: (...) to look more nicer in the design, thanks in advance! :)...

Share this post


Link to post
Share on other sites

just make a function where you pass the string, and a max length. if the string is longer than the max length, trim the string to the max length - 3 (search the PHP manual for it's available string functions), then add the ... and return the formatted string. if the string is less than the max length, just return the string that was originally passed in.

Edited by thescientist

Share this post


Link to post
Share on other sites

ok, first time im making this, so just to clarifie. 1. im going to make a function that will see if the (text inside db) is longer then XXX words...2. if so, then run the function and trim it with the (...) in the end...3. if not longer, just add original text... right understood or missing somfthing?... PS: will not be replying before tomorrow... going to bed now, have a long day ahead of me tomorrow... see ya by then :).

Share this post


Link to post
Share on other sites

If you're trying to control the size of the text then you probably want to use characters instead of words. If you want to use characters then you would use things like strlen and substr, if you want to use words then you would explode the string into an array of words and rebuilt it with the number of words you want.

Share this post


Link to post
Share on other sites
ok, first time im making this, so just to clarifie. 1. im going to make a function that will see if the (text inside db) is longer then XXX words...2. if so, then run the function and trim it with the (...) in the end...3. if not longer, just add original text... right understood or missing somfthing?... PS: will not be replying before tomorrow... going to bed now, have a long day ahead of me tomorrow... see ya by then :).
I would just pass every string to the function, and let it handle the logic of whether to add ellipses or not. The function will either return the formatted string or the original one, based on the max length condition.

Share this post


Link to post
Share on other sites

ok, last reply for the day... i wish not to controle the size of text, but on how many (words/text) there is comming out. and besides, i (do) need the (...)... its a part of our school projects, so ill better add it :P..

Share this post


Link to post
Share on other sites

With MySQL you can use the left() function to get/return the left most certain amount of characters you specify.. So for example:

$query = "SELECT LEFT('description', 50) as  'description' FROM 'product_listing' WHERE 'id' = 5";

This would select the left most 50 characters from the column 'description' from the row of an id of 5. There's also a right() function as well. http://dev.mysql.com...-functions.html Then once you do that, on the page that will display the results... just have (....) after. The (....) can be a link too where when the user clicks on it, it takes them to a page fully displaying the result(s), meaning the rest of the characters/words.

Edited by Don E

Share this post


Link to post
Share on other sites

ok... tried your way Don E. this is my SQL:

SELECTLEFT ('produkter.information', 50) AS 'teksten',produkter.produkt_navn,image_url.img_url,produkter.id,serie.serie_navnFROMprodukterLEFT OUTER JOIN image_url ON image_url.FK_produkt_id = produkter.idLEFT OUTER JOIN serie ON produkter.FK_serie = serie.id

and this is how im taking it out:

echo "<p>".$row['teksten']."</p>"

but getting this as text: 70cp75.png ideas?....

Share this post


Link to post
Share on other sites

and this is how im taking it out:

echo "<p>".$row['teksten']."</p>"but getting this as text:
what do you else expect? Edited by birbal

Share this post


Link to post
Share on other sites

ok, ive finded out myself... just used the (strlen) function to check with a if/else statement that if it was bigger then (XX) numbers, then run this (substr($row['information'],0,187))... and working good... :).

Share this post


Link to post
Share on other sites

Since you are selecting from the main table which is produkter(which I believe contains the column 'information') and joining other tables based on produkter.. for the LEFT function you can probably have it like this for it to work: LEFT ('information', 50) AS 'teksten',Not sure though since I'm not able to test that right now.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...