我正在與下面的代碼的問題:問題與陣列
$ids = '"' . implode('", "', $crumbs) . '"';
$motd = array();
$dober = $db->query("SELECT id, name, msg, datetime FROM tbl_depts td INNER JOIN tbl_motd tm ON td.id = tm.deptid WHERE td.id IN (" . $ids . ")");
while ($row = $dober->fetch_array()) {
$motd[] = $row;
}
一個print_r的揭示這一點:
Array
(
[0] => Array
(
[0] => 1
[id] => 1
[1] => Management
[name] => Management
[2] => New Management Rule!
[msg] => New Management Rule!
[3] =>
[datetime] =>
)
[1] => Array
(
[0] => 2
[id] => 2
[1] => Human Resources
[name] => Human Resources
[2] => DPS
[msg] => DPS
[3] =>
[datetime] =>
)
)
因此,我不能使用此代碼生成的東西:
foreach ($motd[] as &$value) {
if ($motd['msg'] != "") {
if ($i == 0) {
?>
<li><a href="#" title="content_<?php echo $value['id']; ?>"
class="tab active"><?php echo $value['name']; ?></a></li>
<?
} elseif ($i == $len - 1) {
?>
<li><a href="#" title="content_<?php echo $value['id']; ?>"
class="tab"><?php echo $value['name']; ?></a></li>
<?php } else { ?>
<li><a href="#" title="content_<?php echo $value['id']; ?>"
class="tab"><?php echo $value['name']; ?></a></li>
<?
}
$i++;
}
}
任何想法,我在做什麼錯在這裏?
編輯:你可能會發現很容易理解,如果你讀這第一:Optimize this SQL query
請注意,此代碼易受sql注入攻擊。 –
就像一個旁註......可能'fetch_assoc()'(http://www.php.net/manual/de/mysqli-result.fetch-assoc.php)更適合您的需求嗎? – pinkgothic
我可能會錯過某些東西,但是你應該在'foreach'之前放置'$ i = 0;',並且''motd''之後不需要'[]'。 – kapa