Solved

PHP paging does not work

Posted on 2011-09-13
22
314 Views
Last Modified: 2012-05-12
Hi:

I am trying to add paging to display data from the database.  Pages are added but no data is displayed.  If I use view all (without paging) data is displayed.  I am not able to figure out the problem in the code.

Please help.
<?PHP
require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
      <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
      <title>Home page</title>
      <link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css">
</head>
<body>
<div id='fg_membersite_content'>
<h2>Home Page</h2>
Welcome back <?= $fgmembersite->UserFullName(); ?>!
<?php 
$orderCol=$_POST['orderCol'];
$orderWay=$_POST['orderWay'];
$filterCat=$_POST['filterCat'];
$filterCo=$_POST['filterCo']; 

if (!$orderCol) $orderCol="resid";
if (!$orderWay) $orderWay="DESC";

$sql="SELECT * FROM responses WHERE 1=1";
if ($filterCat) $sql.=" AND categories='{$filterCat}'";
if ($filterCo) $sql.=" AND Country='{$filterCo}'";
$sql.=" ORDER BY {$orderCol} {$orderWay}";


        // connect to the database 
        include('connect-db.php'); 
$per_page = 3; 
 
        // get results from database 
        $result = mysql_query($sql)  
                or die(mysql_error());
$total_results = mysql_num_rows($result); 
$total_pages = ceil($total_results / $per_page);
        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1) 
        if (isset($_GET['page']) && is_numeric($_GET['page'])) 
        { 
                $show_page = $_GET['page']; 
                 
                // make sure the $show_page value is valid 
                if ($show_page > 0 && $show_page <= $total_pages) 
                { 
                        $start = ($show_page -1) * $per_page; 
                        $end = $start + $per_page;  
                } 
                else 
                { 
                        // error - show first set of results 
                        $start = 0; 
                        $end = $per_page;  
                }                
        } 
        else 
        { 
                // if page isn't set, show first set of results 
                $start = 0; 
                $end = $per_page;  
        } 
         
        // display pagination 
         
        echo "<p><a href='login-home.php'>View All</a> | <b>View Page:</b> "; 
        for ($i = 1; $i <= $total_pages; $i++) 
        { 
                echo "<a href='view-paginated.php?page=$i'>$i</a> "; 
        } 
        echo "</p>"; 
   

$sqlCat="SELECT DISTINCT categories FROM responses ORDER BY 1";
        $rsCat = mysql_query($sqlCat)  
                or die(mysql_error());   

$sqlCo="SELECT DISTINCT Country FROM responses ORDER BY 1";
        $rsCo = mysql_query($sqlCo)  
                or die(mysql_error());   
                 
        // display data in table 
        echo "<form method=POST>";
        echo "<table border='0' cellpadding='10';>";
	echo "<tr><td>&nbsp;</td><td>&nbsp;</td>";
                echo "<th><select name=filterCat size=1 onChange='this.form.submit();'>";
	echo "<option value='' SELECTED>-choose a filter-</option>";
	while($row = mysql_fetch_array( $rsCat )) { 
		$isSel="";
		if ($row[0]==$filterCat) $isSel=" SELECTED";
		echo "<option value='{$row[0]}' {$isSel}>{$row[0]}</option>";
	}
	echo "</select></th><td>&nbsp;</td><td>&nbsp;</td>";
	echo "<th><select name=filterCo size=1 onChange='this.form.submit();'>";
	echo "<option value='' SELECTED>-choose a filter-</option>";
	while($row = mysql_fetch_array( $rsCo )) { 
		$isSel="";
		if ($row[0]==$filterCo) $isSel=" SELECTED";
		echo "<option value='{$row[0]}' {$isSel}>{$row[0]}</option>";
	}
	echo "</select></th><td>&nbsp;</td>";
        echo "<tr> <th>ID</th> <th>Title</th> <th>Category</th> <th>Description</th> <th>City</th><th>Country</th><th>Zip Code</th></tr>";	
