Link to home
Start Free TrialLog in
Avatar of kbios
kbios

asked on

How do I create an unnumbered list in php from mysql data?

Below is my php code to retrieve some data from a mysql db. The db=test, table=items and the fields to display are id and title. I want to create an unnumbered list as follows:

<ul class="itemlist">
  <li>items.id <li>items.title</li></li>        <-- for as many items returned by my query
</ul>

Display would look like:
XYZ123
title of item XYZ123

ABC123
title of item ABC123

...

Here is the php code:

<ul id="itemlist">
<?php
    $_POST['hiddenItem'];  // using parsed item
                                          
    $conn = mysql_connect("localhost", "X", "X") or die(mysql_error());
                mysql_select_db("test");
            
    $item=mysql_real_escape_string($_POST['hiddenItem']);  // using parsed item
            
    $sql = "SELECT id, title FROM items WHERE id = '$item'";
    $result = mysql_query($sql, $conn) or die(mysql_error());
            
    while($row = mysql_fetch_assoc($result)){
       foreach ($row as $id => $value){
       print "<li>"$value"</li>";
       }
     }
?>
</ul>

Please use my code snipet in your example not some obscure reference to a previously posted question. I'm new to php and need a little hand holding :)
Avatar of kbios
kbios

ASKER

Something I've noticed and do not understand. If I change the print statement above to:

print "li $value li";      

the literal string li appears along with the value of $value, BUT if I insert <li> that text does not appear. Any text will work but not with < > I have tried all combnations of single and double quotes but to no avail.
I think you need to write the <li> statement this way
print "<li>".$value."</li>";

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of PranjalShah
PranjalShah
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kbios

ASKER

thanks for both suggestions. neither one worked.

the contents of 'id' and 'title' display just fine but neither the echo or print statement will display the actual string <li> or </li>. I can replace those tags with other text and the other text appears but not the <li> or </li>. Any suggestions as to why the literal string <li> or </li> does not appear?
Avatar of kbios

ASKER

I think there may be some conflict with the CSS. Are there any php/CSS issues to be aware of?
Avatar of kbios

ASKER

Thanks. I'm still having the problem but I think it's narrowed down to some issue with my CSS. Your code was helpful, so thanks. It's late and I'm accepting this as the final answer.