Jump to content

Recommended Posts

Hello all, Have a bit of a problem you may be able to asist me with. Below is code to display search results and paginate them. With this script needing variables from the url I want to make sure that a message of no search results found if the page is clicked on from a search engine. As of now I have got it to stop saying query was empty but now the message is displaying for only one page of results, along with the results. Your help is greatly appreciated.

if ( $_GET[‘limit’] == NULL ){
$limit = 10;
} else {
$limit = $_GET[‘limit’];
}

if ($_GET[‘page’]==NULL){
$start = 0;
} else {
$start = $_GET[‘page’];
}

// the if above, states that if the variable in the URL named page has a value, $start will be set to it, otherwise by default it is set to 0, meaning that it will start at the beginning of the table by default.

if($_GET[‘page’] <= 0){
$_GET['page']=0;
$_GET['limit']=$limit;
}

// this if above checks that the start variable isn’t negative, because if it is, our script will screw up. If it finds that it is, it redirects to the default page where the start is 0.

//$q="select * from profiles where pthick between '$thick1' and '$thick2' and pwidth between '$height1' and '$height2' and pdesc like '%$descr%' and itemno like '%$item1%' and pdate between '$date1' and '$date2' and ptype like '%$cmbCategory%' ORDER BY pid LIMIT $start,$limit";
if (isset($thick1) && isset($thick2)){
$q="select * from profiles where pthick between '$thick1' and '$thick2' order by pid LIMIT $start,$limit";
}
if (isset($height1) && isset($height2)){
$q="select * from profiles where pwidth between '$height1' and '$height2' order by pid LIMIT $start,$limit";
}
	if(isset($descr)){
	$q="select * from profiles where pdesc like '%$descr%' order by pid LIMIT $start,$limit";
	}
		if(isset($item1)){
		$q="select * from profiles where itemno like '%$item1%' order by pid LIMIT $start,$limit";
		}
			if(isset($date1) && isset($date2)){
			$q="select * from profiles where pdate between '$date1' and '$date2' order by pid LIMIT $start,$limit";
			}
				if(isset($cmbCategory)){
				$q="select * from profiles where ptype like '%$cmbCategory%' order by pid LIMIT $start,$limit";
				}elseif(isset($cmbNewCategory) || isset($_GET['GO'])){
					$q="select * from profiles where ptype like '%$cmbNewCategory%' order by pid LIMIT $start,$limit";
					} 
$query = mysql_query($q);
// the query above now has the [a] and [b] operators incorporated, making our script paginated. All we need to do now is add the next and previous links.
if(!isset($_GET['limit']) || !isset($_GET['start']) || !isset($_GET['page']) || $_GET['page']=1){
echo "No search results found. Please hit the back button and try again.";
$previous="";
$next="";
}else
{
$_GET['page']=1;
$previous = $start + $limit;
$next = $start - $limit;
echo '<a href="presults.php?start='.$previous.'">Previous</a> - ';
echo '<a href="presults.php?start='.$next.'">Next</a>';
}
// the set of statements above displays the previous and next page links
?></p></span>
<? if(isset($_GET['GO'])){
$q="select * from profiles where ptype like '%$cmbNewCategory%' order by pid LIMIT $start,$limit";
$query = mysql_query($q) or die("Error: ".mysql_error());
}else{?>
            <form METHOD="GET" ACTION="#"> 
          </td>
        </tr>
    <tr valign="bottom"> 
          <td width="200"> 
            <p>
              <br>
              <span class="style2"Select another catagory</font><br>
              <select id="select1" name="cmbNewCategory" style="HEIGHT: 22px; WIDTH: 153px">
                <option value="" selected><font face="Verdana" size="2">All 
                Moldings</font></option>
                    <option value="Back">Back Band</option><option value="Base">Base</option><option value="Base_Cap">Base Cap</option><option value="Base_Shoe">Base Shoe</option><option value="Brick">Brick Mold</option><option value="Casing">Casing</option><option value="Chair_Rail">Chair Rail</option><option value="Corner">Corner Mold</option><option value="Cornice">Cornice</option><option value="Cove">Cove</option><option value="Crown">Crown</option><option value="Decorative">Decorative</option><option value="Dentil">Dentil</option><option value="Flat_Stock">Flat Stock</option><option value="Half_Round">Half Round</option><option value="Jamb_Stock">Jamb Stock</option><option value="Panel">Panel Mold</option><option value="Plant">Plant On</option><option value="Quarter_Round">Quarter Round</option><option value="Stop">Stop</option><option value="Stucco">Stucco Mold</option>
              </select>
              <input type="submit" Value="GO" id="cmdJumpto" name="cmdJumpto">
            </p><? } ?>
            <p> <font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br>
              <a HREF="advsearch.html">Start Over</a> </font></p>
            </form>
            <p align="left"> <br>
              <font size="1" face="Verdana, Arial, Helvetica, sans-serif"><span class="style2"> <b>Searching 
              Criteria:</b><br>
              Category: <? echo $cmbCategory; ?><br>
              Thickness: <? echo $thick1; ?> 
              to <? echo $thick2; ?>
              <br>
              Width: <? echo $height1; ?> 
              to <? echo $height2; ?>
              <br>
              Items: <? echo $item1; ?>
              <br>
              Add Dates:<? echo $date1; ?> 
              to <? echo $date2; ?><br />
		  Description: <? echo $descr; ?></span>
              </font></p>
            <p align="center">  </p>
          </td>
        </tr></table></td>
	<td align=center>
	<table border="0" cellpadding="2" cellspacing="2">
	<?
	$previewp="preview/";
	$columns = 7;
	$current_column = 1;	
while($info=mysql_fetch_array($query)){
if ($current_column == 1) {
        echo "<tr>";
    }
	echo "<td align=center><img src=".$previewp.$info['itemno']." height=100 width=100><br><a href=iteminfo.php?itemno=".$info['itemno']." target=_blank><span class=style2>".$info['itemno']."</span></a><br><span class=style2>".$info['pthick']."x".$info['pwidth']."</span></td>";
	$current_column++;
if ($current_column > $columns) {
        echo "</tr>";
        $current_column = 1;
    }
}
//Close out remaining columns (if needed)
if ($current_column>1) {
    for ($col=$current_column; $col<=$columns; $col++) {
        echo "<td> </td>";
    }
    echo "</tr>"; 
}

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/107528-solved-correcting-query-was-empty-error/
Share on other sites

As of now I have got it to stop saying query was empty but now the message is displaying for only one page of results, along with the results.

 

because in that one case, none of your if's is true, so $q is never set, so you get an empty query message.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.