2009-11-06 8 views
0

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 

?> 
+0

코드가 구문 상 올바로되어 있습니다. 몇 가지 질문 : $ parent 배열이란 무엇입니까? $ url을 어디에서 설정하고 있습니까? – jonthornton

+0

코드의 일부를 게시해야 할 수도 있습니다. $ url은 값이 있다고 정의되지 않았습니다. – saleemshafi

+0

전체 코드가 게시되었습니다. – aBc

답변

3

$ url은 (는) 그림에도 없습니다 ... MySQL 결과와 별도로 배열을 반복하는 것처럼 보입니다. 다음과 같은 것이 더 필요합니다.

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

희망이 있습니다.

편집 : 모든

우선, $ URL은리스트()의 다른 바르와 함께 할당해야합니다 - 당신이 당신의 쿼리에서 SELECT *하고있어 이후 주문 있도록 열을 지정해야 할 수도 있습니다 당신의 임무에서 맞습니다. 그런 다음

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

의 반복 :

그런 다음,

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

뭔가처럼 일해야 할 것입니다 ... 당신이 사용하고있는 배열 구조를 가진 다른 변수를 포함 할 수있는 방법이 없습니다 배열은 다음과 같이 변경해야합니다.

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

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

} 
1

제공 한 코드에서 $ parent로부터 $ url을 (를) 선언하지 않았습니다. $ parent에 저장된 것을 제공 할 수있는 기회가 있습니까?


첫 번째 사항 먼저!

당신은 적절한 카테고리를 추출 그런 다음 foreach 루프를 변경하여 mysqli_fetch_array 호출이 비슷한 (나는 URL을 가정하고 열 이름이 테이블에)

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); 
} 

에서 URL을 얻을 필요하고 url