Welcome to WebmasterWorld Guest from 54.167.85.221

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

mysql rows in ordered list

     

Sub_Seven

3:21 am on Mar 2, 2011 (gmt 0)

5+ Year Member



Hello everyone,

So I was trying to display various rows from a MySQL table inside an ordered list and I couldn't do it

They displayed like this:

1. item1
1. item2
1. item3
1. and so on...

I don't have a numeric id inside the DB so I couldn't do an order by id (don't want to anyways 'cause I want to keep the default order(wrapped around the <ol> of course)), I thought this could work but it didn't:
<ol>
<li>
<?php
echo htmlentities($row['cp_cat_name']);
}
?>
</li>
</ol>


I also tried echoing the <li> tags around the echo htmlentities($row['cp_cat_name']); but I got the same result...

Could someone tell me how to pull this off? Thank you all :)

sonjay

5:15 am on Mar 2, 2011 (gmt 0)

10+ Year Member



You need to put just the <li> inside your while loop:
<ol>
<?php
while ($row = mysql_fetch_array($query)) {
echo '<li>'.$row['cp_cat_name'].'</li>';
}
?>
</ol>

Sub_Seven

6:12 am on Mar 2, 2011 (gmt 0)

5+ Year Member



Hi sonjay and thanks for the quick response.

I have one comment and a few questions since I posted the thread; I kept working on my code and things got a little more complicated.

Comment:

Your suggestion works just fine, but it raises my first question.

Question 1:

How is your suggestion different than this?:
<ol>
<?php
while ($row = mysql_fetch_array($result)) {
echo '<li>';
echo $row['cp_cat_name']
echo '</li>';
}
?>
</ol>

I ask because I did try that and it did not work.

Question 2:

I separated the results into columns that are floating left (css) since some of them return 90+ items, if I do this:
<div class="catcolumn">
<ol>
<?php $result = mysql_query("SELECT * FROM cp_category WHERE cp_cat_line = 'ar' ORDER BY cp_cat_name ASC LIMIT 0, 33");
while($row = mysql_fetch_array($result)){
echo '<li>'.htmlentities($row['cp_cat_name']).'</li>';
} ?>
</div>
<div class="catcolumn">
<?php $result = mysql_query("SELECT * FROM cp_category WHERE cp_cat_line = 'ar' ORDER BY cp_cat_name ASC LIMIT 33, 67");
while($row = mysql_fetch_array($result)){
echo '<li>'.htmlentities($row['cp_cat_name']).'</li>';
} ?>
</ol>


The first column gets treated as an <ol> but the second column returns as if it were an <ul>...?

and if I do this:
<div class="catcolumn">
<ol>
<?php $result = mysql_query("SELECT * FROM cp_category WHERE cp_cat_line = 'ar' ORDER BY cp_cat_name ASC LIMIT 0, 33");
while($row = mysql_fetch_array($result)){
echo '<li>'.htmlentities($row['cp_cat_name']).'</li>';
} ?>
</ol>
</div>
<div class="catcolumn">
<ol>
<?php $result = mysql_query("SELECT * FROM cp_category WHERE cp_cat_line = 'ar' ORDER BY cp_cat_name ASC LIMIT 33, 67");
while($row = mysql_fetch_array($result)){
echo '<li>'.htmlentities($row['cp_cat_name']).'</li>';
} ?>
</ol>


Both columns get treated as <ol> but the second column starts off from 1 again and not 34 as it should be.

So how can I separate the results into different columns without losing the ordered list's order?

Anyone willing to help feel free to ask questions if my explanation isn't clear enough.

As always thanks for the help!

omoutop

10:46 am on Mar 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The fact that your counter stars from 1 again (second column) is due to your second <ol>.
You need to declare your own counter/number to show and not rely on <ol>

So, as a general approach:

[b]$my_counter = 0;[/b]
$result = mysql_query("your query here");
while($row = mysql_fetch_array($result))
{
[b]$my_counter++;[/b]
echo $my_counter.' '.$row['column'];
}


Your counter will start at 1 and will end at mysql_num_rows($result)+1;

Sub_Seven

4:29 pm on Mar 2, 2011 (gmt 0)

5+ Year Member



@omoutop

That's why at some point I tried wrapping both columns between one set of <ol></ol> tags, I really thought that would do it, any ways, your suggestion works just great, thank you so much for the help :)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month