Jump to content

Blasted Pagination


herghost

Recommended Posts

Hi guys I am having some problems with getting a pagination script to work.

 

Basically the page this falls on can show multiple different results depending on what is parsed in the browser (ie view.php?cat=4)

 

This is a simple script I found but I cant get it to work:

<?php

    
$query = "SELECT count(*) from merchants"; 
$row=mysql_fetch_assoc(mysql_query($query));
$total_records = $row['Total'];

$records_per_page = 5;
$total_pages = ceil($total_records / $records_per_page);
$page = intval($_GET['p']);
if ($page < 1 || $page > $total_pages) $page = 1;
$offset = ($page - 1) * $records_per_page; 

$limit = " LIMIT $offset, $records_per_page";


if($subid > 0)
{
$query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid' AND subid= '$subid' OR subid0 ='$subid' OR subid1 ='$subid' ");

echo mysql_error() ;
}

else
{
$query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid'");
echo mysql_error() ;

}


while($row = mysql_fetch_assoc($query)){

$name = $row['name'];
$short = $row['short'];
$per = $row['percent'];
$m_id = $row['m_id'];

?>
    
    <div id="cbbox">
    <a href="viewm.php?m_id=<?php echo $m_id;?>">
    	<div class="cbname">
        	<?php echo $name;?>   - Up to <?php echo $per ;?>% Cashback Available
        </div>
        <div class="cbimage">
        	<img src="pimage/<?php echo $name;?>.gif" height="50px" width="100px" />
        </div>
      <div class="cbshort">
        	<?php echo $short;?>
        </div>
        </a>
        <div class="cbtweet">
       	<a href="http://twitter.com/home/?status=Get up to <?php echo $per;?> percent Cashback on purchase from <?php echo $name;?> http://goo.gl/PLkp"><img src="images/cbtweet.png" width="90" height="50" alt="Tweet This" /></a>

        </div>
        

        
    </div>



        <br />
<br />
<?php
}
//Pagination
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a title='page $i' href='?p=$i'>$i - </a>";//link to page
}
?>

 

Basically the issues I am having are:

 

Undefined index: Total - I dont have a row called total in my database, if I use my unique field m_id I get the same message

 

&

 

Undefined index: p - Not sure where this is meant to come from

 

 

Any pointers would be most helpful :)

 

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

$total_records = $row['Total'];

$row came from that SELECT query a couple lines above. If you run that manually, like in phpMyAdmin, what columns do you get back? What you put in $row["..."] has to be one of those, and case-sensitive too.

 

 

$page = intval($_GET['p']);

$_GET is the stuff in the URL. Right now I see

http://www.phpfreaks.com/forums/php-coding-help/blasted-pagination/?action=post;num_replies=0

If configured appropriately, $_GET["action"] == "post" and $_GET["num_replies"] == "0". So $_GET["p"] comes from a ?p= in the URL, and if there isn't one there then trying to use $_GET["p"] will create an error.

Use isset() to check if it exists first. Like

$page = (isset($_GET["p"]) ? intval($_GET["p"]) : 1);

Link to comment
https://forums.phpfreaks.com/topic/218229-blasted-pagination/#findComment-1132358
Share on other sites

If I use this:

$query = "SELECT count(*) from merchants"; 
$row=mysql_fetch_assoc(mysql_query($query));
$total_records = 10;



$records_per_page = 5;
$total_pages = ceil($total_records / $records_per_page);
$page = intval($_GET['p']);
if ($page < 1 || $page > $total_pages) $page = 1;
$offset = ($page - 1) * $records_per_page; 

$limit = " LIMIT $offset, $records_per_page";


if($subid > 0)
{
$query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid' AND subid= '$subid' OR subid0 ='$subid' OR subid1 ='$subid' ");

echo mysql_error() ;
}

else
{
$query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid'");
echo mysql_error() ;

}


