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

    }

?>

And this is my function

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;

}

Browser prints this error message:

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\glasanje\index.php on line 111

But i can't seem to find a way to solve this problem :S Help?

2
  • I Think you have your function messed up. I do not see $glasanje = array(); being used after it is set
    – Daryl Gill
    Mar 29, 2013 at 14:40
  • Marko, Please update your question with code. I wont sit through and format the code you have posted in the text box
    – Daryl Gill
    Mar 29, 2013 at 14:43

2 Answers 2

1

Your function is overwriting $rezultati. I would suggest rewriting it as:

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;
}
3
  • Isn't $glasanje = array(); useless?
    – dansaania
    Mar 29, 2013 at 14:46
  • Because the return is outside of the whileloop, set to return $rows; wouldn't it return only the last param stepped through that while loop? Shouldn't you append it to $glasanje[] = array ($rows); then return it?
    – Daryl Gill
    Mar 29, 2013 at 14:48
  • Daryl Gill, so long as $row['id'] is unique on each iteration, all rows will be returned.
    – webbiedave
    Mar 29, 2013 at 14:53
0

iscupaj_viceve() doesn't always return an array.

$glasanje = array();

should be

$rezultati = array();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.