Jump to content

[SOLVED] php search in database with prev next ...


sheriff

Recommended Posts

Hi ... I try to make a search in database, but with the results on multiple pages...

sorry for my english ..

 

so ..

 

this code is work somehow, but if i click on next,

the next page is empty and the "Error, query failed" is displayed.

 

Please help a noob to solve this problem  ;D

 

this is  the page: http://cristi.amirb.net/ivanyi.com/oferta.php

 

 

this is the search

 

<form method="post" action="search.php" name="search">
<table border="0" cellpadding="1" cellspacing="1">
<tr>
<td align="center" valign="middle">
    <input type="radio" name="category" value="pr_code" checked> <span class="cn_text">Cod</span>
    <input type="radio" name="category" value="pr_name"> <span class="cn_text">Name</span></td>
</tr>
<tr>
<td>
    <input type="text" name="search" size="40" style="background-color: #CCCCCC; border-width:thin">
    </td>
    <td><input type="submit" name="submit" value="Cauta">
    </td>
</tr>
</table>
</form>

 

 

and this is the result page

 

<?php echo "Cuvant cautat: ".$_REQUEST['search']." din categoria ".$_REQUEST['category']."."; ?>
<br>
<table border="1" cellpadding="1" cellspacing="1">
<tr>
    	<td bgcolor="#000000" align="center"><span class="cb_text">Nr.Crt.</span></td>
        <td bgcolor="#000000" align="center"><span class="cb_text">Cod</span></td>
        <td bgcolor="#000000" align="center"><span class="cb_text">Nume</span></td>
        <td bgcolor="#000000" align="center"><span class="cb_text">Descriere</span></td>
    </tr>
    
		<?php
            
            // how many rows to show per page
            $rowsPerPage = 5;
            
            // by default we show first page
            $pageNum = 1;
            
            // if $_GET['page'] defined, use it as page number
            if(isset($_GET['page']))
            {
                $pageNum = $_GET['page'];
            }
            
            // counting the offset
            $offset = ($pageNum - 1) * $rowsPerPage;

		// remove white spaces befor and after the word
            // $trimmed = trim($_REQUEST['search']);

		$query  = "SELECT * FROM products WHERE $_REQUEST[category] LIKE '%$_REQUEST[search]%' LIMIT $offset, $rowsPerPage";
            $result = mysql_query($query) or die('Error, query failed');
            $row_count = "1";
            
            $color1 = "#DDDDDD"; 
            $color2 = "#CCCCCC"; 
            
            
            // print the random numbers
            while($val = mysql_fetch_array($result))
            { 
                $row_color = ($row_count % 2) ? $color1 : $color2;
            
            ?>

<tr>
    	<td bgcolor="<?php echo $row_color; ?>" align="center" width="30"><span class="cn_text"><?php echo $row_count++; ?></span></td>
        <td width="40" bgcolor="<?php echo $row_color; ?>"><span class="cn_text"> <?php echo $val['pr_code']; ?></span></td>
        <td width="150" bgcolor="<?php echo $row_color; ?>"><span class="cn_text"> <?php echo $val['pr_name']; ?></span></td>
        <td width="200" bgcolor="<?php echo $row_color; ?>"><span class="cn_text"> <?php echo $val['pr_desc_tech']; ?></span></td>
</tr><tr>
        <td colspan="5" bgcolor="<?php echo $row_color; ?>"><span class="cn_text">Descriere: <?php echo $val['pr_desc_expl']; ?></span></td>
    </tr>


		<?php
                //echo "$val <br>";
            //$id = $val['pr_id'];
            }

		?>
            
