How to display categories in a list:
Say, you have following categories on your site:
Administration, Sales, Production, Management, Inventory.
And you have to show it in a list.
You can show them simply by using PHP'a array functions.
implode().
Fetch categories from database and append to array.
while ($row = mysqli fetch array) {
$categories[category id] = category name;
}
And while showing categories in ul li:
$categoriesHTML = '';
if (! empty($categories)) {
$categoriesHTML .= '<ul>';
foreach ($categories as $category) {
$categoriesHTML .= '<li>' . $category . '</li>';
}
$categoriesHTML .= '<ul>';
}
echo $categoriesHTML;
OR, if you want to display as a comma separated string,'
just print it using implode().
echo (! empty($categories)) ? implode(', ', $categories) : '';
Say, you have following categories on your site:
Administration, Sales, Production, Management, Inventory.
And you have to show it in a list.
You can show them simply by using PHP'a array functions.
implode().
Fetch categories from database and append to array.
while ($row = mysqli fetch array) {
$categories[category id] = category name;
}
And while showing categories in ul li:
$categoriesHTML = '';
if (! empty($categories)) {
$categoriesHTML .= '<ul>';
foreach ($categories as $category) {
$categoriesHTML .= '<li>' . $category . '</li>';
}
$categoriesHTML .= '<ul>';
}
echo $categoriesHTML;
OR, if you want to display as a comma separated string,'
just print it using implode().
echo (! empty($categories)) ? implode(', ', $categories) : '';