// loop through results of database query, displaying them in the table  
        for ($i = $start; $i < $end; $i++)
        { 
                // make sure that PHP doesn't try to show results that don't exist 
                if ($i == $total_results) { break; } 
         
        // loop through results of database query, displaying them in the table 
        //while($row = mysql_fetch_array( $result )) { 
                // echo out the contents of each row into a table

                echo "<tr align='center' valign='top'>"; 
                echo '<td>' . $row['resid'] . '</td>'; 
//$res=$row['resid'];
                echo '<td>' . $row['title'] . '</td>';
                echo '<td>' . $row['categories'] . '</td>';

                //$position=255; // Define how many character you want to display.
                //$descp = substr($row['description'], 0, $position); 
	//echo '<td style="WORD-BREAK:BREAK-ALL">' . $descp . '</td>'; 
               echo '<td style="WORD-BREAK:BREAK-ALL">' . substr($row['description'],0,255).
             ( (strlen($row['description'])>255) ? " <a href='http://domain.com/registration/partrecord.php?resid={$row['resid']}'>read more&hellip;</a>" : ''). '</td>'; 
                echo '<td>' . $row['city'] . '</td>'; 
                echo '<td>' . $row['country'] . '</td>';
                echo '<td>' . $row['zipcode'] . '</td>'; 
                //echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>'; 
                //echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>'; 
                echo "</tr>"; 
                echo "<tr><td colspan='9'><hr></td></tr>";  
                      }  
        // close table> 
       echo "</table>"; 

?> 
<p><a href="new.php">Add a new record</a></p><p><a href='logout.php'>Logout</a></p>
</div>

</body>
</html>

Open in new window

0
Comment
Question by:adamssap
  • 10
  • 10
  • 2
22 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 36530454
move lines 60 and 61 IMMEDIATELY BEFORE line 47.

THEN get rid of lines 57-62.
0
 
LVL 1

Author Comment

by:adamssap
ID: 36530628
Thanks, after doing that, I am getting this error:

Parse error: syntax error, unexpected T_ELSE on line 61


<?PHP
require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
      <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
      <title>Home page</title>
      <link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css">
</head>
<body>
<div id='fg_membersite_content'>
<h2>Home Page</h2>
Welcome back <?= $fgmembersite->UserFullName(); ?>!
<?php 
$orderCol=$_POST['orderCol'];
$orderWay=$_POST['orderWay'];
$filterCat=$_POST['filterCat'];
$filterCo=$_POST['filterCo']; 

if (!$orderCol) $orderCol="resid";
if (!$orderWay) $orderWay="DESC";

$sql="SELECT * FROM responses WHERE 1=1";
if ($filterCat) $sql.=" AND categories='{$filterCat}'";
if ($filterCo) $sql.=" AND Country='{$filterCo}'";
$sql.=" ORDER BY {$orderCol} {$orderWay}";


        // connect to the database 
        include('connect-db.php'); 
$per_page = 3; 
 
        // get results from database 
        $result = mysql_query($sql)  
                or die(mysql_error());
$total_results = mysql_num_rows($result); 
$total_pages = ceil($total_results / $per_page);
        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1) 
        if (isset($_GET['page']) && is_numeric($_GET['page']))  
$start = 0; 
$end = $per_page;  

        { 
                $show_page = $_GET['page']; 
                 
                // make sure the $show_page value is valid 
                if ($show_page > 0 && $show_page <= $total_pages) 
                { 
                        $start = ($show_page -1) * $per_page; 
                        $end = $start + $per_page;  
                } 
        } 
        else 
         { 
                // if page isn't set, show first set of results 
                $start = 0; 
                $end = $per_page;  
        } 
         
        // display pagination 
         
        echo "<p><a href='login-home.php'>View All</a> | <b>View Page:</b> "; 
        for ($i = 1; $i <= $total_pages; $i++) 
        { 
                echo "<a href='view-paginated.php?page=$i'>$i</a> "; 
        } 
        echo "</p>"; 
   

