Jump to content

[SOLVED] page pagination?


ballhogjoni

Recommended Posts

My script is only showing the first 10 results on every page that my page declares. Basically I have 5 pages and the script shows that correctly on the browser, but when I click on the link to go to page 2 my script pulls up the same results. Can some one help with my coding?

 

<?php
mysql_connect('localhost',$username,$password);
				@mysql_select_db($database) or die( "Unable to select database");

				$limit      = 10;
				$totalrows   = mysql_result(mysql_query("SELECT count(*) FROM cardOffers WHERE cardtype = '".$_GET['cardtype']."'"),0);

				if(!isset($page)){
        				$page = 1;
    				} else {
					$page = $_GET['page'];
				}

                    $limitvalue = (($page * $limit) - $limit);					 
				$query  = "SELECT * FROM cardOffers WHERE cardtype = '".$_GET['cardtype']."' LIMIT $limitvalue, $limit";         
				$result = mysql_query($query) or die("Error: " . mysql_error());

				while($row = mysql_fetch_array($result)){ 
				echo "<table width=\"600\" align=\"center\" border=\"3\" style=\"border-collapse:collapse;\" bordercolor=\"#666666\">
					  <TR>
					  <TD style=\"border-collapse:collapse; background:#666666; font-size:14px;\" valign=\"top\">
					  <div style=\"padding-left:5px;padding-bottom:5px;\">
<a href=\"http://www.xxxxxxx.com/partners/links/cards/details.asp?id=".$row['cid']."&tempid=xxxxxxx\">
<font color=\"white\">".$row['creditcard']."</font></a></div>
					  <table width=\"600\" border=\"1\" style=\"border-collapse:collapse; font-size:12px;\" bordercolor=\"#FFFFFF\">
					  <tr>
					  <td bgcolor=\"#FFFFFF\" style=\"padding-top:5px; padding-left:10px;\" valign=\"top\" width=\"115\">
					  <a href=".$row['link']."><IMG border=\"0\" SRC=".$row['picturehtmlcard']."><br><br><img src=\"https://www.xxxxxxx.com/images/buttons/apply_black.gif\" border=\"0\"></a>
					  </TD>
					  <TD bgcolor=\"#FFFFFF\" style=\"padding-top:5px;\" valign=\"middle\" align=\"left\"><ul>";
						if (!empty($row['ba'])) {
							echo "<li>".$row['ba']."</li>";
						}
						if (!empty($row['bb'])) {
							echo "<li>".$row['bb']."</li>";
						}
						if (!empty($row['bc'])) {
							echo "<li>".$row['bc']."</li>";
						}
						if (!empty($row['bd'])) {
							echo "<li>".$row['bd']."</li>";
						}
						if (!empty($row['be'])) {
							echo "<li>".$row['be']."</li>";
						}
						if (!empty($row['bf'])) {
							echo "<li>".$row['bf']."</li>";
						}
						if (!empty($row['bg'])) {
							echo "<li>".$row['bg']."</li>";
						}
						if (!empty($row['bh'])) {
							echo "<li>".$row['bh']."</li>";
						}
						echo "</ul>
						</TD>
						</TR>
						</TABLE>
						<table width=\"600\" border=\"1\" style=\"border-collapse:collapse; font-size:11px;\" bordercolor=\"#FFFFFF\">
									<tr>
										<td bgcolor=\"#666666\" align=\"center\" style=\"color:white;\">Intro APR</td>
										<td bgcolor=\"#666666\" align=\"center\" style=\"color:white;\">Intro APR Period</td>
										<td bgcolor=\"#666666\" align=\"center\" style=\"color:white;\">Regular APR</td>
										<td bgcolor=\"#666666\" align=\"center\" style=\"color:white;\">Annual Fee</td>
										<td bgcolor=\"#666666\" align=\"center\" style=\"color:white;\">Balance Transfers</td>
										<td bgcolor=\"#666666\" align=\"center\" style=\"color:white;\">Credit Needed</td>
									</tr>
									<tr>";
						if (!empty($row['introductoryrate'])) { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">".$row['introductoryrate']."%</td>"; } else { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">Unknown</td>"; }
						if (!empty($row['timeperiod'])) { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">".$row['timeperiod']."</td>"; } else { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">Unknown</td>"; }
						if (!empty($row['aprpurchnum'])) { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">".$row['aprpurchnum']."%</td>"; } else { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">Unknown</td>"; }
						if (!empty($row['annual'])) { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">".$row['annual']."</td>"; } else { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">Unknown</td>"; }
						if (!empty($row['BALANCETRANSFER'])) { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">".$row['BALANCETRANSFER']."</td>"; } else { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">Unknown</td>"; }
						if (!empty($row['CREDITNEEDED'])) { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">".$row['CREDITNEEDED']."</td>"; } else { echo "<td bgcolor=\"#CCCCCC\" align=\"center\">Unknown</td>"; }
									echo "</tr>
								</table>
							</td>
						</tr>
					</table><br>";

				}
				$numofpages = $totalrows / $limit;
				echo '<div align="center">';
				for($i = 1; $i <= $numofpages; $i++){
					if($i == $page){ 
						echo 'Page '.$i." | "; 
					}else{ 
						echo "<a href=\"http://xxxxxxxxxxxxxxxx/Visa/index.php?cardtype=Visa&page=$i\">Page $i</a> | ";  
					}
				}
				if(($totalrows % $limit) != 0){ 
					if($i == $page){ 
						echo 'Page '.$i." | "; 
					}else{ 
						echo "<a href=\"http://xxxxxxxxxxxxxxxxx/Visa/index.php?cardtype=Visa&page=$i\">Page $i</a> | "; 
					}
				}
				echo '<div>';
			 	mysql_free_result($result);
