thefollower Posted September 11, 2008 Share Posted September 11, 2008 I need some help with my array... I want to find a way to use the array to check that a UserID has already used and if so produce error... This is what i have: <?php // get values from form and build the array etc // // //below: sort array check no duplicates sort($numbers); $check = false; $skip = false; foreach ($numbers as $next) { if ($next == trim($check)) { $skip = 1; } $check = $next; } If($skip){ ?> Duplicate number inserted! <?php }Else{ //some kind of query here to check the $_SESSION Current_User has not already got these 6 numbers to go here // //if mysql num rows from the theorized query idea above is less than 1 then it can be inserted as it is unique with query below. if(mysql_num_rows($idea)<1){ $INSERT = myslq_query("INSERT INTO lottery (UserID,Number1,Number2,Number3,Number4,Number5,Number6,BoughtOn) VALUES ('{$_SESSION['Current_User']}',". implode(',',$numbers) .",'$BoughtOn')") Or die(mysql_error()); }Else{ ?> You already have this combination of 6 numbers <?php } } ?> How ever i don't know how to loop the array in the WHERE CLAUSE from the array... i sort the numbers from lowest to highest so it is easier. Hope you can help Quote Link to comment https://forums.phpfreaks.com/topic/123785-help-with-array/ Share on other sites More sharing options...
Maq Posted September 11, 2008 Share Posted September 11, 2008 Correct me if I'm wrong but this code doesn't make sense: if ($next == trim($check)) { You're saying if $next (next element in array) is equal to a trimmed boolean. So wouldn't $skip always be 0 (false)? Quote Link to comment https://forums.phpfreaks.com/topic/123785-help-with-array/#findComment-639143 Share on other sites More sharing options...
thefollower Posted September 11, 2008 Author Share Posted September 11, 2008 Well it was to remove any white space in the variable. Some one suggested it to me when i was typing the array out. Although it works so thats nothing to worry about Its just the query i need to loop the array in order of the fields at same time so its kinda difficult to know how to do to it. Quote Link to comment https://forums.phpfreaks.com/topic/123785-help-with-array/#findComment-639147 Share on other sites More sharing options...
Maq Posted September 11, 2008 Share Posted September 11, 2008 Haven't tested this code but I think it should help. It makes an array from the selected field from the lottery table where the user is the current user in the session. Then is uses the in_array() function to check if any lottery numbers are in the $number array. $results = mysql_query("SELECT n1,n2,n3,n4,n5,n6 FROM lottery WHERE user_id = '{$_SESSION['Current_User']}'"); $data = mysql_fetch_array($results); $lot_number = array($data['n1'],$data['n2'],$data['n3'],$data['n4'],$data['n5'],$data['n6']); if(in_array($lot_number, $number) { //all ready has this number } else { //perform operation } Quote Link to comment https://forums.phpfreaks.com/topic/123785-help-with-array/#findComment-639159 Share on other sites More sharing options...
thefollower Posted September 11, 2008 Author Share Posted September 11, 2008 Thanks dude shall try it. I also tried : $Check = mysql_query("SELECT UserID FROM lottery WHERE Number1='$numbers[0]' AND Number2='$numbers[1]' AND Number3='$numbers[2]' AND Number4='$numbers[3]' AND Number5='$numbers[4]' AND Number6='$numbers[5]' AND UserID='{$_SESSION['Current_User']}'") Or die(mysql_error()); But not sure if the positions would be correct due to the sort. I shall try yours. Quote Link to comment https://forums.phpfreaks.com/topic/123785-help-with-array/#findComment-639163 Share on other sites More sharing options...
Maq Posted September 11, 2008 Share Posted September 11, 2008 Hmm, I'm not sure which is best. In my example you may have to check each number one at a time instead of using the whole array. For example loop through the $lot_number array and check each one with the in_array() function. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/123785-help-with-array/#findComment-639164 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.