Phpfr3ak Posted February 10, 2012 Share Posted February 10, 2012 Does anybody know how to make the following code correct? real sorry but its just absolutely blowing my mind im awful with loops and have pretty much no clue what the hell im doing in honesty, any help would be great, Thanks $addIDs_ary = array_map('intval', $_POST['chkInv']); $addIDs_ary = array_filter($addIDs_ary); $value = $addIDs_ary; //Check if hit_id is already in hitlist $query = mysql_query("SELECT * FROM hitlist WHERE player_id = '$playerID'"); $result = mysql_query($query); while ($result = mysql_fetch_assoc($result)) foreach ($result as $hit_id => $value){ if($result[$hit_id] == $value); $str = ""; } } else Quote Link to comment https://forums.phpfreaks.com/topic/256806-foreach-loop/ Share on other sites More sharing options...
trq Posted February 10, 2012 Share Posted February 10, 2012 Your forgot to describe your problem and your code makes little sense. Quote Link to comment https://forums.phpfreaks.com/topic/256806-foreach-loop/#findComment-1316500 Share on other sites More sharing options...
Phpfr3ak Posted February 10, 2012 Author Share Posted February 10, 2012 Sorry basically for a page with checkboxes, where you can "investigate" multiple people on the page, all that works fine, id just like to throw it in so you cannot "investigate" the same person twice, the said code is supposed to loop and check if each id, in this case the variable for the id's is $addIDs_ary is already present under the hit_id column in the table hitlist. error wise i'm getting Warning: mysql_query() expects parameter 1 to be string, resource given in C:\Program Files\EasyPHP-5.3.3\www\public_html\PlayerRanks.php on line 21 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\Program Files\EasyPHP-5.3.3\www\public_html\PlayerRanks.php on line 22 which would be the lines $result = mysql_query($query); while ($result = mysql_fetch_assoc($result)) sorry for the terrible explanation hope this clears it up a bit more? as i say im terrible with loops and such so it is potentially wrong, id just like to confirm, and if its incorrect know what im doing wrong, Thanks Quote Link to comment https://forums.phpfreaks.com/topic/256806-foreach-loop/#findComment-1316504 Share on other sites More sharing options...
trq Posted February 10, 2012 Share Posted February 10, 2012 The error you are getting is because you are passing the result from a call to mysql_query to another call to mysql_query. The reason I said your code made little sense is because of these two lines. foreach ($result as $hit_id => $value){ if($result[$hit_id] == $value); $result[$hit_id] will always equal $value. Unfortunately however, I still don't understand your explanation well enough to understand what it is your doing. Quote Link to comment https://forums.phpfreaks.com/topic/256806-foreach-loop/#findComment-1316507 Share on other sites More sharing options...
Phpfr3ak Posted February 10, 2012 Author Share Posted February 10, 2012 Example of the page, 3 boxes ticker, the id's vary (the first number is $addIDs_ary, the second $playerID) (2,17),(3,17),(4,17)3 of 3 Investigated player(s) were successfully added. before inserting them into the database, id like to check each $addIDs_ary to see if its already in the database, $addIDs_ary is stored under hit_id in hitlist, if it already exists id like to set $str = ""; if it doesnt exist it will go to else and continue with the other validation, best i can explain it really, unsure if that will fill you in more or not? worth a try anyhow Quote Link to comment https://forums.phpfreaks.com/topic/256806-foreach-loop/#findComment-1316508 Share on other sites More sharing options...
Phpfr3ak Posted February 10, 2012 Author Share Posted February 10, 2012 also tried: $addIDs_ary = array_filter($addIDs_ary); foreach ($result as $hit_id) { $req = "SELECT * FROM hitlist WHERE hit_id ='$addIDs_ary' and player_id = '$playerID'"; $result = mysql_query($req) or die ('Error :'.mysql_error()); if (mysql_num_rows($result)){ $str = ""; } else For some odd reason that just loops the whole results about 500x :/ (4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed.(4,17),(5,17)2 of 2 Investigated player(s) were successfully Removed. Quote Link to comment https://forums.phpfreaks.com/topic/256806-foreach-loop/#findComment-1316524 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.