judy judy - 2 months ago 6
HTML Question

closing PHP tag before closing while loop

I was watching a video tutorial on how to create a basic CMS with PHP and database but I'm wondering why the reason to open the

<?php
tag two times.

Can't I just use a single PHP block?

<?php
include("includes/db.php");
if(isset($_GET['view_page'])){ //open curly brace which will close
//later..what???
?>
<table width="1000" border="2px" align="center">
<tr>
<td style="text-align:center;background-color:yellow"colspan='6'><h2>All pages here</h2></td>
</tr>

<tr>
<th>Page No.</th>
<th>Page Title</th>
<th>Page Content</th>
<th>Delete</th>
</tr>
<tr>
<?php

$query="SELECT * FROM `pages`";
$run=mysqli_query($conn,$query);
while($row=mysqli_fetch_array($run)){
$p_id =$row['p_id'];
$p_title=$row[1];
$p_desc =substr($row[2],0,100); //on table show 0 to 100 characters long

?>
<td><?php echo $p_id; ?></td>
<td><?php echo $p_title; ?></td>
<td><?php echo $p_desc; ?></td>
<td><a href="delete_page.php?del_page=<?php echo $p_id; ?>">Delete</a></td>
</tr>
<?php }} ?> //THIS IS REASON OF CONFUSION
</table>

Answer

This is the beauty of php it can be inserted any where in html tag's not in html page . Page must be saved with .php extension to write the php code you have to just write

 <?php
 #code
 ?> 

let say if you want to use while loop but you want some html tags in inside the loop you can easily do this by

<?php
while(#condition) {
//inside php tag code
?>
<p> i am html part depend on php codtion</p>
<?php
} //end of while loop
?>

there are other ways to

<p>
<?php 
echo"hello p tag i am from php ";
?>
</p>