Jump to content

foreach loop


Phpfr3ak

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.