I am trying to create a dynamic page links created based on the number of rows in a mysql table. I would like to display 10 results per page and wish to have the php script create links to additional pages.
So I was thinking of using the num_rows and dividing it by 10 however if I have 53 rows the return would be 5.3 where as I would need 6 pages and not 5. I am thinking of using the round function and looping it through a for I statement until $pages > $rows_rounded. And every 10 rows add a link to pages($i) Is this the best method to acheive this or there an alternative simpler route to take?
pagenator class I made. getCurrentPages() returns all the pages you should be displaying in an array. so if you are on page one, and you want to display a total of 9 pages, you would get an array 1-9. if you were on page 10 however, your would get back an array 6-14. if there are 20 total pages and you are on page 20, you would get back an array 11-20.
<?php
class Lev_Pagenator {
private $recordsPerPage;
private $currentPage;
private $numberOfTotalRecords;
private $lastPage = null;
public function __construct($current_page, $number_of_total_records, $records_per_page = 25) {
$this->currentPage = $current_page;
$this->numberOfTotalRecords = $number_of_total_records;
$this->recordsPerPage = $records_per_page;
}
public function getCurrentStartIndex() {
return ($this->currentPage - 1) * $this->recordsPerPage;
}
public function getCurrentPages($number_of_pages_to_display = 9) {
$start_page = $this->currentPage - floor($number_of_pages_to_display / 2);
if ($start_page < 1) $start_page = 1;
$last_page = $this->getLastPage();
$pages = array($start_page);
for ($i = 1; $i < $number_of_pages_to_display; $i++) {
$temp_page = $start_page + $i;
if ($temp_page <= $last_page) {
$pages[] = $temp_page;
} else {
break;
}
}
return $pages;
}
public function getPreviousPage() {
if ($this->currentPage === 1) return false;
return $this->currentPage - 1;
}
public function getNextPage() {
if ($this->currentPage === $this->getLastPage) return false;
return $this->currentPage + 1;
}
public function getLastPage() {
if ($this->lastPage === null) $this->lastPage = ceil($this->numberOfTotalRecords / $this->recordsPerPage);
return $this->lastPage;
}
}
?>
EDIT (USAGE):
<?php
$pagenator = new Lev_Pagenator($current_page, $number_of_total_records, $records_per_page);
$pages_array = $pagenator->getCurrentPages($number_of_pages_to_display);
?>
The idea of a for loop sounds like a good one, you would use something like:
$rows_rounded = ceil(mysql_num_rows($result) / 10);
for($x = 1; $x <= $rows_rounded; $x++){
echo 'Page '.$x.'';
}
But you need to consider detecting the current page, so if, for example, the current page was 3, it might be a good idea to test for that in your for loop and if echoing the 3rd link maybe add some extra class to enable you to style it.
Related
I am trying to make a text game in PHP but i have problem since i am programing in php for few days. I need to call function attack (I need $_mainAttack since it is combination of $_baseAttack and special attack) from defend function so i can calculate the health loss I have placed -5 just to see if it is working..
Also in health i need attack to be able to lower hp so i could calculate the hp loss. My do while loop is wrong and i need help to make it functional. I want when health goes lower than 0 to exit the loop. When it exits the loop it will print game over. I am stuck in endless loop and i have no idea how to fix this.
This is index.php:
<?php
include 'Duel.php';
$duel = new Duel();
$duel->attack();
$duel->defend();
?>
This is my class duel:
<?php
class Duel{
public $_maxHealth = 20;
public $_currentHealth;
public $_baseAttack, $_specialAttack, $_mainAttack;
public $_specialChance, $deflectChance;
public $_defense;
function __construct()
{
echo 'begining of attack <br/>';
}
function attack()
{
$_specialChance = rand(0, 20);
$_specialAttack = 0;
if ($_specialChance < 10) {
$_specialAttack = 0;
} elseif ($_specialChance < 15) {
$_specialAttack = (int) rand(0, 5);
} elseif ($_specialChance <= 20) {
$_specialAttack = (int) rand(5, 10);
}
$_baseAttack = rand(1, 6);
$_mainAttack = $_baseAttack + $_specialAttack;
echo "Base attack is $_baseAttack: and special attack is : $_specialAttack attack is : $_mainAttack<br/>";
}
function defend()
{
$_maxHealth = 20;
do{
$deflectChance = rand(1, 10);
$deflect = 0;
if ($deflectChance < 5) {
$deflect = 0;
echo 'attack cannot be deflected';
}
elseif ($deflectChance > 5) {
$deflect = (int) rand(0, 3);
echo "attack is deflected for {$deflect} damage";
}
$_currentHealth = $_maxHealth + $deflect - 5;
echo "<br/>health is {$_currentHealth} <br/>";
}while($_currentHealth > 0);
if($_currentHealth > 0) echo "Game over";
}
} //end of class
You are always calculating $_currentHealth based on $_maxHealth, not the previous $_currentHealth.
Add before the loop:
$_currentHealth = $_maxHealth;
And change $_currentHealth = $_maxHealth + $deflect - 5; to:
$_currentHealth = $_currentHealth + $deflect - 5;
You can try returning the main attack variable from the attack function and simply call it on the defend function.
you are calculation $_mainAttack but doesn't subtract it from your health, therefore your player can't die and you end within an endless loop.
So basically I am trying to get the sum of AveragePrice of every single page on this api. Right now it only gets first page the things i've tried have only gotten it to go on an endless loop crashing wamp. Heres my code for 1 page of working.
I am just really unsure how I can get it to loop through pages and get sum of every page.
<?php
function getRap($userId){
$url = sprintf("https://www.roblox.com/Trade/InventoryHandler.ashx?userId=" . $userId . "&filter=0&page=1&itemsPerPage=14");
$results = file_get_contents($url);
$json = json_decode($results, true);
$data = $json['data']['InventoryItems'];
$rap = 0;
foreach($data as $var) {
$rap += $var['AveragePrice'];
}
echo $rap;
}
$userId = 1;
getRap($userId);
?>
You may get better answers by looking into the API you are working with regarding how many pages to look for. You want to loop until you hit the max pages. There should be an value in the result of your request that tells you that you've asked for a page that doesn't exist (ie. no more results). If you can get a total number of results to search for then you could do a for loop with that as your limit.
//Change the function to accept the page number as a variable
function getRap($userId, $i){
$url = sprintf("https://www.roblox.com/Trade/InventoryHandler.ashx?userId=" . $userId . "&filter=0&page=" . $i . "&itemsPerPage=14");
//work out how many pages it takes to include your total items
// ceil rounds a value up to next integer.
// ceil(20 / 14) = ceil(1.42..) == 2 ; It will return 2 and you will look for two pages
$limit = ceil($totalItems / $itemsPerPage);
// Then loop through calling the function passing the page number up to your limit.
for ($i = 0; $i < $limit; $i++) {
getRap($userId, $i);
}
If you cannot get the total number of items, you could loop while a fail state hasn't occured
// look for a fail state inside your getRap()
function getRap($userId, $i) {
if ($result = error) { //you will have to figure out what it returns on a fail
$tooMany = TRUE;
}
}
for ($i = 0; $tooMany !== TRUE ; $i++) {
getRap($userId, $i);
}
Edit: Reviewing my answer, looking for the fail state inside your function is poor form (and won't work because of the scope of the variable in this case). You could pass the variable back and forth, but I'll leave that part up to you.
To get the total, make sure that your function doesn't print the result (echo $rap) but returns it for further use.
Full example
<?php
function getRap($userId, $i){
$url = sprintf("https://www.roblox.com/Trade/InventoryHandler.ashx?userId=" . $userId . "&filter=0&page=" . $i . "&itemsPerPage=25");
$results = file_get_contents($url);
$json = json_decode($results, true);
if ($json['msg'] == "Inventory retreived!") {
$data = $json['data']['InventoryItems'];
$rap = 0;
foreach($data as $var) {
$rap += $var['AveragePrice'];
}
return $rap;
} else {
return FALSE;
}
}
$total = 0;
$userId = 1;
for ($i = 0; $i < 1000 /*arbitrary limit to prevent permanent loop*/ ; $i++) {
$result = getRap($userId, $i);
if ($result == FALSE) {
$pages = $i;
break;
} else {
$total += getRap($userId, $i);
}
}
echo "Total value of $total, across $pages pages";
?>
I currently have a method inside my "car class" that display the car:
static function getCars(){
$autos = DB::query("SELECT * FROM automoviles");
$retorno = array();
foreach($autos as $a){
$automovil = automovil::fromDB($a->marca, $a->modelo, $a->version, $a->year, $a->usuario_id, $a->kilometraje, $a->info,
$a->hits, $a->cilindrada, $a->estado, $a->color, $a->categoria, $a->precio, $a->idAutomovil);
array_push($retorno, $automovil);
}
return $retorno;
}
In my index.php I call that function
foreach(car::getCars() as $a){
That allows me to display the info this way ( of course inside the foreach I have a huge code with the details I'll display.
Is there a way to implement a pagination to that thing so I can handle 8 cars per page, instead of showing all of them at the same page?
You can add a $limit and $page parameter on your function so that it will only return a maximum of $limit number of items starting from $limit * $page(or will call it the $offset). You also need to add a function to get the total number of rows you have for automoviles table.
static function getCars($page = 0, $limit = 8){
$offset = $limit * max(0, $page - 1);
//replace this with prepared statement
$autos = DB::query("SELECT * FROM automoviles LIMIT $offset, $limit");
$retorno = array();
foreach($autos as $a){
$automovil = automovil::fromDB($a->marca, $a->modelo, $a->version, $a->year, $a->usuario_id, $a->kilometraje, $a->info,
$a->hits, $a->cilindrada, $a->estado, $a->color, $a->categoria, $a->precio, $a->idAutomovil);
array_push($retorno, $automovil);
}
return $retorno;
}
static function getTotal()
{
//query to get total number of rows in automoviles table
}
In your index.php do this:
foreach(car::getCars((isset($_GET['page']) ? $_GET['page'] : 1)) as $a){
...
}
and add the pagination links.
$total = car::getTotal();
if($total > 8) {
for($i = 1; $i <= intval(ceil(1.0 * $total / $limit)); $i++) {
echo '' . $i . ';
}
}
Using Magento with Zend_Pdf and a couple of custom classes that allow for the printing/downloading of PDFs with certain header/table specifications. The problem is that it cuts off after the first page and doesn't create a new page when the items list is more than about 20 items.
This code:
public function getOutput()
{
$this->pages[] = $this->page;
return $this->render();
}
And this code:
$pdf = new PrintPdf();
$pdf->generate($sourceData);
$output = $pdf->getOutput();
echo $output;
Are where I believe the error is happening in. If I change the "return" in the first code to "echo" it will output the correct data to the browser, but when I then try and pass it through the second code, it only puts out one page of data.
Any advice would be appreciated as I have been working on this for about 2 weeks.
UPDATE:
I was told this piece of code:
private function drawLineItems($tableData)
{
$this->drawHeading($this->__('Line Items'));
// Draw table
$this->decOffset(35);
$this->colorLine(cBLUE);
$this->page->setLineWidth(0.5);
$this->page->drawLine($this->pMargin, $this->yOffset, $this->pWidth - $this->pMargin, $this->yOffset);
$this->fontSize(FONT_SMALL);
$this->colorFill(cBLUE);
$this->decOffset(15);
$sum = ($this->pMargin + 10);
for($idx = 0; $idx < sizeof($tableData['heading']); $idx++) {
$pos = $sum;
$this->page->drawText($tableData['heading'][$idx], $sum, $this->yOffset);
$sum += ($tableData['width'][$idx] + 10);
$tableData['width'][$idx] = $pos;
}
$this->decOffset(10);
$this->page->drawLine($this->pMargin, $this->yOffset, $this->pWidth - $this->pMargin, $this->yOffset);
$this->fontSize(8);
$this->colorFill(cLIGHT);
$this->colorLine(cBORDER);
foreach($tableData['rows'] as $row) {
$this->decOffset(15);
$yOffset = $this->yOffset;
for($idx = 0; $idx < sizeof($row); $idx++) {
if ($tableData['heading'][$idx] == 'Description') {
$lines = $this->_breakTextToLines($row[$idx], $tableData['width'][$idx + 1] - $tableData['width'][$idx]);
foreach ($lines as $line) {
$this->page->drawText($line, $tableData['width'][$idx], $yOffset);
$yOffset -= 10;
}
} else {
$this->page->drawText($row[$idx], $tableData['width'][$idx], $this->yOffset);
}
}
$this->decOffset($this->yOffset - $yOffset);
$this->page->drawLine($this->pMargin, $this->yOffset, $this->pWidth - $this->pMargin, $this->yOffset);
}
$this->decOffset(20);
$this->fontSize(FONT_NORMAL);
$this->colorFill(cDARK);
$this->page->drawText($this->__('Grand Total') . ': ' . $tableData['total'], $this->pWidth - 125, $this->yOffset);
}
is probably where the problem is as it doesn't form a new page after a certain number of items. My problem now lies in accomplishing this task. Help would still be appreciated.
I found that adding this code:
if( $this->yOffset < 25){
$this->yOffset = $this->pHeight - 25;
$yOffset = $this->yOffset;
$this->pages[] = $this->page;
$this->page = $this->newPage(Zend_Pdf_Page::SIZE_A4);
$this->fontSize(8);
$this->colorFill(cLIGHT);
$this->colorLine(cBORDER);
}
after
$yOffest = $this->yOffset
in the third part of the code in the question, fixed the problem!
I'm working on building an ad banner rotation script based on impressions that displays ads evenly throughout the month. The calculations will be done each time the ad is requested to be displayed. So this will be done on the fly. The ads should appear to rotate through, one after another, and not just display one ad for 1000 impressions, then the other ad for 1000 impressions. It for the most part should display for 1 impression, then switch ads (unless of course one ad has a lot more impressions than the other to use up).
Let's say I have 5 ads and each has a different number of impressions that were purchased, what would be the formula/how do you serve up the ads? I'm looking to do this in PHP.
Ad #1: 1,000 purchased impressions
Ad #2: 12,000 purchased impressions
Ad #3: 3,000 purchased impressions
Ad #4: 20,000 purchased impressions
Ad #5: 10,000 purchased impressions
if there are multiple ads that bought 1000 impressions all for the same time frame, it should show one after another until the impressions are used. Though, I think it might be good that if a person bought 1000 impressions for a short time frame, I should account for that and show them at a faster rate. I'm open to suggestions.
I think you should use the best type of algorithm for the best JOB i'll only show you some few possibility on how to implement such
My current example would show using
shuffle
fisherYatesShuffle
robinShuffle
ratioShuffle
You can also implement
Priory Based shuffle
Time Base Shuffle
Percentage
Click Shuffle
etc
Simple Prove of Concept
// Create Add Infroamtion
$ads = array();
$ads[] = new Ad(10, "A.jpg", 2);
$ads[] = new Ad(12, "B.gif", 3);
$ads[] = new Ad(30, "C.png", 7);
$ads[] = new Ad(20, "D.swf", 5);
// Add ads to banner
$banner = new Banner($ads);
// You can also add addional ads
$banner->add(new Ad(10, "E.swf"));
echo "<pre>";
//Lets Emulate first 100 rotations
for($i = 0; $i < 1000; $i ++) {
// Select Algorithm
$banner->randomise("ratioShuffle");
// Display Add
echo $banner->getDisplay(), PHP_EOL;
}
Simple Shuffle Function that can be used
function fisherYatesShuffle(array &$items) {
for($i = count($items) - 1; $i > 0; $i --) {
$j = #mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
}
function robinShuffle(array &$items) {
usort($items, function ($a, $b) {
$a = $a->getDisplay();
$b = $b->getDisplay();
return $a == $b ? 0 : ($a < $b ? - 1 : 1);
});
}
function ratioShuffle(array &$items) {
static $called = false;
if ($called === false) {
$ads = array();
foreach ( $items as &$ad ) {
for($i = 0; $i < $ad->getRatio(); $i ++) {
$ads[] = $ad;
}
}
$called = true;
$items = $ads;
}
shuffle($items);
}
Classes Used
class Ad implements JsonSerializable {
private $impressions;
private $media;
private $ratio = 1;
private $display = 0;
function __construct($impressions, $media = null, $ratio = 1) {
$this->impressions = $impressions;
$this->media = $media;
$this->ratio = $ratio;
}
function torch() {
$this->impressions --;
$this->display ++;
}
public function getImpression() {
return $this->impressions;
}
public function getDisplay() {
return $this->display;
}
public function getRatio() {
return $this->ratio;
}
public function getMeadia() {
return $this->media;
}
public function __toString() {
return json_encode($this->jsonSerialize());
}
public function jsonSerialize() {
return get_object_vars($this);
}
}
class Banner implements Countable, JsonSerializable {
private $totalImpressions;
private $ads = array();
function __construct(array $ads) {
foreach ( $ads as $ad )
$this->add($ad);
}
public function add(Ad $ad) {
$this->ads[] = $ad;
$this->totalImpressions += $ad->getImpression();
}
public function randomise($function = null) {
if (is_callable($function, false, $callable_name)) {
return $callable_name($this->ads);
} else {
return shuffle($this->ads);
}
}
public function getDisplay() {
foreach ( $this->ads as &$ad ) {
if ($ad->getImpression() < 1) {
unset($ad);
continue;
}
$ad->torch();
break;
}
return isset($ad) ? $ad : null;
}
public function jsonSerialize() {
$array = $this->ads;
foreach ( $array as &$ad ) {
$ad = $ad->jsonSerialize();
}
return $array;
}
public function __toString() {
return json_encode($this->jsonSerialize());
}
function count() {
return count($this->ads);
}
}
As you can see this is an example .... Just try and make your solution flexible
Personally, I'd work out what percentage of impressions each ad has received compared to how many are paid for, and use that as the chance that it won't show up. Something like this:
$show = Array();
foreach($ads as $id=>$ad) {
$show[$id] = ceil((1-$ad['impressions']/$ad['paid'])*100);
}
$total = array_sum($show);
$rand = rand(1,$total);
$winner = -1;
do {$rand -= array_shift($show); $winner++;} while($rand && $show);
$ad_to_display = $ads[$winner];
For example, consider four ads, A, B, C and D. All of them have paid for 1,000 impressions, but so far A has been unlucky and gotten zero, while B and C both have had 500 impressions, and D has had 999.
This would mean $show has these values for the ads:
A: ceil((1-0/1000)*100) = 100
B: ceil((1-500/1000)*100) = 50
C: ceil((1-500/1000)*100) = 50
D: ceil((1-999/1000)*100) = 1
$total is therefore equal to 201.
$rand can be any number from 1 to 201 inclusive. Let's say 141.
In this case, we begin our loop:
$rand -= 100, now it's 41. 41 is truthy and we have ads remaining.
$rand -= 50, now it's -9. It has reached zero, so end the loop.
The $winner is 1, which is advert B.