Showing posts with label php categories implode() explode(). Show all posts
Showing posts with label php categories implode() explode(). Show all posts

Thursday, 21 July 2016

Show categories from database and show in a list.

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) : '';

Parenting tips to inculcate learning habits in your kid

Parenting tips to inculcate learning habits in your kid Tip #1) Children do not learn things, they emitate. So, try to do things by yours...