Page 1 of 1
create ordered list with php
Posted: Wed Nov 26, 2008 3:49 am
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
Re: create ordered list with php
Posted: Wed Nov 26, 2008 3:58 am
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

Re: create ordered list with php
Posted: Wed Nov 26, 2008 5:53 am
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?
Re: create ordered list with php
Posted: Wed Nov 26, 2008 6:04 am
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?

Re: create ordered list with php
Posted: Wed Nov 26, 2008 10:20 am
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!
Re: create ordered list with php
Posted: Wed Nov 26, 2008 12:28 pm
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.
Re: create ordered list with php
Posted: Wed Nov 26, 2008 12:33 pm
by papa
while($row=mysql_fetch_array($sql))
Re: create ordered list with php
Posted: Wed Nov 26, 2008 2:57 pm
by dizzad
missing one ")". Thanks!
Re: create ordered list with php
Posted: Thu Nov 27, 2008 5:38 am
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.
Re: create ordered list with php
Posted: Wed Dec 03, 2008 11:31 am
by aceconcepts
You can break to a new line using "\n"