BenGoldberg Posted August 15, 2008 Share Posted August 15, 2008 Hi, I have a script that compares integers and ranks them from lowest to highest, and then submits those rankings to a mysql table. If two or more integers are the same, I have it so they're ranked the same and the next integer is ranked where it would appear if there was no tie, example... $a = 1 $b = 1 $c = 2 $a and $b would be ranked 1st because they're tied for the lowest value while $c would be ranked third, not second. Anyways, I created a relatively simple function to do this and in testing, it's outputting the values I expected it to. The only thing is that it's giving me undefined offset errors, but I don' see why. Here's the function... <?php /* FUNCTION TO ENTER RANKINGS INTO DATABASE */ function post_rankings() { $result = mysql_query("SELECT * FROM auf_records") or die(mysql_error()); $u = 17; $x = 18; $y = 19; while ($u < mysql_num_fields($result)) { /* RETRIEVE DATA FROM TABLE "AUF_RECORDS" */ $level = mysql_field_name($result, $u); $date = mysql_field_name($result, $x); $recrank = mysql_field_name($result, $y); $result2 = mysql_query("SELECT $level, $date, id FROM auf_records") or die(mysql_error()); while ($row = mysql_fetch_row($result2)) { if ($row[0] > 0) { $records[] = $row; } } if (isset($records)) { sort($records); $i = 0; $numrec = count($records[0]); while ($i < $numrec) { $uid = $records[$i][2]; // FIRST UNDEFINED OFFSET if ($i == 0) { $rank = $i + 1; mysql_query("UPDATE auf_records SET $recrank = '$rank' WHERE id = '$uid' ") or die(mysql_error()); } else { $a = $i - 1; if ($records[$i][0] == $records[$a][0]) { // SECONDS UNDEFINED OFFSET $prevuid = $records[$a][2]; // THIRD UNDEFINED OFFSET $result3 = mysql_query("SELECT $recrank FROM auf_records WHERE id = '$prevuid' ") or die(mysql_error()); $row2 = mysql_fetch_row($result3); $rank = $row2[0]; mysql_query("UPDATE auf_records SET $recrank = '$rank' WHERE id = '$uid' ") or die(mysql_error()); } else { $rank = $i + 1; mysql_query("UPDATE auf_records SET $recrank = '$rank' WHERE id = '$uid' ") or die(mysql_error()); } } $i++; } } $u = $u + 3; $x = $x + 3; $y = $y + 3; unset($records); } } ?> I'm getting Undefined offset: 1 for all three undefined offsets but only Undefined offset: 2 for the first two undefined offsets. If $records isn't set if (isset($records)) should take care of that, so why am I getting this error. I'm sure it's something simple, but I can't find it by myself. Any help would be greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/119774-solved-getting-undefined-offset-error-but-dont-see-why/ Share on other sites More sharing options...
btherl Posted August 15, 2008 Share Posted August 15, 2008 Can you add this code after setting $records: var_dump($records); And show us what it displays. Link to comment https://forums.phpfreaks.com/topic/119774-solved-getting-undefined-offset-error-but-dont-see-why/#findComment-617058 Share on other sites More sharing options...
BenGoldberg Posted August 15, 2008 Author Share Posted August 15, 2008 Well it's displaying a lot, but here's the first two arrays it displays... array(3) { [0]=> array(3) { [0]=> string(3) "106" [1]=> string(11) "08 15, 2008" [2]=> string(1) "1" } [1]=> array(3) { [0]=> string(3) "106" [1]=> string(11) "08 14, 2008" [2]=> string(1) "2" } [2]=> array(3) { [0]=> string(3) "108" [1]=> string(11) "08 14, 2008" [2]=> string(1) "3" } } array(1) { [0]=> array(3) { [0]=> string(3) "107" [1]=> string(11) "08 15, 2008" [2]=> string(1) "1" } } It then has 6 undefined offsets and then more arrays and errors... The values that var_dump are displaying are the ones I expected. Does this help? Link to comment https://forums.phpfreaks.com/topic/119774-solved-getting-undefined-offset-error-but-dont-see-why/#findComment-617068 Share on other sites More sharing options...
btherl Posted August 15, 2008 Share Posted August 15, 2008 Ah I think I got it. Try counting $records instead of $records[0] to set $numrec Link to comment https://forums.phpfreaks.com/topic/119774-solved-getting-undefined-offset-error-but-dont-see-why/#findComment-617104 Share on other sites More sharing options...
BenGoldberg Posted August 15, 2008 Author Share Posted August 15, 2008 Holy moly! Thank you so much, works perfectly now. Can I ask why this fixes the problem though? Why does $records[0] not count the values correctly? It seems like count($records) and count($records[0]) would output the same number no matter what. In any case, I can move on with my website now, so thanks again! Link to comment https://forums.phpfreaks.com/topic/119774-solved-getting-undefined-offset-error-but-dont-see-why/#findComment-617112 Share on other sites More sharing options...
btherl Posted August 15, 2008 Share Posted August 15, 2008 Both $records and $records[0] are arrays. But $records is an array that lists rows of data from the database, whereas $records[0] (and [1], [2], etc) are arrays that list data in a single row. So count($records[0]) is always 3, same for the others. But count($records) will give you the number of results. In the first var_dump above, the number of items happens to be exactly the same as the number of items in each result. But in the second var_dump, there is one result, and that result contains 3 items. Counting the number of items ($records) will give you 1, but counting the number of data items in a result ($records[0]) will give you 3. Link to comment https://forums.phpfreaks.com/topic/119774-solved-getting-undefined-offset-error-but-dont-see-why/#findComment-617153 Share on other sites More sharing options...
BenGoldberg Posted August 15, 2008 Author Share Posted August 15, 2008 Ah yes, I see now. Duh. Well I knew it was something silly like that. Link to comment https://forums.phpfreaks.com/topic/119774-solved-getting-undefined-offset-error-but-dont-see-why/#findComment-617168 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.