$sqlCat="SELECT DISTINCT categories FROM responses ORDER BY 1";
        $rsCat = mysql_query($sqlCat)  
                or die(mysql_error());   

$sqlCo="SELECT DISTINCT Country FROM responses ORDER BY 1";
        $rsCo = mysql_query($sqlCo)  
                or die(mysql_error());   
                 
        // display data in table 
        echo "<form method=POST>";
        echo "<table border='0' cellpadding='10';>";
	echo "<tr><td>&nbsp;</td><td>&nbsp;</td>";
                echo "<th><select name=filterCat size=1 onChange='this.form.submit();'>";
	echo "<option value='' SELECTED>-choose a filter-</option>";
	while($row = mysql_fetch_array( $rsCat )) { 
		$isSel="";
		if ($row[0]==$filterCat) $isSel=" SELECTED";
		echo "<option value='{$row[0]}' {$isSel}>{$row[0]}</option>";
	}
	echo "</select></th><td>&nbsp;</td><td>&nbsp;</td>";
	echo "<th><select name=filterCo size=1 onChange='this.form.submit();'>";
	echo "<option value='' SELECTED>-choose a filter-</option>";
	while($row = mysql_fetch_array( $rsCo )) { 
		$isSel="";
		if ($row[0]==$filterCo) $isSel=" SELECTED";
		echo "<option value='{$row[0]}' {$isSel}>{$row[0]}</option>";
	}
	echo "</select></th><td>&nbsp;</td>";
        echo "<tr> <th>ID</th> <th>Title</th> <th>Category</th> <th>Description</th> <th>City</th><th>Country</th><th>Zip Code</th></tr>";	
// loop through results of database query, displaying them in the table  
        for ($i = $start; $i < $end; $i++)
        { 
                // make sure that PHP doesn't try to show results that don't exist 
                if ($i == $total_results) { break; } 
         
        // loop through results of database query, displaying them in the table 
        //while($row = mysql_fetch_array( $result )) { 
                // echo out the contents of each row into a table

                echo "<tr align='center' valign='top'>"; 
                echo '<td>' . $row['resid'] . '</td>'; 
//$res=$row['resid'];
                echo '<td>' . $row['title'] . '</td>';
                echo '<td>' . $row['categories'] . '</td>';

                //$position=255; // Define how many character you want to display.
                //$descp = substr($row['description'], 0, $position); 
	//echo '<td style="WORD-BREAK:BREAK-ALL">' . $descp . '</td>'; 
               echo '<td style="WORD-BREAK:BREAK-ALL">' . substr($row['description'],0,255).
             ( (strlen($row['description'])>255) ? " <a href='http://domain.com/registration/partrecord.php?resid={$row['resid']}'>read more&hellip;</a>" : ''). '</td>'; 
                echo '<td>' . $row['city'] . '</td>'; 
                echo '<td>' . $row['country'] . '</td>';
                echo '<td>' . $row['zipcode'] . '</td>'; 
                //echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>'; 
                //echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>'; 
                echo "</tr>"; 
                echo "<tr><td colspan='9'><hr></td></tr>";  
                      }  
        // close table> 
       echo "</table>"; 

?> 
<p><a href="new.php">Add a new record</a></p><p><a href='logout.php'>Logout</a></p>
</div>

</body>
</html>

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36530715
Change it to this:

                
// make sure the $show_page value is valid 
if ($show_page > 0 && $show_page <= $total_pages) 
{ 
                        $start = ($show_page -1) * $per_page; 
                        $end = $start + $per_page;  
} 
else 
{ 
                // if page isn't set, show first set of results 
                $start = 0; 
                $end = $per_page;  
} 

Open in new window


Delete the line 51 and 60 (empty {} )
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36530729
0
 
LVL 1

Author Comment

by:adamssap
ID: 36530858
I made the changes as below, but still the data is not displayed.
<?PHP
require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
      <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
      <title>Home page</title>
      <link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css">
