Trouble with (Next Page) * (Previous Page)

Discussion in 'PHP' started by LongshotMP, Apr 26, 2017.

  1. #1
    Hi everyone.

    I'm trying very hard to add some sort of pagination on my script, but it seems to be going horribly. I've been trying for hours, and although I feel I am close, it just seems to not be working so I thought I'd ask for help.

    Without the pagination script I have inserted, there is a list of cars.
    When I add it, the list complete disappears.

    The row that I have inserted is abour 3/4 of the way down, starting with
    <?php
    //check if the starting row variable was passed in the URL or not
    if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {


    Please help me!


    <?php include"mainmenu.php"; ?>
    <?php
    if ($_POST['sell'] && $_POST['car']){
    
    $sell = $_POST['car'];
        foreach($sell as $checkboxid)
        {
            $sql="SELECT * from cars WHERE id='$checkboxid'";
    $result=mysql_query($sql);
    while($rows=mysql_fetch_array($result)){ // Start looping table row
    $id= $rows['id'];
    $cprice= $rows['price'];
    $carname= $rows['name'];
    }
    mysql_query("DELETE FROM cars WHERE id='$checkboxid'");
    mysql_query("UPDATE users SET `cash`=`cash`+'$cprice' WHERE username='$username'");
    }
    echo "You sold your car(s)!";   
    
    }
    
    ?>
    
    <?php
    if ($_POST['rcar'] && $_POST['car']){
    
    $rcar = $_POST['car'];
        foreach($rcar as $checkboxid)
        {
       
    $sql="SELECT * from cars WHERE id='$checkboxid'";
    $result=mysql_query($sql);
    
    while($rows=mysql_fetch_array($result)){ // Start looping table row
    
    
    $id= $rows['id'];
    $carname= $rows['name'];
    
    ?>
    
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=topleft NOWRAP>
    </td>
    <td class=leftbar NOWRAP>
    </td>
    <td class=bar NOWRAP>
    
    </td>
    <td class=rightbar NOWRAP>
    </td>
    <td class=topright NOWRAP>
    </td>
    </tr>
    </tbody>
    </table>
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=left NOWRAP>
    </td>
    <td class=main>
    <div align="left">
    
    <?php echo "This car already has 0% damage!"; ?>
    
    
    </div>
    </td>
    <td class=right NOWRAP>
    </td>
    </tr>
    </Tbody>
    </table>
    
    
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=bottomleft NOWRAP>
    </td>
    <td class=bottom NOWRAP>
    </td>
    <td class=bottomright NOWRAP>
    </td>
    </tr>
    </tbody>
    </table>
    
    <?php
    }//loop
    }//loop
    
    }//if submit
    ?>
    
    <?php
    
    if ($_POST['drop'] && $_POST['car']){
    
    $dcar = $_POST['car'];
    
        foreach($dcar as $dropcar)
        {
          mysql_query("DELETE FROM cars WHERE id='$dropcar' AND owner='$username'");
    
        }
    
    echo "<b>Selected cars have been dropped!</b>";
    }//if submit
    ?>
    
    
    <?php
    
    if ($_POST['rcar'] && $_POST['car']){
    
    $rcar = $_POST['car'];
    
        foreach($rcar as $checkboxid)
        {
       
    $sql="SELECT * from cars WHERE id='$checkboxid'";
    $result=mysql_query($sql);
    
    while($rows=mysql_fetch_array($result)){ // Start looping table row
    
    $id= $rows['id'];
    $damage= $rows['damage'];
    $type= $rows['type'];
    
    if($type=="1"){$carname = $car_1;}
    if($type=="2"){$carname = $car_2;}
    if($type=="3"){$carname = $car_3;}
    if($type=="4"){$carname = $car_4;}
    if($type=="5"){$carname = $car_5;}
    if($type=="6"){$carname = $car_6;}
    if($type=="7"){$carname = $car_7;}
    if($type=="8"){$carname = $car_8;}
    if($type=="9"){$carname = $car_9;}
    if($type=="10"){$carname = $car_10;}
    if($type=="11"){$carname = $car_11;}
    if($type=="12"){$carname = $car_12;}
    if($type=="13"){$carname = $car_13;}
    
    if ($damage ==0){
    
    ?>
    
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=topleft NOWRAP>
    </td>
    <td class=leftbar NOWRAP>
    </td>
    <td class=bar NOWRAP>
    
    </td>
    <td class=rightbar NOWRAP>
    </td>
    <td class=topright NOWRAP>
    </td>
    </tr>
    </tbody>
    </table>
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=left NOWRAP>
    </td>
    <td class=main>
    <div align="left">
    
    
    <?php echo "This car allready has 0% damage!"; ?>
    
    </div>
    </td>
    <td class=right NOWRAP>
    </td>
    </tr>
    </Tbody>
    </table>
    
    
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=bottomleft NOWRAP>
    </td>
    <td class=bottom NOWRAP>
    </td>
    <td class=bottomright NOWRAP>
    </td>
    </tr>
    </tbody>
    </table>
    
    
    
       
    
    <?php
    
    }else{
    $c = rand(9,110);
    
    if($repaircar>=1){
    
    
    mysql_query("UPDATE cars SET damage='0' WHERE id='$checkboxid' AND owner='$username'");
    ?>
    
    
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=topleft NOWRAP>
    </td>
    <td class=leftbar NOWRAP>
    </td>
    <td class=bar NOWRAP>
    
    </td>
    <td class=rightbar NOWRAP>
    </td>
    <td class=topright NOWRAP>
    </td>
    </tr>
    </tbody>
    </table>
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=left NOWRAP>
    </td>
    <td class=main>
    <div align="left">
    
    
    <?php echo "You successfully fixed your $carname with $damage% damage!"; ?>
    
    
    </div>
    </td>
    <td class=right NOWRAP>
    </td>
    </tr>
    </Tbody>
    </table>
    
    
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=bottomleft NOWRAP>
    </td>
    <td class=bottom NOWRAP>
    </td>
    <td class=bottomright NOWRAP>
    </td>
    </tr>
    </tbody>
    </table>
    
    
    
       
    <?php
    mysql_query("UPDATE users SET `repaircar`=`repaircar`-'1' WHERE username='$username'");
    
    }else{
    
    if ($c < $damage){
    
    mysql_query("DELETE FROM cars WHERE id='$checkboxid'");
    
    ?>
    
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=topleft NOWRAP>
    </td>
    <td class=leftbar NOWRAP>
    </td>
    <td class=bar NOWRAP>
    
    </td>
    <td class=rightbar NOWRAP>
    </td>
    <td class=topright NOWRAP>
    </td>
    </tr>
    </tbody>
    </table>
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=left NOWRAP>
    </td>
    <td class=main>
    <div align="left">
    
    <?php echo "You failed to repair your $carname with $damage% damage."; ?>
    
    
    </div>
    </td>
    <td class=right NOWRAP>
    </td>
    </tr>
    </Tbody>
    </table>
    
    
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=bottomleft NOWRAP>
    </td>
    <td class=bottom NOWRAP>
    </td>
    <td class=bottomright NOWRAP>
    </td>
    </tr>
    </tbody>
    </table>
    
    
       
    
    <?php
    
    
    }else{
    
    mysql_query("UPDATE cars SET damage='0' WHERE id='$checkboxid' AND owner='$username'");
    ?>
    
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=topleft NOWRAP>
    </td>
    <td class=leftbar NOWRAP>
    </td>
    <td class=bar NOWRAP>
    
    </td>
    <td class=rightbar NOWRAP>
    </td>
    <td class=topright NOWRAP>
    </td>
    </tr>
    </tbody>
    </table>
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=left NOWRAP>
    </td>
    <td class=main>
    <div align="left">
    
    <?php echo "You successfully fixed your $carname with $damage% damage!"; ?>
    
    
    </div>
    </td>
    <td class=right NOWRAP>
    </td>
    </tr>
    </Tbody>
    </table>
    
    
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=bottomleft NOWRAP>
    </td>
    <td class=bottom NOWRAP>
    </td>
    <td class=bottomright NOWRAP>
    </td>
    </tr>
    </tbody>
    </table>
    
    
       
    
    <?php
    
    }//fail else
    }//damage not zero
    
    }//repair car successfully
    }//loop
    }//loop
    
    
    
    }//if submit
    ?>
    
    
    
    
    
    
    
    
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=topleft NOWRAP>
    </td>
    <td class=leftbar NOWRAP>
    </td>
    <td class=bar NOWRAP>
    
    </td>
    <td class=rightbar NOWRAP>
    </td>
    <td class=topright NOWRAP>
    </td>
    </tr>
    </tbody>
    </table>
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=left NOWRAP>
    </td>
    <td class=main>
    <div align="center">
    
    <script language="javascript">
    checked = false;
          function checkAll () {
            if (checked == false){checked = true}else{checked = false}
        for (var i = 0; i < document.getElementById('crimes').elements.length; i++) {
          document.getElementById('crimes').elements[i].checked = checked;
        }
          }
    
    
    </script>
    
     
      <?php       
    //check if the starting row variable was passed in the URL or not
    if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
      //we give the value of the starting row to 0 because nothing was found in URL
      $startrow = 0;
    //otherwise we take the value from the URL
    } else {
      $startrow = (int)$_GET['startrow'];
    }
    //this part goes after the checking of the $_GET var
    $fetch = mysql_query("SELECT * FROM cars WHERE owner='".mysql_real_escape_string($username)."' ORDER BY type ASC LIMIT $startrow, 10")or
    die(mysql_error());
    
    
    while($row = mysql_fetch_array( $result )) {
    
    $id = $row['id'];
    $type = $row['type'];
    $damage = $row['damage'];
    $sale = $row['sale'];
    $price = number_format($row['price']);
    
    if($type=="1"){$carname = $car_1;}
    if($type=="2"){$carname = $car_2;}
    if($type=="3"){$carname = $car_3;}
    if($type=="4"){$carname = $car_4;}
    if($type=="5"){$carname = $car_5;}
    if($type=="6"){$carname = $car_6;}
    if($type=="7"){$carname = $car_7;}
    if($type=="8"){$carname = $car_8;}
    if($type=="9"){$carname = $car_9;}
    if($type=="10"){$carname = $car_10;}
    if($type=="11"){$carname = $car_11;}
    if($type=="12"){$carname = $car_12;}
    if($type=="13"){$carname = $car_13;}
    
       $num=Mysql_num_rows($fetch);
            if($num>0)
            {
            echo "<table>";
            echo "<tr><td>Car name</td><td>Car damage</div></td><td>Repair chance</td><td>Price</td></tr>";
            for($i=0;$i<$num;$i++)
            {
    
            $row=mysql_fetch_row($fetch);    
            ?>
    
    
    <tr>
    <td class="header" colspan="4">
    <div align="center" class="unnamed1">Cars</div>
    </td>
    </tr>
    
    
    <form action='' id=crimes method='post'>
    
    
                
    
    <tr>           
    
    <td width="40%" height="6" align="left" bgcolor="<?php echo "$tabcolor"; ?>" class="tabA">
    <label for="<?php echo $row['id']; ?>">
    <input name="car[]" type="checkbox" value="<? echo $id; ?>">
    <a href="viewcar.php?carid=<? echo $id; ?>"><?php echo $carname; ?></a>
    </label>
    </td>
    
    <td width="20%" height="6" align="left" bgcolor="<?php echo "$tabcolor"; ?>" class="tabA"><label>
    <?php echo $row['damage']."%"; ?>
    </label>
    </td>
    
    
    <td width="20%" height="6" align="left" bgcolor="<?php echo "$tabcolor"; ?>" class="tabA"><label>
    <?php
    if ($damage==0){ echo "100"; }else{echo 100-$damage;}
    ?>
    %</label>
    </td>
    
    <td width="20%" hight="6" align="left" bgcolor="<?php echo "$tabcolor"; ?>" class="tabA"><label>
    $ <?php
    echo $price;
    ?>
    </label>
    </td>
    
           
    </tr>
    
       <?php }// while loop ?>
    
    
    <?PHP      
            }//for
            echo"</table>";
            }
    //now this is the link..
    echo '<div class="pagination"><ul><li class="next"><a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+10).'">Next</a></li>';
    
    $prev = $startrow - 10;
    
    //only print a "Previous" link if a "Next" was clicked
    if ($prev >= 0)
        echo '<li class="prev"><a href="'.$_SERVER['PHP_SELF'].'?startrow='.$prev.'">Previous</a></li></ul></div>';
    ?>
    
    <input type='submit' name='rcar' class='tbox' value='Repair Cars'>
    
    <input type='submit' name='sell' class='tbox' value='Sell Cars'>
    
    </center>
    </form>
    
    
    
    
    </div>
    </td>
    <td class=right NOWRAP>
    </td>
    </tr>
    </Tbody>
    </table>
    
    
    <table cellSpacing=0 cellPadding=0 style="WIDTH: 100%">
    <Tbody>
    <tr>
    <td class=bottomleft NOWRAP>
    </td>
    <td class=bottom NOWRAP>
    </td>
    <td class=bottomright NOWRAP>
    </td>
    </tr>
    </tbody>
    </table>
    <br>
    <br>
    
    
    <?php include"rightside.php"; ?>
    PHP:
     
    LongshotMP, Apr 26, 2017 IP
  2. davcheong

    davcheong Well-Known Member

    Messages:
    1,148
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    175
    #2
    Hi,

    The problems might be link to this :
    1. $username where or when user press link next it will be navigating to another page and it is not consider post to another page, unless the $username were actually store at the back memory such as session or cookies.
     
    Last edited: Jun 14, 2017
    davcheong, Jun 14, 2017 IP