aBc
aBc

Reputation: 163

PHP Foreach Problem?

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

?>

Upvotes: 0

Views: 449

Answers (2)

houmam
houmam

Reputation: 464

From the code you have provided you are not declaring $url from the $parent. Any chance you could provide what is stored in $parent?


So First things first!

You will need to get the URL from your mysqli_fetch_array call something similar to this (I am assuming url is the column name in your table)

while ($row = mysqli_fetch_array($dbc, MYSQLI_NUM)) {
    $id = $row['id'];
    $parent_id = $row['parent_id'];
    $cat = $row['category'];
    $url = $row['url'];

    // Add to the array:
    $link[$parent_id][$id] = array('cat' => $cat, 'url' => $url);
}

Then alter your foreach loop to extract the appropriate category and url

foreach ($parent as $id => $category_array) {

 // Display the item:
 echo '<li><a href="http://' . $category_array['url'] . '" title="">' . $category_array['cat'] . '</a>';

Upvotes: 1

jeffff
jeffff

Reputation: 1319

$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:

foreach ($res as $row) {
    echo '<li><a href="http:' . $row['url'] . '" title="">' . $row['cat'] . '</a>';
}

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...

$link[$parent_id][$id] =  $category;

Would have to be something like:

$link[$parent_id][$id] =  array('category' => $category, 'url' => $url);

Then, iterating over the array would need to be changed to something like:

foreach ($parent as $id => $ary) {

    // Display the item:
    echo '<li><a href="http:' . $ary['url'] . '" title="">' . $ary['category'] . '</a>';

}

Upvotes: 3

Related Questions