2016-04-26 117 views
1

大家好,謝謝你幫助我... 我有1個數據庫和3個查詢在同一時間。每個查詢都選擇不同的年份並從該列中獲取數據。foreach - 從第二個或第三個陣列獲取具體的價值

這是我的代碼

$items = array(); 

     // while we still have rows from the db, display them 
while ($row = mysql_fetch_array($result1)) { 

    $items[] = $row; 
} 

while ($row = mysql_fetch_array($result2)) { 

    $items[] = $row; 
} 

while ($row = mysql_fetch_array($result3)) { 

    $items[] = $row; 
} 


mysql_close($connect); 

?> 

<?php foreach($items as $key => $item) : ?> 

    <?php print "I DONT KNOW WHAT TO DO"; ?> 

<?php endforeach; ?> 

當我鍵入:

<?php echo print_r($keys); ?> 

我可以看到我有0,1和2的陣列我很想得到一個特定的行第一(1)或第二(2)陣列。

我想弄明白,我只是不能...

謝謝大家這麼多!

編輯:

完整代碼:

<?php 
    include ('db.php'); // for db details 
    include ('functions.php'); 

    $connect = @mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this time.</p>'); 
    if (!$connect) { 
     die('Could not connect: ' . mysql_error()); 
    } 



    @mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>'); 

    $query1 = "SELECT * FROM godina2015 WHERE mjesec='8' AND godina='2015'"; 
    $query2 = "SELECT * FROM godina2015 WHERE mjesec='7' AND godina='2015'"; 
    $query3 = "SELECT * FROM godina2015 WHERE mjesec='6' AND godina='2015'"; 

    $result1 = @mysql_query("$query1") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>'); 
    $result2 = @mysql_query("$query2") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>'); 
    $result3 = @mysql_query("$query3") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>'); 


    $items = array(); 

      // while we still have rows from the db, display them 
    while ($row = mysql_fetch_array($result1)) { 

     $items[] = $row; 
    } 

    while ($row = mysql_fetch_array($result2)) { 

     $items[] = $row; 
    } 

    while ($row = mysql_fetch_array($result3)) { 

     $items[] = $row; 
    } 


    mysql_close($connect); 

    ?> 

    <?php foreach($items as $key => $item) : ?> 

     <?php print_r ($item); ?> 

    <?php endforeach; ?> 

編輯#2

<?php 
    include ('db.php'); // for db details 
    include ('functions.php'); 

    $connect = mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this time.</p>'); 
    if (!$connect) { 
     die('Could not connect: ' . mysql_error()); 
    } 



    mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>'); 

    $query = "SELECT * FROM godina2015 WHERE mjesec IN(8,7,6) AND godina='2015'"; 

    $result = mysql_query("$query") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>'); 

    $items = array(); 

      // while we still have rows from the db, display them 
    while ($row = mysql_fetch_array($result)) { 

     $items[] = $row; 
    } 

    mysql_close($connect); 

    ?> 

    <?php foreach($items as $key => $item) : ?> 

     <?php echo $items[1]['AWAsistCTR2']; ?> 

    <?php endforeach; ?> 
+0

如果在foreach循環中執行'print_r($ item);'會怎麼樣? –

+0

這可能有助於http://stackoverflow.com/questions/17139453/php-accessing-multidimensional-array-values –

+0

顯示'var_dump($ items)'。你需要顯示/解釋你的數據結構是什麼樣的。但是考慮到你如何構建數組,你將以'$ items [0] ... $ items [n]'結尾,爲你的第一個查詢結果'$ items [n + 1] ...「 $ items [n + m]'爲第二個查詢,等等... –

回答

0

在循環中做的var_dump($項目)。

您將在瀏覽器中看到數組/對象的結構。

查找具有所需數據的字段(它可能與數據庫中的列名稱相同)。

您可以在循環中使用$ item ['field']來訪問它。或者$ items [array number here(0,1,2)] ['field']。

我相信你說ROW你的意思是COLUMN。

+0

如果代替剛剛添加的代碼,您在代碼之後執行了var_dump($ items) mysql_close($ connect)並在這裏粘貼輸出。 – wavvves

0

我的第一個答案......太激動了。 我有點知道你在做什麼,因爲當我使用代碼來「弄清楚」不同(動態)查詢響應如何相互關聯時,我必須做同樣的事情。我通常嵌套結果,以便我可以進行邏輯分析的關係。

while ($row = mysql_fetch_array($result1)) { 
    $items[0][] = $row; 
} 

while ($row = mysql_fetch_array($result2)) { 
    $items[1][] = $row; 
} 

while ($row = mysql_fetch_array($result3)) { 
    $items[2][] = $row; 
} 

這將區分不同的記錄,以便在需要時您可以解析你想要的任何方式排列,甚至遞歸。

(array)$newArray = parseArray($items); 

從那裏......

function parseArray($inArray) { 
    (array)$outArray = Array(); 

    foreach($inArray as $element) { 
     if('some condition with $element') {  //maybe $element['date']=10? 
      $outArray[] = parseArray($element); 
     } else { 
      $outArray[] = $element; 
     } 
    } 
    return $outArray(); 
} 

難以回答(和測試)不知道長什麼樣數據,如,但也許一些想法。我現在支持我的第一次反對票。

相關問題