php - Notice: Undefined offset: 0 in -
i getting php error, mean?
notice: undefined offset: 0 in c:\xampp\htdocs\mywebsite\reddit_vote_tut\src\votes.php on line 41
from code:
<?php include("config.php"); function getallvotes($id) { $votes = array(); $q = "select * entries id = $id"; $r = mysql_query($q); if(mysql_num_rows($r)==1)//id found in table { $row = mysql_fetch_assoc($r); $votes[0] = $row['votes_up']; $votes[1] = $row['votes_down']; } return $votes; } function geteffectivevotes($id) { $votes = getallvotes($id); $effectivevote = $votes[0] - $votes[1]; //error thrown here return $effectivevote; } $id = $_post['id']; $action = $_post['action']; //get current votes $cur_votes = getallvotes($id); //ok, update votes if($action=='vote_up') //voting { $votes_up = $cur_votes[0]+1; //and error thrown here $q = "update threads set votes_up = $votes_up id = $id"; } elseif($action=='vote_down') { $votes_down = $cur_votes[1]+1; $q = "update threads set votes_down = $votes_down id = $id"; } $r = mysql_query($q); if($r) { $effectivevote = geteffectivevotes($id); echo $effectivevote." votes"; } elseif(!$r) //voting failed { echo "failed!"; } ?>
you asking value @ key 0
of $votes
. array not contain key.
the array $votes
not set, when php trying access key 0
of array, encounters undefined offset [0] , [1] , throws error.
if have array:
$new_array = array('1','2','3');
we can access:
$new_array[0]; $new_array[1]; $new_array[2];
if try , access:
$new_array[3];
we error "notice: undefined offset: 0"
Comments
Post a Comment