Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Foreach Problem?
    text
    copied!<p>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.</p> <p>I now I'm doing something wrong.</p> <p>Here is the partial code.</p> <pre><code>// Loop through each subarray: foreach ($parent as $id =&gt; $cat) { // Display the item: echo '&lt;li&gt;&lt;a href="http:' . $url . '" title=""&gt;' . $cat . '&lt;/a&gt;'; </code></pre> <p>Here is the complete code.</p> <pre><code>&lt;?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 '&lt;ol&gt;'; // Loop through each subarray: foreach ($parent as $id =&gt; $cat) { // Display the item: echo '&lt;li&gt;&lt;a href="http://' . $url . '" title=""&gt;' . $cat . '&lt;/a&gt;'; // Check for sublink: if (isset($link[$id])) { // Call this function: make_list($link[$id]); } // Complete the list item: echo '&lt;/li&gt;'; } // End of FOREACH loop. // Close the ordered list: echo '&lt;/ol&gt;'; } // 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 ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload