Jump to content

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/123785-help-with-array/
Share on other sites

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. :(

Link to comment
https://forums.phpfreaks.com/topic/123785-help-with-array/#findComment-639147
Share on other sites

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 }

Link to comment
https://forums.phpfreaks.com/topic/123785-help-with-array/#findComment-639159
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/123785-help-with-array/#findComment-639163
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/123785-help-with-array/#findComment-639164
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.