Jump to content

Pagination Problem


derek barnstorm

Recommended Posts

Hi, I have been trying to paginate the following script:

 

<?php
if($_SERVER[php_SELF]=="/include/headlist.inc.php")
{
    header("Location: /index.php");
    exit;
}

include("include/get_profile_fields.inc.php");



$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");


$sql = "SELECT * FROM $tbl_profiles WHERE profile_id != \"00000001\"";
$result = @mysql_query($sql,$connection) or die("Couldn't execute profile query.");
$num=mysql_num_rows($result);
if($num < 1){
echo "<p align=\"center\">No Members found.</p>";

?>


<?
include("include/footer.inc.php");
exit;
}


?>
<table border="1" align="center" cellpadding="4" cellspacing="0" width="100%">
<?
if($result && mysql_num_rows($result) > 0)
{
    $i = 0;
    $max_columns = 2;
    while($row = mysql_fetch_array($result))        
   {
       // make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

$p_name = $row['p_name'];
$profile_url = $row['profile_url'];
$location = $row['location'];
$profile_id = $row['profile_id'];
$sql_img = "SELECT * FROM $tbl_images WHERE image_id = \"$profile_id\" AND default_pic = \"yes\"";
$result_i = @mysql_query($sql_img,$connection) or die("Couldn't execute image query.");
$irow = mysql_fetch_array($result_i);

$friend_pic = $irow['image'];
$friend_url = $irow['url'];

if(empty($friend_url)) {$newfurl="$imgdir";}else{$newfurl="$userdir/$friend_url";}
if(empty($friend_pic))$friend_pic="pic.gif";
$friend_img="/$newfurl/$friend_pic";
echo "
        <td>
            <p><a href=\"$userurl/$profile_url\"><IMG SRC=\"../../thumbs/phpThumb.php?src=$friend_img&w=100\" align=\"absmiddle\" border=\"0\"><b> $p_name</a></b></p>
            <p>Location: $location</p>
        </td>
    ";
    // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td> </td>";
}

include("include/footer.inc.php");
exit;

?>

 

I found the this tutorial http://www.tonymarston.net/php-mysql/pagination.html amongst others but after hours of messing about I'm still having problems.

Using that tutorial I adapted the above script to the one below, but it won't connect to the database. Every other way I've tried it, just gives errors.

 

<?php
if($_SERVER[php_SELF]=="/include/headlist.inc.php")
{
    header("Location: /index.php");
    exit;
}

include("include/get_profile_fields.inc.php");

if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
} // if



$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");


$query = "SELECT COUNT(*) FROM $tbl_profiles WHERE profile_id != \"00000001\"";
$result = @mysql_query($query,$connection) or die("Couldn't execute profile query.");
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

$rows_per_page = 15;
$lastpage = ceil($numrows/$rows_per_page);

$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
} // if
if ($pageno < 1) {
$pageno = 1;
} // if

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

$query = "SELECT * FROM table $limit";
$result = @mysql_query($query,$connection) or die("Couldn't execute profile query.");
$num=mysql_num_rows($result);
if($num < 1){
echo "<p align=\"center\">No Members found.</p>";

?>


<?
include("include/footer.inc.php");
exit;
}
if ($pageno == 1) {
echo " FIRST PREV ";
} else {
echo  "<a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
$prevpage = $pageno-1;
echo "<a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
} // if

echo " ( Page $pageno of $lastpage ) ";

if ($pageno == $lastpage) {
echo " NEXT LAST ";
} else {
$nextpage = $pageno+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
} // if

?>
<table border="1" align="center" cellpadding="4" cellspacing="0" width="100%">
<?
if($result && mysql_num_rows($result) > 0)
{
    $i = 0;
    $max_columns = 2;
    while($row = mysql_fetch_array($result))        
   {
       // make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

$p_name = $row['p_name'];
$profile_url = $row['profile_url'];
$location = $row['location'];
$profile_id = $row['profile_id'];
$sql_img = "SELECT * FROM $tbl_images WHERE image_id = \"$profile_id\" AND default_pic = \"yes\"";
$result_i = @mysql_query($sql_img,$connection) or die("Couldn't execute image query.");
$irow = mysql_fetch_array($result_i);

$friend_pic = $irow['image'];
$friend_url = $irow['url'];

if(empty($friend_url)) {$newfurl="$imgdir";}else{$newfurl="$userdir/$friend_url";}
if(empty($friend_pic))$friend_pic="pic.gif";
$friend_img="/$newfurl/$friend_pic";
echo "
        <td>
            <p><a href=\"$userurl/$profile_url\"><IMG SRC=\"../../thumbs/phpThumb.php?src=$friend_img&w=100\" align=\"absmiddle\" border=\"0\"><b> $p_name</a></b></p>
            <p>Location: $location</p>
        </td>
    ";
    // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td> </td>";
}

include("include/footer.inc.php");
exit;

?>

 

Any help or a point in the right direction would be great.

 

Cheers.

 

Link to comment
https://forums.phpfreaks.com/topic/111402-pagination-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.