Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional PHP Design Patterns
This is the forum to discuss the Wrox book Professional PHP Design Patterns by Aaron Saray ISBN: 978-0-470-49670-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional PHP Design Patterns section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 28th, 2010, 05:26 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Oklahoma City, Oklahoma, USA.
Posts: 249
Thanks: 0
Thanked 0 Times in 0 Posts
Default iterator Pattern

Hello,
I am working through the iterator pattern but when I try to loop through the array to build the table, I am getting Notice: Trying to get property of non-object errors.

I am using information from a database that I have build, same type of relationship $name, $email, $communityGear. So If I do a var_dump I am getting the following.


object(searchBySolorooerIterator)#1 (2) { ["__solorooerDetails:private"]=> array(1) { [0]=> object(solorooer)#4 (3) { ["name"]=> string(10) "Mike" ["email"]=> string(27) "fakeemail@email.com" ["communityGear"]=> array(2) { [0]=> string(7) "Doritos" [1]=> string(5) "Water" } } } ["__valid:private"]=> bool(false) }


Here is my construct function, I did things a little differently, but it should work.

Code:
	public function __construct($email) {
	
		$sql = "SELECT user.solo_ID, user.solo_Name, user.solo_Email, bbq.bbq_ItemName 
				FROM user 
				LEFT JOIN bbq
				ON user.solo_ID = roo_bbq_beerbq.bbq_SoloID 
				WHERE user.solo_Email = ?";
				
		$stmt = $conn->prepare($sql);
		$stmt->bind_param("s", $email);
		$stmt->execute();
		
		$solorooerID = 0;
		$solorooer = NULL;
		
		$stmt->bind_result($solo_ID, $solo_Name, $solo_Email, $bbq_ItemName);
		
		while ($stmt->fetch()) {
			if ($solo_ID !== $solorooerID) {
				if (!is_null($solorooer)) {
					$this->__solorooerDetails[] = $solorooer;
				}
				$solorooerID = $solo_ID;
				$solorooer = new solorooer($solo_Name, $solo_Email);
			}
			
			$solorooer->addGear($bbq_ItemName);
			
		}
		
		$this->__solorooerDetails[] = $solorooer;
		
		$stmt->free_result();
		$stmt->close();
		$conn->close();

	}
Thanks In Advanced

Mike Moore
__________________
Peace
Mike
http://www.eclecticpixel.com

Last edited by harpua; December 29th, 2010 at 05:47 PM..
 
Old December 29th, 2010, 05:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Oklahoma City, Oklahoma, USA.
Posts: 249
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If I do this it works, it is like there is another layer of array deminsion being added.

Code:
foreach($rooers as $cd) {
	//$cd is an array and cannot access the "name" property
	foreach($cd as $cd2) {
		echo $cd2->name; //this works
	}
}
__________________
Peace
Mike
http://www.eclecticpixel.com
 
Old January 5th, 2011, 10:29 PM
Wrox Author
Points: 118, Level: 2
Points: 118, Level: 2 Points: 118, Level: 2 Points: 118, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2009
Location: Milwaukee, WI
Posts: 15
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Hey - if you can post the rest of your class, that might be helpful. I'm quite interested to know how you implemented the methods that the iterator interface requires. That may hold a bit of clue.

Finally - a bit of description about some of the business logic may also help us get to the bottom. :) thx!
__________________
-aaron
--
aaronsaray.com || <-- yeah... try it.
 
Old January 5th, 2011, 11:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Oklahoma City, Oklahoma, USA.
Posts: 249
Thanks: 0
Thanked 0 Times in 0 Posts
Default

here is the full code I have and how I am getting it to work.
I am pulling info from a database I have and formatting it to fit the book example, so ignore the fieldnames.

Code:
<?php
class CD {
	public $band = '';
	public $title = '';
	public $trackList = array();

	
	public function __construct($band, $title) {
		$this->band = $band;
		$this->title = $title;
	}

	public function addTrack($track) {
		$this->trackList[] = $track;
	}
}

class CDSearchByBandIterator implements Iterator {
	private $__CDs = array();
	private $__valid = FALSE;
	
	public function __construct($bandName) {
	
		require_once 'connection.inc.php';
		
		$sql = "SELECT user.ID, user.Name, user.Email, bbq.ItemName 
				FROM user 
				LEFT JOIN bbq 
				ON user.ID = bbq.bbqID 
				WHERE user.solo_Email = ?";
				
		$stmt = $conn->prepare($sql);
		$stmt->bind_param("s", $bandName);
		$stmt->execute();
		
		$cdID = 0;
		$cd = NULL;
		
		$stmt->bind_result($id, $band, $title, $tracktitle);
		
		while ($stmt->fetch()) {
			if ($id !== $cdID) {
				if (!is_null($cd)) {
					$this->__CDs[] = $cd;
				}
				$cdID = $id;
				$cd = new CD($band, $title);
			}
			
			$cd->addTrack($tracktitle);
			
		}
		
		$this->__CDs[] = $cd;
		
		$stmt->free_result();
		$stmt->close();
		$conn->close();

	}
	
	public function next() {
		$this->__valid = (next($this->__CDs)===FALSE) ? FALSE : TRUE;
	}
	
	public function rewind() {
		$this->__valid = (reset($this->__CDs)===FALSE) ? FALSE : TRUE;
	}
	
	public function valid() {
		return $this->__valid;
	}
	
	public function current() {
		return $this->__CDs;
	}
	
	public function key() {
		return key($this->__CDs);
	}
}


?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<title>Untitled</title>
</head>
<body>
<?php

$queryItem = 'email@example.com';
$cds = new CDSearchByBandIterator($queryItem);

foreach($cds as $cd) {
	foreach($cd as $cd2) {
		echo $cd2->band;
	}
}

/*
print '<h1>Found the following</h1>';
print '<table cellpadding="0">';
print '<tr><th>Band</th><th>Title</th><th>Num Tracks</th></tr>';

foreach($cds as $cd) {
	print '<tr>'.$cd->band.'<td>'.$cd->title.'</td>';
	print '<td>'.count($cd->trackList).'</td></tr>';
}

print '</table>';
*/
?>
</body>
</html>
__________________
Peace
Mike
http://www.eclecticpixel.com
 
Old January 5th, 2011, 11:24 PM
Wrox Author
Points: 118, Level: 2
Points: 118, Level: 2 Points: 118, Level: 2 Points: 118, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2009
Location: Milwaukee, WI
Posts: 15
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Hi - try this code:

PHP Code:
public function current() {
        return 
current($this->__CDs);
    } 
Let me know if that works for you. If it does, it may be a bug in the example code. Thanks!
__________________
-aaron
--
aaronsaray.com || <-- yeah... try it.
 
Old January 5th, 2011, 11:40 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Oklahoma City, Oklahoma, USA.
Posts: 249
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That Worked! Many Thanks, very good book by the way, very easy to understand compared to some others I have tried.
__________________
Peace
Mike
http://www.eclecticpixel.com




Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: Arrays using JavaScript,/JSP Iterator in IE celticbhoy Javascript How-To 3 December 11th, 2007 10:55 AM
Iterator Scope Problem smokey_gun Javascript How-To 1 July 4th, 2007 01:26 AM
iterator..... s Larryz C# 2005 0 May 9th, 2007 04:06 PM
DirectoryInfo.GetFiles(pattern): search pattern fo arif_1947 VS.NET 2002/2003 1 October 19th, 2004 11:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.