John Smith John Smith - 1 month ago 7
PHP Question

Inserting a table into a PHP while statement

I have created wrote PHP code which includes an array and a while statement. I need to put my results into a table like shown below. Tried many different ways but had no luck if anyone can point me in the right direction with this I would be very grateful. (New to PHP)

Table Example

http://main.xfiddle.com/b5a1cb57/test.php

<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="main.css"/>
<title>Loops</title>
</head>
<body>
<?php
echo "<b>Associative Arrays </b><br />";

$prices = array('t-shirt'=>'9.99','cap'=>'4.99','mug'=>'6.99');
echo "<br />";
print_r($prices);

echo "<h1>Loops</h1><br />";

echo "<b>While Looop</b><br />";
echo "<br />";


$shirt_price = $prices['t-shirt'];
$counter = 1;

while ($counter<=10)

{$price2 = ($counter*$shirt_price);

echo $counter . ' ' . $price2 . '<br />';
$counter++;
}

?>





</body>
</html>

Answer

Try starting out with something simple like this

echo '<table>';
while ($counter<=10)
{
    $price2 = ($counter*$shirt_price);
    echo "<tr><td>$counter</td><td>$price2</td></tr>";
    $counter++;
}
echo '</table>';

Once that is working you can look at things you can do to a table to make it look better etc.

Comments