</head>
<body>
<div id='fg_membersite_content'>
<h2>Home Page</h2>
Welcome back <?= $fgmembersite->UserFullName(); ?>!
<?php 
$orderCol=$_POST['orderCol'];
$orderWay=$_POST['orderWay'];
$filterCat=$_POST['filterCat'];
$filterCo=$_POST['filterCo']; 

if (!$orderCol) $orderCol="resid";
if (!$orderWay) $orderWay="DESC";

$sql="SELECT * FROM responses WHERE 1=1";
if ($filterCat) $sql.=" AND categories='{$filterCat}'";
if ($filterCo) $sql.=" AND Country='{$filterCo}'";
$sql.=" ORDER BY {$orderCol} {$orderWay}";


        // connect to the database 
        include('connect-db.php'); 
$per_page = 3; 
 
        // get results from database 
        $result = mysql_query($sql)  
                or die(mysql_error());
$total_results = mysql_num_rows($result); 
$total_pages = ceil($total_results / $per_page);
                        $start = 0; 
                        $end = $per_page;  
        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1) 
        if (isset($_GET['page']) && is_numeric($_GET['page'])) 
        { 
//                $show_page = $_GET['page']; 
                 
                                
// make sure the $show_page value is valid 
if ($show_page > 0 && $show_page <= $total_pages) 
{ 
                        $start = ($show_page -1) * $per_page; 
                        $end = $start + $per_page;  
} 
else 
{ 
                // if page isn't set, show first set of results 
                $start = 0; 
                $end = $per_page;  
}  
        } 
        else 
        { 
                // if page isn't set, show first set of results 
                $start = 0; 
                $end = $per_page;  
        } 
         
        // display pagination 
         
        echo "<p><a href='login-home.php'>View All</a> | <b>View Page:</b> "; 
        for ($i = 1; $i <= $total_pages; $i++) 
        { 
                echo "<a href='view-paginated.php?page=$i'>$i</a> "; 
        } 
        echo "</p>"; 
   

$sqlCat="SELECT DISTINCT categories FROM responses ORDER BY 1";
        $rsCat = mysql_query($sqlCat)  
                or die(mysql_error());   

$sqlCo="SELECT DISTINCT Country FROM responses ORDER BY 1";
        $rsCo = mysql_query($sqlCo)  
                or die(mysql_error());   
                 
        // display data in table 
        echo "<form method=POST>";
        echo "<table border='0' cellpadding='10';>";
	echo "<tr><td>&nbsp;</td><td>&nbsp;</td>";
                echo "<th><select name=filterCat size=1 onChange='this.form.submit();'>";
	echo "<option value='' SELECTED>-choose a filter-</option>";
	while($row = mysql_fetch_array( $rsCat )) { 
		$isSel="";
		if ($row[0]==$filterCat) $isSel=" SELECTED";
		echo "<option value='{$row[0]}' {$isSel}>{$row[0]}</option>";
	}
	echo "</select></th><td>&nbsp;</td><td>&nbsp;</td>";
	echo "<th><select name=filterCo size=1 onChange='this.form.submit();'>";
	echo "<option value='' SELECTED>-choose a filter-</option>";
	while($row = mysql_fetch_array( $rsCo )) { 
		$isSel="";
		if ($row[0]==$filterCo) $isSel=" SELECTED";
		echo "<option value='{$row[0]}' {$isSel}>{$row[0]}</option>";
	}
	echo "</select></th><td>&nbsp;</td>";
        echo "<tr> <th>ID</th> <th>Title</th> <th>Category</th> <th>Description</th> <th>City</th><th>Country</th><th>Zip Code</th></tr>";	
