Solved

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

Posted on 2011-09-13
6
262 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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 …
Creating and Managing Databases with phpMyAdmin in cPanel.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

911 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

23 Experts available now in Live!

Get 1:1 Help Now