5 Replies - 335 Views - Last Post: 07 June 2012 - 04:29 PM Rate Topic: -----

#1 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Get value is being incremented/decremented once too many

Posted 07 June 2012 - 02:43 PM

I've been giving OOP with PHP a go lately and I think this is the root of my problem right now, since PHP is a stateless language. Alright, so my problem is I have setup two buttons, one called back, the other called next. The purpose of these buttons is to traverse forward and backwards to different pages of an image gallery. When I click the buttons they update $_GET['page'] properly, but if I click one button and then click the other button on the page reload the value is updated in the wrong direction. For example, I'm on page 0, I click next, I'm now on page 1, I click back, I'm taken to page 2 for some reason. If I click the back button again though the page variable will be updated in the correct direction. I've been studying my code for at least 2 hours and I'm about to pull my hair out, I think I'm just too close to the project at this point. If someone could take a look at my code and find out why this is happening I will be extremely grateful. My code is bellow:

<?php
include_once '../Debug.php';

class PageGenerator {
	const DEFAULT_PAGE_SIZE = 10;
	const DEFAULT_ROW_SIZE = 2;
	const MIN_PAGE_SIZE = 4;
	const MIN_ROW_SIZE = 2;
	private static $instance = null;
	private $visible;
	private $picsPerRow;
	private $picsPerPage;
	private $numPictures;
	private $pictureStack;
	private $loadedPictures = array();
	private $currentPage;
	private $page = 0;
	private $skip;
	
	/*
	 * Constructor, does constructor things.
	 */
	private function __construct($pictures, $args) {
		$this->page = $this->processButtons();
		unset($_GET['page']);
		$this->pictureStack = $this->extractPictures($pictures); 
		$this->numPictures = sizeof($this->pictureStack);
		$this->extractArgs($args);
		
	}
	
	public static function getInstance($pictures, $args) {
		if(self::$instance == null) {
			self::$instance = new PageGenerator($pictures, $args);
		}
		
		return self::$instance;
	}
	
	private function extractPictures($pictures) {
		$temp = array();
		
		for($i=0; $i < sizeof($pictures); $i++) {
			$temp[$i] = $pictures[$i];
		}
		
		return $temp;
	}
	
	/*
	 * Private function used to extract the arguements from the $args parameter in
	 * the constructor. If no keys match the required name, default values are assigned.
	 */
	private function extractArgs($args) {
		if(is_array($args)) {
			foreach($args as $key=>$value) {
				if($key == 'picsPerRow') {
					$this->picsPerRow = $value;
				}
				
				if($key == 'picsPerPage') {
					$this->picsPerPage = $value;
				}
			}
		}
		
		if(!isset($this->picsPerPage)){
			$this->picsPerPage = PageGenerator::DEFAULT_PAGE_SIZE;
		}
		
		if(!isset($this->picsPerRow)){
			$this->picsPerRow = PageGenerator::DEFAULT_ROW_SIZE;
		}
		
		if($this->picsPerRow > $this->picsPerPage) {
			$this->picsPerRow = $this->picsPerPage;
		}
	}

private function buildButtonPanel() {
		?> <div class="buttonDiv">
			<form action="" method="get">
			<input type="hidden" name="page" value="<?php echo $this->page; ?>" />
			<input type="submit" name="back" value="back" />
		    <input type="submit" name="next" value="next" />
		    </form>
			</div>
		<?php
	}
	
	private function getPage() {
		if(!isset($_GET['page'])) {
			return 0;
		}
		
		return $_GET['page'];
	}
	
	private function processButtons() {
		Debug::printArray($_GET);
		
		if(isset($_GET['next'])) {
			$_GET['page']++;
		}
		
		if(isset($_GET['back'])) {
			$_GET['page']--;
		}
		return $_GET['page'];
	}
	
	public function buildPage() {
		echo '<div class="photobody">'
			.'<table>';
		$total = 0;
		
		for($i=0; $i<=$this->picsPerPage; $i++) {
			echo (!($total >= $this->picsPerPage) && !($total >= $this->numPictures)) ? '<tr>': '';
			for($x=0; $x<$this->picsPerRow; $x++) {
				if(!($total >= $this->numPictures) && !($total >= $this->picsPerPage)) {
					echo '<td>'
						.'<img src="' . $this->getPictureAt($total) . '" />'
						.'</td>';
					$total++;
				}
			}
			$this->lastIndex = $total;
			echo (!($total >= $this->picsPerPage) && !($total >= $this->numPictures)) ? '</tr>': '';
		}
		echo '</table></div>';
		$this->buildButtonPanel();
	}
	
