Solved

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

Posted on 2011-09-13
6
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Webinar: MariaDB® Server 10.2: The Complete Guide

Join Percona’s Chief Evangelist, Colin Charles as he presents MariaDB Server 10.2: The Complete Guide on Tuesday, June 27, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

 

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

Webinar: MongoDB® Index Types

Join Percona’s Senior Technical Services Engineer, Adamo Tonete as he presents “MongoDB Index Types, How, When and Where Should They be Used?” on Wednesday, July 12, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…

728 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