$story_query = "SELECT table_name, id FROM planning WHERE parent = '$novelnum'";
$story_result = db_query($story_query);
while($story_row = db_fetch_array($story_result)) {
$taleTable_Name = $story_row['table_name'];
$postid[] = $story_row['id'];
$q2 = "Select * from $taleTable_Name where approved='Y' order by id";
$bset2 = db_query($q2);
while($rset2 = db_fetch_array($bset2)) {
$i[] = $rset2['id'];
$t[] = $rset2['thread'];
$s[] = $rset2['subject'];
$a[] = $rset2['author'];
$d[] = $rset2['datestamp'];
}
}
if(isset($d)) {
$fc = count($d);
if($fc > 20) {
$xs = $fc - 20;
}
else {
$xs = 0;
}
for($c=$xs;$c<$fc;$c++) {
if($s[$c] != "") {
$newpost .= $d[$c];
$newpost .= " <a href='../forums/read.php?f=";
$newpost .= end($postid);
$newpost .= "&i=";
$newpost .= $i[$c];
$newpost .= "&t=";
$newpost .= $t[$c];
$newpost .= "'>" ;
$newpost .= $s[$c];
$newpost .= "</a> by ";
$newpost .= $a[$c];
$newpost .= $taleTable_Name;
$newpost .= "<br>\n";
}
}
}
else {
$newpost = "There are no posts for this scroll yet.";
}
上面的代码正确地向我展示了使用$ taleTable_Name找到的所有条目,但是当我打印$ newpost时,它只显示$ postid的最后一个变量。我想要id($ postid匹配table_name($ taleTable_Name),以便创建的url实际上进入正确的论坛。
答案 0 :(得分:3)
首先,我不知道你的变量名是什么意思。我试过猜测,但你应该考虑重命名它们。为了保持维护程序员的理智。
查看你的代码,我知道它在做什么,但我不知道它应该做什么,或者为什么。下面是尝试重命名您的变量名称,但我不知道它是否正确,或接近右侧:
$story_query = "SELECT table_name, id FROM planning WHERE parent = '$novelnum'";
$story_result = db_query($story_query);
while($story_row = db_fetch_array($story_result)) {
$taleTable_Name = $story_row['table_name'];
$postid[] = $story_row['id'];
$query2 = "Select * from $taleTable_Name where approved='Y' order by id";
$bset2 = db_query($query2);
while($rowset2 = db_fetch_array($bset2)) {
$id[] = $rowset2['id'];
$thread[] = $rowset2['thread'];
$subject[] = $rowset2['subject'];
$author[] = $rowset2['author'];
$datetime[] = $rowset2['datestamp'];
}
}
if(isset($datetime)) {
$forumcount = count($datetime);
if($forumcount > 20) {
$xs = $forumcount - 20;
}
else {
$xs = 0;
}
for($postnum=$xs;$postnum<$forumcount;$postnum++) {
if($subject[$postnum] != "") {
$newpost .= $datetime[$postnum];
$newpost .= " <a href='../forums/read.php?f=";
$newpost .= end($postid);
$newpost .= "&id=";
$newpost .= $id[$postnum];
$newpost .= "&thread=";
$newpost .= $thread[$postnum];
$newpost .= "'>" ;
$newpost .= $subject[$postnum];
$newpost .= "</a> by ";
$newpost .= $author[$postnum];
$newpost .= $taleTable_Name;
$newpost .= "<br>\n";
}
}
}
else {
$newpost = "There are no posts for this scroll yet.";
}
以下变量是什么意思? :$xs
,$c
,&i
,&t
和$fc
?这些应该是有意义的名字。我已经重命名了你的其他变量,但对于这些我猜对了。我可能会离开。有意义的变量名称可以帮助某人阅读没有注释的代码。事实上,如果你有一个有意义的变量名,I may not need comments - 虽然'20'的magic number在做什么?这个号码有什么意义呢?
答案 1 :(得分:0)
我注意到你的代码有一件事是没有打印出$ newpost的地方。在for循环结束之前,需要使用$ newpost完成某些操作。在这种情况下,您只是每次丢弃结果(或更正确地,覆盖它)。您需要使用$ newpost(打印它,将其存储在另一个值,将其发送回浏览器等)。
答案 2 :(得分:0)
怎么样?
unset($post); // ensure start afresh
$story_query = "SELECT table_name, id FROM planning WHERE parent = '$novelnum'";
$story_result = db_query($story_query);
while($story_row = db_fetch_array($story_result)) {
$taleTable_Name = $story_row['table_name'];
$postid = $story_row['id'];
$q2 = "Select * from $taleTable_Name where approved='Y' order by id";
$bset2 = db_query($q2);
while($rset2 = db_fetch_array($bset2)) {
$post[$postid] = $rset2;
}
}
if(isset($post)) {
/***
* Page control here
****/
foreach($post as $id => $msg) {
if($row['subject'] != "") {
$newpost .= $msg['datestamp'];
$newpost .= " <a href='../forums/read.php?f=";
$newpost .= $id;
$newpost .= "&i=".$msg['id'];
$newpost .= "&t=".$msg['thread'];
$newpost .= "'>" ;
$newpost .= $msg['subject'];
$newpost .= "</a> by ";
$newpost .= $msg['author'];
$newpost .= $taleTable_Name;
$newpost .= "<br>\n";
}
}
}
else {
$newpost = "There are no posts for this scroll yet.";
}