Jump to content

Recommended Posts

Sigh. I am learning PHP, started today. So, pursuant to becoming more comfortable with this as a language I started on one a little echo tutorial I found somewhere and expanded it to this code. My issue is that it's only going through $main_loop one time, thus rendering the table a single time, rather than the expected 3 time. Somewhere my while{} is falling apart. It still runs and does its thing, but only the one time. The comments are for my own sake.

<?php

$main_loop = 0;
$column_loop = 0;
$row_loop = 0;
$column_loop = 0;
/* do it all this way until you get comfortable with this var system */
$table_start = "<table BORDER = 1> ";	
$table_end = "</table> ";
$table_row_start = "<tr>";
$table_row_end = "</tr>";
$table_d_start = "<td>";
$table_d_end = "</td>";


echo "<html><head></head><body>";
/* Begins writing the table(s)*/
while($main_loop != 4) /* writes entire table 4 times */
{ /* ------------------------------------------while main loop-------------------------------*/
echo $table_start; /* code block to render a table */
while($row_loop !=  /* specifies teh number of rows -1 */ 
{ /*-------------------while row loop-----------------------*/
	echo $table_row_start;
	while($column_loop !=  /* specifies the number of <td> per row -1 */
	{/*----------------while column loop ---------------------------*/
		if ($row_loop == 0) /* part one of checking to see if we are at row 0 col 0 */
		{
			if($column_loop == 0) /* part two of checking to see if row 0 col */ 
			{
			echo $table_d_start;
		    echo "  "; /* if row == 0 and col == 0 leave a blank box */
		    echo $table_d_end;
		    $column_loop++;
			}
			else
			{
			  echo $table_d_start;
			  if($row_loop == 0)
				   {
					$row_num = $row_loop + 1; /* if the row is 0 set it to one to prevent 0's everywhere */
					}   
					else 
					{
						$row_num = $row_loop; 
					}

				if($column_loop == 0)
				   {
					$col_num = $column_loop + 1; 
					} 	
					else
					{
						$col_num = $column_loop;
					}
		        echo $row_num * $col_num; /* multiply row x col */
		        echo $table_d_end;
		        $column_loop++;
		       
			}
		}
		else
		{
		        if($row_loop == 0)
				   {
					$row_num = $row_loop + 1; 
					}   
					else 
					{
						$row_num = $row_loop;
					}
				if($column_loop == 0)
				   {
					$col_num = $column_loop + 1; 
					} 	
					else
					{
						$col_num = $column_loop;
					}
		echo $table_d_start;
		echo $row_num * $col_num;
		echo $table_d_end;
		$column_loop++;
		}

	}/*----------------while column loop ---------------------------*/
	echo $table_row_end;
	$column_loop = 0;
	$row_loop++;
}/*-------------------while row loop-----------------------*/
echo $table_end;
echo "<BR>";
$main_loop++;

} /* ------------------------------------------while main loop-------------------------------*/
echo "</body></html>";
?>


Sorry I've been drupal alot lately.

Link to comment
https://forums.phpfreaks.com/topic/214753-leanrning-me-some-php-logics/
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.