Solved

PHP paging does not work

Posted on 2011-09-13
22
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

763 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