while($row = mysql_fetch_assoc($query)){

$name = $row['name'];
$short = $row['short'];
$per = $row['percent'];
$m_id = $row['m_id'];

?>
    
    <div id="cbbox">
    <a href="viewm.php?m_id=<?php echo $m_id;?>">
    	<div class="cbname">
        	<?php echo $name;?>   - Up to <?php echo $per ;?>% Cashback Available
        </div>
        <div class="cbimage">
        	<img src="pimage/<?php echo $name;?>.gif" height="50px" width="100px" />
        </div>
      <div class="cbshort">
        	<?php echo $short;?>
        </div>
        </a>
        <div class="cbtweet">
       	<a href="http://twitter.com/home/?status=Get up to <?php echo $per;?> percent Cashback on purchase from <?php echo $name;?> http://goo.gl/PLkp"><img src="images/cbtweet.png" width="90" height="50" alt="Tweet This" /></a>

        </div>
        

        
    </div>



        <br />
<br />
<?php
}
//Pagination
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a title='page $i' href='?catid=" . $catid ."&";
if($subid > 0)
{
echo $subid . "&";
}
echo "p=$i'>$i - </a>";//link to page
}
?>

 

Then I get no errors, however the results are not paginating and the same results are shown on page 2!

 

Link to comment
https://forums.phpfreaks.com/topic/218229-blasted-pagination/#findComment-1132366
Share on other sites

So then take a look at what I said

$total_records = $row['Total'];

$row came from that SELECT query a couple lines above. If you run that manually, like in phpMyAdmin, what columns do you get back? What you put in $row["..."] has to be one of those, and case-sensitive too.

and take a guess. There is no "Total" column but there is a "count(*)" column, right?

Link to comment
https://forums.phpfreaks.com/topic/218229-blasted-pagination/#findComment-1132380
Share on other sites

ahhhhh!

 

The penny drops!

 

Thanks, got it going with this:

<?php

$query = "SELECT count(*) from merchants"; 
$row=mysql_fetch_assoc(mysql_query($query));
$total_records = $row['count(*)'];

$records_per_page = 5;
$total_pages = ceil($total_records / $records_per_page);
$page = intval($_GET['p']);
if ($page < 1 || $page > $total_pages) $page = 1;
$offset = ($page - 1) * $records_per_page; 

$limit = " LIMIT $offset, $records_per_page";


if($subid > 0)
{
$query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid' AND subid= '$subid' OR subid0 ='$subid' OR subid1 ='$subid' $limit ");

echo mysql_error() ;
}

else
{
$query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid'  $limit ");
echo mysql_error() ;

}


while($row = mysql_fetch_assoc($query)){

$name = $row['name'];
$short = $row['short'];
$per = $row['percent'];
$m_id = $row['m_id'];

?>
    
    <div id="cbbox">
    <a href="viewm.php?m_id=<?php echo $m_id;?>">
    	<div class="cbname">
        	<?php echo $name;?>   - Up to <?php echo $per ;?>% Cashback Available
        </div>
        <div class="cbimage">
        	<img src="pimage/<?php echo $name;?>.gif" height="50px" width="100px" />
        </div>
      <div class="cbshort">
        	<?php echo $short;?>
        </div>
        </a>
        <div class="cbtweet">
       	<a href="http://twitter.com/home/?status=Get up to <?php echo $per;?> percent Cashback on purchase from <?php echo $name;?> http://goo.gl/PLkp"><img src="images/cbtweet.png" width="90" height="50" alt="Tweet This" /></a>

        </div>
        

        
    </div>



        <br />
<br />
<?php
}
//Pagination
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a title='page $i' href='?catid=" . $catid ."&";
if($subid > 0)
{
echo $subid . "&";
}
echo "p=$i'>$i - </a>";//link to page
}
?>

 

Guess Ill have some hair left when it comes to search results yet then :)

 

Many thanks again :)

 

Link to comment
https://forums.phpfreaks.com/topic/218229-blasted-pagination/#findComment-1132389
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.