// loop through results of database query, displaying them in the table  
        for ($i = $start; $i < $end; $i++)
        { 
                // make sure that PHP doesn't try to show results that don't exist 
                if ($i == $total_results) { break; } 
         
        // loop through results of database query, displaying them in the table 
        //while($row = mysql_fetch_array( $result )) { 
                // echo out the contents of each row into a table

                echo "<tr align='center' valign='top'>"; 
                echo '<td>' . $row['resid'] . '</td>'; 
//$res=$row['resid'];
                echo '<td>' . $row['title'] . '</td>';
                echo '<td>' . $row['categories'] . '</td>';

                //$position=255; // Define how many character you want to display.
                //$descp = substr($row['description'], 0, $position); 
	//echo '<td style="WORD-BREAK:BREAK-ALL">' . $descp . '</td>'; 
               echo '<td style="WORD-BREAK:BREAK-ALL">' . substr($row['description'],0,255).
             ( (strlen($row['description'])>255) ? " <a href='http://domain.com/registration/partrecord.php?resid={$row['resid']}'>read more&hellip;</a>" : ''). '</td>'; 
                echo '<td>' . $row['city'] . '</td>'; 
                echo '<td>' . $row['country'] . '</td>';
                echo '<td>' . $row['zipcode'] . '</td>'; 
                //echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>'; 
                //echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>'; 
                echo "</tr>"; 
                echo "<tr><td colspan='9'><hr></td></tr>";  
                      }  
        // close table> 
       echo "</table>"; 

?> 
<p><a href="new.php">Add a new record</a></p><p><a href='logout.php'>Logout</a></p>
</div>

</body>
</html>

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36530870
How about we get back to your original code and add a line 66:

                $show_page = 1;

Open in new window


so this section looks like this:

        else 
        { 
                $show_page = 1;
                // if page isn't set, show first set of results 
                $start = 0; 
                $end = $per_page;  
        } 

Open in new window

0
 
LVL 1

Author Comment

by:adamssap
ID: 36530893
Hi Roads_Roads:

I added this $show_page = 1; on line 60 but still the data is not displayed.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36530902
OK, take a look at the article, line 41-47

      // STARTRECORD IS USED FOR THE MYSQL QUERY
      // SO WE KNOW WHERE WE CAN START FROM
      // LIMIT WITH IPP (ITEMS PER PAGE)
      if(!empty($page)) $startrecord = ($page-1)*$ipp;
      else $startrecord = 0;             
      
      $query = "select * from pagination limit $startrecord,$ipp";

You need to do the same - limit your query
0
 
LVL 1

Author Comment

by:adamssap
ID: 36530915
Can you give me some finger holding here?  I am totally lost.
0
 
LVL 82

Expert Comment

by:hielo
ID: 36530922
>>Parse error: syntax error, unexpected T_ELSE on line 61
Like I said, immediately BEFORE , not after, which is what you did. So it should look as follows:
// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1) 
$start = 0; 
$end = $per_page;  

        if (isset($_GET['page']) && is_numeric($_GET['page']))  
        { 
         ...
        }

Open in new window

0
 
LVL 1

Author Comment

by:adamssap
ID: 36530964
Hi hielo:

Yes, I added it before now, error is gone but no data is displayed.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36531235
Your line 113
//while($row = mysql_fetch_array( $result )) {

should be the $row result for the query where you limit your sql with $start, $_per_page

It would be much easier if you would post the database structure with some sample data.
0
 
LVL 1

Author Comment

by:adamssap
ID: 36531288
This is how the data structure is image
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36531345
Take a look here, it might be some closer to the solution:

<?php
error_reporting(E_ALL);
require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
      <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
      <title>Home page</title>
      <link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css">
</head>
<body>
<div id='fg_membersite_content'>
<h2>Home Page</h2>

<?php 

echo"ok";

$orderCol=$_POST['orderCol'];
$orderWay=$_POST['orderWay'];
$filterCat=$_POST['filterCat'];
$filterCo=$_POST['filterCo']; 

if (!$orderCol) $orderCol="resid";
if (!$orderWay) $orderWay="DESC";

$sql="SELECT * FROM responses WHERE 1=1";
if ($filterCat) $sql.=" AND categories='{$filterCat}'";
if ($filterCo) $sql.=" AND Country='{$filterCo}'";
$sql.=" ORDER BY {$orderCol} {$orderWay}";


        // connect to the database 
        include('connect-db.php'); 
$per_page = 3; 


 
        // get results from database 
        $result = mysql_query($sql)  
                or die(mysql_error());
