search engine and pagination problem

Jan

New Member
Hello!

I am trying to merge the code for pagination and search and need help to do it.
I dont know how to make results display like
1, 2,
3, 4; I want to have the results dispalyed in two columns.
Also, the pagination numbers, next and prev button doesn't appear.


PHP:
<h2>Results:</h2>
<?php
  // create short variable names
  $searchtype=$_POST['searchtype'];
  $searchterm=trim($_POST['searchterm']);

  if (!$searchtype || !$searchterm) {
     echo 'You have not entered search details.  Please go back and try again.';
     exit;
  }

  if (!get_magic_quotes_gpc()){
    $searchtype = addslashes($searchtype);
    $searchterm = addslashes($searchterm);
  }
@ $db = new mysqli('localhost', 'user', 'pass', 'db');
 
  if (mysqli_connect_errno()) {
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }
 //start
//max displayed per page
$per_page = 4;

//get start variable
$start = $_GET['start'];

//count records
$record_count = mysql_num_rows(mysql_query("SELECT * FROM tablename"));

if (!$start)
   $start = 0;
   
//display data

//end
 $get = $query; 
 $query =("select * from tablename where $max".$searchtype." like '%".$searchterm."%' LIMIT $start, $per_page");
  $result = $db->query($query);

  $num_results = $result->num_rows;
 
  echo "<p>Number of items found: ".$num_results."</p>";

  for ($i=0; $i <$num_results; $i++) {
     $row = $result->fetch_assoc();
   echo "<div class=iskalnikbg>";
	 echo "<p><strong>".($i+1).". Št. artikla: ";
     echo htmlspecialchars(stripslashes($row['st_artikla']));
     echo "</strong><br />Opis: ";
     echo stripslashes($row['opis']);
     echo "<br />Barva: ";
     echo stripslashes($row['barva']);
     echo "<br />Št.: ";
     echo stripslashes($row['st.']);
     echo "<br />Cena: €";
     echo stripslashes($row['cena']);
echo "</div>";
echo "<div class=thumb_position>";
	 
	 echo stripslashes($row['ozadje']);
echo "</div>";
	 echo "</p>";
  
}

//start
//setup prev and next variables

$prev = $start - $per_page;
$next = $start + $per_page;

echo "<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>";
//show prev button
if (!($start<=0))
       echo "<a href=index.php?start=$prev>Prev</a> ";

//show page numbers

//set variable for first page
$i=1;

for ($x=0;$x<$record_count;$x=$x+$per_page)
{
 if ($start!=$x)
    echo " <a href='index.php?start=$x'>$i</a> ";
 else
    echo " <a href='index.php?start=$x'><b>$i</b></a> ";
 $i++;
}

//show next button
if (!($start>=$record_count-$per_page))
       echo " <a href='index.php?start=$next'>Next</a>";
//end
 
  $result->free();
  $db->close();

?>
Can somebody give me a hint on how to solve this problem?
Thanks!
 
Last edited:

smoovo

New Member
It seems like you have problem with your connection to the server. One of the two, you haven't setup connection yet (no host, no username and no password), or you did but it's not right.
 

Jan

New Member
It seems like you have problem with your connection to the server. One of the two, you haven't setup connection yet (no host, no username and no password), or you did but it's not right.

Hi smoovo & thanks for your reply.

The connection details are correct, if you meant this: @ $db = new mysqli('localhost', 'user', 'pass', 'db');
Anyway, when I cut off the code below the error messages don't appear...
PHP:
$record_count = mysql_num_rows(mysql_query("SELECT * FROM tablename"));
Maybe it's a good idea to show what I'm trying to merge:
search engine code (results.php only taken from the book PHP & MYSQL Web Development):
PHP:
<?php
  // create short variable names
  $searchtype=$_POST['searchtype'];
  $searchterm=trim($_POST['searchterm']);

  if (!$searchtype || !$searchterm) {
     echo 'You have not entered search details.  Please go back and try again.';
     exit;
  }

  if (!get_magic_quotes_gpc()){
    $searchtype = addslashes($searchtype);
    $searchterm = addslashes($searchterm);
  }

 @ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');

  if (mysqli_connect_errno()) {
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }
 



  $query = "select * from srajce where $max".$searchtype." like '%".$searchterm."%'";
  $result = $db->query($query);

  $num_results = $result->num_rows;
 
  echo "<p>Number of books found: ".$num_results."</p>";

  for ($i=0; $i <$num_results; $i++) {
     $row = $result->fetch_assoc();
   echo "<div class=iskalnikbg>";
	 echo "<p><strong>".($i+1).". Št. artikla: ";
     echo htmlspecialchars(stripslashes($row['st_artikla']));
     echo "</strong><br />Opis: ";
     echo stripslashes($row['opis']);
     echo "<br />Barva: ";
     echo stripslashes($row['barva']);
     echo "<br />Št.: ";
     echo stripslashes($row['st.']);
     echo "<br />Cena: €";
     echo stripslashes($row['cena']);
echo "</div>";
echo "<div class=thumb_position>";
	 
	 echo stripslashes($row['ozadje']);
echo "</div>";
	 echo "</p>";
  
}
 
  $result->free();
  $db->close();

?>

Pagination (tutorial code taken from www.phpacademy.org):
PHP:
<?php

//connecting to the database
$error = "Could not connect to the database";
mysql_connect('localhost','root','') or die($error);
mysql_select_db('pagination') or die($error);

//max displayed per page
$per_page = 36;

//get start variable
$start = $_GET['start'];

//count records
$record_count = mysql_num_rows(mysql_query("SELECT * FROM data"));

//count max pages
$max_pages = $record_count / $per_page; //may come out as decimal

if (!$start)
   $start = 0;
   
//display data
$get = mysql_query("SELECT * FROM data LIMIT $start, $per_page");
while ($row = mysql_fetch_assoc($get))
{
 // get data
 $name = $row['name'];
 $age = $row['age'];
 
 echo $name." (".$age.")<br />";

}

//setup prev and next variables
$prev = $start - $per_page;
$next = $start + $per_page;

//show prev button
if (!($start<=0))
       echo "<a href='index.php?start=$prev'>Prev</a> ";

//show page numbers

//set variable for first page
$i=1;

for ($x=0;$x<$record_count;$x=$x+$per_page)
{
 if ($start!=$x)
    echo " <a href='index.php?start=$x'>$i</a> ";
 else
    echo " <a href='index.php?start=$x'><b>$i</b></a> ";
 $i++;
}

//show next button
if (!($start>=$record_count-$per_page))
       echo " <a href='index.php?start=$next'>Next</a>";

?>

Thanks & have a good day,
Jan
 
Top