Jump to content

Recommended Posts

No, but you can use strpos.

if(strpos($the_random_num,$looking_for) !== false) {  $print_this = '5 is here!';}

Of course, if the string is "4,55,12" it's going to find the "5" in "55". You can split the string into an array with explode.

Link to post
Share on other sites

I don't see the problem why it can't

<?php$a = array('1.10', 12.4, 1.13);if (in_array('12.4', $a, true)) {	echo "'12.4' found with strict check\n";}if (in_array(1.13, $a, true)) {	echo "1.13 found with strict check\n";}?>The above example will output:1.13 found with strict check

Update:Oops a string, not an array... Yeah you should use strpos for that.But btw why not make an array?

Edited by davidb52
Link to post
Share on other sites

ok so if i use...

$the_random_num = "3,7,12,33,43,47";$the_random_num_explode = explode(",", $the_random_num);if(in_array(1,$the_random_num_explode)) {$print_this .= '1 is here!';}else{$print_this .= '';}if(in_array(2,$the_random_num_explode)) {$print_this .= '2 is here!';}else{$print_this .= '';}if(in_array(3,$the_random_num_explode)) {$print_this .= '3 is here!';}else{$print_this .= '';}// it will go on like this for 70 numbers

will this work?i do test my scripts but it takes me hours to change my code ;)

Link to post
Share on other sites

Why? create a random array numbers, sort then turn into string, then back into array, and check seventy times if the number is there? just print out the six number in the original array, i'm pretty sure you will get the same result

<?php $random_numbers = array();$total_num = 6;while ( count($random_numbers) < $total_num )	{	$current_rand_num = mt_rand(1,75);	if (!in_array($current_rand_num,$random_numbers))		{		$random_numbers[] = $current_rand_num; // creates an array of random numbers		}	} sort($random_numbers); $the_random_num=""; for($i=0;$i<count($random_numbers);$i++)	{$the_random_num.=$i < $total_num-1 ? $random_numbers[$i].', ' : $random_numbers[$i]; // turn rand numbers into string	}	echo $the_random_num.'<br />';  	$the_random_num_explode = explode(",", $the_random_num); /// now turn string back into array???????  echo 'You might not believe this, but the array turned into string of numbers, then back into array using explode has the SAME numbers as the original array, who\'d believe it hey<br />'; foreach($the_random_num_explode as $explode_rand_num){echo $explode_rand_num.' exploded array number is here<br />';}echo '<br />';foreach($random_numbers as $hi_remember_me){echo $hi_remember_me.' original array number is still here<br />';}    ?>

Edited by dsonesuk
Link to post
Share on other sites

if you wish to loop through 70+ numbers, might be best to use this

for($i=1;$i<=70;$i++){$found=false;foreach($random_numbers as $hi_remember_me){if($hi_remember_me==$i)    {    $found=true;break;    }}if($found)    {    echo $i.' original array number is still here<br />';    }else    {    echo $i.' is definitely not<br />';    }    }

Link to post
Share on other sites

i need 6 random numbers so am using this...

$random_numbers = array();$total_num = 6;while ( count($random_numbers) < $total_num ){$current_rand_num = mt_rand(1,75);if (!in_array($current_rand_num,$random_numbers)){$random_numbers[] = $current_rand_num;}}sort($random_numbers);$the_random_num = "";for($i=0;$i<count($random_numbers);$i++){//$the_random_num.=$random_num.', ';$the_random_num.=$i < $total_num-1 ? $random_numbers[$i].', ' : $random_numbers[$i];}

then i need to see if the random numbers are in my DB, i have 75 numbers in my DB set up as$number1 = enum '0','1' (if 1 its true)$number2 = enum '0','1'$number3 = enum '0','1'$number4 = enum '0','1'and so on.. to find a mach in the DB, so fare i have this...

if(in_array("1",$the_random_num_explode)) {$number1 = "1";}else{$number1 = "0";}if(in_array("2",$the_random_num_explode)) {$number2 = "1";}else{$number2 = "0";}if(in_array("3",$the_random_num_explode)) {$number3 = "1";}else{$number3 = "0";}

and so on... then at the end ask the DB for a mach. how dose it look?

Edited by westman
Link to post
Share on other sites

in the end... i used...

$random_numbers = array();$total_num = 6;while ( count($random_numbers) < $total_num ){$current_rand_num = mt_rand(1,75);if (!in_array($current_rand_num,$random_numbers)){$random_numbers[] = $current_rand_num;}}sort($random_numbers);$the_random_num = "";for($i=0;$i<count($random_numbers);$i++){//$the_random_num.=$random_num.', ';$the_random_num.=$i < $total_num-1 ? $random_numbers[$i].', ' : $random_numbers[$i];}$the_random_num_explode = explode(",", $the_random_num);

then..

if(in_array("1",$the_random_num_explode)) {$number1 = "1";}else{$number1 = "0";}if(in_array("2",$the_random_num_explode)) {$number2 = "1";}else{$number2 = "0";}//// 75 of them in total

then ...

$sql = mysql_query("SELECT * FROM numbers WHERE number1 = '$number1' AND number2 = '$number2'  AND number3 = '$number3'// 75 in total

and it seams to work however i have never got a mach from the random number generater (i have less than 50 results in my DB).if i set the random number to one in the DB if will find it will no problem. can some one tell me if it is working ok?again i have never got a match from the random number generater.

Link to post
Share on other sites

You shouldn't have to copy somehting over 75 times. Computers were invented exactly to prevent repetitive tasks. You can shorten the second part of your code to this:

$number = array();for($i = 1; $i <= 75; $i++) {  $number[$i] = intval( in_array(strval($i),$the_random_num_explode) );}

And the third part of your code to this:

$query = 'SELECT * FROM numbers WHERE '; $first = true;foreach($number as $key=>$value) {  if(!$first) {	$query .= 'AND ';  } else {	$first = false;  }  $query .= 'number' . $key. "='{$value}'";}mysql_query($query);

Link to post
Share on other sites

i have tested it but i have less than 50 results in my DB (it could take days to find a mach) so i cant see if the random number generater is workinghowever when i set the random number to one in the DB it will find it with no problem.

Edited by westman
Link to post
Share on other sites

Do you have 75 different fields in your database? What exactly is this program for? You will never manage to put a result for every single combination of 75 bits in your database. The amount of combinations is in the order of 10,000,000,000,000,000,000,000

Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...