【发布时间】:2011-08-29 12:01:46
【问题描述】:
此代码有效:
$row = array(5,6,89,97,101);
$found = array();
$post = 89;
$count_row = count($row);
for($i = 0; $i < $count_row; $i++){
if($row[$i]==$post){
$found[] = $row[$i-2];
$found[] = $row[$i-1];
$found[] = $row[$i];
$found[] = $row[$i+1];
$found[] = $row[$i+2];
var_dump($found);
}
}
这并没有,可能在 mysql_fetch_array 中做错了;
$found = array();
$q = "SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id='$term_tax_id'";
$rs = mysql_query($q) or die(mysql_error());
$row = mysql_fetch_array($rs, MYSQL_NUM);
$count_row = count($row);
for($i = 0; $i < $count_row; $i++){
print_r($row);
if($row[$i]==$post){
$found[] = $row[$i-2];
$found[] = $row[$i-1];
$found[] = $row[$i];
$found[] = $row[$i+1];
$found[] = $row[$i+2];
var_dump($found);
}
}
如果帖子大于 1,则不显示任何内容。 有谁知道解决这个问题的方法吗?
【问题讨论】:
-
试试
print_r($row):输出是什么?更多:在第二个示例中,我没有看到 $found 和 $post 初始化... -
请定义“不起作用”?
-
所以,您的查询是错误的,或者没有返回您所期望的我认为...如果您希望我们帮助您,请向我们展示一些代码!!!
-
@Marco 添加了查询,查询有效,我用 while 循环并检查了它。
-
任何时候循环的主体都是
if语句,这都是一种浪费,尤其是当if的主体只会被输入一次时。如果您只对 5 条记录感兴趣,请仅获取这 5 条记录。