	public function getNumberOfPictures() {
		return $this->numPictures;
	}
	
	public function getPicturesPerPage() {
		return $this->picsPerPage;
	}
	
	public function getPicturesPerRow() {
		return $this->picsPerRow;
	}
	
	/*
	 * Makes sure $num is both set and a numeric value, then
	 * assigns the integer value of $num to picsPerPage if and only if $num
	 * is greater than the MIN_PAGE_SIZE. 
	 */
	public function setPicturesPerPage($num) {
		if(isset($num) && is_numeric($num)) {
			$this->picsPerPage = ($num >= MIN_PAGE_SIZE) ? (int)$num : MIN_PAGE_SIZE;
		}
	}
	
	/*
	 * Makes sure $num is both set and a numeric value, then
	 * assigns the integer value of $num to picsPerRow if and only if $num
	 * is greater than the MIN_ROW_SIZE. 
	 */
	public function setPicturesPerRow($num) {
		if(isset($num) && is_numeric($num)) {
			$this->picsPerRow = ($num >= MIN_ROW_SIZE) ? (int)$num : MIN_PAGE_SIZE;
		}
	}
}

$PageGenerator = PageGenerator::getInstance($pictures, array('picsPerRow'=>5, 'picsPerPage'=>10));
$PageGenerator->buildPage();
echo $PageGenerator->toString();


This post has been edited by giggly kisses: 07 June 2012 - 02:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Get value is being incremented/decremented once too many

#2 e_i_pi  Icon User is online

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,663
  • Joined: 30-January 09

Re: Get value is being incremented/decremented once too many

Posted 07 June 2012 - 03:28 PM

Line 164 has this:
$this->picsPerRow = ($num >= MIN_ROW_SIZE) ? (int)$num : MIN_PAGE_SIZE;


...which should probably be this...
$this->picsPerRow = ($num >= MIN_ROW_SIZE) ? (int)$num : MIN_ROW_SIZE;


Not sure if that will help your problem though.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Get value is being incremented/decremented once too many

Posted 07 June 2012 - 03:33 PM

I can't speak for certain about PHP, but in all the languages I do know, the trailing increment (++) takes place AFTER the rest of the call. So in this case the call to $_Get takes place THEN the increment happens. So based on your example, the call to page 1 happens, then increments to page two - then you click back but the call to the page happens first (with the now incremented value), then the decrement happens. So all your calls to $_Get take place with the previously incremented or decremented value.

102        if(isset($_GET['next'])) {
103            $_GET['page']++;


I don't know the PHP syntax for doing the increment before but you could try that.

In C# its the difference between

int x = 5;
int y = x++;// Increment after
and
int y = ++x;// Increment before

Was This Post Helpful? 1
  • +
  • -

#4 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Get value is being incremented/decremented once too many

Posted 07 June 2012 - 03:51 PM

Thanks for the replies. e_i_pi, I'm almost certain you saved me a huge headache down the road. Nice catch.

tlhIn`toq I had tried to put the ++ and -- before the call to $_GET but it didn't change the results. :/ Since it didn't change anything I just changed it back to the post increment since it's the more 'traditional' form of incrementing.

I've been messing around with the code since I posted this topic and still no solution. I don't know what it is about PHP but I'm starting to realize I'm pretty terrible at using it. Not sure if it's due to how loosely typed the language is or that I don't have the proper server side scripting mentality, but I seem to have trouble with the simplest scripts where I could easily write this in a language like Java. *sigh*
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Get value is being incremented/decremented once too many

Posted 07 June 2012 - 03:58 PM

Can you break it down into two statements instead of one?
Increament 'page'
Then call your get operation with the new value

I'm totally guessing at the syntax here but..

           if(isset($_GET['next'])) {
           $page++;
           $_GET['page'];

Was This Post Helpful? 0
  • +
  • -

#6 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Get value is being incremented/decremented once too many

Posted 07 June 2012 - 04:29 PM

The problem with that is when the page reloads (whenever the user clicks the 'next' or 'back' button) $page is reset, so doing the $page++ in the processButtons() method won't do anything since it's called in the constructor of the object. I have tried so many different placements of the call to processButtons() and setting the $page variable, but no luck so far. :/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1