Hi,
I have this code:

<?PHP
                require ("connect.php");
                //$db_handle = mysql_connect($server, $user_name, $password); =>$connect
                $db_found = mysql_select_db($_mysql_db, $connect);

                if ($db_found) {

                $SQL = "SELECT * FROM cheats";
                $result = mysql_query($SQL);
                $i=0;
                while ($db_field = mysql_fetch_assoc($result)) 
                {   



                    if($i%2===0)
                    {

                        $mesaj="<li><a href=\"{$db_field['Image']}\" class=\"pirobox\" title=\"{$db_field['Titlu']}\"><img src=\"{$db_field['Image']}\"/></a><p>{$db_field['Content']}</p><div class=\"button\"><pre><a href=\"#\">Read more</a>      <a href=\"{$db_field['Download']}\">Download</a></pre></div></li>";
                        print ("$mesaj");
                    }
                    else
                    {

                        $mesaj="<li class=\"odd\"><a href=\"{$db_field['Image']}\" class=\"pirobox\" title=\"{$db_field['Titlu']}\"><img src=\"{$db_field['Image']}\"/></a><p>{$db_field['Content']}</p><div class=\"button\"><pre><a href=\"#\">Read More</a>        <a href=\"{$db_field['Download']}\">Download</a></pre></div></li>";
                        print ("$mesaj");
                    }
                $i++;
                }

                mysql_close($connect);

                }
                else {
                print "Database NOT Found ";
                mysql_close($connect);
                }

            ?>

That takes the info from database and list them.

Now i want to make it create another page after a number of elements .
I have no ideea how could i do this please some help. I'm just a beginer.

Recommended Answers

All 3 Replies

Member Avatar for diafol

Ok, seems that you're looking for 'pagination'. This topic has been done to death recently. Try searching this forum for 'pagination'. If you're still stuck, come back.

# Heading Here #
<?php
session_start();
$user_check=$_SESSION['login_user'];
if(!isset($user_check))
{
header("Location: index.php");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Registration Data</title>
<link rel="stylesheet" href="style.css" type="text/css" media="all" />

</head>

<body>
<form class="form" name="form" method="post" action="registration.php">
<center><table class="box" align="center" style="width:600px;background-color: #CC99FF; text-align:justify;" border="1">
<tr><td align="right" style="padding-right:20px;" colspan="4"><strong><a href="logout.php" >Logout</a></strong></td></tr>
<tr><td align="center" style="height:60px;" colspan="4">
<?php echo "<center><h2>Registration Data</h2></center>"; ?>
</td></tr>
            <tr align="center" style="height:40px;">
            <th>Name</th>
            <th>E-mail</th>
            <th>Phone No.</th>
            <th>Query</th>
            </tr>
            <?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("letuwin_cdc", $con);

$result = mysql_query("SELECT * FROM registration");

$tbl_name="registration";       //your table name
    // How many adjacent pages should be shown on each side?
    $adjacents = 3;

    /* 
       First get total number of rows in data table. 
       If you have a WHERE clause in your query, make sure you mirror it here.
    */
    $query = "SELECT COUNT(*) as num FROM $tbl_name";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages[num];   
    /* Setup vars for query. */
    $targetpage = "registration.php";   //your file name  (the name of this file)
    $limit = 10;                                //how many items to show per page
    $page = $_GET['page'];
    if($page) 
        $start = ($page - 1) * $limit;          //first item to display on this page
    else
        $start = 0;                             //if no page var is given, set start to 0

    /* Get data. */
    $sql = "SELECT * FROM $tbl_name LIMIT $start, $limit";
    $result = mysql_query($sql);

    /* Setup page vars for display. */
    if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
    $prev = $page - 1;                          //previous page is page - 1
    $next = $page + 1;                          //next page is page + 1
    $lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
    $lpm1 = $lastpage - 1;                      //last page minus 1

    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination1 = "";
    if($lastpage > 1)
    {   
        $pagination1 .= "<div class=\"pagination\">";
        //previous button
        if ($page > 1) 
            $pagination1.= "<a href=\"$targetpage?page=$prev\">  previous</a>";
        else
            $pagination1.= "<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)
                    $pagination1.= "<span class=\"current\">$counter</span>";
                else
                    $pagination1.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                  
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page < 1 + ($adjacents * 2))     
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination1.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination1.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                  
                }
                $pagination1.= "...";
                $pagination1.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination1.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";        
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination1.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination1.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination1.= "...";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination1.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination1.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                  
                }
                $pagination1.= "...";
                $pagination1.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination1.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";        
            }
            //close to end; only hide early pages
            else
            {
                $pagination1.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination1.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination1.= "...";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination1.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination1.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                  
                }
            }
        }

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

while($row1= mysql_fetch_array($result))



  {
  ?>
  <tr>
 <td><?php echo $row1['name'];?></td>
  <td><?php echo$row1['email'] ;?></td> 
  <td><?php echo$row1['phone'] ;?></td>
   <td><?php echo$row1['query'] ;?></td>
   </tr>
   <?php 

  }
  ?>

<tr class="pagging">
<td colspan="4"> 
<?php echo $pagination1; ?></td></tr>
<br/>
<br/>
<tr><td  align="center" colspan="4"><a href="registration_results.php">Go Back</a></td></tr>


</table></center>
<center><div align="center" style="width:600px;" class="box" id="footer">Copyright @ LetUWin CDC.com</div></center>



</form>





</body>

</html>
//Here is a simple code that worked fine for me.

<?php
    require_once("common.php");
    $dbname="xyz";
    $ob=new common_function();
    //Data Base Connection
    $connect=$ob->db_connect($dbname);
    if(isset($_GET["page"])) 
    { 
        $page  = $_GET["page"];
    } 
    else 
    { 
        $page=1; 
    }
    $start_from = ($page-1) * 5;
    $sql = "select * from trn_batch LIMIT 5  OFFSET $start_from";
    $rs_result=$ob->select($sql,$connect);
    echo "<br><br><table border=\"1\" width=\"100\">
    <tr><td>Batch id</td><td>Batch name</td><td>Instructor1</td><td>Instructor2</td><td>Instructor3</td><td>Start date</td><td>End date</td><td>Training Hall</td></tr>";
    while($row = pg_fetch_row($rs_result)) 
    {
        echo "<tr>
        <td>".$row[0]."</td>
        <td>".$row[1]."</td>
        <td>".$row[2]."</td>
        <td>".$row[3]."</td>
        <td>".$row[4]."</td>
        <td>".$row[6]."</td>
        <td>".$row[7]."</td>
        <td>".$row[8]."</td>
        </tr>";
    }
    echo"</table>";
    $off++;
    $sql = "SELECT COUNT(batch_id) FROM trn_batch";
    $rs_result=$ob->select($sql,$connect);
    while($row = pg_fetch_row($rs_result))
    {
        $total_records = $row[0];
    }
    $total_pages = ceil($total_records / 5);
    for ($i=1; $i<=$total_pages; $i++) 
    {
        echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
    }
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.