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!
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:
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"
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"
<?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()
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...*