2016-04-26 19 views
1

皆さん、ありがとうございました... 私は1つのデータベースと3つのクエリを同時に持っています。すべてのクエリで別の年が選択され、その列からデータが取得されます。foreach - 2番目または3番目の配列から特定の値を取得

私が入力するとこれは私のコード

$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番目の配列(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; ?> 

EDIT#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

これは、 'var_dump($ items)'を表示するのに役立ちますhttp://stackoverflow.com/questions/17139453/php-accessing-multidimensional-array-values –

+0

。あなたのデータ構造がどのように見えるかを正確に表示/説明する必要があります。配列をどのように構築しているかを考えれば、最初のクエリ結果の '$ items [0] ... $ items [n]'、 '$ items [n + 1] ... 2番目のクエリの$ items [n + m] 'など –

答えて

0

はのvar_dump($アイテム)を行います。

ブラウザに配列/オブジェクトの構造が表示されます。

必要なデータがあるフィールドを探します(おそらく、データベースの列と同じ名前になります)。

ループ内で$ item ['field']を使ってアクセスできます。または$ items [array number here(0,1,2)] ['field']。

そして、あなたが行を言ったときにあなたは列を意味すると思います。

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

答えるのは難しい(とテスト)

どのように見えるかのデータを知ることなしに、多分いくつかのアイデア。私は今、私の最初の下降投票を支持します。

関連する問題