PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 1st, 2013, 07:02 AM
volterony volterony is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 60 volterony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 54 m 32 sec
Reputation Power: 3
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

Reply With Quote
  #2  
Old February 1st, 2013, 07:08 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,875 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 35 m 16 sec
Reputation Power: 813
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.

Reply With Quote
  #3  
Old February 1st, 2013, 07:11 AM
volterony volterony is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 60 volterony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 54 m 32 sec
Reputation Power: 3
Quote:
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

Reply With Quote
  #4  
Old February 1st, 2013, 07:21 AM
NotionCommotion NotionCommotion is offline
Contributing User
Click here for more information.
 
Join Date: Sep 2006
Posts: 1,464 NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 6 h 21 m 36 sec
Reputation Power: 526
Use three files. File 1 just includes the array definition. Then file 2 and 3 can require_once() the first file.

Reply With Quote
  #5  
Old February 1st, 2013, 09:17 AM
volterony volterony is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 60 volterony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 54 m 32 sec
Reputation Power: 3
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

Reply With Quote
  #6  
Old February 1st, 2013, 10:26 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,875 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 35 m 16 sec
Reputation Power: 813
Quote:
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.

Reply With Quote
  #7  
Old February 1st, 2013, 10:43 AM
volterony volterony is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 60 volterony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 54 m 32 sec
Reputation Power: 3
Quote:
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

Reply With Quote
  #8  
Old February 1st, 2013, 05:19 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,944 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 10 h 16 m 54 sec
Reputation Power: 7053
Quote:
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
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
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

Reply With Quote
  #9  
Old February 2nd, 2013, 02:05 AM
sir_drinxalot's Avatar
sir_drinxalot sir_drinxalot is offline
Known to taste like chicken
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: In front of my computer
Posts: 377 sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level)sir_drinxalot User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 3 h 27 m 44 sec
Reputation Power: 293
Send a message via MSN to sir_drinxalot
Quote:
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?

Reply With Quote
  #10  
Old February 3rd, 2013, 06:53 AM
volterony volterony is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 60 volterony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 54 m 32 sec
Reputation Power: 3
Quote:
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

Reply With Quote
  #11  
Old February 3rd, 2013, 06:56 AM
volterony volterony is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 60 volterony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 54 m 32 sec
Reputation Power: 3
Quote:
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Display Array in another PHP Page

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap