Returning an array of objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
9yards
Forum Newbie
Posts: 2
Joined: Fri Aug 20, 2010 5:48 pm

Returning an array of objects

Post by 9yards »

I am creating a library that searches and returns information from a huge relational database. I have run into a problem with one of my search functions. I cant seem to get this function to return a proper array of objects:

Code: Select all

public function BlueprintSearch($keyword) {
			$db = new EveStaticDb();
			$db->connectDb();
			$q = "SELECT typeID FROM `invTypes` WHERE typeName LIKE '%".$keyword."%'";
			$r = $db->queryDb($q);
			$i = 0;
			while($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
				$db->connectDb();
				$q1 = "SELECT blueprintTypeID FROM `invBlueprintTypes` WHERE productTypeID LIKE ".$row[typeID];
				$r1 = $db->queryDb($q1);
				$row1 = mysql_fetch_array($r1, MYSQL_ASSOC);
				if($row1[blueprintTypeID] != NULL) {
					$bp =  new BlueprintType($row1[blueprintTypeID]);
					$blueprintResult[$i] = $bp;
					$i++;
				}
				
			}
			return $blueprintResult;
		}
When I try to access this array of objects I recieve this error: "Fatal error: Call to a member function get_typeName() on a non-object in /BlueprintType_example.php on line 15"

Line 15:

Code: Select all

echo("<tr><td colspan='4'>".$bpArray->get_typeName()."</td></tr>");
Here is the BlueprintType_example.php

Code: Select all

<?php
			$searchKey = $_GET[blueprint];
			$dbSearch = new SearchDb();
			$bpArray = $dbSearch->BlueprintSearch($searchKey);
			for($i = 0; $i < count($bpArray); $i++) {
				echo("
				<table border='1'>
					<tr><td colspan='4'>".$bpArray->get_typeName()."</td></tr>
					<tr><td>Icons</td>
						<td><img src='".$bpArray[$i]->Icon(16)."' />
						<td><img src='".$bpArray[$i]->Icon(32)."' />
						<td><img src='".$bpArray[$i]->Icon(64)."' />
					</tr>
					<tr><td>Description</td><td colspan='3'>".$bpArray[$i]->get_description()."</td></tr>
					<tr><td>Category</td><td colspan='3'>".$bpArray[$i]->CategoryName()."</td></tr>
					<tr><td>Group Name</td><td colspan='3'>".$bpArray[$i]->GroupName()."</td></tr>
					<tr><td>Market Group</td><td colspan='3'>".$bpArray[$i]->MarketGroupName()."</td></tr>");
					
					$matList = $bpArray[$i]->get_materialList(); // This function returns an array : $mats[n][typeID][quantity] where n is the total number of mats for that item
					$numMats = $bpArray[$i]->get_materialTypeCount();
					echo("<tr><th rowspan='".$numMats."' valign='top' align='left'>Mats</th>\n");
					for ($i = 0; $i < $numMats; $i++) {
						$matItem = new InvType($matList[$i][typeID]);
						if($i != 0)
							echo("<tr>");
						echo("<td>".$matItem->get_typeName()."</td><td colspan='2' align='right'>".$matList[$i][quantity]."</td></tr>\n");
					}
					echo("</tr>
					<tr><td>Base Price</td><td colspan='3'><?php echo($testInv->BasePriceFormatted()); ?></td></tr>
				</table>");
			}
		?>
What am I doing wrong?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Returning an array of objects

Post by Weirdan »

9yards wrote: When I try to access this array of objects I recieve this error: "Fatal error: Call to a member function get_typeName() on a non-object in /BlueprintType_example.php on line 15"

Here is the BlueprintType_example.php

Code: Select all

<?php
			$searchKey = $_GET[blueprint];
			$dbSearch = new SearchDb();
			$bpArray = $dbSearch->BlueprintSearch($searchKey);
			for($i = 0; $i < count($bpArray); $i++) {
				echo("
				<table border='1'>
					<tr><td colspan='4'>".$bpArray->get_typeName()."</td></tr>
					<tr><td>Icons</td>
						<td><img src='".$bpArray[$i]->Icon(16)."' />
						<td><img src='".$bpArray[$i]->Icon(32)."' />
						<td><img src='".$bpArray[$i]->Icon(64)."' />
					</tr>
					<tr><td>Description</td><td colspan='3'>".$bpArray[$i]->get_description()."</td></tr>
					<tr><td>Category</td><td colspan='3'>".$bpArray[$i]->CategoryName()."</td></tr>
					<tr><td>Group Name</td><td colspan='3'>".$bpArray[$i]->GroupName()."</td></tr>
					<tr><td>Market Group</td><td colspan='3'>".$bpArray[$i]->MarketGroupName()."</td></tr>");
					
					$matList = $bpArray[$i]->get_materialList(); // This function returns an array : $mats[n][typeID][quantity] where n is the total number of mats for that item
					$numMats = $bpArray[$i]->get_materialTypeCount();
					echo("<tr><th rowspan='".$numMats."' valign='top' align='left'>Mats</th>\n");
					for ($i = 0; $i < $numMats; $i++) {
						$matItem = new InvType($matList[$i][typeID]);
						if($i != 0)
							echo("<tr>");
						echo("<td>".$matItem->get_typeName()."</td><td colspan='2' align='right'>".$matList[$i][quantity]."</td></tr>\n");
					}
					echo("</tr>
					<tr><td>Base Price</td><td colspan='3'><?php echo($testInv->BasePriceFormatted()); ?></td></tr>
				</table>");
			}
		?>
What am I doing wrong?
Even though $bpArray is an array of objects, it's still an array. You can't call methods on php arrays since they are not objects themselves. What you want (likely) is to call the method on the current element of the array: $bpArray[$i]->get_typeName()
9yards
Forum Newbie
Posts: 2
Joined: Fri Aug 20, 2010 5:48 pm

Re: Returning an array of objects

Post by 9yards »

/facepalm

Well I feel like an idiot. You have no idea how many times I have looked at that over and over and never noticed the missing [$i]... You notice I have that on the rest of the calls.

*9yards puts the dunce cap on and sits in his corner...*
Post Reply