Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Where am I blowing it with this query?

Posted on 2015-02-20
2
Medium Priority
?
50 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 111

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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

610 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