Hi Everyone

I just wanted to know how i can display the content of one of my database field in a list format.

I have field called Ingredient, the data type of that field is Text, i tried using css but it just turns the whole content as 1 list, but i want it to break.

Currently like this
Ingredient

  1. orange, berry, tomato

I want it like this
Ingredient

  1. orange
  2. berry
  3. tomato
  4. etc

Thanks in advance

Recommended Answers

All 11 Replies

Member Avatar for diafol

If you have it like this:

$var = "orange, berry, tomato";
$array = explode(",",$var);

$output = "<ol>";
foreach($array as $item){
  $output .= "<li>$item</li>";
}
$output .= "</ol>";
...
echo $output;

However if you;re getting data from a DB:

$output = "<ol>";
while(...){
 $output .= "<li>{$row['ingredients']}</li>";
}
$output .= "</ol>";
...
echo $output;

I have tried the second method, it appears the same way as my css version.

1. orange, berry, tomato

Do i need to format the content in the field in any way to get the desired output?

Member Avatar for diafol

Well look, how's your $row ? Single item or a list? If a list, combine the two methods:

I assume you're only echoing out one ingredient list:

$data = mysql_fetch_array($result);
$array = explode(",",$data['ingredients']);
$output = "<ol>";
foreach($array as $item){
  $output .= "<li>$item</li>";
}
$output .= "</ol>";
...
echo $output;

No Results are being inserted, within the ingredient field

@joban.ali: if you dont mind, can you post youre query here? I wonder why ardav suggestions didn't work in youre script..

I don't mind, I played around with it and it kind of changed, I'll post what I had and what I have now, I do have a slight problem with the new may someone can help me. Me and my codes have departed for a while, taking a break from each, much needed. I'll post in an hours time.

Thanks.

I believe i tried doing something like this, but not exactly sure.
I believe it didn't work cause i may not have did it properly in the first place.

$data = mysql_fetch_array($sql);
$array = explode(",",$data['ingredient']);
$output = "<ol>";
while($row = mysql_fetch_array($sql)){ 
$rid = $row["rid"]; 
$foodtype = $row["foodtype"]; 
foreach($array as $item){
  $output .= "<li>$item</li>";
}
    $prep = $row["prep"]; 
$output .= "</ol>";
}

This is what i have now:
The results appear as intended, however first line is always 'Array' for some reason.

while($row = mysql_fetch_array($sql)){ 

$rid = $row["rid"]; 
        $foodtype = $row["foodtype"]; 
	$ingredient = explode(",", $row["ingredient"]);
	foreach($ingredient as $item) {
		$ingredient .= "<li>$item</li>";
	}
    $prep = $row["prep"];
Member Avatar for diafol

Give an example of the ingredient field content.

You've called the list ($ingredient) the same as the array ($ingredient).

Try this:

$list .= "<li>$item</li>";

and then echo $list.

@ Ardav, The content of the field is now a basic list of words separated with commas, but initially I had measurements and other bits and bobs in there to which the explode broke apart and made it look messy. Maybe a more complex explode maybe needed to work the original content.

I have did what you suggested and has worked.

Member Avatar for diafol

WOnderful. If it is solved, mark it so.

thanks ardav

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.