Print table using do while loop in php

Do while loop in PHP

The do...while Loop executes the statement once and then check the condition.

Syntax

	do
	{
	 code to be executed;
  	}
	while [condition];
	

Eg

Output
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

In the above example,
variable $i hold value="1". first execute the statement inside do.
after that it check while condition[$i

In the above example,
we display more than one table, nested do while loop is used.
Declare three variable $n hold vale="1"
$i hold value="0"
$t hold value="0"

I am new at php and am trying to learn CRUD applications. For this part of the code I have a table set up in a database [mysql]. I am trying to interact with the table. I want a user to login [that part works] and when they are logged in- I store a message in $_SESSION that says ie. 'success'. This lets me know the user is logged in. Not the problem. The second piece is that IF there are rows IN THE TABLE already [from previous sessions] I want to print out these rows in a table. If there are no rows I want to print out "No rows"

I am trying to use simple logic here where first I create the $row variable by doing a fetch that comes from a pdo object[also fine]. If that row is TRUE ie. it exists, print the table. If that row is false, print no rows. However, I keep getting the whole table printed, say 5 times. Instead of the 5 rows in the table.

Also, I am not sure I am using the correct type of while [] statement. Which right now reads,
while [$row = $stmt->fetch[PDO::FETCH_ASSOC]]

And then I get the WHOLE table and all rows 5 whole times. fetch and PDO::FETCH_ASSOC are a bit unclear to me, and I am understanding the $row as either true or false.

$stmt= $pdo->query["SELECT make, model, year, mileage from autos"];
$row = $stmt->fetch[PDO::FETCH_ASSOC]; 

if [   isset[$_SESSION['success']] && $row == true  ] {

while [$row = $stmt->fetch[PDO::FETCH_ASSOC]]{

echo [''."\n"];
    echo ""];
    
    echo "";
  }



  } elseif [    isset[$_SESSION['success']] && $row == false  ]{

  echo "No rows found";
  }

While loop in PHP is very simple and similar to while loop in C programming. In PHP while loop the condition is checked at the beginning of the loop and the statement inside is executed till the end of the loop, then again the condition is checked and loop executed. Like this loop is continued till the condition became false and  the loop is then stopped.

while[condition checked]{
code part to execute 
}
Here if the condition became false inside the loop then also the loop continue to execute till the next time the condition is checked.  Here is one simple example of while loop.
What is the output of this?
value of $j= 5
 value of $j= 6 
 value of $j= 7 
 value of $j= 8 
 value of $j= 9 
Here we are checking the while condition at the starting of the loop. So the execution of the loop will be done only if condition is satisfied at the beginning. We can read on the difference between do while loop and while loop here.

Exit inside the loop by using break

Prematurely we can come out of the loop by using break statement. We used one if condition to check the value and then apply break statement.

Using NULL

Check this code
$a=NULL;

while[$a]{
echo "hi";	
break;
}
In above code the line echo "hi"; will not be executed as NULL is same as False. This concept we will be using in database record set where pointer will return NULL when there is no more record to return.
However below code will print the output once, as we have used break; to come out of the loop after printing hi.
$a=True;

while[$a]{
echo "hi";	
break;
}

Display elements of an array by using While loop

Let us declare one array with some elements. Then by using while loop we can display the key and value of the array.
The output is here
0 > Three
1 > two
2 > Four
3 > five
4 > ten

Create [ 2 to 10 ]multiplication table using While loop

To generate multiplication tables we will start with 2 table
Here is the simple code
$i=2;
$j=1;
while[$j

Chủ Đề

"; echo "Make"; echo [""]; echo "Model"; echo [""]; echo "Year"; echo [""]; echo "Mileage"; echo [""]; echo "Action"; echo ["
"; echo [htmlentities[$row['make']]]; echo [""]; echo [htmlentities[$row['model']]]; echo [""]; echo [htmlentities[$row['year']]]; echo [""]; echo [htmlentities[$row['mileage']]]; echo [""]; echo ['Edit'.'/'.'Delete']; echo "