Bullet list from MySQL / PHP

  • chapel
  • Born
  • Born
  • chapel
  • Posts: 2

Post 3+ Months Ago

I have a mysql database field that contains names that are seperated by commas (John, Paul, Jill, Jake) I need to dispaly them as bullets like in the sample below.

  • John
  • Paul
  • Jill
  • Jake


I have seen this done on as ASP page but need to know how to do it with PHP. Any help and I will be thankfull.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

If you view the source of this webpage, you'll see that the list you posted above looks like this in HTML:

Code: [ Select ]
<ul>
<li>John</li>
<li>Paul</li>
<li>Jill</li>
<li>Jake</li>
</ul>
  1. <ul>
  2. <li>John</li>
  3. <li>Paul</li>
  4. <li>Jill</li>
  5. <li>Jake</li>
  6. </ul>


The <ul> tag begins and ends your bulleted list, and each item is in a child <li> tag. Simply write a loop wraps each item in the correct tag before outputting it to the browser.

For example:

PHP Code: [ Select ]
// explode() splits your comma-delimited list into an array
$items = explode(',', $string_of_names);
 
// now, iterate through and display each one
echo "<ul>\n";
foreach( $items as $item )
{
   echo "<li>$item</li>\n";
}
echo "</ul>";
  1. // explode() splits your comma-delimited list into an array
  2. $items = explode(',', $string_of_names);
  3.  
  4. // now, iterate through and display each one
  5. echo "<ul>\n";
  6. foreach( $items as $item )
  7. {
  8.    echo "<li>$item</li>\n";
  9. }
  10. echo "</ul>";


Obviously, you will have to adjust this to your own code, but you get the idea.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

NOTE: This is bad, MySQL memory is better used maintaining indexes.

You could get the database to give it to you already formatted easily if thoose names are a single field.

Code: [ Select ]
SELECT
CONCAT('<ul><li>',REPLACE(`field_name`,',','</li><li>'),'</li></ul>')
  1. SELECT
  2. CONCAT('<ul><li>',REPLACE(`field_name`,',','</li><li>'),'</li></ul>')


Then all PHP has to do is ask for it & pass it on.

PHP Code: [ Select ]
<?
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if ( ! $link)
{
    die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT CONCAT('<ul><li>',REPLACE(`field_name`,',','</li><li>'),'</li></ul>')");
if ( ! $result)
{
    die('Invalid query: ' . mysql_error());
}
$list = mysql_fetch_row($result);
   
echo $list[0];
?>
 
  1. <?
  2. $link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
  3. if ( ! $link)
  4. {
  5.     die('Could not connect: ' . mysql_error());
  6. }
  7. $result = mysql_query("SELECT CONCAT('<ul><li>',REPLACE(`field_name`,',','</li><li>'),'</li></ul>')");
  8. if ( ! $result)
  9. {
  10.     die('Invalid query: ' . mysql_error());
  11. }
  12. $list = mysql_fetch_row($result);
  13.    
  14. echo $list[0];
  15. ?>
  16.  
  • chapel
  • Born
  • Born
  • chapel
  • Posts: 2

Post 3+ Months Ago

Thanks for the great info.
  • bluedaniel
  • Born
  • Born
  • bluedaniel
  • Posts: 2

Post 3+ Months Ago

hey guys ive been searching the internet high and low for a topic like this as im having the same problem. My recordset is in place and is determined from another page so I have one field from one record that i cant get bullet pointed, its structed to be seperated by commas.

<?php echo $row_rsRecipeDetail['ingredients']; ?>

Currently that shows the correct info as below

200g penne pasta, 3 fat sausages, 1 tbsp olive oil, 250g bag spinach, 3 tbsp pine nuts , 50g parmesan grated

can you help please?

thanks
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Code: [ Select ]
<?php echo '<li>' . str_replace(',', '</li><li>', $row_rsRecipeDetail['ingredients']) . '</li>'; ?>
  • bluedaniel
  • Born
  • Born
  • bluedaniel
  • Posts: 2

Post 3+ Months Ago

mate your a genius, seriously i was tearing my hair out all day.

i owe you a beer

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: Liamw411 and 119 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.