results not displaying

Discussion in 'PHP' started by gilgalbiblewheel, Jul 9, 2008.

  1. #1
    I don't understand why but by transferring codes from one page to the next, the result doesn't display. This is the non functioning code:
    <?php
    $searchThese = $_GET['searchthese'];
    $searchTheseArr = $searchThese;
    $searchTheseArr=str_replace(":", "", $searchTheseArr);
    $searchTheseArr=str_replace(";", "", $searchTheseArr);
    $searchTheseArr=str_replace(",", "", $searchTheseArr);
    $searchTheseArr=str_replace(".", "", $searchTheseArr);
    $searchTheseArr=str_replace("?", "", $searchTheseArr);
    //common words
    $searchTheseArr=str_replace("shall", "", $searchTheseArr);
    $searchTheseArr=str_replace("shalt", "", $searchTheseArr);
    $searchTheseArr = explode(" ", $searchTheseArr);
    
    require_once "Paginated.php";
    require_once "DoubleBarLayout.php";
    /******************************************************************************/
    //connection
    $con = mysql_connect("","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("kjv", $con);
    /***********************************************************************************************************************/
    function highlight ( $str, $words )
            {
                if ( is_array( $words ) ) {
                                    rsort($words);
                    $words = array_map( 'preg_quote', $words );
                    $words = join( $words, '|' );
                } else {
                    $words = preg_quote( (string)$words );
                }
    			//$re = '<span style="color: red; font-weight:bold;">$1</span>';
    				$color[0] = "red";
    				$color[1] = "blue";
    			//for($c=0; $c < 8; $c++){
    				//echo $color[$c]."<br />";
    				$re = '<span style="color: '.$color[0].'; font-weight:bold;">$1</span>';
    				//}
    			return preg_replace( '/\b(' . $words . ')\b/i', $re, $str );
    		}
    /***********************************************************************************************************************/		
    function bookTitles($searchTheseArr){
    	$booktitlesarray = array();
    	$sql="SELECT * FROM bible WHERE 1=1 AND";
    $j=0;
    //to sort out all words with length less than 4 like AND, OR, BUT...
    for ($i=0; $i < count($searchTheseArr); $i++){
    	if(strlen($searchTheseArr[$i]) > 4){
    		$newSearchTheseArr[$j] = $searchTheseArr[$i];
    		//j renumbers the words of length 4 and up		
    		$j++;
    		}
    	}
    //sql searches only length 4 and up
    for ($j=0; $j < count($newSearchTheseArr); $j++){
    	$sql.=" CASE WHEN text_data LIKE '%" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
    	if($j!=count($newSearchTheseArr)-1){
    		$sql.=" +";
    		}else{
    		//removes the OR from the last line and replaces with the following	
    		$sql.= " > 3";
    		}
    	}
    $sql .= " ORDER BY id";
    echo $sql."<br />\n";
    
    $result = mysql_query($sql) OR exit( 'Error: ' . mysql_error());
    echo "<span style='padding: 5px; background-color: #D2C5A0; display: block; border: 1px solid #7C7C7C;'>Searching for:<br />\n";
    for ($j=0; $j < count($newSearchTheseArr); $j++){
    	echo "\t<span style='color: red;font-weight:bold;'>".$newSearchTheseArr[$j]."</span>\n\t\t";
    	if($j != count($newSearchTheseArr)-1){
    		echo " + \n\t\t";
    		}
    	}
    echo "\t</span><br />\n";
    
    	$result = mysql_query($sql);
    	if($result){
    		while($row = mysql_fetch_assoc($result)){
    			$booktitlesarray[] = "";
    			/*$booktitlesarray[] .= "\t<span style='font-weight: bold; color: black; background-color: #B4B3A9; display: block; border: 1px solid #7C7C7C;'>\n";  
    			$booktitlesarray[] .= "\t\t<span id='regular[]' style='padding: 5px; background-color: #D2C5A0; display: block; border: 1px solid #7C7C7C; color: black;'>\n";
    			$booktitlesarray[] .= "\t\t<span style='font-weight: normal;'> ";
    			$booktitlesarray[] .= $recCount;
    			$booktitlesarray[] .= "\t\t</span> ";
    
    			$booktitlesarray[] .= "\t\t\t<span class='goToBookChapter' style='font-weight: bold;'>";
    			$booktitlesarray[] .= "\t\t\t".$row['book_title']." ".$row['chapter'].":".$row['verse'];
    			$booktitlesarray[] .= "\t\t\t</span><br />\n";*/
    			
    			$booktitlesarray[] .= "\t\t\t<span class='goToBookChapter' style='font-weight: bold;'>".$row['book_title']." ".$row['chapter'].":".$row['verse']."</span><br />\n\n\t\t\t".$row['text_data'];
    
    			//$str = $row['text_data'];
    			//$booktitlesarray[] .= highlight( $str, $newSearchTheseArr);
    			
    			/*$booktitlesarray[] .= "\t\t</span>\n";
    			$booktitlesarray[] .= "\t</span><br />\n";*/
    		}
    	}
    	return $booktitlesarray;
    
    }
    
    	$page = $_GET['page'];
    	//constructor takes three parameters
    	//1. array to be paged
    	//2. number of results per page (optional parameter. Default is 10)
    	//3. the current page (optional parameter. Default  is 1)
    	/*$pagedResults = new Paginated(bookTitles($searchTheseArr), 10, $page);
    	echo "<div>\n";
    	while($row = $pagedResults->fetchPagedRow()){
    		$recCount = 1;
    		echo "{$row}\n";
    			$recCount++;		
    	}
    	echo "</div>\n";
    	*/
    		
    	//$pagedResults = new Paginated($names, 10, $page);
    	$pagedResults = new Paginated(bookTitles($searchTheseArr), 10, $page);
    	echo "<div>\n";
    	
    	while($row = $pagedResults->fetchPagedRow()){
    	//print_r(bookTitles($booktitlesarray));
    		echo "\t\t<span>\n{$row}\n\t\t</span><br />\n";
    		
    	}
    	echo "</div>\n";
    	echo "hi";
    	$queryVars .= "";
    	//for the url
    		for ($j=0; $j < count($newSearchTheseArr); $j++){
    			if($j!= count($newSearchTheseArr)-1){
    				$queryVars .= $newSearchTheseArr[$j]."+";
    				
    			}else{
    				$queryVars .= $newSearchTheseArr[$j];
    			}
    		}
    		echo $queryVars;
    	//important to set the strategy to be used before a call to fetchPagedNavigation
    	$pagedResults->setLayout(new DoubleBarLayout());
    	echo $pagedResults->fetchPagedNavigation($queryVars);
    	
    mysql_close($con);
    ?>
    PHP:
    this is the functioning code:
    <?php
    $searchThese = $_GET['searchthese'];
    $searchTheseArr = $searchThese;
    $searchTheseArr=str_replace(":", "", $searchTheseArr);
    $searchTheseArr=str_replace(";", "", $searchTheseArr);
    $searchTheseArr=str_replace(",", "", $searchTheseArr);
    $searchTheseArr=str_replace(".", "", $searchTheseArr);
    $searchTheseArr=str_replace("?", "", $searchTheseArr);
    //common words
    $searchTheseArr=str_replace("shall", "", $searchTheseArr);
    $searchTheseArr=str_replace("shalt", "", $searchTheseArr);
    $searchTheseArr = explode(" ", $searchTheseArr);
    
    require_once "Paginated.php";
    require_once "DoubleBarLayout.php";
    ?>
    <html>
    <head>
    <title>Pagination</title>
    
    <!-- Just a little style formatting. Has no bearing on example -->
    <style type="text/css">
    	body {
    		font-family: Verdana;
    		font-size: 13px;
    	}
    	
    	a {
    		text-decoration: none;
    	}
    	
    	a:hover {
    		text-decoration: underline;
    	}
    </style>
    <!-- End style formatting -->
    </head>
    
    <body>
    
    	<?php
    	$con = mysql_connect("","root","");
    	if (!$con){
    		die('Could not connect: ' . mysql_error());
    	}
    	mysql_select_db("kjv", $con);
    
    	function bookTitles($searchTheseArr){
       $booktitlesarray = array();
       /*
       $query = "SELECT * FROM bible WHERE 1=1 AND";
       $query .= " CASE WHEN text_data LIKE '%" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
       $query .= " ORDER BY id";
       */
       $query="SELECT * FROM bible WHERE 1=1 AND";
    	$j=0;
    	//to sort out all words with length less than 4 like AND, OR, BUT...
    	for ($i=0; $i < count($searchTheseArr); $i++){
    		if(strlen($searchTheseArr[$i]) > 4){
    			$newSearchTheseArr[$j] = $searchTheseArr[$i];
    			//j renumbers the words of length 4 and up		
    			$j++;
    			}
    		}
    	//sql searches only length 4 and up
    	for ($j=0; $j < count($newSearchTheseArr); $j++){
    		$query.=" CASE WHEN text_data LIKE '%" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
    		if($j!=count($newSearchTheseArr)-1){
    			$query.=" +";
    			}else{
    			//removes the OR from the last line and replaces with the following	
    			$query.= " > 3";
    			}
    		}
    	$query .= " ORDER BY id";
       echo $query."<br />";
       echo "Words searched for: ";
       for ($j=0; $j < count($newSearchTheseArr); $j++){
       		echo $newSearchTheseArr[$j]." ";
       }
       $result = mysql_query($query);
       if($result){
          while($row = mysql_fetch_assoc($result)){
              $booktitlesarray[] = "\t\t\t<span class='goToBookChapter' style='font-weight: bold;'>".$row['book_title']." ".$row['chapter'].":".$row['verse']."</span><br />\n\n\t\t\t".$row['text_data'];
          }
       }
       return $booktitlesarray;
    } 
    
    	//$names = bookTitles(); 
    
    	$page = $_GET['page'];
    	
    	//constructor takes three parameters
    	//1. array to be paged
    	//2. number of results per page (optional parameter. Default is 10)
    	//3. the current page (optional parameter. Default  is 1)
    	//$pagedResults = new Paginated($names, 10, $page);
    	$pagedResults = new Paginated(bookTitles($searchTheseArr), 10, $page);
    	echo "<div>\n";
    	while($row = $pagedResults->fetchPagedRow()){
    		echo "\t\t<span>\n{$row}\n\t\t</span><br />\n";
    	}
    	echo "</div>\n";
    	mysql_close($con);
    	//important to set the strategy to be used before a call to fetchPagedNavigation
    	$pagedResults->setLayout(new DoubleBarLayout());
    	echo $pagedResults->fetchPagedNavigation();
    	?>
    </body>
    </html>
    PHP:

     
    gilgalbiblewheel, Jul 9, 2008 IP
  2. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #2
    There was one, a highlight function that didn't exist in the first correct file. But I blocked it to see if it would work without it but it didn't. The reason I transferred is because I'm using AJAX to upload the file in a tag.

    I inserted:
    ini_set ("display_errors", "1");
    error_reporting(E_ALL);
    PHP:
    To know the errors and got:

    Line 136 is starting:
    $queryVars .= "";
    	//for the url
    		for ($j=0; $j < count($newSearchTheseArr); $j++){
    			if($j!= count($newSearchTheseArr)-1){
    				$queryVars .= $newSearchTheseArr[$j]."+";
    				
    			}else{
    				$queryVars .= $newSearchTheseArr[$j];
    			}
    		}
    		echo $queryVars;
    	//important to set the strategy to be used before a call to fetchPagedNavigation
    	$pagedResults->setLayout(new DoubleBarLayout());
    	echo $pagedResults->fetchPagedNavigation($queryVars);
    PHP:

    138 is 2 lines under above.

    But I don't understand is why the almost same for loop worked in the page before this on line 61-69:
    for ($j=0; $j < count($newSearchTheseArr); $j++){
    	$sql.=" CASE WHEN text_data LIKE '%" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
    	if($j!=count($newSearchTheseArr)-1){
    		$sql.=" +";
    		}else{
    		//removes the OR from the last line and replaces with the following	
    		$sql.= " > 3";
    		}
    	}
    PHP:
    And the sql string shows exactly as it ought to.
     
    gilgalbiblewheel, Jul 10, 2008 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Well I joined two for loops together since they have the same formula:
    	$queryVars .= "";
    
    
    
    for ($j=0; $j < count($newSearchTheseArr); $j++){
    	$sql.=" CASE WHEN text_data LIKE '%" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
    	if($j!=count($newSearchTheseArr)-1){
    		$sql.=" +";
    		$queryVars .= $newSearchTheseArr[$j]."+";
    		}else{
    		//removes the OR from the last line and replaces with the following	
    		$sql.= " > 3";
    		$queryVars .= $newSearchTheseArr[$j];
    		}
    	}
    PHP:
    One is the SQL statement and the other is the $queryVars which is for the URL extension ?...&...&....

    What I had done before is block the codes and insert my codes to make this example file fit to both the database and AJAX uploading in a tag:
    <?php
    class DoubleBarLayout implements PageLayout {
    
    	public function fetchPagedLinks($parent, $queryVars) {
    	
    		$currentPage = $parent->getPageNumber();
    		$str = "";
    
    		if(!$parent->isFirstPage()) {
    			if($currentPage != 1 && $currentPage != 2 && $currentPage != 3) {
    					$str .= "<a href=\"JavaScript: var page=1; var searchthese='$queryVars'; aTextPage();\" title='Start'>&lt; First</a> ";
    			}
    		}
    
    		//write statement that handles the previous and next phases
    	   	//if it is not the first page then write previous to the screen
    		if(!$parent->isFirstPage()) {
    			$previousPage = $currentPage - 1;
    			$str .= "<a href=\"JavaScript: var page=$previousPage; var searchthese='$queryVars'; aTextPage();\">&lt; previous</a> ";
    		}
    
    		for($i = $currentPage - 2; $i <= $currentPage + 2; $i++) {
    			//if i is less than one then continue to next iteration		
    			if($i < 1) {
    				continue;
    			}
    	
    			if($i > $parent->fetchNumberPages()) {
    				break;
    			}
    	
    			if($i == $currentPage) {
    				$str .= "<span style='font-weight: bold;'>Page $i</span>";
    			}
    			else {
    				//$str .= "<a href=\"?page=$i$queryVars\">$i</a>";
    
    				$str .= " <a href=\"JavaScript: var page={$i}; var searchthese='$queryVars'; aTextPage();\">Page $i</a>";
    			}
    			($i == $currentPage + 2 || $i == $parent->fetchNumberPages()) ? $str .= " " : $str .= " | ";              //determine if to print bars or not
    		}//end for
    
    		if (!$parent->isLastPage()) {
    			if($currentPage != $parent->fetchNumberPages() && $currentPage != $parent->fetchNumberPages() -1 && $currentPage != $parent->fetchNumberPages() - 2)
    			{
    				$str .= "<a href=\"JavaScript: var page=".$parent->fetchNumberPages()."; var searchthese='$queryVars'; aTextPage();\" title=\"Last\">Last(".$parent->fetchNumberPages().") &gt;</a> ";
    			}
    		}
    
    		if(!$parent->isLastPage()) {
    			$nextPage = $currentPage + 1;
    			$str .= "<a href=\"JavaScript: var page=$nextPage; var searchthese='$queryVars'; aTextPage();\">next &gt;</a>";
    		}
    		return $str;
    	}
    }
    ?>
    PHP:
     
    gilgalbiblewheel, Jul 10, 2008 IP