homepage Welcome to WebmasterWorld Guest from 54.196.168.78
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
mysql rows in ordered list
Sub_Seven




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

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




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

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




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

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




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

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




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

@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 :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved