Solved

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

Posted on 2011-09-13
6
261 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now