Solved

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

Posted on 2011-09-13
6
264 Views
Last Modified: 2012-05-12
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 :)
0
Comment
Question by:kbios
  • 4
6 Comments
 

Author Comment

by:kbios
ID: 36533264
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.
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 36533331
I think you need to write the <li> statement this way
print "<li>".$value."</li>";

Open in new window

0
 
LVL 8

Accepted Solution

by:
PranjalShah earned 500 total points
ID: 36533346
Try changing your while loop to this...
while($row = mysql_fetch_array($result)) {
     echo "<li>".$row['id']."<li>".$row['title']."</li></li>";
}

Open in new window

0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:kbios
ID: 36533599
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?
0
 

Author Comment

by:kbios
ID: 36533616
I think there may be some conflict with the CSS. Are there any php/CSS issues to be aware of?
0
 

Author Closing Comment

by:kbios
ID: 36533665
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.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to access a remote mysql database with xampp 3 23
XML extra information 8 29
PHP breaks when used in Wordpress template file 3 30
Ahax pagination 9 32
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question