Hi there,
I would like to be able to display multidimensional array items on another page.
I already have an array.php with the following, which also has the css etc.
<style type="text/css">
css for the deals is here
</style>
$deals = array(
"Nike"=> array(
array('productTitle' => "Nike Trainers 1",'productImage' => "deals/nike1.jpg",'productDescription' => " Fantastic Savings on Nike Trainers",'rrp' => "£69.99",'salePrice' => "£60.00"),
array('productTitle' => "Nike Trainers 2",'productImage' => "deals/nike2.jpg",'productDescription' => " Fantastic Savings on Nike Trainers",'rrp' => "£69.99",'salePrice' => "£60.00")),
"Addidas"=> array(
array('productTitle' => "Addidas Boots 1",'productImage' => "deals/addidas1.jpg",'productDescription' => " Fantastic Savings on Addidas Boots",'rrp' => "£69.99",'salePrice' => "£60.00"),
array('productTitle' => "Addidas Boots 2",'productImage' => "deals/addidas2.jpg",'productDescription' => " Fantastic Savings on Addidas Boots",'rrp' => "£69.99",'salePrice' => "£60.00")),
);//close $deals array
foreach ($deals as $brand =>$items)
{
echo "<div class=\\"header\\"><h2>$brand:</h2><br>";
echo "<div id=\\"dealwrapper\\">";
foreach ($deal as $items)
{
}
echo "</div>";
}
?>
This displays all the brands and deals on the one page. However I also have individual .php pages for both Nike and Addidas, and I would like to be able to display only the Nike items from the main array in Nike.php and Addidas items in Addidas.php??
I have tried this so far, which is probably way off the mark
PHP Code:
<?php
foreach (glob("array/*.php/{$v}") as $key => $val)
{
echo $ key =>$val;
}
?>
Any help much appreciated.
Volterony
You could just have a filter and just one file.
You may have listing.php?brand=nike or listing.php (for a full listing)
<?php
$CURRENT = isset( $_GET['brand'] ) ? $_GET['brand'] : '';
$deals = array(
// [ ... ]
);
if( false == empty($CURRENT) && isset($deals[$CURRENT]) ) {
$arrayToUse = array( $CURRENT => $deals[$CURRENT] );
} else {
$arrayToUse = $deals;
}
foreach ($deals as $brand =>$items) {
// [ ... ]
}
?>
if you need to have two files, just make a common file, like “brands.php” that will have the brands array and include it in your other files.
I don’t get the point, what’s with that glob?
What if I were to include the array.php in both the nike.php and addidas.php, which will obviously display the products from both brands. However what would I need to write in say the Nike.php to just display the items from the included array.php?
Cheers
This seems like information that should be stored in a db and then retrieved when needed…
True!
Let’s say, nike.php
<?php
require 'common_brands.php';
// now we have $deals
echo "<div class=\\"header\\"><h2>Nike</h2><br>";
echo "<div id=\\"dealwrapper\\">";
foreach ($deals['Nike'] as $items) {
echo 'Whatever you need '.$items['productTitle'];
}
echo "</div>";
?>
Why not this:
tbl_products
--------
id
product_name
product_brand
product_type
rest of your product information...
$db = new PDO('mysql:host=127.0.0.1;dbname=mydb', 'username', 'pass');
$statement = $db->prepare("select * from tbl_products where product_brand = 'Nike'");
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $product) {
echo $product['product_name'] . "</br>";
}
Then you can start getting into re-usable classes where common statements are already written and it becomes something like this, with less code to change when you alter your designs:
//define your products class, which will extend your db connection class
$result = new products(array('brand' => 'Nike'));
foreach($result as $product) {
echo $product['product_name'] . "</br>";
}