</table><br>

            <?php

            // how many rows we have in database
            //$query   = "SELECT COUNT('$val[pr_id]') AS numrows FROM products";
            //$result  = mysql_query($query) or die('Error, query failed');
            //$row     = mysql_fetch_array($result, MYSQL_ASSOC);
            $numrows = $row_count++; // $row['numrows'];
            
            // how many pages we have when using paging?
            $maxPage = ceil($numrows/$rowsPerPage);
            
            $self = $_SERVER['PHP_SELF'];
            
            // creating 'previous' and 'next' link
            // plus 'first page' and 'last page' link
            
            // print 'previous' link only if we're not
            // on page one
            if ($pageNum > 1)
            {
                $page = $pageNum - 1;
                $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
                
                $first = " <a href=\"$self?page=1\">[First Page]</a> ";
            } 
            else
            {
                $prev  = ' [inapoi] ';       // we're on page one, don't enable 'previous' link
                $first = ' [Prima Pagina] '; // nor 'first page' link
            }
            
            // print 'next' link only if we're not
            // on the last page
            if ($pageNum < $maxPage)
            {
                $page = $pageNum + 1;
                $next = " <a href=\"$self?page=$page\">[inainte]</a> ";
                
                $last = " <a href=\"$self?page=$maxPage\">[ultima Pagina]</a> ";
            } 
            else
            {
                $next = ' [Next] ';      // we're on the last page, don't enable 'next' link
                $last = ' [Last Page] '; // nor 'last page' link
            }
            
            // print the page navigation link
            echo $first . $prev . " Pagina <strong>$pageNum</strong> din <strong>$maxPage</strong> pagini " . $next . $last;
            
            ?>

 

 

thanks !

Link to comment
Share on other sites

Ok, first step, lets see whats actualyl being passed into the query. Change this:

<?php
$query  = "SELECT * FROM products WHERE $_REQUEST[category] LIKE '%$_REQUEST[search]%' LIMIT $offset, $rowsPerPage";
           $result = mysql_query($query) or die('Error, query failed');
?>

to:

<?php
$query  = "SELECT * FROM products WHERE $_REQUEST[category] LIKE '%$_REQUEST[search]%' LIMIT $offset, $rowsPerPage";
           $result = mysql_query($query) or die("Query failed: $query<br /><br />mysql_error()");
?>

Link to comment
Share on other sites

ok .. I found the problem ...

 

I don't know it's a good method, but in this way is work!

 

Please tell me if it's ok ...

 

Thanks for help.

 

 

 

if ($pageNum > 1)
            {
                $page = $pageNum - 1;
                $prev = " <a href=\"$self?page=$page&search=$_REQUEST[search]&category=$_REQUEST[category]\">[Prev]</a> ";
                
                $first = " <a href=\"$self?page=1&search=$_REQUEST[search]&category=$_REQUEST[category]\">[First Page]</a> ";
            } 
            else
            {
                $prev  = ' [inapoi] ';       // we're on page one, don't enable 'previous' link
                $first = ' [Prima Pagina] '; // nor 'first page' link
            }
            
            // print 'next' link only if we're not
            // on the last page
            if ($pageNum < $maxPage)
            {
                $page = $pageNum + 1;
                $next = " <a href=\"$self?page=$page&\">[inainte]</a> ";
                
                $last = " <a href=\"$self?page=$maxPage\">[ultima Pagina]</a> ";
            } 
            else
            {
                $next = ' [Next] ';      // we're on the last page, don't enable 'next' link
                $last = ' [Last Page] '; // nor 'last page' link
            }

 

if ($pageNum > 1)
            {
                $page = $pageNum - 1;
                $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
                
                $first = " <a href=\"$self?page=1\">[First Page]</a> ";
            } 
            else
            {
                $prev  = ' [inapoi] ';       // we're on page one, don't enable 'previous' link
                $first = ' [Prima Pagina] '; // nor 'first page' link
            }
            
            // print 'next' link only if we're not
            // on the last page
            if ($pageNum < $maxPage)
            {
                $page = $pageNum + 1;
                $next = " <a href=\"$self?page=$page&search=$_REQUEST[search]&category=$_REQUEST[category]\">[inainte]</a> ";
                
                $last = " <a href=\"$self?page=$maxPage&search=$_REQUEST[search]&category=$_REQUEST[category]\">[ultima Pagina]</a> ";
            } 
            else
            {
                $next = ' [Next] ';      // we're on the last page, don't enable 'next' link
                $last = ' [Last Page] '; // nor 'last page' link
            }

Link to comment
Share on other sites

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.