0

I want to output my data into a list ordered like this

<ul>
<li>[I]category name[/I]</li>
<ul>
<li>[I]category item[/I]</li>
</ul>
<li>[I]category name[/I]</li>
<ul>
<li>[I]category item[/I]</li>
</ul>
</ul>

when I try a foreach loop like this

<? $previous_catagory = null; ?>
<? foreach ($providers as $provider): ?>

<?  if ($provider['catname'] !=$previous_catagory)
{
$previous_catagory = $provider['catname'];
echo '<li>' . $provider['catname'] . '</li>' ;
 }
 ?>
 <ul>
 <? $previous_provider = null; ?>
	<? foreach($providers as $provider): ?>
 <? if($provider['name'] !=$previous_provider)
 {
 $previous_provider = $provider['name'];
 echo '<li>';
 echo '<a href=?id="' . $provider['id'] .  '">';
 echo $provider['name'];
 echo '</a>';
 echo '</li>';
 }
 ?>
 <? endforeach ?>
</ul>
<? endforeach ?>

it outputs the category title but when it makes the category items it puts all items in every category.
I just want it to output the items in that category into the ul
mysql for this is :

$sql = 'SELECT name, providers.id, catid, catagory.catagoryname
FROM providers
INNER JOIN catagory ON catid = catagory.id
ORDER BY providers.catid';

if that helps

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by tape enterprise
1

your code cleaned up

<?php  
    $previous_catagory = null; 
    foreach ($providers as $provider)
    {
        if ($provider['catname'] !=$previous_catagory)
        {
            $previous_catagory = $provider['catname'];
            echo '<li>' . $provider['catname'] . '</li>' ;
        }
        echo "<ul>";
        $previous_provider = null;
        foreach($providers as $provider)
        {
            if($provider['name'] !=$previous_provider)
            {
                $previous_provider = $provider['name'];
                echo '<li>';
                echo '<a href=?id="' . $provider['id'] .  '">';
                echo $provider['name'];
                echo '</a>';
                echo '</li>';
            }
        }
        echo "</ul>";
    }
?>

the problem is in line 11

    $previous_provider = null;

Edited by mike_2000_17: Fixed formatting

0

Hey thanks for the code but also what I needed to do was fix the if statement

$previous_provider = $provider['catid2'];
foreach($providers as $provider)
{
if($provider['name'] ==$previous_provider)
{
$previous_provider = $provider['name'];
echo '<li>';
echo '<a href=?id="' . $provider['id'] . '">';
echo $provider['name'];
echo '</a>';
echo '</li>';
}

so it checks the catagory id from the name against the catagory id from the catagories table if its true it prints all the names when it returns false it puts in the new catagory id

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.