create ordered list with php
Moderator: General Moderators
create ordered list with php
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
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: create ordered list with php
You can use <ol></ol>. Obviously the order will depend on how you query for the data.
This is a very crude and untested attempt but should give you some idea on one approach 
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>';
Re: create ordered list with php
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?
2) second thing to do
3) 3rd thing to do
and so on. Any way of accomplishing this?
Re: create ordered list with php
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?
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
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!
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
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.
<?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
while($row=mysql_fetch_array($sql))
Re: create ordered list with php
missing one ")". Thanks!
Re: create ordered list with php
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.
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.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: create ordered list with php
You can break to a new line using "\n"