$total_results = mysql_num_rows($result); 
$total_pages = ceil($total_results / $per_page);
        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1) 
        if (isset($_GET['page']) && is_numeric($_GET['page'])) 
        { 
                $show_page = $_GET['page']; 
                 
                // make sure the $show_page value is valid 
                if ($show_page > 0 && $show_page <= $total_pages) 
                { 
                        $start = ($show_page -1) * $per_page; 
                        $end = $start + $per_page;  
                } 
                else 
                { 
                        // error - show first set of results 
                        $start = 0; 
                        $end = $per_page;  
                }                
        } 
        else 
        { 
                $show_page = 1;
                // if page isn't set, show first set of results 
                $start = 0; 
                $end = $per_page;  
        } 
        
        // ==============
        // the main query
        // ==============
        $q = "select * from responses limit $start, $end";
        $r = mysql_query($q);
         
        // display pagination 
         
        echo "<p><a href='login-home.php'>View All</a> | <b>View Page:</b> "; 
        for ($i = 1; $i <= $total_pages; $i++) 
        { 
                echo "<a href='view-paginated.php?page=$i'>$i</a> "; 
        } 
        echo "</p>"; 
   

$sqlCat="SELECT DISTINCT categories FROM responses ORDER BY 1";
        $rsCat = mysql_query($sqlCat)  
                or die(mysql_error());   

$sqlCo="SELECT DISTINCT Country FROM responses ORDER BY 1";
        $rsCo = mysql_query($sqlCo)  
                or die(mysql_error());   
                 
        // display data in table 
        echo "<form method=POST>";
        echo "<table border='0' cellpadding='10';>";
	echo "<tr><td>&nbsp;</td><td>&nbsp;</td>";
                echo "<th><select name=filterCat size=1 onChange='this.form.submit();'>";
	echo "<option value='' SELECTED>-choose a filter-</option>";
	while($row = mysql_fetch_array( $rsCat )) { 
		$isSel="";
		if ($row[0]==$filterCat) $isSel=" SELECTED";
		echo "<option value='{$row[0]}' {$isSel}>{$row[0]}</option>";
	}
	echo "</select></th><td>&nbsp;</td><td>&nbsp;</td>";
	echo "<th><select name=filterCo size=1 onChange='this.form.submit();'>";
	echo "<option value='' SELECTED>-choose a filter-</option>";
	while($row = mysql_fetch_array( $rsCo )) { 
		$isSel="";
		if ($row[0]==$filterCo) $isSel=" SELECTED";
		echo "<option value='{$row[0]}' {$isSel}>{$row[0]}</option>";
	}
	echo "</select></th><td>&nbsp;</td>";
        echo "<tr> <th>ID</th> <th>Title</th> <th>Category</th> <th>Description</th> <th>City</th><th>Country</th><th>Zip Code</th></tr>";	
// loop through results of database query, displaying them in the table  
        
        //for ($i = $start; $i < $end; $i++)
        while($row = mysql_fetch_array($r))
        { 
                // make sure that PHP doesn't try to show results that don't exist 
                if ($i == $total_results) { break; } 
         
        // loop through results of database query, displaying them in the table 
        //while($row = mysql_fetch_array( $result )) { 
                // echo out the contents of each row into a table

                echo "<tr align='center' valign='top'>"; 
                echo '<td>' . $row['resid'] . '</td>'; 
//$res=$row['resid'];
                echo '<td>' . $row['title'] . '</td>';
                echo '<td>' . $row['categories'] . '</td>';

                //$position=255; // Define how many character you want to display.
                //$descp = substr($row['description'], 0, $position); 
	//echo '<td style="WORD-BREAK:BREAK-ALL">' . $descp . '</td>'; 
               echo '<td style="WORD-BREAK:BREAK-ALL">' . substr($row['description'],0,255).
             ( (strlen($row['description'])>255) ? " <a href='http://domain.com/registration/partrecord.php?resid={$row['resid']}'>read more&hellip;</a>" : ''). '</td>'; 
                echo '<td>' . $row['city'] . '</td>'; 
                echo '<td>' . $row['country'] . '</td>';
                echo '<td>' . $row['zipcode'] . '</td>'; 
                //echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>'; 
                //echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>'; 
                echo "</tr>"; 
                echo "<tr><td colspan='9'><hr></td></tr>";  
                      }  
        // close table> 
       echo "</table>"; 

