pamelaanne Posted February 6, 2012 Share Posted February 6, 2012 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <?php $sushi = array( 'Fresh Tuna Sushi' => array ( 'Fresh Tuna Fillet' => '8 ounces', 'Sushi Rice' => '2 cups', 'Wasabi Paste' => '2 tablespoons' ), 'Shrimp Sushi' => array ( 'Large Shrimps' => '10 pieces', 'Vinegar' => '2 tablespoons', 'Sushi Rice' => '2 cups', 'Wasabi Paste' => '2 tablespoons', ), 'Nori Crab Sushi' => array ( 'Nori Seaweeds' => '4 sheets', 'Crabmeat' => '8 ounces', 'Cream Cheese' => '8 ounces', 'Worcestershire Sauce' => '3/4 teaspoon', 'Garlic Salt' => '1/4 teaspoon' ), 'Vegetarian Sushi' => array ( 'Rice Vinegar' => '1/2 cup', 'Sesame Oil' => '2 tablespoons', 'Nori Seaweeds' => '4 sheets', 'Sushi Rice' => '2 cups', 'Cucumber Strips' => '1 cup', 'Sweet Potatoes' => '1 cup', 'Sesame Seeds' => '2 tablespoon' ), 'California Roll Sushi' => array ( 'Sushi Rice' => '1 cup', 'Nori Seaweeds' => '8 sheets', 'Sesame Seeds' => '2 tablespoons', 'Crabmeat' => '1/2 cup', 'Mayonnaise' => '3 tablespoons', 'Avocado' => '1 piece', 'Cucumber Strips' => '1/2 cup' ) ); /* foreach($sushi as $type => $value){ //faster than for loop foreach($value as $ingredients => $measurement){ echo '<br/>' . $type . '-' . $ingredients . '=>' . $measurement . '</br>'; } }*/ ?> <table border="1"> <tr> <th>Sushi</th> <th colspan="2">Ingredients</th> </tr> <?php foreach ($sushi as $type => $ingredients){ ?> <tr> <td rowspan="<?php echo count($sushi[$ingredients]) + 1; ?>"> <?php echo $type; ?> </td> <?php foreach ($ingredients as $name => $meas){ ?> <td><?php echo $name;?></td> <td><?php echo $meas;?></td> <?php } ?> </tr> <?php } ?> </table> </body> </html> So here's mhy code in PHP. I'm trying to create a table from an array but I just can't.........get.........it..........right! The output's supposed to look like this: Please help Any help would be graeatly appreciated. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/256532-converting-from-array-to-table/ Share on other sites More sharing options...
pamelaanne Posted February 6, 2012 Author Share Posted February 6, 2012 I have this other code.. Still, can't get the output right.. <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <?php $sushi = array( 'Fresh Tuna Sushi' => array ( 'Fresh Tuna Fillet' => '8 ounces', 'Sushi Rice' => '2 cups', 'Wasabi Paste' => '2 tablespoons' ), 'Shrimp Sushi' => array ( 'Large Shrimps' => '10 pieces', 'Vinegar' => '2 tablespoons', 'Sushi Rice' => '2 cups', 'Wasabi Paste' => '2 tablespoons', ), 'Nori Crab Sushi' => array ( 'Nori Seaweeds' => '4 sheets', 'Crabmeat' => '8 ounces', 'Cream Cheese' => '8 ounces', 'Worcestershire Sauce' => '3/4 teaspoon', 'Garlic Salt' => '1/4 teaspoon' ), 'Vegetarian Sushi' => array ( 'Rice Vinegar' => '1/2 cup', 'Sesame Oil' => '2 tablespoons', 'Nori Seaweeds' => '4 sheets', 'Sushi Rice' => '2 cups', 'Cucumber Strips' => '1 cup', 'Sweet Potatoes' => '1 cup', 'Sesame Seeds' => '2 tablespoon' ), 'California Roll Sushi' => array ( 'Sushi Rice' => '1 cup', 'Nori Seaweeds' => '8 sheets', 'Sesame Seeds' => '2 tablespoons', 'Crabmeat' => '1/2 cup', 'Mayonnaise' => '3 tablespoons', 'Avocado' => '1 piece', 'Cucumber Strips' => '1/2 cup' ) ); /* foreach($sushi as $type => $value){ //faster than for loop foreach($value as $ingredients => $measurement){ echo '<br/>' . $type . '-' . $ingredients . '=>' . $measurement . '</br>'; } }*/ ?> <table border="1"> <tr> <th>Sushi</th> <th colspan="2">Ingredients</th> </tr> <?php foreach ($sushi as $type => $ingredients){ ?> <tr> <td rowspan="<?php echo count($sushi[$ingredients]) + 1; ?>" colspan="2"> <?php echo $type; ?> </td> <td><?php foreach ($ingredients as $name => $meas){ echo $name . '</br>'; } ?></td> <td><?php foreach ($ingredients as $name => $meas){ echo $meas . '</br>'; } ?></td> </tr> <?php } ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/256532-converting-from-array-to-table/#findComment-1315102 Share on other sites More sharing options...
noXstyle Posted February 6, 2012 Share Posted February 6, 2012 What you mean? The latter one displays just fine. Just add some styles and you'll have a similar table as shown on the picture. You could also use another table for the ingredients, like: <table border="1"> <tr> <th>Sushi</th> <th colspan="2">Ingredients</th> </tr> <?php foreach($sushi as $k=>$v):?> <tr> <td><?php echo $k?></td> <td> <table border="1" width="100%"> <?php foreach($v as $k2=>$v2):?> <tr> <td><?php echo $k2; ?></td> <td><?php echo $v2; ?></td> </tr> <?php endforeach; ?> </table> </td> </tr> <?php endforeach; ?> </table> But anyhow, you still cant avoid from styling the table if you want one similar to the picture. Quote Link to comment https://forums.phpfreaks.com/topic/256532-converting-from-array-to-table/#findComment-1315109 Share on other sites More sharing options...
Psycho Posted February 6, 2012 Share Posted February 6, 2012 I think the problem you may be having is that the first row needs to include the type, but the subsequent ones do not. So, you need some conditional logic to handle the first row of each recipe differently than the rest. <?php $sushi = array( 'Fresh Tuna Sushi' => array ( 'Fresh Tuna Fillet' => '8 ounces', 'Sushi Rice' => '2 cups', 'Wasabi Paste' => '2 tablespoons' ), 'Shrimp Sushi' => array ( 'Large Shrimps' => '10 pieces', 'Vinegar' => '2 tablespoons', 'Sushi Rice' => '2 cups', 'Wasabi Paste' => '2 tablespoons', ), 'Nori Crab Sushi' => array ( 'Nori Seaweeds' => '4 sheets', 'Crabmeat' => '8 ounces', 'Cream Cheese' => '8 ounces', 'Worcestershire Sauce' => '3/4 teaspoon', 'Garlic Salt' => '1/4 teaspoon' ), 'Vegetarian Sushi' => array ( 'Rice Vinegar' => '1/2 cup', 'Sesame Oil' => '2 tablespoons', 'Nori Seaweeds' => '4 sheets', 'Sushi Rice' => '2 cups', 'Cucumber Strips' => '1 cup', 'Sweet Potatoes' => '1 cup', 'Sesame Seeds' => '2 tablespoon' ), 'California Roll Sushi' => array ( 'Sushi Rice' => '1 cup', 'Nori Seaweeds' => '8 sheets', 'Sesame Seeds' => '2 tablespoons', 'Crabmeat' => '1/2 cup', 'Mayonnaise' => '3 tablespoons', 'Avocado' => '1 piece', 'Cucumber Strips' => '1/2 cup' ) ); $recipe_output = ""; foreach ($sushi as $recipe => $ingredientsAry) { $ingredientCount = count($ingredientsAry); $typeCell = "<td rowspan='{$ingredientCount}'>{$recipe}</td>\n"; $first = true; foreach($ingredientsAry as $ingredient => $amount) { $recipie_output .= "<tr>\n"; if($typeCell != false) { $recipe_output .= $typeCell; $typeCell = false; } $recipe_output .= "<td>{$ingredient}</td>\n"; $recipe_output .= "<td>{$amount}</td>\n"; $recipe_output .= "</tr>\n"; } } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <table border="1"> <tr> <th>Sushi</th> <th colspan="2">Ingredients</th> </tr> <?php echo $recipe_output; ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/256532-converting-from-array-to-table/#findComment-1315112 Share on other sites More sharing options...
PFMaBiSmAd Posted February 6, 2012 Share Posted February 6, 2012 Similar to what Psycho just posted above - <table border="1"> <tr><th>Sushi</th><th colspan="2">Ingredients</th></tr> <?php // each row needs a <tr></tr> // the first row under each type is a complete row // the 2nd - nth row under each type do not have any <td></td> for the 1st column, but need to start with a <tr> foreach ($sushi as $type => $ingredients){ echo "<tr><td rowspan=" . count($ingredients) . ">$type</td>"; // start the first row $first = true; foreach ($ingredients as $name => $meas){ if(!$first) echo "<tr>"; // start the 2nd - nth row under each type $first = false; echo "<td>$name</td><td>$meas</td></tr>"; // the rest of each row } } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/256532-converting-from-array-to-table/#findComment-1315130 Share on other sites More sharing options...
Psycho Posted February 6, 2012 Share Posted February 6, 2012 Similar to what Psycho just posted above - LOL, I saw you used a flag variable ($first) and I was going to comment that I originally took that approach but made a last minute change in my code and instead used a temp variable to hold the content of the first cell (which I cleared after the first iteration of each type). Then I noticed that I still had the line $first = true; in my code which no longer had any purpose. Too funny. Not only did we go with the same approach, but we used the same exact variable name/value combination. As they say, great minds think alike. Quote Link to comment https://forums.phpfreaks.com/topic/256532-converting-from-array-to-table/#findComment-1315141 Share on other sites More sharing options...
pamelaanne Posted February 8, 2012 Author Share Posted February 8, 2012 OMG! I am this happy -> :D THANK YOU SO MUCH noXstyle, Psycho, and PFMaBiSmAd for all the help and explanations. THANK YOU.. VERY MUCH. :'( Quote Link to comment https://forums.phpfreaks.com/topic/256532-converting-from-array-to-table/#findComment-1315701 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.