SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: need help pls

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help pls

    Hello,

    I have a page (product.php) with image link which passing a value (catalog.php?cat=11987) to a page catalog.php. on catalog.php i am getting the value ($cat=$_GET['cat'].
    with this value I have used pagination on catalog.php.

    everything is fine one first page but when i clion on NEXT or page number 2 i get nothing. i understand why is happening. i think as catalog.php is getting the value from product.php and when i click page 2 or next page my query doesn't have this value.

    can anyone please help me to fix this. how can i hold this value for my pagination query. pls need help. thanks.

    product.php
    Code:
    <a href="catalog.php?cat=11987 "><img src="images/phone.jpg" /></a>
    catalog.php

    Code:
      <?php
    
            include "dbconnect.php";
            $link=dbconnect();
             
            $cat=$_GET['cat'];
    
            $adjacents = 3;
    
            $query = "SELECT COUNT(*) as num FROM product where cat='$cat'";
            $total_pages = mysql_fetch_array(mysql_query($query));
            $total_pages = $total_pages[num];
    
    
            $targetpage = "catalog.php";
            $limit = 12;
            $page = $_GET['page'];
            if($page)
                    $start = ($page - 1) * $limit;
            else
                    $start = 0;
    
    
            $sql="select * from product where cat='$cat' order by date desc LIMIT $start, $limit";
            $result=mysql_query($sql);
    
    
            if ($page == 0) $page = 1;
            $prev = $page - 1;
            $next = $page + 1;
            $lastpage = ceil($total_pages/$limit);
            $lpm1 = $lastpage - 1;
    
    
            $pagination = "";
            if($lastpage > 1)
            {
                    $pagination .= "<div class=\"pagination\">";
                    //previous button
                    if ($page > 1)
                            $pagination.= "<a href=\"$targetpage?page=$prev\">previous</a>";
                    else
                            $pagination.= "<span class=\"disabled\">previous</span>";
    
                    //pages
                    if ($lastpage < 7 + ($adjacents * 2))        //not enough pages to bother breaking it up
                    {
                            for ($counter = 1; $counter <= $lastpage; $counter++)
                            {
                                    if ($counter == $page)
                                            $pagination.= "<span class=\"current\">$counter</span>";
                                    else
                                            $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
                            }
                    }
                    elseif($lastpage > 5 + ($adjacents * 2))        //enough pages to hide some
                    {
    
                            if($page < 1 + ($adjacents * 2))
                            {
                                    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                                    {
                                            if ($counter == $page)
                                                    $pagination.= "<span class=\"current\">$counter</span>";
                                            else
                                                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
                                    }
                                    $pagination.= "...";
                                    $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                                    $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
                            }
    
                            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
                            {
                                    $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                                    $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                                    $pagination.= "...";
                                    for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                                    {
                                            if ($counter == $page)
                                                    $pagination.= "<span class=\"current\">$counter</span>";
                                            else
                                                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
                                    }
                                    $pagination.= "...";
                                    $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                                    $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
                            }
    
                            else
                            {
                                    $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                                    $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                                    $pagination.= "...";
                                    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                                    {
                                            if ($counter == $page)
                                                    $pagination.= "<span class=\"current\">$counter</span>";
                                            else
                                                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
                                    }
                            }
                    }
    
    
                    if ($page < $counter - 1)
                            $pagination.= "<a href=\"$targetpage?page=$next\">next</a>";
                    else
                            $pagination.= "<span class=\"disabled\">next</span>";
                    $pagination.= "</div>\n";
            }
    ?>
    
            <?php
    
    
                    $cols=4;
                    echo "<table width=660 border=0 cellspacing=0>";
                    do{
                       echo "<tr>";
                       for($i=1;$i<=$cols;$i++){
    
                            $row=mysql_fetch_array($result);
                            if($row){
    
              ?>
               <td>
               <center>
               <table id="table-3">
               <tbody>
               <tr>
               <td>
                <table width="150" height="200" border=0>
                    <tr valign="top">
                        <td valign="top" bgcolor="#FFFFFF">
                            <center>
                              <?echo "<img src=./admin/upload/".$row['image2'] ." width='140' height='120' >";?>
                            </center>                      <b>
                      <center><span class="style1"><?echo '<a href="details.php?pid=' .urlencode($row['pid']).' & cat=' . urlencode($row['cat']).' & subcat=' . urlencode($row['subcat']) .'">'. $row['title']. '</a> '; ?></span>
                      </center>
                      </b>                      <center>
                        <span class="style1"><?=$row['price'] ?></span>
                            </center>
                                                    <br><br><br><center><?echo '<a href="details.php?pid=' .urlencode($row['pid']).' & cat=' . urlencode($row['cat']).' & subcat=' . urlencode($row['subcat']) .'">'.'<img src="images/vd.jpg" border="0">'.'</a> '; ?></center>
                                                    </td>
    
                    </tr>
    
               </table>
                </td>
                </tr>
                </tbody>
                </table></center>
    
            </td>
    <?
                            }
                            else{
                                    echo "<td>&nbsp;</td>";
                            }
                    }
            } while($row);
            echo "</table>";
    
    
            ?>
    
    <?=$pagination?>

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,808
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    You will need to replace "$targetpage?page=$variable" with "$targetpage?cat=$cat&page=$variable" (replace $variable with the appropriate page variable you are using throughout your code.

    You are losing your data because for each subsequent page you are now telling it what category to show.

    Also, you have a SQL Injection regarding your $cat variable, if $cat is always an integer, change it to $cat = intval($_GET['cat']);

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello cpradio,

    thanks for your reply. can you please show me exactly how can i write the code? i am bit new in php and confused. thanks.

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,808
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pavelazad View Post
    Hello cpradio,

    thanks for your reply. can you please show me exactly how can i write the code? i am bit new in php and confused. thanks.
    Sure thing, here is the updated php code with the changes I recommended (you can compare the code to yours to see the differences)
    PHP Code:
    <?php

            
    include "dbconnect.php";
            
    $link=dbconnect();
             
            
    $cat=intval($_GET['cat']);

            
    $adjacents 3;

            
    $query "SELECT COUNT(*) as num FROM product where cat='$cat'";
            
    $total_pages mysql_fetch_array(mysql_query($query));
            
    $total_pages $total_pages[num];


            
    $targetpage "catalog.php";
            
    $limit 12;
            
    $page $_GET['page'];
            if(
    $page)
                    
    $start = ($page 1) * $limit;
            else
                    
    $start 0;


            
    $sql="select * from product where cat='$cat' order by date desc LIMIT $start$limit";
            
    $result=mysql_query($sql);


            if (
    $page == 0$page 1;
            
    $prev $page 1;
            
    $next $page 1;
            
    $lastpage ceil($total_pages/$limit);
            
    $lpm1 $lastpage 1;


            
    $pagination "";
            if(
    $lastpage 1)
            {
                    
    $pagination .= "<div class=\"pagination\">";
                    
    //previous button
                    
    if ($page 1)
                            
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=$prev\">previous</a>";
                    else
                            
    $pagination.= "<span class=\"disabled\">previous</span>";

                    
    //pages
                    
    if ($lastpage + ($adjacents 2))        //not enough pages to bother breaking it up
                    
    {
                            for (
    $counter 1$counter <= $lastpage$counter++)
                            {
                                    if (
    $counter == $page)
                                            
    $pagination.= "<span class=\"current\">$counter</span>";
                                    else
                                            
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=$counter\">$counter</a>";
                            }
                    }
                    elseif(
    $lastpage + ($adjacents 2))        //enough pages to hide some
                    
    {

                            if(
    $page + ($adjacents 2))
                            {
                                    for (
    $counter 1$counter + ($adjacents 2); $counter++)
                                    {
                                            if (
    $counter == $page)
                                                    
    $pagination.= "<span class=\"current\">$counter</span>";
                                            else
                                                    
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=$counter\">$counter</a>";
                                    }
                                    
    $pagination.= "...";
                                    
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=$lpm1\">$lpm1</a>";
                                    
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=$lastpage\">$lastpage</a>";
                            }

                            elseif(
    $lastpage - ($adjacents 2) > $page && $page > ($adjacents 2))
                            {
                                    
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=1\">1</a>";
                                    
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=2\">2</a>";
                                    
    $pagination.= "...";
                                    for (
    $counter $page $adjacents$counter <= $page $adjacents$counter++)
                                    {
                                            if (
    $counter == $page)
                                                    
    $pagination.= "<span class=\"current\">$counter</span>";
                                            else
                                                    
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=$counter\">$counter</a>";
                                    }
                                    
    $pagination.= "...";
                                    
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=$lpm1\">$lpm1</a>";
                                    
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=$lastpage\">$lastpage</a>";
                            }

                            else
                            {
                                    
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=1\">1</a>";
                                    
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=2\">2</a>";
                                    
    $pagination.= "...";
                                    for (
    $counter $lastpage - (+ ($adjacents 2)); $counter <= $lastpage$counter++)
                                    {
                                            if (
    $counter == $page)
                                                    
    $pagination.= "<span class=\"current\">$counter</span>";
                                            else
                                                    
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=$counter\">$counter</a>";
                                    }
                            }
                    }


                    if (
    $page $counter 1)
                            
    $pagination.= "<a href=\"$targetpage?cat=$cat&page=$next\">next</a>";
                    else
                            
    $pagination.= "<span class=\"disabled\">next</span>";
                    
    $pagination.= "</div>\n";
            }
    ?>

            <?php


                    $cols
    =4;
                    echo 
    "<table width=660 border=0 cellspacing=0>";
                    do{
                       echo 
    "<tr>";
                       for(
    $i=1;$i<=$cols;$i++){

                            
    $row=mysql_fetch_array($result);
                            if(
    $row){

              
    ?>
               <td>
               <center>
               <table id="table-3">
               <tbody>
               <tr>
               <td>
                <table width="150" height="200" border=0>
                    <tr valign="top">
                        <td valign="top" bgcolor="#FFFFFF">
                            <center>
                              <?echo "<img src=./admin/upload/".$row['image2'] ." width='140' height='120' >";?>
                            </center>                      <b>
                      <center><span class="style1"><?echo '<a href="details.php?pid=' .urlencode($row['pid']).' & cat=' urlencode($row['cat']).' & subcat=' urlencode($row['subcat']) .'">'$row['title']. '</a> '?></span>
                      </center>
                      </b>                      <center>
                        <span class="style1"><?=$row['price'?></span>
                            </center>
                                                    <br><br><br><center><?echo '<a href="details.php?pid=' .urlencode($row['pid']).' & cat=' urlencode($row['cat']).' & subcat=' urlencode($row['subcat']) .'">'.'<img src="images/vd.jpg" border="0">'.'</a> '?></center>
                                                    </td>

                    </tr>

               </table>
                </td>
                </tr>
                </tbody>
                </table></center>

            </td>
    <?
                            
    }
                            else{
                                    echo 
    "<td>&nbsp;</td>";
                            }
                    }
            } while(
    $row);
            echo 
    "</table>";


            
    ?>

    <?=$pagination?>

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi cpradio

    thanks a lot for your help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •