Text Area To Menu or List

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Subliminal
Forum Commoner
Posts: 40
Joined: Thu Oct 23, 2003 8:12 pm

Text Area To Menu or List

Post by Subliminal »

Hi,

I would like to have a list of values entered into a text area (seperated by return) that are stored in a mysql database. These values are then pulled out and placed into a list or menu. Possible?

Thanks in advance,
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Yes. Here is the basic structure of the code you would want.

1. Query and get the values from the database
2. [php_man]explode[/php_man] the results by \n
3. Loop through the results and echo each value.

Example:

Code: Select all

<?php
$query = "SELECT values FROM tablename";
$result = mysql_query($query);
$valuelist = explode("\n", $result);
foreach ($valuelist as $key => $value) {
echo "$value<br>\n";
}
?>
Subliminal
Forum Commoner
Posts: 40
Joined: Thu Oct 23, 2003 8:12 pm

Post by Subliminal »

Hi,

Thanks for the reply. With the script you provided I just get an output of Resource id #3 what does that mean?

Also what would the menu code look like?

Thanks,
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

I've always wonder what exactly that means, but I do know it is the $result when you query the database. I forgot to add one important line, here is a fixed example:

Code: Select all

<?php
$query = "SELECT values FROM tablename";
$result = mysql_query($query);
while ($r = mysql_fetch_array($result))
{
  $valuelist = explode("\n", $r['values']);  //change the word 'values' to whatever the column is called in the database
  echo "<table border="1"><tr><th>Menu</th></tr>\n";
  foreach ($valuelist as $key => $value) 
  {
  echo "<tr><td>";
  echo "$value<br>\n";  // for every value, another menu item is generated
  echo "</tr></td>";       // so this is where you would change the menu code  
  }
  echo "</table>";
}
?>
Post Reply