大家好,謝謝你幫助我... 我有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; ?>
如果在foreach循環中執行'print_r($ item);'會怎麼樣? –
這可能有助於http://stackoverflow.com/questions/17139453/php-accessing-multidimensional-array-values –
顯示'var_dump($ items)'。你需要顯示/解釋你的數據結構是什麼樣的。但是考慮到你如何構建數組,你將以'$ items [0] ... $ items [n]'結尾,爲你的第一個查詢結果'$ items [n + 1] ...「 $ items [n + m]'爲第二個查詢,等等... –