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

Recommended Answers

All 2 Replies

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;

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

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.