Where am I blowing it with this query?

Here's my code:

	public function term_display() {
		
		global $mysqli;
		
		if(!isset($_GET['letter']))
		{
			$sql=" SELECT * FROM `terms` WHERE term REGEXP CONCAT('^(the |a )?', 'a') and id%2=1";
			if(!$query=$mysqli->query($sql))
			{
				$err = 'your search query failed because: '
				.'ERRNO:'
				.$mysqli->errno
				.' ERROR:'
				.$mysqli->error
				.PHP_EOL
				.' and the epic failure we\'re talking about is: '
				.$query.PHP_EOL;
				trigger_error($err, E_USER_NOTICE);
			}			
		}
		else
		{
			$the_letter = $_GET['letter'];
			
			$sql=" SELECT * FROM `terms` WHERE term REGEXP CONCAT('^(the |a )?', '$the_letter') and id%2=1";
			if(!$query=$mysqli->query($sql))
			{
				$err = 'your search query failed because: '
				.'ERRNO:'
				.$mysqli->errno
				.' ERROR:'
				.$mysqli->error
				.PHP_EOL
				.' and the epic failure we\'re talking about is: '
				.$query.PHP_EOL;
				trigger_error($err, E_USER_NOTICE);
			}	
		}
		$row_count=mysqli_num_rows($query);
		$result_array=array();
		while ($row=$query->fetch_array())
		{
			//this is your odd number of results
			$result_array[]=$row;
		}
		
		
		if(!isset($_GET['letter']))
		{
			$sql_1="SELECT * FROM `terms` WHERE term REGEXP CONCAT('^(the |a )?', 'a') and id%2=0";
			echo $sql_1;
			if(!$query_1=$mysqli->query($sql))
			{
				$err = 'your search query failed because: '
				.'ERRNO:'
				.$mysqli->errno
				.' ERROR:'
				.$mysqli->error
				.PHP_EOL
				.' and the epic failure we\'re talking about is: '
				.$query.PHP_EOL;
				trigger_error($err, E_USER_NOTICE);
			}			
		}
		else
		{
			$the_letter = $_GET['letter'];
			
			$sql_1="SELECT * FROM `terms` WHERE term REGEXP CONCAT('^(the |a )?', '$the_letter') and id%2=0";
			if(!$query_1=$mysqli->query($sql_1))
			{
				$err_1 = 'your search query failed because: '
				.'ERRNO:'
				.$mysqli->errno
				.' ERROR:'
				.$mysqli->error
				.PHP_EOL
				.' and the epic failure we\'re talking about is: '
				.$query_1.PHP_EOL;
				trigger_error($err_1, E_USER_NOTICE);
			}	
		}
		$result_array_1=array();
		while ($row_1=$query_1->fetch_array())
		{
			//this is your even number of results
			$result_array_1[]=$row_1;
		}
	return array ($result_array, $result_array_1);
	}

Open in new window


What I'm doing is grabbing the even and odd members of a recordset based on some search criteria. The array that's being returned, I think, is the odd rows followed by the even rows.

When I go to display them, here's what I'm using:

		foreach ($term_display[0] as $term)
		{
			
			$briefcase .="<tr><td><b>";
			$briefcase .= $count;
			$briefcase .=")&nbsp;";
			$briefcase .=stripslashes($term['term']);
			$briefcase.="</b>&nbsp;<a href=\"#\" class=\"show_hide\">show / hide</a><div class=\"slidingDivTerms\"><br>";
			$briefcase .=stripslashes($term['definition']);
			$briefcase .="<br><br></div></td></tr><tr><td>&nbsp;</td></tr>";
			$count=$count+2;
		}
		$briefcase.='</table></div>';
		return $briefcase;
	}
	
	public function term_list_even($term_display)
	{
		
		$briefcase="";
		$count=2;
		
		$briefcase.="
		<script type=\"text/javascript\">
		 $(document).ready(function(){
		 
				$(\".slidingDivTerms\").hide();
				$(\".show_hide\").show();
		 
			$('.show_hide').click(function(){
			$(this).next().slideToggle();
			});
			
		});
		 </script>
		";
		
		$briefcase.="<table>";
		foreach ($term_display[1] as $term_1)
		{
			
			$briefcase .="<tr><td><b>";
			$briefcase .= $count;
			$briefcase .=")&nbsp;";
			$briefcase .=stripslashes($term_1['term']);
			$briefcase.="</b>&nbsp;<a href=\"#\" class=\"show_hide\">show / hide</a><div class=\"slidingDivTerms\"><br>";
			$briefcase .=stripslashes($term_1['definition']);
			$briefcase .="<br><br></div></td></tr><tr><td>&nbsp;</td></tr>";
			$count=$count+2;
		}
		$briefcase.='</table></div>';
		return $briefcase;
	}

Open in new window


I get no errors, but all I get is two columns of the recordset that is exclusively odd. I don't get any of my even numbered rows.

What am I missing? I'm thinking that maybe the array that I return (return array ($result_array, $result_array_1);), while it would appear that theoretically I've got my odd and even rows stored appropriately, something gets lost when I go to retrieve / display them: foreach ($term_display[0] as $term) and foreach ($term_display[1] as $term_1) - I'm getting the same data.

Where am I blowing it?
brucegustPHP DeveloperAsked:
Who is Participating?
 
Slick812Connect With a Mentor Commented:
greetings brucegust, it is my view that you are doing this the difficult way, first you use TWO separate code blocks if the test of -
if(!isset($_GET['letter']))

when you could just Set the $the_letter to the default "a" and then substitute the $_GET if it exists -
$the_letter = "a";
if(isset($_GET['letter'])) {
    $the_letter = $_GET['letter'];
    $the_letter = $mysqli->real_escape_string($the_letter);
    }

and then you can just use a Single SQL maybe like -
   $sql=" SELECT * FROM `terms` WHERE term REGEXP CONCAT('^(the |a )?', '$the_letter')";

Also please notice that I have omitted the -
    and id%2=1

because, It is less code writing if you Get ALL of the Odd and Even in one query and then Later in PHP do code to write BOTH of the  $briefcase strings (odd and even)  in ONE FOR loop.  see code below
the $term_display is from the query and has ALL rows in it
public function term_list($term_display)
	{
	// two different strings in $briefArray 
		$briefArray = array("<table>","<table>");;

	// $term_display array now has ALL of the Rows odd and even
		$counter = 2;
		foreach ($term_display as $term)
		{
			$sNum = 0;
			if (($counter % 2) == 1) {
				$sNum = 1; // switch array string on odd
				}
			$briefArray[$sNum] .="<tr><td><b>";
			$briefArray[$sNum] .= $count;
			$briefArray[$sNum] .=")&nbsp;";
			$briefArray[$sNum] .=stripslashes($term['term']);
			$briefArray[$sNum] .="</b>&nbsp;<a href=\"#\" class=\"show_hide\">show / hide</a><div class=\"slidingDivTerms\"><br>";
			$briefArray[$sNum] .=stripslashes($term['definition']);
			$briefArray[$sNum] .="<br><br></div></td></tr><tr><td>&nbsp;</td></tr>";
			$counter++; // Increase $counter
		}
		$briefArray[0] .='</table></div>';
		$briefArray[1] .='</table></div>';
		return $briefArray;
	}

Open in new window


ask questions if you need more info
0
 
Ray PaseurCommented:
Can you please show us a small data set and show us the results set you want to get out of it?  Unworkable code is not really helpful (I have enough of that in my own struggles, thank you) and if we are to be helpful we can probably do a better job for you if we work with the data instead of the unworkable programming.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.