lucan Posted October 5, 2011 Share Posted October 5, 2011 I am using the following search code with a paginate. the code works fine but how do i hide the paginate if the search results are less than the default settings mysql_connect ("localhost", "user","pword") or die (mysql_error()); mysql_select_db ("database"); $term = $_GET['term']; $cat = $_GET['cat']; //max display per page $per_page = 4; //get start variable $start = $_GET['start']; // cout records $record_count = mysql_num_rows(mysql_query("SELECT * FROM table")); //count max pages $max_pages = $record_count / $per_page; //may count as decimal if (!$start) $start =0; $sql="SELECT * FROM table WHERE productname like '%$term%' and category like '%$cat%' LIMIT $start, $per_page"; $result = mysql_query($sql); $sql = mysql_query("SELECT COUNT(*) as total FROM table"); list($number) = mysql_fetch_row($sql); if (!mysql_num_rows($result)) { die("NO RECORD FOUND") ; exit; } else { echo "Your matches for: <b>$term</b><br><br>"; } while ($row = mysql_fetch_array($result)){ $num_rows = mysql_num_rows($number); if (!$i++) echo "Total records $number"; ?> <br /> <br /> <?php echo 'ID: '.$row['id']; ?> <br /> <?php echo '<br/> Product Name: '.$row['productname'];?> <br /> <?php echo '<br/> Price: '.$row['price']; ?><br /> <?php echo '<br/> category: '.$row['category']; ?> <br /> <?php } ?> <?php //setup prev and next variables $prev = $start - $per_page; $next = $start + $per_page; // show prev button if(!($start<=0)) echo " <a href='ttr.php?start=$prev'>Prev </a>"; //set var for first page $i=1; for ($x=0;$x<$record_count;$x=$x+$per_page) { if ($start!=$x) echo "<a href='page.php?start=$x'>$i</a> "; else echo "<a href='page.php?start=$x'><b>$i</b></a> "; $i++; } //show next button if (!($start>=$record_count-$per_page)) echo " <a href='page.php?start=$next'>Next</a>"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.