boo_lolly Posted January 3, 2007 Share Posted January 3, 2007 i'm populating a bulleted list of categories from a MySQL table. simple stuff. however, this list is going to be on the left side of a table column. like a navigation menu on the left of a website. the problem is, if the name in the database that's populating the bulleted list is too long, it extends into the rest of the website. i want to incorporate a script in my while loop, so that if the string name is over whatever characters long, go back to the 20th character, and grab the whitespace to the left of it, then skip a line.for example, i want this...[code]+--------------+| Categories |+--------------+| 1. Plates || 2. Spoons || 3. Forks || 4. Knives || 5. Pressure Cookers || 6. Bed Sheets|| 7. Pots and Pans || 8. Wine || 9. Salt and Pepper |+--------------+[/code]to become this:[code]+--------------+| Categories |+--------------+| 1. Plates || 2. Spoons || 3. Forks || 4. Knives || 5. Pressure || Cookers || 6. Bed Sheets|| 7. Pots and || Pans || 8. Wine || 9. Salt and || Pepper |+--------------+[/code]here's my code now...[code]<UL><?php $sql = mysql_query("SELECT * FROM categories WHERE parent=0"); while($row = mysql_fetch_array($sql)){ echo "<LI><a href=\"index.php?cat=". $row[id] ."\">". $row[name] ."</a></LI>\n"; }?></UL>[/code]$row['name'] is the string being manipulated. where would i start? i'm sure there's many ways to skin this cat, which method(s) should i use and why?thanks for all your help. Link to comment https://forums.phpfreaks.com/topic/32756-solved-string-manipulation-question/ Share on other sites More sharing options...
thepip3r Posted January 3, 2007 Share Posted January 3, 2007 [code=php:0]if (strlen($row['name']) > 15) { $first_half = substr($row['name'], 0, 15) $second_half = substr($row['name'], 16, strlen($row['name'])); echo "<li><a href=\"index.php?cat=". $row[id] ."\">$first_half <br> $second_half</a></LI>\n";} else { echo "<LI><a href=\"index.php?cat=". $row[id] ."\">". $row[name] ."</a></LI>\n";}[/code]...you may not like this method though as you'll find that some letters have a longer pixel length than others. Like while 15 characters might work for all words with the first letter capitalized, try a word with a lot of m's or w's which tend to take up more space and it still may end up wrapping. Unfortunately, I've never figured out an efficient way to get around this problem. Link to comment https://forums.phpfreaks.com/topic/32756-solved-string-manipulation-question/#findComment-152486 Share on other sites More sharing options...
trq Posted January 3, 2007 Share Posted January 3, 2007 Take a look at [url=http://php.net/wordwrap]wordwrap[/url]. Link to comment https://forums.phpfreaks.com/topic/32756-solved-string-manipulation-question/#findComment-152487 Share on other sites More sharing options...
boo_lolly Posted January 3, 2007 Author Share Posted January 3, 2007 [quote author=thepip3r link=topic=120892.msg496394#msg496394 date=1167861356][code=php:0]if (strlen($row['name']) > 15) { $first_half = substr($row['name'], 0, 15) $second_half = substr($row['name'], 16, strlen($row['name'])); echo "<li><a href=\"index.php?cat=". $row[id] ."\">$first_half <br> $second_half</a></LI>\n";} else { echo "<LI><a href=\"index.php?cat=". $row[id] ."\">". $row[name] ."</a></LI>\n";}[/code]...you may not like this method though as you'll find that some letters have a longer pixel length than others. Like while 15 characters might work for all words with the first letter capitalized, try a word with a lot of m's or w's which tend to take up more space and it still may end up wrapping. Unfortunately, I've never figured out an efficient way to get around this problem.[/quote]thanks a LOT piper! i'll be sure to see if i can use that code. i don't think i'm going to run into a problem with too many m's or w's. the worst case scenario doesn't invlove that concern. thanks a lot tho. Link to comment https://forums.phpfreaks.com/topic/32756-solved-string-manipulation-question/#findComment-152493 Share on other sites More sharing options...
thepip3r Posted January 3, 2007 Share Posted January 3, 2007 I've never used wordwrap before but that may be a more efficient way to do it boo. good luck on you project. Link to comment https://forums.phpfreaks.com/topic/32756-solved-string-manipulation-question/#findComment-152494 Share on other sites More sharing options...
boo_lolly Posted January 3, 2007 Author Share Posted January 3, 2007 [code]<UL><?php $sql = mysql_query("SELECT * FROM categories WHERE parent=0"); while($row = mysql_fetch_array($sql)){ echo "<LI><a href=\"index.php?cat=". $row[id] ."\">". wordwrap($row[name], 18, "<br />\n") ."</a></LI>\n"; }?></UL>[/code]works GREAT! thanks so much thorpe!!! Link to comment https://forums.phpfreaks.com/topic/32756-solved-string-manipulation-question/#findComment-152499 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.