Jump to content

Arrays


Hooch

Recommended Posts

Hey all. This is my situation. I have a lotto 6/49 pool at work. For you non Canadians this is a lottery where you pick 6 numbers. I have everyones numbers set in a DB. I also have a field where you enter this weeks winning numbers and I want to highlight the matching numbers in the DB. I'm having trouble condensing the code. I can have about a 200 linefunction check each DB entry 6 times. But there must be a way to shorten this.Here is a break down of what I have so far...( $r['p1'] $r['p2'] $r['p3'] $r['p4'] $r['p5'] $r['p6'] are the DB numbers)( $nums1 $nums2 $nums3 $nums4 $nums5 $nums6 are the winning numbers)( I need to check each of the 6 DB entry's 6 times)

		if ($r['p1'] == $nums1)			{			$r['p1'] = preg_replace( "'($nums1)'si" , "<span class=\"black-yellow-bg-24\">".$nums1."</span>" , $r['p1']);			}

I can then proceed to do this...

		if ($r['p1'] == $nums2)			{			$r['p1'] = preg_replace( "'($nums2)'si" , "<span class=\"black-yellow-bg-24\">".$nums2."</span>" , $r['p1']);			}

Then I need to check $r['p2'] six times $r['p3'] 6 times, etc etc...Thank you for your time and help.Hooch

Link to comment
Share on other sites

I went ahead an tried the code how I know...This may clarify what I am wanting.

		if ($r['p1'] == $nums1)			$r['p1'] = preg_replace( "'($nums1)'si" , "<span class=\"black-yellow-bg-23\">".$nums1."</span>" , $r['p1']);		if ($r['p1'] == $nums2)			$r['p1'] = preg_replace( "'($nums2)'si" , "<span class=\"black-yellow-bg-23\">".$nums2."</span>" , $r['p1']);		if ($r['p1'] == $nums3)			$r['p1'] = preg_replace( "'($nums3)'si" , "<span class=\"black-yellow-bg-23\">".$nums3."</span>" , $r['p1']);		if ($r['p1'] == $nums4)			$r['p1'] = preg_replace( "'($nums4)'si" , "<span class=\"black-yellow-bg-23\">".$nums4."</span>" , $r['p1']);		if ($r['p1'] == $nums5)			$r['p1'] = preg_replace( "'($nums5)'si" , "<span class=\"black-yellow-bg-23\">".$nums5."</span>" , $r['p1']);		if ($r['p1'] == $nums6)			$r['p1'] = preg_replace( "'($nums6)'si" , "<span class=\"black-yellow-bg-23\">".$nums6."</span>" , $r['p1']);

