Hi.
I have a db table with links containing the name,url and a category.
I want to dynamicly display all the links in the table in a structure like this:
Category1
Link1
Link2
Link3
Category2
Link4
Link5
Link6
How can i do that?
| SitePoint Sponsor |
Hi.
I have a db table with links containing the name,url and a category.
I want to dynamicly display all the links in the table in a structure like this:
Category1
Link1
Link2
Link3
Category2
Link4
Link5
Link6
How can i do that?
PHP Code:<?php
#Capture
$sSQL = "SELECT name, url, category FROM table";
$rResult = mysql_query($sSQL);
$aFinal = array();
while($aRow = mysql_fetch_assoc($rResult))
{
array_push(
$aFinal[$aRow['category']], array(
'name' => $aRow['name'],
'url' => $aRow['url']
)
);
}
/*
$aFinal = array(
'Books' => array(
array(
'name' => 'Book One',
'url' => 'http://bookOne.com'
),
array(
'name' => 'Book Two',
'url' => 'http://bookTwo.com'
)
),
'Cakes' => array(
array(
'name' => 'Cake One',
'url' => 'http://cakeOne.com'
),
array(
'name' => 'Cake Two',
'url' => 'http://cakeTwo.com'
)
),
'Teas' => array()
);
*/
#Display
foreach($aFinal as $sCategoryName => $aCategoryItems)
{
printf(
'<h4>%s</h4>',
$sCategoryName
);
if(count($aCategoryItems) > 0)
{
foreach($aCategoryItems as $aCategoryItem)
{
printf(
'<a href="%s"><p>%s</p></a>',
$aCategoryItem['url'],
$aCategoryItem['name']
);
}
}
else
{
echo '<p>No Items</p>';
}
}
?>
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


anthony, you're going to want to use an ORDER BY clause in your query
![]()
Bookmarks