?>

Link to comment
Share on other sites

make sure $page is > 0 so that you cant go into negative pages ie page -1, -2 etc.

 

Well, not just that, but ensure that $_GET['page'] is a number to begin with. Even though you are building the links there is nothing to stop a user from entering whatever they want into the query string. It would be fairly simple to add additional data tot he query string to perform malicious action to your database by returning records you wouldn't want them to see or to edit or remove data.

 

Always, always validate any data you receive through GET or POST!

 

Change this:

<?php
    if(!isset($_GET['page'])){
        $page = 1;
    } else {
        $page = $_GET['page'];
    }
?>

 

To this:

<?php

$page = (!is_int($_GET['page']) || $_GET['page']<1) ? 1 : $_GET['page'];

?>

 

Link to comment
Share on other sites

It's short hand.  It should be avoided whenever possible.

 

The correct nomenclature is the ternary operator and it is perfectly acceptable. It is actually faster than an if/else, so I have no idea why you are stating it should be avoided at all costs. Do you have any evidence to back up that claim?

Link to comment
Share on other sites

It can be called "shorthand".  Ternary operater is just one of the operators used with shorthand.

I listed my reasons above.

It is "Rumored" to be faster, which as I mentioned is a good thing. I do not like/use shorthand, and generally a new programmer has problems understanding/reading/learning short hand code as opposed to normal code.  On top of that I find it

* Ugly

* Hard to read

* messy.

I recommend new developer's to avoid it, because it is in partial "opinion" it's also pretty hard to disprove the above points of it being messy and hard to read.  The last thing a new programmer need's is unneeded difficulty in the learning process.

Shorthand also refers to doing <?= in order to start an echo statement.

 

http://www.php.net/manual/en/language.basic-syntax.php - here they claim that the original method is preferred.

 

http://en.wikipedia.org/wiki/Ternary_operation - this operator is a way to do shorthand it is not the full definition of what shorthand is.  IT is just one operator used during it.

 

It also encompasses the dumb habit of avoid brackets

if (whatever)

do this

else

do this

 

That is dumb, dangerous, and hard to read.  Shorthand overall I feel should be avoided  However other's have there opinions, I will continue to point developers away from shorthand as much as possible.  This means avoiding short opening tags, short else statements, ternary operator and anything else that is considered shorthand.

 

 

 

Link to comment
Share on other sites

Using the ternary operator and shorthand PHP tags are two entirely different things. So, your first link has nothing to do with the ternary operator.

 

I agree it can be confusing at first, but after the initial speed bump usage of the ternary operator can greatly speed up comdin and comprehension of the code.

 

The PHP manual that describes ternary operators (note: not referred to as shorthand anywhere) only cautions against using nested statements. The example listed above is the perfect usage of the operator.

 

I can appreciate your position, but stating "it should be avoided at all costs" is disingenuous because it is your opinion and not fact.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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