MySQL 데이터베이스에서 표시 할 $ url 값을 얻으려고하고 있지만 정확히 표시 할 $ cat 값만 가져올 수 있습니다. $ url 값을 표시하는 방법을 배우는 데 도움이 될 수 있습니까?PHP Foreach 문제?
나는 지금 뭔가 잘못하고있다.
다음은 부분 코드입니다.
// Loop through each subarray:
foreach ($parent as $id => $cat) {
// Display the item:
echo '<li><a href="http:' . $url . '" title="">' . $cat . '</a>';
다음은 전체 코드입니다.
<?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
?>
코드가 구문 상 올바로되어 있습니다. 몇 가지 질문 : $ parent 배열이란 무엇입니까? $ url을 어디에서 설정하고 있습니까? – jonthornton
코드의 일부를 게시해야 할 수도 있습니다. $ url은 값이 있다고 정의되지 않았습니다. – saleemshafi
전체 코드가 게시되었습니다. – aBc