i'm trying to build a news website using PHP and MYSQL so i am storing the news and stories in a database and i'm trying to print the categories of the stored news and the $result of the query when i try to fetch the categories from a database is
array(2) {
[0]=> array(3) {
["id"]=> string(1) "1"
["category"]=> string(5) "Music"
["parent"]=> string(1) "1"
}
[1]=> array(3) {
["id"]=> string(1) "2"
["category"]=> string(7) "Fashion"
["parent"]=> string(1) "1"
}
}
I've tried to use a foreach loop as shown in the code below using that sql query
"SELECT * FROM categories WHERE parent = 1";
and the main php code is
echo "<h1>Topics</h1>";
$sql = "SELECT * FROM categories WHERE parent = 1";
$result = $db -> getRows($sql,[]);
$numrows = $db -> getrows("SELECT count(*) FROM stories",[]);
if($numrows == 0) {
echo "<p>No categories</p>";
} else {
foreach($result as $row["category"]=>$cat){
echo implode('-', $cat) . "\n";
}
}
i want to have thous results
*fashion
*Music
what i'm doing wrong and what should i change in my code to make it work ?
Your foreach loop can be made a little simpler and produce what you want