isedeasy Posted May 3, 2010 Share Posted May 3, 2010 I have the following array $reputation = array ( "comments" => array ( 1 => array ( "condition" => 1, "description" => "Added first Comment", "points" => 5 ), 2 => array ( "condition" => 50, "description" => "Added 50 Comments", "points" => 10 ), 3 => array ( "condition" => 100, "description" => "Added 100 Comments", "points" => 25 ) ) ) Thats stripped down but you should get the idea. I am looping through this array as follows foreach($reputation as $v) { foreach($v as $vk=>$vv) { ?> <tr> <td><?php echo $vv['points'] ?></td> <td><?php echo $vv['description'] ?></td> </tr> <?php } } I also have a table called rep with the following structure id, users and date with id being the same as reputation['comments'][$i]. basically I want to check to see if the user has the "achievment" and if so add the date from the database and a class to the row. I am not sure on the best way to go about this, cheers for any pointers Quote Link to comment https://forums.phpfreaks.com/topic/200585-loop-through-array-and-check-against-db-query/ Share on other sites More sharing options...
Ken2k7 Posted May 3, 2010 Share Posted May 3, 2010 The better way would be to store the array values in a database table instead of an array. Quote Link to comment https://forums.phpfreaks.com/topic/200585-loop-through-array-and-check-against-db-query/#findComment-1052561 Share on other sites More sharing options...
isedeasy Posted May 3, 2010 Author Share Posted May 3, 2010 The better way would be to store the array values in a database table instead of an array. Sorry for my ignorance but could you explain why it would be better Quote Link to comment https://forums.phpfreaks.com/topic/200585-loop-through-array-and-check-against-db-query/#findComment-1052564 Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 The better way would be to store the array values in a database table instead of an array. Sorry for my ignorance but could you explain why it would be better My Take: Because you would reduce the code significantly and you would also run the query on a database table that has at least 1 indexed field making the process more efficient and ressiliant. Quote Link to comment https://forums.phpfreaks.com/topic/200585-loop-through-array-and-check-against-db-query/#findComment-1052572 Share on other sites More sharing options...
isedeasy Posted May 3, 2010 Author Share Posted May 3, 2010 Any ideas on the query side? how would I get an array of all the id in the rep table that matches the user id (user). I could then check to see if $vk is in that array in my loop, would that be a good method todo this? Quote Link to comment https://forums.phpfreaks.com/topic/200585-loop-through-array-and-check-against-db-query/#findComment-1052665 Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 I think you are looking for... $filter = "achievment"; $query = "SELECT id, users, date FROM rep WHERE rep.users = ".$filter; $result = mysql_query($query) or die (mysql_error)); $rep_array = mysql_fetch_assoc($result); that's generic, and you will need to establish a connection to the database prior to including that on a page. It will however give you an array containing the following array fields: $rep_array['id'] $rep_array['users'] $rep_array['date'] and be populated ony with the records that match the filter. You could effectivly just echo that list to get the same results as checking through your array (from what I gather you said you were doing with it, I may have gotten things wrong). If that's not what you are looking for let me know, I'll be happy to work through it some more with you. Quote Link to comment https://forums.phpfreaks.com/topic/200585-loop-through-array-and-check-against-db-query/#findComment-1052675 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.