Page 1 of 1

Show More button

Posted: Thu Dec 17, 2009 6:50 pm
by kapil1089theking
I want to use a button 'show more' which will show more 30 entries from the table. initially it will show 20 entries then 50..80..110...

So the code looks something like this:

Code: Select all

 
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
<?php 
//Connect statements
 
    $query = "SELECT * FROM comments_table LIMIT $kk";
//Execute query and show table with $kk rows.
 ?>
<form name="form1" method="post" action="<?php print $_SERVER['PHP_SELF']; ?>">
      <input type="submit" name="showMore" value="showMore">
</form>
</td>
</tr>
</body>
</html>
 
 
I want to increase $kk +=30; how to do that on clicking showMore.

Re: Show More button

Posted: Thu Dec 17, 2009 8:52 pm
by dbsuk
Hi again,

Interesting one...

Heres my idea:

Code: Select all

 
<?
 
     // start a session - note this must be done before any headers are sent.
     session_start();
     
     // default number of results
     $default_results = 20;
 
     // Increment 
     $increment = 30;
     
     // check if the session variable $_SESSION['num_results'] isset
     if(!$_SESSION['num_results']) {
         
        // if its not then set it as the $default_results
        session_register('num_results');
        $_SESSION['num_results']=$default_results;
        
     }
     
     // Check if show more has been pressed
     if($_POST['showMore']) {
  
          // Add 30 to the $_SESSION['num_results'];   
          $_SESSION['num_results']+=$increment;
 
     }
     
     // Check if show default has been pressed
     if($_POST['showDefault']) {
         
        // reset to default value
        $_SESSION['num_results']=$default_results;
         
     }
 
?>
 
 <html>
 <head>
 <title>Untitled Document</title>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 </head>
  
 <body>
 <?php
 //Connect statements
  
        // Create $kk from Session.
        $kk = $_SESSION['num_results'];
  
     $query = "SELECT * FROM comments_table LIMIT $kk";
   //Execute query and show table with $kk rows.
  ?>
 <form name="form1" method="post" action="<?php print $_SERVER['PHP_SELF']; ?>">
     <input type="submit" name="showMore" value="Show 30 More" />
     <input type="submit" name="showDefault" value="Show Default" />
    </form>
 
 </body>
 </html>
 
 
 
 
This makes use of PHP sessions to store the number of rows as $_SESSION['num_results'];

I have also included a reset button.

Enjoy.

Richard

Re: Show More button

Posted: Thu Dec 17, 2009 9:15 pm
by kapil1089theking
Notice: Undefined index: num_results in C:\wamp\www\comment\index.php on line 13

Notice: Undefined index: showMore in C:\wamp\www\comment\index.php on line 22

Notice: Undefined index: showDefault in C:\wamp\www\comment\index.php on line 30

these errors am getting after pressing show 30 more this error I am getting:


Notice: Undefined index: showDefault in C:\wamp\www\comment\index.php on line 30

Re: Show More button

Posted: Thu Dec 17, 2009 10:16 pm
by dbsuk
It looks like you have error_reporting set to E_ALL.

My default is lower so I missed the errors.

To get rid of the warnings surround the test variables with isset() as below:

Code: Select all

<?
    error_reporting(E_ALL);
    ini_set("display_errors", 1); 
 
     // start a session - note this must be done before any headers are sent.
     session_start();
     
     // default number of results
     $default_results = 20;
     
     // Increment 
     $increment = 30;
     
     // check if the session variable $_SESSION['num_results'] isset
     if(!isset($_SESSION['num_results'])) {
         
        session_register('num_results');
        $_SESSION['num_results']=$default_results;
        
     }
     
     // Check if show more has been pressed
     if(isset($_POST['showMore'])) {
  
          // Add 30 to the $_SESSION['num_results'];   
          $_SESSION['num_results']+=$increment;
 
     }
     
     // Check if show default has been pressed
     if(isset($_POST['showDefault'])) {
         
        $_SESSION['num_results']=$default_results;
         
     }
 
   print 'Current value of $_SESSION[\'num_results\'] = '. $_SESSION['num_results'];
 
 
?>
 
 <html>
 <head>
 <title>Untitled Document</title>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 </head>
  
 <body>
 <?php
 //Connect statements
  
        // Create $kk from Session.
        $kk = $_SESSION['num_results'];
  
     #$query = "SELECT * FROM comments_table LIMIT $kk";
   //Execute query and show table with $kk rows.
  ?>
 <form name="form1" method="post" action="<?php print $_SERVER['PHP_SELF']; ?>">
     <input type="submit" name="showMore" value="Show 30 More" />
     <input type="submit" name="showDefault" value="Show Default" />
    </form>
 
 </body>
 </html>
The above code is running here on my test server: http://silverside.co.uk/testSpace/showMore/index.php

Regards

Richard