Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php]Invalid argument supplied for foreach()
Forum PHP.pl > Forum > Przedszkole
Duke Nukem
Witam!
Wyskakuje mi taki blad:
Invalid argument supplied for foreach() i wskazuje on linie na ten kod:
Kod
foreach ($arrVotes[$i] as $intVote)
        {
            if ($intVote && $arrSumvotes[$i])
            {
                $arrPercentvotes[$i][$j] = ($intVote / $arrSumvotes[$i]) * 100;
                $arrPercentvotes[$i][$j] = round($arrPercentvotes[$i][$j]);
            }
                else
            {
                $arrPercentvotes[$i][$j] = 0;
            }
            $j++;
        }


Co jest zle? Czemu to nie dziala?
nospor
to znaczy ze $arrVotes[$i] nie jest tablicą.

i prosze nie duplikowac tematow
Duke Nukem
$arrVotes jest tablica
no bo jest tak: $arrVotes = array(array());
Co poczac? Jakies inne pomysly? Czy to moze miec zwiazek z wersja php?
nospor
no wlasnie: $arrVotes = array(array());
to oznacza, ze $arrVotes[0] jest tablicą;
ale juz $arrVotes[$i] nie jest tablicą, gdyż $i to na przyklad 1
Duke Nukem
czyli wystarczy zmienic $arrVotes = array(array()); na $arrVotes = array();??
nospor
a zamien, wtedy cala logike twojego skryptu szlag trafi... smile.gif
sluchaj, to ty powinienes wiedziec co chciales osiągnąc a nie ja. a jesli nie wiesz to pokaz wiekszy kawalek kodu, to moze cos sie wymysli
Duke Nukem
Kod
/**
* Show active poll
*/
if (!isset($_GET['action']))
{
    if (!$objPollid -> fields['id'])
    {
        $smarty -> assign("Pollid", 0);
    }
        else
    {
        $objPoll = $db -> Execute("SELECT `poll`, `votes`, `days`, `members` FROM `polls` WHERE `id`=".$objPollid -> fields['id']);
        $arrPoll = array();
        $arrVotes = array();
        $intVotes = 0;
        $i = 0;
        while (!$objPoll -> EOF)
        {
            if ($objPoll -> fields['votes'] < 0)
            {
                $strQuestion = $objPoll -> fields['poll'];
                $intDays = $objPoll -> fields['days'];
                $intMembers = $objPoll -> fields['members'];
            }
                else
            {
                $arrPoll[$i] = $objPoll -> fields['poll'];
                $arrVotes[$i] = $objPoll -> fields['votes'];
                $intVotes = $intVotes + $objPoll -> fields['votes'];
                $i++;
            }
            $objPoll -> MoveNext();
        }
        /**
        * Count percent for each option
        */
        $arrPercentvotes = array();
        $i = 0;
        foreach ($arrVotes as $intVote)
        {
            if ($intVote && $intVotes)
            {
                $arrPercentvotes[$i] = ($intVote / $intVotes) * 100;
                $arrPercentvotes[$i] = round($arrPercentvotes[$i]);
            }
                else
            {
                $arrPercentvotes[$i] = 0;
            }
            $i++;
        }
        /**
        * Count percent for voting players
        */
        if ($intDays)
        {
            $objQuery = $db -> Execute("SELECT count(*) FROM `players`");
            $intMembers = $objQuery -> fields['count(*)'];
            $objQuery -> Close();
            $fltVoting = ($intVotes / $intMembers) * 100;
        }
            else
        {
            $fltVoting = ($intVotes / $intMembers) * 100;
        }
        $fltVoting = round($fltVoting, 2);
        /**
        * Check for aviable for voting
        */
        if ($intDays)
        {
            $strVoting = 'Y';
        }
            else
        {
            $strVoting = 'N';
        }
        if ($player -> poll == 'N' && $strVoting == 'Y')
        {
            $strVoting = 'Y';
        }
            else
        {
            $strVoting = 'N';
        }
        $objPoll -> Close();
        /**
         * Check amount of comments to poll
         */
        $objComments = $db -> Execute("SELECT count(*) FROM `polls_comments` WHERE `pollid`=".$objPollid -> fields['id']);
        $intComments = $objComments -> fields['count(*)'];
        $objComments -> Close();
        $smarty -> assign(array("Pollid" => $objPollid -> fields['id'],
            "Question" => $strQuestion,
            "Answers" => $arrPoll,
            "Voting" => $strVoting,
            "Votes" => $arrVotes,
            "Summaryvotes" => $intVotes,
            "Percentvotes" => $arrPercentvotes,
            "Summaryvoting" => $fltVoting,
            "Commentsamount" => $intComments,
            "Acomments" => A_COMMENTS,
            "Days" => $intDays));
    }
}

