Solved

Where am I blowing it with this query?

Posted on 2015-02-20
2
46 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 109

Expert Comment

by:Ray Paseur
ID: 40622375
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 34

Accepted Solution

by:
Slick812 earned 500 total points
ID: 40623422
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mysql query for sum() 3 28
Wordpress Security 29 48
Wordpress Cron - Send XML Data to external server FTP 3 21
jQuery Toggle & Anchor Links 5 43
These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

808 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