Then the next bit of code would be...

		if ($r['p2'] == $nums1)			$r['p2] = preg_replace( "'($nums1)'si" , "<span class=\"black-yellow-bg-23\">".$nums1."</span>" , $r['p2']);		if ($r['p2'] == $nums2)			$r['p2'] = preg_replace( "'($nums2)'si" , "<span class=\"black-yellow-bg-23\">".$nums2."</span>" , $r['p2']);		if ($r['p2'] == $nums3)			$r['p2'] = preg_replace( "'($nums3)'si" , "<span class=\"black-yellow-bg-23\">".$nums3."</span>" , $r['p2']);		if ($r['p2'] == $nums4)			$r['p2'] = preg_replace( "'($nums4)'si" , "<span class=\"black-yellow-bg-23\">".$nums4."</span>" , $r['p2']);		if ($r['p2'] == $nums5)			$r['p2'] = preg_replace( "'($nums5)'si" , "<span class=\"black-yellow-bg-23\">".$nums5."</span>" , $r['p2']);		if ($r['p2'] == $nums6)			$r['p2'] = preg_replace( "'($nums6)'si" , "<span class=\"black-yellow-bg-23\">".$nums6."</span>" , $r['p2']);

Link to comment
Share on other sites

Since all your code lines look alike, you just need to change little bits. So use arrays like real arrays. Stash your data in vars like $r[0] and $nums[0]; Then you can iterate in loops using some sort of $i++ and $j++ indexes. (There is no reason on earth to use a var like $nums6. Even you didn't have a purpose for an array, which you do, declaring a thing like that as an array emphasizes the relationship and makes more understandable code.)

Link to comment
Share on other sites

Thanks D's Dad.I do have the winning numbers in an array...

	if($_POST['search'])		{		$nums = trim($_POST['numbers']);//Remove spaces from ends of the form field		$nums = explode(" ", $nums);//Lets get all 6 numbers to an array		$nums1 = ltrim($nums[0], '0');//Remove preceeding 0's		$nums2 = ltrim($nums[1], '0');//Remove preceeding 0's		$nums3 = ltrim($nums[2], '0');//Remove preceeding 0's		$nums4 = ltrim($nums[3], '0');//Remove preceeding 0's 		$nums5 = ltrim($nums[4], '0');//Remove preceeding 0's		$nums6 = ltrim($nums[5], '0');//Remove preceeding 0's 		$bonus = trim($_POST['bonus_num']);//Remove spaces from the bonus number 		} else {}

But looping 2 checks in one loop was a bit much for me.I will try to do the 1st check to have a start. Kinda late now, but I will try after work tomorrow night.Again thank you for your help!

Link to comment
Share on other sites

Here's my success so far.From this

		if ($r['p1'] == $bonus)			$r['p1'] = preg_replace( "'($bonus)'si" , "<span class=\"black-orange-bg-23\">".$bonus."</span>" , $r['p1']);					if ($r['p2'] == $bonus)			$r['p2'] = preg_replace( "'($bonus)'si" , "<span class=\"black-orange-bg-23\">".$bonus."</span>" , $r['p2']);		if ($r['p3'] == $bonus)			$r['p3'] = preg_replace( "'($bonus)'si" , "<span class=\"black-orange-bg-23\">".$bonus."</span>" , $r['p3']);		if ($r['p4'] == $bonus)			$r['p4'] = preg_replace( "'($bonus)'si" , "<span class=\"black-orange-bg-23\">".$bonus."</span>" , $r['p4']);		if ($r['p5'] == $bonus)			$r['p5'] = preg_replace( "'($bonus)'si" , "<span class=\"black-orange-bg-23\">".$bonus."</span>" , $r['p5']);		if ($r['p6'] == $bonus)			$r['p6'] = preg_replace( "'($bonus)'si" , "<span class=\"black-orange-bg-23\">".$bonus."</span>" , $r['p6']);

to

		for ($i = 1; $i <= 6; $i++) 			{			if ($r['p'.$i.''] == $bonus)				$r['p'.$i.''] = preg_replace( "'($bonus)'si" , "<span class=\"black-orange-bg-23\">".$bonus."</span>" , $r['p'.$i.'']);			  }

I have 2 more snippets to trim down yet.The next bit I am tackling is not working so well.I need to go from

		$nums0 = ltrim($array[0], '0');//Remove preceeding 0's 		  		$nums1 = ltrim($array[1], '0');//Remove preceeding 0's		$nums2 = ltrim($array[2], '0');//Remove preceeding 0's		$nums3 = ltrim($array[3], '0');//Remove preceeding 0's		$nums4 = ltrim($array[4], '0');//Remove preceeding 0's 		$nums5 = ltrim($array[5], '0');//Remove preceeding 0's		echo "<strong class=\"style3\">( </strong)<strong class=\"black-yellow-bg-16\">".$nums0." ".$nums1." ".$nums2." ".$nums3." ".$nums4." ".$nums5."</strong>";

to (my work in progress)

		echo "<strong class=\"style3\">( </strong)<strong class=\"black-yellow-bg-16\">";				for ($i = 0; $i <= 6; $i++) 			{			  $nums.$i = ltrim($array[$i], '0');//Remove preceeding 0's			echo $nums.$i." ";			  }		echo "</strong>";		echo " <strong class=\"black-orange-bg-16\">Bonus ".$bonus."</strong><strong class=\"style3\"> )</strong)";

This one is a stumper.But I'm still trying.**Edit** Ha, I just noticed my code to try and slim down is actually 1 line longer.But I still would like to figure it out.

Link to comment
Share on other sites

I'm thinking the following 2 lines may be an issue

 $nums.$i = ltrim($array[$i], '0');//Remove preceeding 0's  echo $nums.$i." ";

Since $i starts at 0. This is needed for the 1st array though.

Link to comment
Share on other sites

Here's my loop in a loop attempt.From this

		 $result = mysql_query("SELECT * FROM `649` ORDER by `winnings` DESC, 'lname' ASC")or die(mysql_error());			 while ($r = mysql_fetch_assoc($result))//Lets start a loop to see if there are any matches with the DB	if ($r['p1'] == $nums0)			$r['p1'] = preg_replace( "'($nums0)'si" , "<span class=\"black-yellow-bg-23\">".$nums0."</span>" , $r['p1']);		if ($r['p1'] == $nums1)			$r['p1'] = preg_replace( "'($nums1)'si" , "<span class=\"black-yellow-bg-23\">".$nums1."</span>" , $r['p1']);		if ($r['p1'] == $nums2)			$r['p1'] = preg_replace( "'($nums2)'si" , "<span class=\"black-yellow-bg-23\">".$nums2."</span>" , $r['p1']);		if ($r['p1'] == $nums3)			$r['p1'] = preg_replace( "'($nums3)'si" , "<span class=\"black-yellow-bg-23\">".$nums3."</span>" , $r['p1']);		if ($r['p1'] == $nums4)			$r['p1'] = preg_replace( "'($nums4)'si" , "<span class=\"black-yellow-bg-23\">".$nums4."</span>" , $r['p1']);		if ($r['p1'] == $nums5)			$r['p1'] = preg_replace( "'($nums5)'si" , "<span class=\"black-yellow-bg-23\">".$nums5."</span>" , $r['p1']);		if ($r['p2'] == $nums0)			$r['p2'] = preg_replace( "'($nums0)'si" , "<span class=\"black-yellow-bg-23\">".$nums0."</span>" , $r['p2']);		if ($r['p2'] == $nums1)			$r['p2'] = preg_replace( "'($nums1)'si" , "<span class=\"black-yellow-bg-23\">".$nums1."</span>" , $r['p2']);		if ($r['p2'] == $nums2)			$r['p2'] = preg_replace( "'($nums2)'si" , "<span class=\"black-yellow-bg-23\">".$nums2."</span>" , $r['p2']);		if ($r['p2'] == $nums3)			$r['p2'] = preg_replace( "'($nums3)'si" , "<span class=\"black-yellow-bg-23\">".$nums3."</span>" , $r['p2']);		if ($r['p2'] == $nums4)			$r['p2'] = preg_replace( "'($nums4)'si" , "<span class=\"black-yellow-bg-23\">".$nums4."</span>" , $r['p2']);		if ($r['p2'] == $nums5)			$r['p2'] = preg_replace( "'($nums5)'si" , "<span class=\"black-yellow-bg-23\">".$nums5."</span>" , $r['p2']);		if ($r['p3'] == $nums0)			$r['p3'] = preg_replace( "'($nums0)'si" , "<span class=\"black-yellow-bg-23\">".$nums0."</span>" , $r['p3']);		if ($r['p3'] == $nums1)			$r['p3'] = preg_replace( "'($nums1)'si" , "<span class=\"black-yellow-bg-23\">".$nums1."</span>" , $r['p3']);		if ($r['p3'] == $nums2)			$r['p3'] = preg_replace( "'($nums2)'si" , "<span class=\"black-yellow-bg-23\">".$nums2."</span>" , $r['p3']);		if ($r['p3'] == $nums3)			$r['p3'] = preg_replace( "'($nums3)'si" , "<span class=\"black-yellow-bg-23\">".$nums3."</span>" , $r['p3']);		if ($r['p3'] == $nums4)			$r['p3'] = preg_replace( "'($nums4)'si" , "<span class=\"black-yellow-bg-23\">".$nums4."</span>" , $r['p3']);		if ($r['p3'] == $nums5)			$r['p3'] = preg_replace( "'($nums5)'si" , "<span class=\"black-yellow-bg-23\">".$nums5."</span>" , $r['p3']);		if ($r['p4'] == $nums0)			$r['p4'] = preg_replace( "'($nums0)'si" , "<span class=\"black-yellow-bg-23\">".$nums0."</span>" , $r['p4']);		if ($r['p4'] == $nums1)			$r['p4'] = preg_replace( "'($nums1)'si" , "<span class=\"black-yellow-bg-23\">".$nums1."</span>" , $r['p4']);		if ($r['p4'] == $nums2)			$r['p4'] = preg_replace( "'($nums2)'si" , "<span class=\"black-yellow-bg-23\">".$nums2."</span>" , $r['p4']);		if ($r['p4'] == $nums3)			$r['p4'] = preg_replace( "'($nums3)'si" , "<span class=\"black-yellow-bg-23\">".$nums3."</span>" , $r['p4']);		if ($r['p4'] == $nums4)			$r['p4'] = preg_replace( "'($nums4)'si" , "<span class=\"black-yellow-bg-23\">".$nums4."</span>" , $r['p4']);		if ($r['p4'] == $nums5)			$r['p4'] = preg_replace( "'($nums5)'si" , "<span class=\"black-yellow-bg-23\">".$nums5."</span>" , $r['p4']);		if ($r['p5'] == $nums0)			$r['p5'] = preg_replace( "'($nums0)'si" , "<span class=\"black-yellow-bg-23\">".$nums0."</span>" , $r['p5']);		if ($r['p5'] == $nums1)			$r['p5'] = preg_replace( "'($nums1)'si" , "<span class=\"black-yellow-bg-23\">".$nums1."</span>" , $r['p5']);		if ($r['p5'] == $nums2)			$r['p5'] = preg_replace( "'($nums2)'si" , "<span class=\"black-yellow-bg-23\">".$nums2."</span>" , $r['p5']);		if ($r['p5'] == $nums3)			$r['p5'] = preg_replace( "'($nums3)'si" , "<span class=\"black-yellow-bg-23\">".$nums3."</span>" , $r['p5']);		if ($r['p5'] == $nums4)			$r['p5'] = preg_replace( "'($nums4)'si" , "<span class=\"black-yellow-bg-23\">".$nums4."</span>" , $r['p5']);		if ($r['p5'] == $nums5)			$r['p5'] = preg_replace( "'($nums5)'si" , "<span class=\"black-yellow-bg-23\">".$nums5."</span>" , $r['p5']);		if ($r['p6'] == $nums0)			$r['p6'] = preg_replace( "'($nums0)'si" , "<span class=\"black-yellow-bg-23\">".$nums0."</span>" , $r['p6']);		if ($r['p6'] == $nums1)			$r['p6'] = preg_replace( "'($nums1)'si" , "<span class=\"black-yellow-bg-23\">".$nums1."</span>" , $r['p6']);		if ($r['p6'] == $nums2)			$r['p6'] = preg_replace( "'($nums2)'si" , "<span class=\"black-yellow-bg-23\">".$nums2."</span>" , $r['p6']);		if ($r['p6'] == $nums3)			$r['p6'] = preg_replace( "'($nums3)'si" , "<span class=\"black-yellow-bg-23\">".$nums3."</span>" , $r['p6']);		if ($r['p6'] == $nums4)			$r['p6'] = preg_replace( "'($nums4)'si" , "<span class=\"black-yellow-bg-23\">".$nums4."</span>" , $r['p6']);		if ($r['p6'] == $nums5)			$r['p6'] = preg_replace( "'($nums5)'si" , "<span class=\"black-yellow-bg-23\">".$nums5."</span>" , $r['p6']);

To this (work in progress)

	 $result = mysql_query("SELECT * FROM `649` ORDER by `winnings` DESC, 'lname' ASC")or die(mysql_error());			 	 while ($r = mysql_fetch_assoc($result))	 $num = trim($_POST['numbers']);	 $array = explode(" ", $num);	 	{				//Check each DB pick 'p1' through 'p6'		for ($a = 1; $a <= 6; $a++)			{						//The following inner loop will see if each of the DB pick will match any of the 6 winning numbers arrays 0 through 5			for ($b = 0; $b <= 5; $b++)				{				$var = 'nums' . $b;				$$var = $array[$b];				if ($r['p'.$a.''] == $$var )					$r['p'.$a.''] = preg_replace( "'($$var )'si" , "<span class=\"black-orange-bg-23\">".$$var ."</span>" , $r['p'.$a.'']);				}			}

Right now code does not work**EDIT**Got the code working

	 $num = trim($_POST['numbers']);//Remove spaces from ends of the form field and set the $num variable	 $array = explode(" ", $num);//Lets get all 6 numbers to an array		 $result = mysql_query("SELECT * FROM `649` ORDER by `winnings` DESC, 'lname' ASC")or die(mysql_error());			 	 while ($r = mysql_fetch_assoc($result))//Lets start a loop to see if there are any matches with the DB	 	{		for ($a = 1; $a <= 6; $a++)//Check each DB pick 1 through 6			{						for ($b = 0; $b <= 5; $b++)//The following inner loop will see if each of the DB pick will match any of the 6 winning numbers arrays 0 through 5				{				if ($r['p'.$a.''] == $array[$b] )//We now need to see if the winning numbers match					{					$r['p'.$a.''] = preg_replace( "'($array[$b])'si" , "<span class=\"black-yellow-bg-23\">".$array[$b] ."</span>" , $r['p'.$a.'']);					} else {}				}			}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...