Jump to content

[SOLVED] Display MYSQL result in differents page with PHP


alexander007

Recommended Posts

Hello....I have a table database in MYSQL...I select the table and I get the results...but I want that the result will be displayed in differents pages...something like 20 per pages...

 

This is my code that is working right know but it shows all the table in one page...

 

<?php
session_start();
if(!session_is_registered(username)){
header("location:admin.php");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {
font-size: x-large;
font-weight: bold;
}
-->
</style>
</head>

<body>

<div align="center" class="style1">
  <p>Lista de Visitantes</p>
</div>
</blockquote>
<table border='1' align="center">
  <tr>
    <th>Nombre</th>
    <th>Telefono</th>
    <th>Email</th>
    <th>Comentario</th>
    <th> </th>
  </tr>
  
<?php
include"config.php";
$tbl_name="visitantes"; 
$sql="SELECT * FROM $tbl_name ORDER BY id";
$result=mysql_query($sql);
$count=mysql_num_rows($result); 
?> 

<?php
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )){
// Print out the contents of each row into a table
?>
  <tr>
    <td><center><?php echo $row['nombre'];?></center></td>
    <td><center><?php echo $row['telefono'];?></center></td>
    <td><center><?php echo $row['email'];?></center></td>
    <td><textarea name="" cols="20"><?php echo $row['comentario'];?></textarea></td>
    <td><form action="editar.php" method="get">
<input type="hidden" name="id" value="<? echo $row['id']; ?>">
<input type="hidden" name="tbl_name" value="<? echo $tbl_name; ?>">
<input type="submit" name="Submit" value="Editar">
</form>
    </td>
    <?php
}
?>
</table>
</body>
</html>

Link to comment
Share on other sites

<?
session_start();
if(!session_is_registered(username)){
header("location:admin.php");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {
font-size: x-large;
font-weight: bold;
}
-->
</style>
</head>

<body>

<div align="center" class="style1">
  <p>Lista de Visitantes</p>
</div>
</blockquote>
<table border='1' align="center">
  <tr>
    <th>Nombre</th>
    <th>Telefono</th>
    <th>Email</th>
    <th>Comentario</th>
    <th> </th>
  </tr>
  
<?php
$start_from = $_GET['start'];
if(!isset($start_from)){$start_from=0;}
$results_per_page = "15";
include"config.php";
$tbl_name="visitantes"; 
$sql="SELECT * FROM $tbl_name ORDER BY id limit ".$start_from.",".$results_per_page;
$result=mysql_query($sql);
$count=mysql_num_rows($result); 
?> 

<?php
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )){
// Print out the contents of each row into a table
?>
  <tr>
    <td><center><?php echo $row['nombre'];?></center></td>
    <td><center><?php echo $row['telefono'];?></center></td>
    <td><center><?php echo $row['email'];?></center></td>
    <td><textarea name="" cols="20"><?php echo $row['comentario'];?></textarea></td>
    <td><form action="editar.php" method="get">
<input type="hidden" name="id" value="<? echo $row['id']; ?>">
<input type="hidden" name="tbl_name" value="<? echo $tbl_name; ?>">
<input type="submit" name="Submit" value="Editar">
</form>
    </td>
    <?php
}
?>
</table>

 

So let's say your page is called "view.php".

If you enter the url "http://yourwebsite.com/view.php" it will show the first 15 records.

Now if you want to see the next 15 you would enter the url "http://yourwebsite.com/view.php?start=15" (so it will start from the 15 row and fetch the next 15 rows from there.

Link to comment
Share on other sites

Umm you will need to write a couple of IF statements for that.

<?
$previous_num = ($start_from-15);
$next_num = ($start_from+15);
if($previous_num<0){
$previous_num=0;
}
?>

<a href='view.php?start=<? echo $previous_num; ?>'>Previous</a>
<a href='view.php?start=<? echo $next_num; ?>'>Next</a>

That's just basic you might want to run a check and see if you should display the next/previous links.

Link to comment
Share on other sites

<?
session_start();
if(!session_is_registered(username)){
header("location:admin.php");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {
font-size: x-large;
font-weight: bold;
}
-->
</style>
</head>

<body>

<div align="center" class="style1">
  <p>Lista de Visitantes</p>
</div>
</blockquote>
<table border='1' align="center">
  <tr>
    <th>Nombre</th>
    <th>Telefono</th>
    <th>Email</th>
    <th>Comentario</th>
    <th> </th>
  </tr>
  
<?php
$start_from = $_GET['start'];
if(!isset($start_from)){$start_from=0;}
$results_per_page = "15";
include"config.php";
$tbl_name="visitantes";
//get the total number of users starts here
$result = mysql_query("SELECT count(id) as total FROM $tbl_name") or die(mysql_error());  
$row = mysql_fetch_array( $result );
$total_users = $row['total'];
$previous_num = ($start_from-$results_per_page);
$next_num = ($start_from+$results_per_page);
if($previous_num<"0"){$previous_num=0;}
if($next_num>$total_users){$next_num=($total_users-$results_per_page);}
if($next_num<"0"){$next_num=0;}
//getting the total number of users ends here
$sql="SELECT * FROM $tbl_name ORDER BY id limit ".$start_from.",".$results_per_page;
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?> 

<?php
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )){
// Print out the contents of each row into a table
?>
  <tr>
    <td><center><?php echo $row['nombre'];?></center></td>
    <td><center><?php echo $row['telefono'];?></center></td>
    <td><center><?php echo $row['email'];?></center></td>
    <td><textarea name="" cols="20"><?php echo $row['comentario'];?></textarea></td>
    <td><form action="editar.php" method="get">
<input type="hidden" name="id" value="<? echo $row['id']; ?>">
<input type="hidden" name="tbl_name" value="<? echo $tbl_name; ?>">
<input type="submit" name="Submit" value="Editar">
</form>
    </td>
    <?php
}
?>
</tr>
<tr>
<td colspan='5' style='text-align:center;'><a href='?start=<? echo $previous_num; ?>'>Previous Page</a> || <a href='?start=<? echo $next_num; ?>'>Previous Page</a></td>
</tr>
</table>

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.