Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Shelby Sarisky
6,536 PointsCreating an If..Else Statement that Calls multiple arrays
I'm having trouble figuring out how to call multiple arrays in an if statement without having to duplicate my code. Here is a simplified version what I have in my arrays file:
$products = array(); $products[116] = array( "name" => "name", "model" => "model", "for_use_with" => "for use with", "description" =>"description, "dimensions" => "dimensions", "includes" => "includes" );
Now in my Php file where I'm calling my arrays, I want to create an if statement for certain products to display additional information when users click on a certain product.
<?php if( $product_id == 116 ){ echo "The product information "; } else { echo""; } );
What I'm trying to do is echo the product information for multiple arrays. I've tried concantenating like this: if( $product_id == "116" , "117" , "118" ){ and just using commas in between the product arrays..I'm lost, I know that i shouldn't duplicate the same code for all three.. I'm in need of some direction
Thanks!
2 Answers
Nicholas Wimsatt
4,633 PointsSounds like a loop would be needed for each $product_id. I am thinking if a user selects multiple products then you can store each product id in an array and just use a foreach loop to display each product's information.
Something like:
$product_selection = array(116, 117, 118);
foreach($product_selection as $product) { echo "Name: " . $products[$product]["name"] . " Description: " . $products[$product]["description"]; }
Shelby Sarisky
6,536 PointsI'm already echoing out the information that I need for each product. This is my code that I don't want to have duplicated.
if( $product_id == 116){
echo "Some information
";
}
if( $product_id == 117){
echo " Some information
";
}
if( $product_id == 118){
echo "Some Information
";
}
?>
I guess to really simplify what I'm asking is: Instead of echoing the same thing out for 116, 117, and 118, how do I use the one echo statement for all 3 arrays? Writing this out is really confusing. Sorry.
The variations of this that I've tried do not work work: <?php if( $product_id == "116" , "117" , "118"){ echo "Some Information "; }
Shelby Sarisky
6,536 PointsShelby Sarisky
6,536 PointsI don't think that will work because in of my products arrays I have different information for each, ie. 2 of my arrays have the same information as each other while the other 3 have the same information but different than the first two, (I hope that makes sense). Which is why I created an if else statement to echo out information that is specific to those 3 products.
$products = array(); $products[116] = array( "name" => "name", "model" => "model", "for_use_with" => "for use with", "description" =>"description, "dimensions" => dimensions", "includes" => "includes" );
$products[117] = array( "different name" => "different name", "different model" => "different model", "different for_use_with" => "different for use with", "different description" =>"different description, "different dimensions" => "different dimensions", "different includes" => "different includes" );
$products[118] = array( "different name" => "different name", "different model" => "different model", "different for_use_with" => "different for use with", "different description" =>"different description, "different dimensions" => "different dimensions", "different includes" => "different includes" );
So if the user clicks on either product 117 or 118 bringing them to a specific product page it will show the additional information for those two product pages and not on the product page for product 116.
I don't want to add the additional information to the array because I would like it to be separately styled because it is not product specific.
I hope that makes more sense.. Thank you for your response Nicholas.
Nicholas Wimsatt
4,633 PointsNicholas Wimsatt
4,633 PointsI think I understand but may need clarified. In your example if a user clicks on product 117 or 118 it will bring them to the same product page is that correct?
If that is the case then the two products share some similar information that can be tied together on the same product page like if both products are "for_use_with" => "Woodworking" then either product when clicked on will send them to the "Woodworking" product page and show other products with "Woodworking" attributes.
Kind of like product categories.
Shelby Sarisky
6,536 PointsShelby Sarisky
6,536 PointsYes, each array contains different information for each individual product, although a few products have the same additional information.
So product 116 117 and 118 are individual product with different specifications that should should the same additional information.
Nicholas Wimsatt
4,633 PointsNicholas Wimsatt
4,633 PointsSomething like this could be used to pull up the other products in the array with the same additional information. They would need to share the same exact spelling to show up in the results however.
$user_product_choice = 117;
$product_category = $products[$user_product_choice]["for_use_with"];
foreach($products as $product) { if($product["for_use_with"] == $product_category) { echo "Name: " . $products[$product]["name"] . " Description: " . $products[$product]["description"]; } }