0

I have a code that works but I can not split up the table into different pages. (Page1, Page2, Page3 etc. but the same table continues on each page...)

Here is what it looks like:

http://test.mehmetakb.se/test1.jpg

Here is my code.

Is it easy to do what I want?

<!DOCTYPE HTML>
<html lang="sv-SE"/>
<head>
<meta charset="UTF-8"/>
<title>Images</title>
<style media="screen" type="text/css">
tr {display: inline-block;}

#gallery {
    height: 510px;
    width: 670px;
    background-color: grey;
    margin-left: auto;
    margin-right: auto;
    text-align: center;
    display-align: center;
    }

table {
    margin: 0 auto;
    border-spacing: 30px 3px;
    }
</style>

</head>
<body>
<div id="gallery">
<?php

$mysqli = new mysqli('localhost', 'root', '', 'images');

        echo "<table>";
                $sql = "SELECT * FROM table1";
                $result = $mysqli->query($sql);

                    while($myRow = $result->fetch_array())
                    {   echo "<div id='container'>";
                        echo "<tr>";
                        echo "<td><a href='./display2.php?number=".$myRow["number"]."'> <img src=".$myRow["image"]." height='100' width='100'> </a> </td>"; 
                        echo "<td style='display: block;'><a href='./item.php?produktid=".$myRow["number"]."'> <h4>".$myRow["name"]."</h4></a> </td>";
                        echo "</tr>";
                        echo "</div>";
                        } 
        echo "</table>"
?>
</div>
</body>
</html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Temhem
  • 1
  • 5
  • 1
    possible duplicate of [simple php pagination](http://stackoverflow.com/questions/3705318/simple-php-pagination) – Mr. Llama May 14 '15 at 15:19

1 Answers1

0

Thank you so much. It works now. A pure Image/Product gallery that you can use as you want.

<!DOCTYPE HTML>
<html lang="sv-SE"/>
<head>
<meta charset="UTF-8"/>
<title>Images</title>
<style media="screen" type="text/css">
tr {display: inline-block;}

#gallery {
    height: 710px;
    width: 670px;
    background-color: grey;
    margin-left: auto;
    margin-right: auto;
    text-align: center;
    display-align: center;
    }

table {
    margin: 0 auto;
    border-spacing: 30px 3px;
    }


body{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}

div#pagination_controls{font-size:21px;}

div#pagination_controls > a{ color:#06F; }

div#pagination_controls > a:visited{ color:#06F; }
</style>

</head>
<body>
<div id="gallery">
<?php

$mysqli = new mysqli('localhost', 'root', '', 'images');

            //The first query is just to get total count of rows
                $sql = "SELECT count(number) FROM table1";

                $query = mysqli_query($mysqli, $sql);
                $row = mysqli_fetch_row($query);

                //$row = mysqli_fetch_row($result);

            //Here we have the total row count
                    $rows = $row[0];
            //This is the number of results we want displayed per page
                    $page_rows = 9;
            // This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
    $last = 1;
}
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) { 
    $pagenum = 1; 
} else if ($pagenum > $last) { 
    $pagenum = $last; 
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// This is your query again, it is for grabbing just one page worth of rows by applying $limit
$sql = "SELECT * FROM table1 ORDER BY id DESC $limit";
$query = mysqli_query($mysqli, $sql);
// This shows the user what page they are on, and the total number of pages
$textline1 = "Produkter (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';

        // If there is more than 1 page worth of results
if($last != 1){
    /* First we check if we are on page one. If we are then we don't need a link to 
       the previous page or the first page so we do nothing. If we aren't then we
       generate links to the first page, and to the previous page. */
    if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';
        // Render clickable number links that should appear on the left of the target page number
        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
                $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
            }
        }
    }
    // Render the target page number, but without it being a link
    $paginationCtrls .= ''.$pagenum.' &nbsp; ';
    // Render clickable number links that should appear on the right of the target page number
    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
        if($i >= $pagenum+4){
            break;
        }
    }
    // This does the same as above, only checking if we are on the last page, and then generating the "Next"
    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
    }
}
$list = '';
        echo "<table>";

//List out the tables of image gallery
                $sql = "SELECT * FROM table1 ORDER BY number DESC $limit";
                $result = $mysqli->query($sql);

    while($myRow = $result->fetch_array())
                    {   echo "<div id='container'>";
                        echo "<tr>";
                        echo "<td><a href='./display2.php?number=".$myRow["number"]."'> <img src=".$myRow["image"]." height='100' width='100'> </a> </td>"; 
                        echo "<td style='display: block;'><a href='./item.php?produktid=".$myRow["number"]."'> <h4>".$myRow["name"]."</h4></a> </td>";
                        echo "</tr>";
                        echo "</div>";
                        } 

        echo "</table>";
// Close your database connection
mysqli_close($mysqli);
?>
<!DOCTYPE html>


<div>
  <h2><?php echo $textline1; ?> Listade </h2>
  <p><?php echo $textline2; ?></p>
  <p><?php echo $list; ?></p>
  <div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>


</div>
</body>

Temhem
  • 1
  • 5