SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display Array Items in Another PHP page

    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.


    PHP Code:
    <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 Code:
    <?php 
    foreach (glob("array/*.php/{$v}") as $key => $val
                                     

        echo $ 
    key =>$val


    ?>

    Any help much appreciated.

    Volterony

  2. #2
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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?

  3. #3
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    This seems like information that should be stored in a db and then retrieved when needed...

  5. #5
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    This seems like information that should be stored in a db and then retrieved when needed...
    True!

    Let's say, nike.php
    PHP Code:
    <?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>";

    ?>

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Why not this:

    Code:
    tbl_products
    --------
    id
    product_name
    product_brand
    product_type
    rest of your product information...
    Code PHP:
    $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:

    Code PHP:
    //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>";
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •