#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5

    Display Array 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
    foreach (glob("array/*.php/{$v}") as $key => $val)
                                    
    {
        echo $ 
    key =>$val;

    }
    ?>
    Any help much appreciated.

    Volterony
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    the products should be in a database, not hard coded into some PHP script.

    Then you can select any data you want at any time.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5
    Originally Posted by Jacques1
    Hi,

    the products should be in a database, not hard coded into some PHP script.

    Then you can select any data you want at any time.
    Hi there, I totally agree with you. Unfortunately these are the parameters I need to work with. I could easily create two tables, one for brands and the other for deals and just do select statement for the specific data,

    However, I am very restricted so this is what I have to work with, so looking for a workaround.

    Cheers
    Volterony
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,031
    Rep Power
    535
    Use three files. File 1 just includes the array definition. Then file 2 and 3 can require_once() the first file.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5
    Ok what if I include the array.php in the nike.php page (which will obviously display all product from both brands:

    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>";  
    }  
    ?> 
    So in the Nike.php (which has array.php included) how could I changed the loop so that it only displays the Nike products and ignores the addidas?

    Cheers
    Volterony
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by volterony
    Unfortunately these are the parameters I need to work with.
    May I ask why that is? A few months ago you did consider to use a database.

    Because, really, storing products in an array in a PHP script and constantly updating it is just horrible. Forget one comma just once, and your whole website crashes.

    If this is your own website, grab a few dollars and switch to a real webhoster with MySQL support. If you're doing this for someone else, try to explain to your boss/customer why this is a really, really bad idea: it's slow, it's dangerous, and it's prone to data corruption.

    Any school kid has access to a database. So you should have, too.

    If there's really no chance to do this right, then you should at least put the data into an external file with a proper data format (JSON, YAML, XML, whatever). This is still slow and fragile, but at least you won't be fumbling with core scripts all the time.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5
    Originally Posted by Jacques1
    May I ask why that is? A few months ago you did consider to use a database.

    Because, really, storing products in an array in a PHP script and constantly updating it is just horrible. Forget one comma just once, and your whole website crashes.

    If this is your own website, grab a few dollars and switch to a real webhoster with MySQL support. If you're doing this for someone else, try to explain to your boss/customer why this is a really, really bad idea: it's slow, it's dangerous, and it's prone to data corruption.

    Any school kid has access to a database. So you should have, too.

    If there's really no chance to do this right, then you should at least put the data into an external file with a proper data format (JSON, YAML, XML, whatever). This is still slow and fragile, but at least you won't be fumbling with core scripts all the time.

    Jacques1 thanks for getting back. Before I began this specific task, I suggested using a database, however I was told it was "a rubbish idea, and that it would be too slow." I am working with a 'senior' php developer and I am nowhere near experienced enough to argue with him, therefore I have gone ahead with the array idea for this specific task.

    I totally understand how this problem would be easily solved by a DB, but I personally don't think that 'he' wants me to mess with his code. We both use git and he's got the master repository, so any commits I do needs to be checked by him.

    Don't want to go on and on and on...... but I'm pretty stuck with this.

    Cheers for all the help and time for reading this.

    Volterony
  14. #8
  15. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    So in the Nike.php (which has array.php included) how could I changed the loop so that it only displays the Nike products and ignores the addidas?
    Don't copy and paste the array into Nike.php and Addidas.php, put it in its own file, data.php, that does not include anything except the array:
    PHP Code:
    return 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")),           
    ); 
    Then in your Nike.php and Addidas.php files, use an include on the file:
    PHP Code:
    $deals = include('data.php'); 
    Duplicating the array will be a data maintenance nightmare (even more so than not using a database).

    Then changing your foreach loop is straight forward. You are fixing one dimension to the array, so you do not need the outer foreach loop to iterate over it. Instead, you just hard-code the array index that you are fixing.

    PHP Code:
    $brand 'Nike';
    echo 
    "<div class=\"header\"><h2>$brand:</h2><br>";  
    echo 
    "<div id=\"dealwrapper\">";  
    foreach (
    $deals[$brand] as $items)  
    {  

    }  
    echo 
    "</div>"
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  16. #9
  17. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    312
    Originally Posted by volterony
    ...I was told it was "a rubbish idea, and that it would be too slow." I am working with a 'senior' php developer and I am nowhere near experienced enough to argue with him, therefore I have gone ahead with the array idea for this specific task...
    Wow, that's one arrogant / dumb dude... I know you're looking for answers on the PHP side (the includes which others have mentioned are sounding like a pretty good move to me), but I wanted to mention some work stuff too. I would be looking to get out of that company asap. Get as much experience as you can while you are there, but look for a better place to work. If that 'senior php developer' is their standard, then you wont learn much and you personally don't want to be associated with a place like that - it reflects badly on you too. Companies get a reputation within the industry, and that tends to get put on the staff that work there too.

    Depending on the "political" climate in your workplace, you might want to have a chat with someone more senior than that developer about your concerns. If it's a bit of a boys club, then maybe just sit back and bare with it until you find a better place.

    I feel for you dude, there is nothing worse then knowing what you are doing is not right, but being powerless to do anything about it.

    Comments on this post

    • Jacques1 agrees
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5
    Originally Posted by sir_drinxalot
    Wow, that's one arrogant / dumb dude... If that 'senior php developer' is their standard, then you wont learn much and you personally don't want to be associated with a place like that - it reflects badly on you too......

    If it's a bit of a boys club, then maybe just sit back and bare with it until you find a better place.

    I feel for you dude, there is nothing worse then knowing what you are doing is not right, but being powerless to do anything about it.
    sir_drinxalot, you are bang on there. Yes unfortunately, he's in with the foundations so I need to be proactive and do more self development!! It is very easy to doubt yourself when working with someone who thinks they are better than everyone, and I mean everyone!!!!!

    But I'm not going to waste my time and energy worrying about it and I need to focus on what I can do for myself.

    I love programming, and no doubt I will meet other people like that in my programming journey, but it is dangerous when you start to define yourself by the way others see you!!! Hopefully people will learn from this experience and be vigilant.

    Volterony
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5
    Originally Posted by E-Oreo
    Don't copy and paste the array into Nike.php and Addidas.php, put it in its own file, data.php, that does not include anything except the array:
    PHP Code:
    return 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")),           
    ); 
    Then in your Nike.php and Addidas.php files, use an include on the file:
    PHP Code:
    $deals = include('data.php'); 
    Duplicating the array will be a data maintenance nightmare (even more so than not using a database).

    Then changing your foreach loop is straight forward. You are fixing one dimension to the array, so you do not need the outer foreach loop to iterate over it. Instead, you just hard-code the array index that you are fixing.

    PHP Code:
    $brand 'Nike';
    echo 
    "<div class=\"header\"><h2>$brand:</h2><br>";  
    echo 
    "<div id=\"dealwrapper\">";  
    foreach (
    $deals[$brand] as $items)  
    {  

    }  
    echo 
    "</div>"
    Awesome, this is exactly what the solution was, however I was unsure how to tweak the foreach loop to filter by brand only.

    Thanks for your time to help me with this.

    Volterony

IMN logo majestic logo threadwatch logo seochat tools logo