首先,我的英语并不完美,所以尽量解释我的问题。所以我有一个问题,为foreach()提供的无效参数,我不知道什么是错的。我想创建一个评论系统,我可以在其中放置一个注册用户。如果您有任何想法或工作评论系统,我可以使用我的数据库与用户,我将不胜感激。
这是我的代码
<?php
function get_comments($file_id) {
require 'polaczenie.php';
$result = "SELECT * FROM `comments` WHERE `file_id`='$file_id' AND `is_child`=FALSE ORDER BY `date` DESC";
$row_cnt =mysql_query($result);
echo '<h1>Comments ('.$row_cnt.')</h1>';
echo '<div class="comment">';
new_comment();
echo '</div>';
foreach($result as $item) {
$date = new dateTime($item['date']);
$date = date_format($date, 'M j, Y | H:i:s');
$auth = $item['author'];
$par_code = $item['com_code'];
$chi_result ="SELECT * FROM `comments` WHERE `par_code`='$par_code' AND `is_child`=TRUE";
$chi_cnt = mysql_query($chi_result);
echo '<div class="comment" name="'.$item['com_code'].'">'
.'<span class="author">'.$auth.'</span><br />'
.$item['comment'].'<br />'
.'<span class="date">Posted: '.$date.'</span><br />';
if($chi_cnt == 0) {
echo '<span class="replies">No replies</span>'
.'<span class="replies"> Reply</span>';
} else {
echo '<span class="replies">[+] '.$chi_cnt.' replies</span>'
.'<span class="replies" Reply</span>';
add_comment($item['author'], $item['com_code']);
echo '<div name="children" id="children">';
foreach($chi_result as $com) {
$chi_date = new dateTime($com['date']);
$chi_date = date_format($chi_date, 'M j, Y | H:i:s');
echo '<div class="child" name="'.$com['com_code'].'">'
.'<span class="author">'.$com['author'].'</span><br />'
.$com['comment'].'<br />'
.'<span class="date">Posted: '.$chi_date.'</span><br />'
.'</div>';
}
echo '</div>';
}
echo '</div>';
}
mysql_close($conn);
}
function add_comment($reply, $code) {
echo '<form action="reply.php" method="post" enctpye="" name="new_comment">'
.'<input type="hidden" name="par_code" value="'.$code.'" />'
.'<textarea class="text_cmt" name="text_cmt" placeholder="Reply to '.$reply.'"></textarea><br />'
.'<input type="submit" value="Reply" />'
.'</form>';
}
function new_comment() {
echo '<form action="new.php" method="post" enctpye="" name="new_comment">'
.'<textarea class="text_cmt" name="text_cmt" placeholder="Post a new comment"></textarea><br />'
.'<input type="submit" value="Post" />'
.'</form>';
}
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$characterLength = strlen($characters);
$randomString = '';
for($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $characterLength - 1)];
}
return $randomString;
}
function checkString($com_code) {
include 'database.php';
$rand = generateRandomString();
$result ="SELECT * FROM `comments` WHERE `com_code`='$com_code'";
$row_cnt = mysql_query($result);
if($row_cnt != 0) {
return $rand;
} else {
checkString($rand);
}
}
&GT;
答案 0 :(得分:0)
可能
foreach($result as $item) {
应该是
foreach($row_cnt as $item) {
您正在向foreach函数传递查询字符串。
答案 1 :(得分:0)
这里,$ result只是一个包含查询的变量。 foreach循环需要一个可以遍历的数组或对象。你应该使用while循环。
示例场景:
$result = "SELECT * FROM `comments` WHERE `file_id`='$file_id' AND `is_child`=FALSE ORDER BY `date` DESC";
$comments = mysql_query($result);
while($comment = mysql_fetch_array($comments))
{
//Do whatever you want with your each comment
}
我希望这会有所帮助:) 或者,您可以按照此处W3schools mySql Select with php while loop
的步骤操作