I’m trying to get the $url value to display from the MySQL database but I can only get the $cat value to display correctly can someone please help me learn how to display the $url value.
I now I’m doing something wrong.
Here is the partial code.
// Loop through each subarray:
foreach ($parent as $id => $cat) {
// Display the item:
echo '<li><a href="http:' . $url . '" title="">' . $cat . '</a>';
Here is the complete code.
<?php
require_once ('./mysqli_connect.php'); // Connect to the db.
// Receives one argument: an array.
function make_list ($parent) {
// Need the main $link array:
global $link;
// Start an ordered list:
echo '<ol>';
// Loop through each subarray:
foreach ($parent as $id => $cat) {
// Display the item:
echo '<li><a href="http://' . $url . '" title="">' . $cat . '</a>';
// Check for sublink:
if (isset($link[$id])) {
// Call this function:
make_list($link[$id]);
}
// Complete the list item:
echo '</li>';
} // End of FOREACH loop.
// Close the ordered list:
echo '</ol>';
} // End of make_list() function.
// Connect to the database:
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM categories ORDER BY parent_id, category ASC");
if (!$dbc) {
// There was an error...do something about it here...
print mysqli_error();
}
// Initialize the storage array:
$link = array();
while (list($id, $parent_id, $category) = mysqli_fetch_array($dbc, MYSQLI_NUM)) {
// Add to the array:
$link[$parent_id][$id] = $category;
}
make_list($link[0]);
mysqli_close($mysqli); // close the connection
?>
$url isn’t in the picture even… It looks like you’re iterating over an array separate from the MySQL result. You would need something more like:
Hope this helps.
Edit:
First of all, $url needs to be assigned along with the other vars in your list() – since you’re doing SELECT * in your query you may need to specify columns so the order is correct in your assignment.
Then, there’s no way to include another variable with the array structure you’re using…
Would have to be something like:
Then, iterating over the array would need to be changed to something like: