Page 1 of 1

[SOLVED] string manipulation question

Posted: Wed Jan 03, 2007 3:45 pm
by boo_lolly
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: Select all

+--------------+
|  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 |
+--------------+
to become this:

Code: Select all

+--------------+
|  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    |
+--------------+
here's my code now...

Code: Select all

<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>
$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.

Posted: Wed Jan 03, 2007 3:55 pm
by Burrito
have a look at strlen(), str_replace().

Posted: Wed Jan 03, 2007 3:57 pm
by feyd
The <li> set the the proper CSS should do it. No need to use code. :)

Posted: Wed Jan 03, 2007 4:00 pm
by boo_lolly
feyd wrote:The <li> set the the proper CSS should do it. No need to use code. :)
could you be a little more specific?

Posted: Wed Jan 03, 2007 4:03 pm
by jyhm
WOuld something like this work?

Code: Select all

if (strlen($row)>19) {
	for ($n=0; $n<1; $n++) {
		preg_replace('\s', '\n\s', $row);
		}
}
I haven't tested this rubbish but maybe it will give you an idea! :D

Posted: Wed Jan 03, 2007 4:16 pm
by boo_lolly
i looked into wordwrap() and it worked perfect for me.

Code: Select all

<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>

Posted: Wed Jan 03, 2007 4:25 pm
by jyhm
Awesome Boo_lolly,

I didn't even know about word wrap. How did you find out about that? Web search or directly on PHP.net function list?

Posted: Wed Jan 03, 2007 4:39 pm
by boo_lolly
jyhm wrote:Awesome Boo_lolly,

I didn't even know about word wrap. How did you find out about that? Web search or directly on PHP.net function list?
i checked the php.net function list =)