Notice: Undefined index: Error help again please

Hi again everyone another question again sorry, here’s an error we’re getting on viewing results after voting on a poll:

Notice: Undefined index: pollId in E:\Apache\N00090291\UVote\viewPollResults.php on line 8
Fatal error: Call to a member function getChoice1() on a non-object in E:\Apache\N00090291\UVote\viewPollResults.php on line 50

<?php
require_once 'Result.php';
require_once 'ResultDAO.php';
require_once 'PollDAO.php';
require_once 'Poll.php';


$poll_id = $_GET['pollId'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html"/>
        <title>Results</title>
        <link rel="stylesheet" type="text/css" href="myStyle.css"/>
    </head>
    <body>
        <div id="pageWrap"><!--Start Page Wrap-->
            <div id="logo2"><a href="index.php"><img src="images/main_logo.png"/></a></div><!--Logo Here-->
            <!--My Account/Sign Out buttons here -->
            <div id="account">
                <ul>
                    <li><a href="myAccount.php"><img id="acimg" src="images/account.png"/></a></li>
                    <li><a href="logout.php"><img id="sigimg" src="images/signOut.png"/></a></li>
                </ul>
            </div>
            <!--My Account/Sign Out buttons here -->
            <div id="content">
                <div id="navbar">
                    <ul>
                        <li><a href="createPollForm.php">Create Poll</a></li>
                        <li><a href="viewPolls.php" >View Polls</a></li>
                        <li><a href="friends.php" id="onlink1">Friends</a></li>
                    </ul>
                </div>
                <div id="polresult"><!--Poll result div-->
                    <legend>Polls Results</legend>
                    <!-- Poll Results List here -->

                    <?php
                    $resultDAO = new ResultDAO();
                    $results = $resultDAO->getPollResults($poll_id);
                    $Choice1 = $resultDAO->getchoice1($poll_id);
                    $Choice2 = $resultDAO->getchoice2($poll_id);
                    $Choice3 = $resultDAO->getchoice3($poll_id);
                    $Choice4 = $resultDAO->getchoice4($poll_id);

                    $pollDAO = new PollDAO();
                    $polls = $pollDAO->getPoll($poll_id);
                    $choiceName1 = $polls->getChoice1();
                    $choiceName2 = $polls->getChoice2();
                    $choiceName3 = $polls->getChoice3();
                    $choiceName4 = $polls->getChoice4();

                    $numVotes = count($Choice1) + count($Choice2) + count($Choice3) + count($Choice4);

                    $percentChoice1 = ($numVotes != 0) ? ((float) $Choice1) / $numVotes : 0;
                    $percentChoice2 = ($numVotes != 0) ? ((float) $Choice2) / $numVotes : 0;
                    $percentChoice3 = ($numVotes != 0) ? ((float) $Choice3) / $numVotes : 0;
                    $percentChoice4 = ($numVotes != 0) ? ((float) $Choice4) / $numVotes : 0;
                    ?>
                    </ul>
                    <ul>
                        <li>
                            <span class="total-votes"><?php echo count($Choice1); ?>%</span><?php echo $choiceName1; ?>
                            <br/>
                            <div class="results-bar" style="width: <?php echo round($percentChoice1, 2); ?>%">
                            </div>
                        </li>
                        <li>
                            <span class="total-votes"><?php echo count($Choice2); ?>%</span><?php echo $choiceName2; ?>
                            <br/>
                            <div class="results-bar" style="width: <?php echo round($percentChoice2, 2); ?>%;">
                            </div>
                        </li>
                        <li>
                            <span class="total-votes"><?php echo count($Choice3); ?>%</span><?php echo $choiceName3; ?>
                            <br/>
                            <div class="results-bar" style="width: <?php echo round($percentChoice3, 2); ?>%;">
                            </div>
                        </li>
                        <li>
                            <span class="total-votes"><?php echo count($Choice4); ?>%</span><?php echo $choiceName4; ?>
                            <br/>
                            <div class="results-bar" style="width: <?php echo round($percentChoice4, 2); ?>%;">
                            </div>
                        </li>
                    </ul>
                    <h5>Total votes:<?php echo $numVotes; ?></h5>
                    <a href="viewPolls.php">Back to View Polls</a>
                </div><!--Poll result div ends here-->
            </div>
        </div>
        <div id="footer">
            <h3>UVote © 2012</h3>
            <ul>
                <li><a href="about.php">About </a>-</li>
                <li><a href="contactForm.php">Contact Us </a></li>
            </ul>
        </div>
    </body>
</html>
<?php ?>


Any suggestions would be greatly welcomed thanks again for the help with the last error… :slight_smile:

It’s more than likely because $_GET[‘pollId’] does not exist. If you append the url it should go away.

http://www.sample.com/viewPollResults.php?pollId=1

To avoid this you can check the $_GET array index with the isset(), then set a default value if it does not exist.

if( isset( $_GET['pollId'] ) )
{
   // assign value
}
else
{
   // assign default
}

As for the error on line 50, you will have to look at how your objects are working. It is trying to call an object that has not been instantiated.

Thanks for the advice JeremyC I’ll give that a try :), thanks again for all your help and quick replies :slight_smile:

Or you could use the ternary operator:


$pollId = isset($_GET['pollId']) ? $_GET['pollId'] : 1;

Two other remarks about your code if I may :

  1. It’s quite repetitive, you can clean it up to be more compact. If you want/need any help with that just shout.
  2. It’s hard coded to 4 choices. Aren’t there ever more than 4?

Thanks for the help man, we tracked down an the issue to a votePollForm.php and in the form action we’ve forgotten to echo the pollID so it wasn’t being found in the poll results page.

We just entered this piece of code and boom project is working fully now:

<form action="viewPollResults.php?pollId=<?php echo $poll->getPollId(); ?>" id="votePoll" name="votePoll" method="POST">

JermeyC thanks for all the help! :slight_smile:

You may indeed ScallioXTX criticism is welcome to help us improve we’re only started PHP this year in college so we’re still new too it all. As with regards your suggestions we’ll definatley think about changing the amount of choices, we just decided to keep it to 4 at the moment and change it at a later stage and maybe allow a user to choose between the amount of choices with maybe a max amout of 10 or something.

Really liking this site and the forums are super helpful. Thanks again everyone! :slight_smile: