Glese Posted November 30, 2011 Share Posted November 30, 2011 Here is an example of the beginning of a rating system script: // RATING SYSTEM - START if ((isset($array[0]) || isset($array[1])) && isset($_SESSION['user_id'])) { // check to see if user has already voted on given contribution $query = "SELECT * FROM votes WHERE con_id = '$array[2]' AND user_id = '$array[3]'"; $query_run = mysqli_query ($dbc, $query) or die (mysqli_error($dbc)); $num_rows = mysqli_num_rows ($query_run); $assoc = mysqli_fetch_assoc ($query_run); $rating = $assoc['rating']; // check to see if it's user's own contribution $query = "SELECT * FROM con WHERE con_id = '$array[2]'"; $query_run = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc)); $assoc = mysqli_fetch_assoc ($query_run); $con_user_id = $assoc['user_id']; For every array variable I am getting the error message "Undefined Offset". Any ideas how to solve this one, I do not fully know yet what is common practice in these cases. The warnings also do not always happen, they usually happen, they happen in a specific moment when I use a drop down menu, to show table content. Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/ Share on other sites More sharing options...
KevinM1 Posted November 30, 2011 Share Posted November 30, 2011 Undefined offset means you don't have a value in array[n], where n is the offset. Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1292591 Share on other sites More sharing options...
Glese Posted November 30, 2011 Author Share Posted November 30, 2011 Excuse my confusion, I am a new comer, so how would I avoid the notice, obviously I am getting the notice, because the function is not in use, thus the variable is empty, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1292614 Share on other sites More sharing options...
KevinM1 Posted November 30, 2011 Share Posted November 30, 2011 Well, where does $array come from? Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1292615 Share on other sites More sharing options...
ManiacDan Posted November 30, 2011 Share Posted November 30, 2011 if ((isset($array[0]) || isset($array[1])) && isset($_SESSION['user_id'])) { // check to see if user has already voted on given contribution $query = "SELECT * FROM votes WHERE con_id = '$array[2]' AND user_id = '$array[3]'"; That section checks for array elements zero and one, and then uses array elements two and three. If we had the actual error message, we might be able to tell if that's the problem. Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1292617 Share on other sites More sharing options...
Glese Posted November 30, 2011 Author Share Posted November 30, 2011 This is the actual error message: Notice: Undefined offset: 2 in C:\xampp\htdocs\php_projects\myproject\controller\rating_system\rating_system_func.php on line 14 Notice: Undefined offset: 3 in C:\xampp\htdocs\php_projects\myproject\controller\rating_system\rating_system_func.php on line 14 Notice: Undefined offset: 2 in C:\xampp\htdocs\php_projects\myproject\controller\rating_system\rating_system_func.php on line 22 Notice: Undefined offset: 2 in C:\xampp\htdocs\php_projects\myproject\controller\rating_system\rating_system_func.php on line 64 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1[b][/b] Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1292648 Share on other sites More sharing options...
KevinM1 Posted November 30, 2011 Share Posted November 30, 2011 That's telling you, and confirms Dan's suspicions, that your $array only has two elements - $array[0] and $array[1]. So, again, where does $array come from? What would lead you to believe it would have additional elements? Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1292650 Share on other sites More sharing options...
Glese Posted November 30, 2011 Author Share Posted November 30, 2011 Array comes from this function, you can see array at the very bottom: function knuffix_list ($query, $dbc) { $data = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc)); // Loop through the array of data while ($row = mysqli_fetch_array ($data)) { global $array; // Variables for the table $con_id = $row['con_id']; $likes_count = $row['likes']; $dislikes_count = $row['dislikes']; $dbuser_name = $row['nickname']; $user_id = $row['user_id']; $contribution = $row['contribution']; $title = $row['name']; $avatar_file_name = $row['avatar']; $avatar_folder = "../profile/avatar/"; // The TABLE include('character_artwork_table.php'); // POST BUTTONS inside the table if (isset($_POST['likes'])) $likes = $_POST['likes']; if (isset($_POST['dislikes'])) $dislikes = $_POST['dislikes']; if (isset($_POST['hidden_con_id'])) { $con_id = $_POST['hidden_con_id']; //$favorite = $_POST['favorite']; } // $array = array ($likes, $dislikes, $con_id, $user_id); if (isset($likes)) $array[] = $likes; if (isset($dislikes)) $array[] = $dislikes; if (isset($con_id)) $array[] = $con_id; if (isset($user_id)) { $array[] = $user_id; } } mysqli_close($dbc); } Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1292680 Share on other sites More sharing options...
ManiacDan Posted November 30, 2011 Share Posted November 30, 2011 So at least two of those are unset. See how that works? Your array is randomly one or more of those 4 items, yet your function assumes all 4 will be there. Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1292703 Share on other sites More sharing options...
KevinM1 Posted November 30, 2011 Share Posted November 30, 2011 Also, slight digression, but never use the 'global' keyword. Use a function's argument list to pass in all parameters. Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1292708 Share on other sites More sharing options...
ManiacDan Posted November 30, 2011 Share Posted November 30, 2011 Also, slight digression, but never use the 'global' keyword. Use a function's argument list to pass in all parameters. That too. Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1292725 Share on other sites More sharing options...
Glese Posted December 1, 2011 Author Share Posted December 1, 2011 So at least two of those are unset. See how that works? Your array is randomly one or more of those 4 items, yet your function assumes all 4 will be there. if (isset($likes)) $array[] = $likes; if (isset($dislikes)) $array[] = $dislikes; if (isset($con_id)) $array[] = $con_id; if (isset($user_id)) { $array[] = $user_id; } What I do not understand is, the code is stating IF it is set, do assign it to the array, so if it is not set, do not assign it to the array, in this sense, I should not get the notice, as I stated I usually, do not get the notice only in a specific situation, when I do choose certain entries from a drop down menu, and with other entries I do not get the error. Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1292926 Share on other sites More sharing options...
Glese Posted December 1, 2011 Author Share Posted December 1, 2011 I solved it by putting an isset condition check around everything possible. Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1292930 Share on other sites More sharing options...
ManiacDan Posted December 1, 2011 Share Posted December 1, 2011 You're exactly right about the first part, then you get wrong. When you create the array, you do an isset call and only add to the array when the item is set. Yes, exactly right. Then you attempt to use the fourth element in the array. What happens if that's not set? You get the error. Also note that this code has no way of telling which variable is which in this array. You assume that $array[2] is con_id. What if $dislikes isn't set? Then con_id will be in $array[1] You need to rethink this whole thing and look into naming your array keys, so you have $array['con_id'] instead of $array[2] Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1293039 Share on other sites More sharing options...
Glese Posted December 1, 2011 Author Share Posted December 1, 2011 You need to rethink this whole thing and look into naming your array keys, so you have $array['con_id'] instead of $array[2] Is that what you would recommend in general, or only specifically in this case? Thanks for the recommendation though, I did not even notice that one. Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1293048 Share on other sites More sharing options...
ManiacDan Posted December 1, 2011 Share Posted December 1, 2011 In general, if you're storing specific variables in an array, name the indexes so you know which is which. If you're storing a bunch of variables that are all the same kind, you don't need to name them. Quote Link to comment https://forums.phpfreaks.com/topic/252122-undefined-offset-notice-warning/#findComment-1293068 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.