Jump to content

[SOLVED] string manipulation question


boo_lolly

Recommended Posts

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
Share on other sites

[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
Share on other sites

[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
Share on other sites

[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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.