J'essaie d'obtenir la valeur de $ url à afficher à partir de la base de données MySQL mais je peux seulement obtenir la valeur $ cat pour afficher correctement quelqu'un peut m'aider s'il vous plaît à apprendre comment afficher la valeur $ url.PHP Foreach Problème?
Maintenant, je fais quelque chose de mal.
Voici le code partiel.
// Loop through each subarray:
foreach ($parent as $id => $cat) {
// Display the item:
echo '<li><a href="http:' . $url . '" title="">' . $cat . '</a>';
Voici le code complet.
<?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
?>
Votre code est syntaxiquement correct. Quelques questions: Qu'est-ce que le tableau $ parent? Où définissez-vous $ url? – jonthornton
vous devrez peut-être poster plus de code .. de ce que vous nous avez donné jusqu'à présent, $ url n'est même pas défini pour avoir une valeur .. – saleemshafi
Code complet affiché. – aBc