I have created an array of data from mysql database. This is how that array looks like:
// Fetch all the records:
while ($stmt->fetch()) {
$output = "<a>
";
$output .= "<h3>{$product_name}</h3>
";
$output .= "<span class='price'><span class='amount'>BD.{$price}</span></span>
";
$output .= "</a>
";
$output .= "<div class='short_desc'>
";
$output .= "$product_des
";
$output .= "</div>
";
//Add output to array
$products[] = $output;
}
Since I want to use this array values from outside my while
loop and This is how I use this $products[]
array in my page.
echo $products[0];
echo $products[1];
echo $products[2];
echo $products[3];
My question is if this $products[]
array have one result set I can get an error.
My error message is like this: An error occurred in script 'C:\wamp\www\Computer\index.php' on line 208: Undefined offset: 2
So I tried to fix this problem using array_key_exists()
function like this way for each echo:
if(!empty($products) && array_key_exists("$products[1]", $products)) echo $products[1]; else echo "No Product";
But still I can get error. Can anybody tell me what is wrong with this?
Thank you.