?> 
<p><a href="new.php">Add a new record</a></p><p><a href='logout.php'>Logout</a></p>
</div>

</body>
</html>

Open in new window


This code contains a query added after defining the start and end limits, and the loop is made with that query
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36531349
Sorry for some additional lines and that "ok" - that was just checking
0
 
LVL 1

Author Comment

by:adamssap
ID: 36531401
Yes, yes, getting closer...Data is displayed in pages, works fine but the following errors appear on the page.  Also, the sorting on Category and Country fields don't work any more.

Notice: Undefined index: orderCol in /hermes/bosweb/web073/b734/d5.domain/public_html/registration/view-paginated.php on line 27

Notice: Undefined index: orderWay in /hermes/bosweb/web073/b734/d5.domain/public_html/registration/view-paginated.php on line 28

Notice: Undefined index: filterCat in /hermes/bosweb/web073/b734/d5.domain/public_html/registration/view-paginated.php on line 29

Notice: Undefined index: filterCo in /hermes/bosweb/web073/b734/d5.domain/public_html/registration/view-paginated.php on line 30
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 36531601
And this:

<?php
error_reporting(E_ERROR);
require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
      <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
      <title>Home page</title>
      <link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css">
</head>
<body>
<div id='fg_membersite_content'>
<h2>Home Page</h2>

<?php 

$sql="SELECT * FROM responses WHERE 1=1";

$filterCat = $_POST['filterCat'];
$filterCo = $_POST['filterCo'];

if(!empty($orderCol)) $orderCol=$_POST['orderCol']; else $orderCol="resid";
if(!empty($orderWay)) $orderWay=$_POST['orderWay']; else $orderWay="DESC";

if(!empty($filterCat)) $sql .= " AND categories='".$filterCat."'"; else $filterCat=" ";
if(!empty($filterCo)) $sql .= " AND Country='".$filterCo."'"; else $filterCo=" ";

$sql.=" ORDER BY {$orderCol} {$orderWay}";



        // connect to the database 
        include('connect-db.php'); 
$per_page = 3; 


 
        // get results from database 
        $result = mysql_query($sql)  
                or die(mysql_error());
$total_results = mysql_num_rows($result); 
$total_pages = ceil($total_results / $per_page);
        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1) 
        if (isset($_GET['page']) && is_numeric($_GET['page'])) 
        { 
                $show_page = $_GET['page']; 
                 
                // make sure the $show_page value is valid 
                if ($show_page > 0 && $show_page <= $total_pages) 
                { 
                        $start = ($show_page -1) * $per_page; 
                        $end = $start + $per_page;  
                } 
                else 
                { 
                        // error - show first set of results 
                        $start = 0; 
                        $end = $per_page;  
                }                
        } 
        else 
        { 
                $show_page = 1;
                // if page isn't set, show first set of results 
                $start = 0; 
                $end = $per_page;  
        } 
        
        // the main query
        $q = $sql." limit $start, $end";
        $r = mysql_query($q);
         
        // display pagination 
         
        echo "<p><a href='login-home.php'>View All</a> | <b>View Page:</b> "; 
        for ($i = 1; $i <= $total_pages; $i++) 
        { 
                echo "<a href='view-paginated.php?page=$i'>$i</a> "; 
        } 
        echo "</p>"; 
           

$sqlCat="SELECT DISTINCT categories FROM responses ORDER BY 1";
        $rsCat = mysql_query($sqlCat)  
                or die(mysql_error());  
              

