iceblox Posted October 29, 2007 Share Posted October 29, 2007 Hi Everyone, I have this script which i got from PHPFreaks.com and im trying to print the results that are being shown the only way i can see doing it is like below but I cant see me coding that for every page. Has anyone got any better ideas? if($page == "1") { $page_num = '1-40'; } elseif($page == "2") { $page_num = '41-80'; } elseif($page == "3") { $page_num = '81-120'; } elseif($page == "4") { $page_num = '121-160'; } else { $page_num = ''; } Pagination Code: $limit = 40; // Sets how many results shown per page if(empty($page)){ // Checks if the $page variable is empty (not set) $page = 1; // If it is empty, we're on page 1 } $limitvalue = $page * $limit - ($limit); $query_count = "SELECT Count(*) FROM deals WHERE $net1 MonthlyCost BETWEEN 0 AND '{$_GET['MonthlyRental']}' AND $net PhoneCost BETWEEN 0 AND '{$_GET['PhoneCost']}' AND $net2 ContractLength BETWEEN 0 AND '{$_GET['ContractLength']}' AND FreeMins BETWEEN '{$_GET['FreeMins']}' AND 9999 AND FreeTxts BETWEEN '{$_GET['FreeTxts']}' AND 9999 AND $net3 FreeGift <> 'Offer.FreeGift' ORDER BY TotalCost ASC"; // Sets what we want to pull from the database // count(*) is better for large databases (thanks Greg!) $result_count = mysql_query($query_count) or die(mysql_error()); // Pulls what we want from the database $totalrows = mysql_result($result_count,0,0); // This counts the number of users $query = "SELECT * FROM deals WHERE $net1 MonthlyCost BETWEEN 0 AND '{$_GET['MonthlyRental']}' AND $net PhoneCost BETWEEN 0 AND '{$_GET['PhoneCost']}' AND $net2 ContractLength BETWEEN 0 AND '{$_GET['ContractLength']}' AND FreeMins BETWEEN '{$_GET['FreeMins']}' AND 9999 AND FreeTxts BETWEEN '{$_GET['FreeTxts']}' AND 9999 AND $net3 FreeGift <> 'Offer.FreeGift' ORDER BY TotalCost ASC LIMIT $limitvalue, $limit"; $result = mysql_query($query) or die(mysql_error()); if (mysql_num_rows($result) > 0) { if($page == "1") { $page_num = '1-40'; } elseif($page == "2") { $page_num = '41-80'; } elseif($page == "3") { $page_num = '81-120'; } elseif($page == "4") { $page_num = '121-160'; } else { $page_num = ''; } echo 'Currently viewing Page ' . $page_num. ' of ' . $totalrows . ' available mobile phone deals'; echo '<table width=840>'; echo '<tr>'; echo '<td width=17% align=center bgcolor="#87C542"><b>Tariff</b></td>'; echo '<td width=50% align=center bgcolor="#87C542"><b>The Detail</b></td>'; echo '<td width=20% align=center bgcolor="#87C542"><b>The Price</b></td>'; echo '<td width=13% align=center bgcolor="#87C542"><b>Where To Buy</b></td>'; echo '</tr>'; while($row = mysql_fetch_row($result)) { if($rowcounter%2==1) $bgcolor="#F4F4F4"; else $bgcolor="#FFFFFF"; $price =$row[2] / $row[17]; $price2 =$row[1] - $row[2]; if($row[17] == "12") { $net = '<font color="#000099">' . $row[17] . ' Month Contract</font>'; } else { $net = '<font color="#FF0000">' . $row[17] . ' Month Contract</font>'; } if($row[7] == "0.00") { $deal = ' - <font color="#FF0000">' . $row[8] . ' Months FREE Line Rental</font>'; } elseif($row[5] >= "0.01") { $deal = ' includes £' . $row[5] . ' Cashback</b>'; } elseif($price2 == "0.00") { $deal = ' - <font color="#FF0000">' . $row[8] . ' Months FREE Line Rental</font>'; } elseif($row[7] < "$row[18]") { $deal = ' - ' . $row[8] . ' Months at £' . $row[7] . ''; } else { $deal = ''; } if($row[6] == "1") { $auto = '<font color="#FF0000">Automatic Cashback - No Redemtion Required!</font><br><br>'; } else { $auto = ''; } if($row[20] == "0") { $say2 = ''; } else { $say2 = '' . $row[20] . ' ' . $row[21] . ' minutes to any network '; } if($row[22] == "0") { $say3 = ''; } else { $say3 = 'plus ' . $row[22] . ' texts per month'; } echo '<tr bgcolor="' . $bgcolor . '">'; if($row[1] == "0") { $phone = '<b><font color="#FF0000">FREE</font> ' . $row[11] . ' ' . $row[12] . '</b>'; } else { $phone = '<b>' . $row[11] . ' ' . $row[12] . ' only £' . $row[1] . '</b>'; } if($row[4] == "") { $gift = ''; } else { $gift = ' with a <font color="#FF0000">FREE</font> ' . $row[4] . ''; } if($row[4] == "") { $giftimg = ''; } elseif($row[4] == "PAYG Handset") { $giftimg = ''; } else { $giftimg = ' <img src="images/' . $row[4] . '.jpg" alt="FREE ' . $row[4] . '" align=left>'; } if($row[22] == "0") { $say10 = ''; } else { $say10 = 'plus ' . $row[22] . ' texts per month'; } if($row[24] == "Vodafone") { $say4 = 'Voda ' . str_replace("£","£",$row[19] ) . ''; } elseif($row[24] == "O2") { $say4 = 'O2 ' . str_replace("£","£",$row[19] ) . ''; } elseif($row[24] == "Virgin") { $say4 = 'Virgin ' . str_replace("£","£",$row[19] ) . ''; } elseif($row[24] == "BT Mobile") { $say4 = 'BT Mobile ' . str_replace("£","£",$row[19] ) . ''; } else { $say4 = '' . str_replace("£","£",$row[19] ) . ''; } echo '<tr bgcolor="' . $bgcolor . '">'; echo '<td align=center><img src=' . $row[25] . '><br>' . $say4 . '<br>' . $net . '<br> £' . $row[18] . ' Per Month</td>'; echo '<td align=center>'; if($row[23] == "") { $tmob = ''; } else { $tmob = ' T-Mobile Flext gives you an Allowance of £' . $row[23] . ' to spend how you like on minutes or texts. This could allow you to have '; } echo'<img src="' . $row[14] . '" alt="' . $row[11] . ' ' . $row[12] . '" align=left> ' . $giftimg . ' ' . $phone . ' ' . $deal . ' ' . $gift . ' <br> ' . $auto . ' ' . $tmob . ' ' . $say2 . ' ' . $say3 . ' standard Line rental is £' . $row[18] . ' per month.</td>'; echo '<td align=center>Average line rental after cash back will be '; if($row[2] == "0.00") { echo' <font color="#FF0000">£' . number_format($price, 2, '.', '') . '</font><br><br>Total cost of this contract: <font color="#FF0000"><b>FREE</b></font></td>'; } else { echo' £' . number_format($price, 2, '.', '') . '<br><br>Total cost of this contract: £' . number_format($row[2], 2, '.', '') . '</td>'; } echo '<td align=center><a target="_blank" href=http://www.yoursite.com/banners.php?op=dealsclick&ID=' . $row[0] . '><img border=0 alt="' . $row[26] . '" src=' . $row[27] . '><br><br> <img border=0 alt="Buy Now - ' . $row[26] . '" src=http://www.yoursite.com/mobile-phone-deals/images/buynow.gif width="80" height="20"></a></td>'; echo '</tr>'; $rowcounter++; } echo '</table>'; } mysql_free_result($result); mysql_close($connection); ?> Quote Link to comment Share on other sites More sharing options...
otuatail Posted October 29, 2007 Share Posted October 29, 2007 If you can keep track of the page number then. $page = 2; // egsample $max_results = 40; $from = (($page * $max_results) - $max_results); $sql = mysql_query("SELECT * FROM pages LIMIT $from, $max_results"); That should do it. Desmond. Quote Link to comment Share on other sites More sharing options...
iceblox Posted October 29, 2007 Author Share Posted October 29, 2007 I dont think i explained my self well enough. I want to be able to print the data like this Showing 1 to 50 of 17798 available mobile phone deals I already have the data coming out of the database in the right way i would just like to print where its at Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 29, 2007 Share Posted October 29, 2007 1to 50 of 17798 17798<<<result from num rows or counting of ids or field 1to 50<<< result when you divide numrows to a number of records to be displayed problem with that? Quote Link to comment Share on other sites More sharing options...
iceblox Posted October 29, 2007 Author Share Posted October 29, 2007 I understand that the problem im having is like below Currently viewing 41 to 80 of 49 available mobile phone deals Is that a formula i can put in place that will check that "80" doesnt go above the number of deals? Apart from that its working fine, Thanks for your help so far Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 29, 2007 Share Posted October 29, 2007 sorry but y? Quote Link to comment Share on other sites More sharing options...
iceblox Posted October 29, 2007 Author Share Posted October 29, 2007 So that rather than displaying.. Currently viewing 41 to 80 of 49 available mobile phone deals I can get it to display as there are not 80 deals avalible Currently viewing 41 to 49 of 49 available mobile phone deals Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 29, 2007 Share Posted October 29, 2007 just change this part if (mysql_num_rows($result) > 0) { if($page == "1") { $page_num = '1-40'; } elseif($page == "2") { $page_num = '41-80'; } elseif($page == "3") { $page_num = '81-120'; } elseif($page == "4") { $page_num = '121-160'; } else { $page_num = ''; } if thats not clear. what is limit of data to be displayed eg 1-20,20-40 etc..? Quote Link to comment Share on other sites More sharing options...
iceblox Posted October 29, 2007 Author Share Posted October 29, 2007 Sorry I took on some of otuatail suggestion so this is my code. $to = (($page * $limit) - $limit + 40); $from = (($page * $limit) - $limit + 1); echo 'Currently viewing ' . $from. ' to ' . $to. ' of ' . $totalrows . ' available mobile phone deals'; Which is working apart from the error i described. Quote Link to comment Share on other sites More sharing options...
sasa Posted October 30, 2007 Share Posted October 30, 2007 //$to = (($page * $limit) - $limit + 40); $to = $page * $limit > $totalrows ? $totalrows : $page * $limit; $from = (($page * $limit) - $limit + 1); echo 'Currently viewing ' . $from. ' to ' . $to. ' of ' . $totalrows . ' available mobile phone deals'; Quote Link to comment Share on other sites More sharing options...
iceblox Posted October 30, 2007 Author Share Posted October 30, 2007 Thanks Again Sasa Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.