alexander007 Posted September 29, 2007 Share Posted September 29, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/71180-solved-display-mysql-result-in-differents-page-with-php/ Share on other sites More sharing options...
desithugg Posted September 29, 2007 Share Posted September 29, 2007 <? 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. Quote Link to comment https://forums.phpfreaks.com/topic/71180-solved-display-mysql-result-in-differents-page-with-php/#findComment-358020 Share on other sites More sharing options...
alexander007 Posted September 29, 2007 Author Share Posted September 29, 2007 Wow you are fast!....Thats exactly what I want...BUt how I add an Previous...Next button... Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/71180-solved-display-mysql-result-in-differents-page-with-php/#findComment-358023 Share on other sites More sharing options...
desithugg Posted September 29, 2007 Share Posted September 29, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71180-solved-display-mysql-result-in-differents-page-with-php/#findComment-358028 Share on other sites More sharing options...
desithugg Posted September 29, 2007 Share Posted September 29, 2007 <? 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> Quote Link to comment https://forums.phpfreaks.com/topic/71180-solved-display-mysql-result-in-differents-page-with-php/#findComment-358040 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.