Solved

Where am I blowing it with this query?

Posted on 2015-02-20
2
42 Views
Last Modified: 2015-02-23
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?
0
Comment
Question by:brucegust
2 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
Comment Utility
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now