Reputation:
I'd like to have some class that has hold all about item that want to be sell, there are many item such as design, product, default product etc. and all product wanted to be hold in one database, so i can easily has a connection to table images, one to many.
so I created a class that would be hold all data in the item
table...
the class has to be know what kind of product it is.
but the problem is when a certain column is undefined in function, but it is defined in script.
here is the MySQL table...
create table item (
itemID int not null auto_increment,
itemDiscount int default 0,
orderCount int default 0,
itemName text,
designID int,
embedID int,
productID int,
defaultProductID int,
undesignID int,
.
.
-- constraint declaration
);
itemDiscount
is for discount of productdesignID
is for description of the design (constraint with design
table) productID
is for the product from user and it already designed (constraint with product
table) undesignID
is product from user but haven't designed (constraint with undesign
table) defaultProductID
is for product that has provided by the web. (constraint with defaultPorduct
table) embedID
is for embedding either designID
+
defaultProductID
or designID
+ undesignID
that has been organized in embed
tableI have inserted some data, here is the data (only for debugging purposes)
itemID ... designID productID defaultProductID undesignID embedID 1 1 2 1 3 1 4 1
class item{
public $type;
.
.
/**
* Determine what kind of product in certain row.
* @param array $row
* @return string
*/
static function itemType($row){
if(!is_null($row["productID"])){
return "product";
}else if(!is_null($row["defaultProductID"])){
return "default";
}else if(!is_null($row["designID"])){
return "design";
}else if(!is_null($row["embedID"])){
$query = "select * from embed where embedID={$row["embedID"]}";
$result = execute($query);
$row = mysqli_fetch_assoc($result);
return "embed_" . self::itemType($row);
}
}
function __construct(){
//do stuff
}
}
so i tried my static function :
$query = "select * from item";
$result = execute($query);
while($row = mysqli_fetch_assoc($result)){
echo item::itemType($row) . "<br/>";
//debug($row);
echo $row["defaultProductID"] . "<br/>";
}
And this is I got:
design default 1 product Notice: Undefined index: defaultProductID in C:\Users\jodi\Documents\Visual Studio 2013\Projects\Prodegitv2\Prodegitv2\includes\item.php on line 17 embed_design
Why I got that kind of notice?
1
is printed, so index defaultProductID
has to be defined.
so I checked locals in debugging mode, here is the link for screenshot: http://postimg.org/image/ssfqv5f6h/
in visual studio debug mode, defaultProductID
is defined...
Why does $row["defaultProductID"]
be undefined in function??
is there any alternative solution??
Upvotes: 0
Views: 45
Reputation: 2631
Your problem is with this code
}else if(!is_null($row["embedID"])){
$query = "select * from embed where embedID={$row["embedID"]}";
$result = execute($query);
$row = mysqli_fetch_assoc($result); <-----
return "embed_" . self::itemType($row); <-----
}
You assign the query result of your query to the embed table to the $row
variable overwriting what was in there before (the data of the item table). Then you call the itemType function again with your new $row
data (from embed table). I guess there is no defaultProductId field in the embed table. This is the reason your code fails.
Upvotes: 2
Reputation: 2452
Try changing these lines:
$row = mysqli_fetch_assoc($result);
return "embed_" . self::itemType($row);
to:
$res = mysqli_fetch_assoc($result);
return "embed_" . self::itemType($res);
Upvotes: 1