/**
* Vote in poll
*/
if (isset($_GET['action']) && $_GET['action'] == 'vote')
{
    if (!isset($_POST['answer']))
    {
        error(ERROR);
    }
    if (!(int)$_GET['poll'] || $_GET['poll'] != $objPollid -> fields['id'])
    {
        error(ERROR);
    }
    if ($player -> poll == 'Y')
    {
        error(ERROR);
    }
    $strAnswer = $db -> qstr($_POST['answer'], get_magic_quotes_gpc());
    $db -> Execute("UPDATE `polls` SET `votes`=`votes`+1 WHERE `id`=".$_GET['poll']." AND `poll`=".$strAnswer);
    $db -> Execute("UPDATE `players` SET `poll`='Y' WHERE `id`=".$player -> id);
    $smarty -> assign("Message", VOTE_SUCC);
}

$objPollid -> Close();

/**
* Show last 10 polls
*/
if (isset($_GET['action']) && $_GET['action'] == 'last')
{
    $objPollsid = $db -> SelectLimit("SELECT `id` FROM `polls` WHERE `lang`='".$player -> lang."' AND `votes`=-1 ORDER BY `id` DESC", 10) or $db -> ErrorMsg();
    $arrQuestions = array();
    $arrPolls = array(array());
    $arrVotes = array(array());
    $arrSumvotes = array();
    $arrPercentvotes = array(array());
    $arrPercentmembers = array();
    $arrPercentvoting = array();
    $arrCommenst = array();
    $arrPollid = array();
    $i = 0;
    while (!$objPollsid -> EOF)
    {
        $j = 0;
        $objPoll = $db -> Execute("SELECT `poll`, `votes`, `members` FROM `polls` WHERE `id`=".$objPollsid -> fields['id']);
        while (!$objPoll -> EOF)
        {
            if ($objPoll -> fields['votes'] < 0)
            {
                $arrQuestions[$i] = $objPoll -> fields['poll'];
                $arrSumvotes[$i] = 0;
                if ($objPoll -> fields['members'])
                {
                    $intMembers = $objPoll -> fields['members'];
                }
                    else
                {
                    $objQuery = $db -> Execute("SELECT count(*) FROM `players`");
                    $intMembers = $objQuery -> fields['count(*)'];
                    $objQuery -> Close();
                }
            }
                else
            {
                $arrPolls[$i][$j] = $objPoll -> fields['poll'];
                $arrVotes[$i][$j] = $objPoll -> fields['votes'];
                $arrSumvotes[$i] = $arrSumvotes[$i] + $objPoll -> fields['votes'];
                $j++;
            }
            $objPoll -> MoveNext();
        }
        $objPoll -> Close();
        /**
        * Count percent for each option
        */
        $j = 0;
        foreach ($arrVotes[$i] as $intVote)
        {
            if ($intVote && $arrSumvotes[$i])
            {
                $arrPercentvotes[$i][$j] = ($intVote / $arrSumvotes[$i]) * 100;
                $arrPercentvotes[$i][$j] = round($arrPercentvotes[$i][$j]);
            }
                else
            {
                $arrPercentvotes[$i][$j] = 0;
            }
            $j++;
        }
        /**
        * Count percent for voting players
        */
        if ($arrSumvotes[$i] && $intMembers)
        {
            $arrPercentvoting[$i] = ($arrSumvotes[$i] / $intMembers) * 100;
            $arrPercentvoting[$i] = round($arrPercentvoting[$i], 2);
        }
            else
        {
            $arrPercentvoting[$i] = 0;
        }
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.