Solved

Where am I blowing it with this query?

Posted on 2015-02-20
2
44 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
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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

862 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

21 Experts available now in Live!

Get 1:1 Help Now