create ordered list with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dizzad
Forum Newbie
Posts: 6
Joined: Tue Nov 25, 2008 5:21 pm

create ordered list with php

Post by dizzad »

Hi, I'm very new to php so bear with me please. I want to retrieve data from a mysql database and put it into an ordered list. I can get the data OK, but can't figure out how to put that data into a list format automatically. I hope someone understands this question
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: create ordered list with php

Post by aceconcepts »

You can use <ol></ol>. Obviously the order will depend on how you query for the data.

Code: Select all

 
//query for data
$sql=mysql_query("SELECT * FROM tbale_name ORDER BY field_id");
 
//open the HTML ordered list
echo'<ol>';
 
//loop query results
while($row=mysql_fetch_array($sql)
{
  echo'<li>'.$row['field_from_database'].'</li>';
}
 
//close the HTML ordered list
echo'</ol>';
 
This is a very crude and untested attempt but should give you some idea on one approach :D
dizzad
Forum Newbie
Posts: 6
Joined: Tue Nov 25, 2008 5:21 pm

Re: create ordered list with php

Post by dizzad »

Thank You for your reply, but I wasn't too clear. What I have is a database of recipes. I want to show a random recipe on the main page. I'm showing the title, ingredients and preparation. I'm using the <pre> tag for the ingredients(that looks fine) I'd like the preparations to be in an <ol> format i.e. ----1) 1st thing to do
2) second thing to do
3) 3rd thing to do
and so on. Any way of accomplishing this?
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: create ordered list with php

Post by mintedjo »

How are the instructions retreived from the database?

aceconcepts assumed there was a row in the database for each cooking instruction but if there was then i think you would have used his response so... How are these instructions stored? :-)
dizzad
Forum Newbie
Posts: 6
Joined: Tue Nov 25, 2008 5:21 pm

Re: create ordered list with php

Post by dizzad »

Hi, maybe I did something wrong. I didn't get the data to look like a <ol>. The list was like
1) 1st instruction 2nd instruction 3rd instruction. To answer your question, each instruction is a new row. I'll try again and post back. Thanks!
dizzad
Forum Newbie
Posts: 6
Joined: Tue Nov 25, 2008 5:21 pm

Re: create ordered list with php

Post by dizzad »

this is what I'm working with. I simplified it to work with just what i need and now I get an error message.
<?php

mysql_connect("server", "username", "password") or die(mysql_error());
mysql_select_db("database_name") or die(mysql_error());

//query for data
$sql=mysql_query("SELECT * FROM table_name ORDER BY ID");

//open the HTML ordered list
echo'<ol>';

//loop query results
while($row=mysql_fetch_array($sql)
{
echo'<li>'.$row['PreparationDetails'].'</li>';
}

//close the HTML ordered list
echo'</ol>';

?>

I'm getting -Parse error: syntax error, unexpected '{' on line 14
Like I said I'm veeerrry new to php and it's not sinking in. This should be very simple, but I don't get it.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: create ordered list with php

Post by papa »

while($row=mysql_fetch_array($sql))
dizzad
Forum Newbie
Posts: 6
Joined: Tue Nov 25, 2008 5:21 pm

Re: create ordered list with php

Post by dizzad »

missing one ")". Thanks!
dizzad
Forum Newbie
Posts: 6
Joined: Tue Nov 25, 2008 5:21 pm

Re: create ordered list with php

Post by dizzad »

Hi guys, I'm still just getting the list as I described previous. Like 1) first. second. third. instead of 1) first.
2) second.
3) so on and so on.

I'm wondering if there is a way to determine new paragraphs with php. I changed the <ol> to a <pre> tag and remembered that the instructions were put into the database using a new line for each new instruction. So...the format is correct just the numbering needs to be added.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: create ordered list with php

Post by aceconcepts »

You can break to a new line using "\n"
Post Reply