$sqlCo="SELECT DISTINCT Country FROM responses ORDER BY 1";
        $rsCo = mysql_query($sqlCo)  
                or die(mysql_error());   
                 
        // display data in table 
        echo "<form method=POST>";
        echo "<table border='0' cellpadding='10';>";
	echo "<tr><td>&nbsp;</td><td>&nbsp;</td>";
                echo "<th><select name=filterCat size=1 onChange='this.form.submit();'>";
	echo "<option value='' SELECTED>-choose a filter-</option>";

	while($rowcat = mysql_fetch_array( $rsCat )) { 
		$isSel="";
		if ($rowcat[0]==$filterCat) $isSel=" SELECTED";
		echo "<option value='{$rowcat[0]}' {$isSel}>{$rowcat[0]}</option>";
	}
	echo "</select></th><td>&nbsp;</td><td>&nbsp;</td>";
	echo "<th><select name=filterCo size=1 onChange='this.form.submit();'>";
	echo "<option value='' SELECTED>-choose a filter-</option>";
	while($rowcou = mysql_fetch_array( $rsCo )) { 
		$isSel="";
		if ($rowcou[0]==$filterCo) $isSel=" SELECTED";
		echo "<option value='{$rowcou[0]}' {$isSel}>{$rowcou[0]}</option>";
	}
	echo "</select></th><td>&nbsp;</td>";
        echo "<tr> <th>ID</th> <th>Title</th> <th>Category</th> <th>Description</th> <th>City</th><th>Country</th><th>Zip Code</th></tr>";	
// loop through results of database query, displaying them in the table  
        
        //for ($i = $start; $i < $end; $i++)
        while($row = mysql_fetch_array($r))
        { 
                // make sure that PHP doesn't try to show results that don't exist 
                if ($i == $total_results) { break; } 
         
        // loop through results of database query, displaying them in the table 
        //while($row = mysql_fetch_array( $result )) { 
                // echo out the contents of each row into a table

                echo "<tr align='center' valign='top'>"; 
                echo '<td>' . $row['resid'] . '</td>'; 
//$res=$row['resid'];
                echo '<td>' . $row['title'] . '</td>';
                echo '<td>' . $row['categories'] . '</td>';

                //$position=255; // Define how many character you want to display.
                //$descp = substr($row['description'], 0, $position); 
	//echo '<td style="WORD-BREAK:BREAK-ALL">' . $descp . '</td>'; 
               echo '<td style="WORD-BREAK:BREAK-ALL">' . substr($row['description'],0,255).
             ( (strlen($row['description'])>255) ? " <a href='http://domain.com/registration/partrecord.php?resid={$row['resid']}'>read more&hellip;</a>" : ''). '</td>'; 
                echo '<td>' . $row['city'] . '</td>'; 
                echo '<td>' . $row['country'] . '</td>';
                echo '<td>' . $row['zipcode'] . '</td>'; 
                //echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>'; 
                //echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>'; 
                echo "</tr>"; 
                echo "<tr><td colspan='9'><hr></td></tr>";  
                      }  
        // close table> 
       echo "</table>"; 

?> 
<p><a href="new.php">Add a new record</a></p><p><a href='logout.php'>Logout</a></p>
</div>

</body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:adamssap
ID: 36531660
Great thanks....pagination works great.  

The total records in the database are 14

On the 1st page 3 records are shown - perfect on data display
On the 2nd page 6 records are shown
On the 3rd page 8 records are shown
On the 4th page 5 records are shown
On the 5th page 2 records are shown - perfect on data display

0
 
LVL 1

Author Comment

by:adamssap
ID: 36531672
Great thanks....pagination works great.  

The total records in the database are 14

On the 1st page 3 records are shown - perfect on data display
On the 2nd page 6 records are shown - not right
On the 3rd page 8 records are shown - not right
On the 4th page 5 records are shown - not right
On the 5th page 2 records are shown - perfect on data display

0
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 500 total points
ID: 36531745
OK, line 78 change to

        $q = $sql." limit $start, $per_page";
0
 
LVL 1

Author Comment

by:adamssap
ID: 36531779
Thanks a ton....
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36531789
Glad to help.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now