Jump to content

Newbie: Creating html table using php problem


bugzy

Recommended Posts

Hello I've registered here last March and I'm just lurking around reading threads about php.

 

Yesterday I've started to self studied php and today I've test some basic codes and I'm having difficulties on creating a table rows and columns.

 

Here's my code.

 

<?php

echo "<table width=\"100\" border=\"1\">";



$text = 'php';
$mac_var = 1;
$mac_plus = 4;




while ($mac_var < $mac_plus)
{
echo "<tr>";
	{
		while ($mac_var < $mac_plus)
		{
		echo "<td>". $text . "</td>";
		$mac_var++;
		}
	}
echo "</tr>";
$mac_var++;
}



echo "</table>";

?>

 

 

 

what I want is, it should have 3 columns and 3 rows.

 

But I'm getting only 3 columns and 1 row only.

 

I wonder what is the problem?

 

 

Sorry for my ignorance as I'm on the 1st basic phase of using php codes. TIA!  :(

 

Link to comment
Share on other sites

Because you increment the condition counter in both loops so it hits 4 in the inner loop and the outer loop only executes the first time.  Plus you've got extra curly braces in there.  Try:

 

$text = 'php';
$max_rows = 3;
$max_cols = 3;

echo "<table>";
$rows = 1;
while ($rows <= $max_rows)
{
    echo "<tr>";
    $cols = 1; // reset columns to 1 for each row
    while ($cols <= $max_cols)
    {
         echo "<td>". $text . "</td>";
         $cols++; // column counter
    }
   echo "</tr>";
   $rows++;  //row counter
}
echo "</table>";

Link to comment
Share on other sites

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.