Jump to content

Recommended Posts

<!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!  :confused:

The output's supposed to look like this:

35i0v80.png

 

Please help :(  :shrug:

Any help would be graeatly appreciated. Thank you.

Link to comment
https://forums.phpfreaks.com/topic/256532-converting-from-array-to-table/
Share on other sites

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>

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.

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>

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>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.