0
<?php

    foreach (iscupaj_viceve() as $id => $glasanje){

        $result = mysql_query("SELECT * FROM vicevi WHERE id = $id");
        while ($row = mysql_fetch_array($query)) {
            echo    "<h2><center>".$row['Title']."</h2>";
            echo    "<div id='linkovi1'>" .nl2br($row["VicText"]). "<br></div>";
        }
?>
    <p>

            <a href="?glasanje=gore&id=<?php echo $id; ?>"><img src="../images/plus_sign.png" width="29" height="29"></a>
            <a href="?glasanje=dolje&id=<?php echo $id; ?>"><img src="../images/minus_sign.png" width="29" height="29"></a>
        Ukupno glasova : [
        <?php
            include 'core/db/connect.php';
            // Check connection
            $result = mysql_query("SELECT glasanje FROM vicevi WHERE id = $id");

            while($row = mysql_fetch_array($result))
            {
            echo nl2br($row['glasanje']);
            }
        ?>]
    </p>
<?php

    }

?>

这是我的功能

function iscupaj_viceve(){


$sql = "SELECT id, VicText, glasanje FROM vicevi ORDER BY id DESC LIMIT 0, 10";

$rezultati = mysql_query($sql);
$glasanje = array();

while (($row = mysql_fetch_assoc($rezultati)) !== false){
    $rezultati[$row['id']] = $row['VicText'];
}
return $rezultati;

}

浏览器打印此错误消息:

警告:在第 111 行的 C:\xampp\htdocs\glasanje\index.php 中为 foreach() 提供的参数无效

但我似乎找不到解决这个问题的方法:S 帮助?

4

2 回答 2

1

您的功能正在覆盖$rezultati。我建议将其重写为:

function iscupaj_viceve() {

    $rows = array();

    $sql = "SELECT id, VicText, glasanje FROM vicevi ORDER BY id DESC LIMIT 0, 10";

    $rezultati = mysql_query($sql);

    while (($row = mysql_fetch_assoc($rezultati)) !== false) {
        $rows[$row['id']] = $row['VicText'];
    }

    return $rows;
}
于 2013-03-29T14:44:35.413 回答
0

iscupaj_viceve() 并不总是返回一个数组。

$glasanje = array();

应该

$rezultati = array();
于 2013-03-29T14:43:38.790 回答