Why this is Used <?php }?> - php

From a database we are getting data, but my question is that after tbody starting tag and before tbody ending tag, why we are ending php in this way using <?php }?>
Why is it used in these lines?
<div class="container">
<div class="table-responsive">
<table class="table table-striped" >
<thead>
<tr>
<th scope="col">CNIC</th>
<th scope="col" >Name</th>
<th scope="col" >DOB</th>
<th scope="col" >Address</th>
<th scope="col" >City</th>
<th scope="col" >Degree Program</th>
<th scope="col" >Gender</th>
<th scope="col" >Email</th>
<th scope="col" >Mobile</th>
<th scope="col" ></th>
<th scope="col" ></th>
</tr>
</thead>
<tbody>
<?php while($student = mysqli_fetch_assoc($resultSet)){?>
<tr>
<td scope="row" ><?php echo $student['cnic']; ?></td>
<td><?php echo $student['fname'] . " ". $student['lname']; ?></td>
<td><?php echo $student['dateofbirth']; ?></td>
<td><?php echo $student['address']; ?></td>
<td><?php echo $student['city']; ?></td>
<td><?php echo $student['degree']; ?></td>
<td><?php echo $student['gender']; ?></td>
<td><?php echo $student['email']; ?></td>
<td><?php echo $student['mobile']; ?></td>
<td> <a class="btn btn-primary" href=<?php echo "update_student.php?u_id=".$student['u_id']; ?> >Update</a> </td>
<td> <a class="btn btn-primary" href=<?php echo "delete_student.php?u_id=".$student['u_id']; ?> >Delete</a> </td>
</tr>
<?php }?>
</tbody>
</table>
</div>

Here,
like your code snippet, we can decide what is to be displayed in the DOM (html page).in your code the whole contents in
<tr>...</tr>
will print on the html page only till the while condition satisfies.if the while loop runs for ten times there will be ten rows in
<tbody>...</tbody>
here the while loop will continue till fetching the last record from the specified table.

This:
<?php }?>
Is actually closing your while loop that starts at the begining:
<?php while($student = mysqli_fetch_assoc($resultSet)){?>.
If that would not be present, the code would throw you an error.
Loops in PHP (should) start and end with curly brackets. So at the begining you have something like:
for($i; $i< 10, $i++) {
or
foreach($a as $b) {
or
while($someCondition){
And at the end always a closing curly bracket:
}
BR

With this <?php ..... ?> inside html code, you are (probably) showing html code inside php file. So basically you have (for example) a HTML template in php file, hence the php opening/closing tags. So while you are tehnically in a PHP script, you are actually showing something like:
<!DOCTYPE html>
<html>
<head>
<title><?php echo "Some title" ?></title>
</head>
<body>
<h1><?php echo "Hello World" ?></h1>
</body>
</html>
If you can, I'd suggest to use a templating engine for this, like twig.
So in your case, you are fetching result from DB, looping over it and shwoing it in HTML table.
BR

PHP has a cool way of deciding to show content. Instead of echo'ing HTML based on a variable, you can use the PHP <?php if (...): ?> html here <?php endif; ?> and the HTML inside will show only if that if condition is met. It's the same thing here, except with a while instead of an if

A PHP while loop works like this:
while($x === true){
//execute some code
}
This will execute the code inside the while loop an infinite number of times, or until the $x variable is changed (within the loop) to equal true. The opening and closing brackets, { and } are what determine where the while loop starts and ends. In your code, PHP is printing out all of those <tr> and <td> tags until mysqli_fetch_assoc($resultSet) no longer returns a result. The line <?php }?> just tells the program, "the loop stops here, go back to the start"

Related

View cart issue

All the other parts of my application are working but my users view cart is not working I do not know whether if it is about the indexing
I have tried using outer php
<!DOCTYPE html>
<html>
<head>
<title>View table</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs /popper.js/1.14.7/umd/popper.min.js"></script>
</head>
<body>
<table class="table">
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">brand</th>
<th scope="col">flavour/type</th>
<th scope="col">quantity</th>
<th scope="col">Number of units</th>
<th scope="col">Number of packets</th>
<th scope="col">price</th>
<th scope="col">Cost</th>
<th scope="col">Picture</th>
<th scope="col">D</th>
</tr>
</thead>
<tbody>
<?php
require_once("config.php");
error_reporting(0);
$email_address=$_SESSION['email_address'];
$sql="SELECT product_name ,brand,flavour_type,quantity,number_of_units,price ,units,image_path
FROM gokcen.product NATURAL JOIN gokcen.cart
WHERE cart.email_address=:email_addres";
$stmt=$db->prepare($sql);
$stmt->bindParam(':email_address',$email_address);
$stmt->execute();
$result=$stmt->fetchAll();
foreach($result as $product)
{
?> <tr>
<td><?php echo $product['product_name'];?></td>
<td><?php echo $product['brand'];?></td>
<td><?php echo $product['flavour_type'];?></td>
<td><?php echo $product['quantity'];?></td>
<td><?php echo $product['number_of_units'];?></td>
<td><?php echo $product['units'];?></td>
<td><?php echo $product['price'];?></td>
<td><?php echo $product['price']* $prouct['units'];?></td>
<td> <img src="pics/<?php echo $product['image_path'];?>" width="80" height="80"/></td>
<td><a href="deletefromcart.php?item=<?echo product['product_name'];?>"> delete <a></td>
</tr>
<?php }?>
</tbody>
</table>
<a href="#" class="btn btn-primary" >Buy</a>
</body>
</html>
There are no results It does not give any errors it shows the header part only and Im not sure about the indexing.
there are several typos in your code.
When I am right there should be another "s" at the end? cart.email_address=:email_addres
There is a "d" missing at line <?php echo $product['price']* $prouct['units'];?>
You also missed the "$" sign at this point as well as the "php" after the question mark: <?echo product['product_name'];?>
You should set display_errors to On in your php.ini and comment out the error_reporting(0); for debugging purposes :)
Greetings
You have a typo email_addres email_address.
WHERE cart.email_address=:email_addres";
$stmt->bindParam(':email_address',$email_address);
Thank you guys you are the best you rock my coding life It was the typos and also that
I had not put a
session_start()
To initialize the value of $email_address on
$email_address=$_SESSION['email_address'];
All the best!

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>Delete</td>
</tr>
<?php }} ?> //THIS IS REASON OF CONFUSION
</table>
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>
Try like this
<?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
echo '<td>'. $p_id; .'</td>';
echo '<td>'. $p_title .'</td>';
echo '<td>' .$p_desc .'</td>';
echo '<td>Delete</td>';
}
?>
</tr>
</table>
<?php } ?>
the problem is that you have added one addition } and additionally you have closed <tr> inside the loop that should be done outside... Like this
<td>Delete</td>
<?php } ?>
</tr>
the second } that is of if(isset(...)) shpuld be closed after the <table> has been closed

Ill formatted HTML from a PHP loop

I am looping through an array and building tables. The HTML is then sent to DOMPDF. However, DOMPDF will not create the PDF if the HTML is ill formatted. I assume that is what is happening in my case. Here is my loop:
<?php foreach($credits as $credit) : ?>
<?php if($credit['credit_type'] == "short") : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;">
<tr>
<td><strong><?php echo $credit['category_title']; ?></strong></td>
</tr>
<tr>
<td><?php echo $credit['credit_heading']; ?></td>
</tr>
</table>
<?php endif; ?>
<?php if($credit['credit_type'] == "long") : ?>
<?php if($credit['category_title'] != $oldvalue) : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;">
<tbody>
<?php endif; ?>
<tr>
<?php if($credit['category_title'] != $oldvalue) : ?>
<td width="25%"><strong><?php echo trim($credit['category_title']); ?></strong></td>
<td width="25%"><strong>Title</strong></td>
<td width="25%"><strong>Role</strong></td>
<td width="25%"><strong>Director</strong></td>
<?php endif; ?>
</tr>
<tr>
<td width="25%"><?php echo $credit['credit_heading'];?></td>
<td width="25%"><?php echo $credit['credit_title']; ?></td>
<td width="25%"><?php echo $credit['credit_role']; ?></td>
<td width="25%"><?php echo $credit['credit_director']; ?></td>
</tr>
<?php if($credit['category_title'] != $oldvalue) : ?>
</tbody>
</table>
<?php endif; ?>
<?php $oldvalue = $credit['category_title']; ?>
<?php endif; ?>
<?php endforeach; ?>
I cannot for the life of me work out which tag I am not closing. If anyone could give some insight, that would be fab!
Specifically, the loop is creating rows that show some headings, and then spit out futher rows whenever the category title changes.
This may be a simple solution but perhaps not the best:
I recommend you to use PHP's Tidy class (eventually you'll have to install it first...)
Here is the link for the Tidy class Manual.
At the first line:
ob_start();
This command buffers everything what is outputed by your follwing script.
The code below should be added at the end of your file, or there where you want to show the output.
It first gets the buffer with ob_get_contents() and than it cleans the code up.
Note that you'll eventually have to change the configuration parameters for your needs, there are really very much.
$raw_output = ob_get_clean();
$config = array('indent' => true, 'output-xhtml' => true, 'wrap' => 0);
$tidy = new Tidy;
$tidy->parseString($raw_output, $config, 'utf8');
$tidy->cleanRepair();
echo $tidy;
This Example Code was modified by the original version of the example on php.net.
Hope that helps you.
It's a bit difficult to parse without known more about your data. For example, why is a table for "short" credit open and closed with the record, but the table for "long" credit is conditional on the previous record? Is it because you have a flat data structure so related data shows up as a series of consecutive rows? If that's the case things would be easier if the data were a bit more normalized. I.e. you could iterate through each credit record then through the details separately. Any possibility of fixing your data structure?
Analyzing the code you have, your problem appears to be in the logic for the second section of the code. You are setting the value of the variable $oldvalue at the end of the loop. This is after the logic that closes the table. So if you parse two records that have the same category title the second record will output it's table rows completely outside a table (never mind that it will also have a completely empty row). Additionally, if you have a short credit type following a long the table will never be closed.
That being said, working with what you have I'm guessing you may need something like the following:
// build a dummy "previous" record for the first iteration so the conditionals don't break.
<?php $previous_credit = array('credit_type'=>null,'category'=>null); ?>
<?php foreach($credits as $credit) : ?>
<?php if($credit['credit_type'] == "short" || ($previous_credit['credit_type'] == "long" && $previous_credit['category'] != $credit['category'])) : ?>
</tbody>
</table>
<?php endif; ?>
<?php if($credit['credit_type'] == "short") : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;">
<tr>
<td><strong><?php echo $credit['category_title']; ?></strong></td>
</tr>
<tr>
<td><?php echo $credit['credit_heading']; ?></td>
</tr>
</table>
<?php endif; ?>
<?php if($credit['credit_type'] == "long") : ?>
<?php if($credit['category_title'] != $previous_credit['category_title']) : ?>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:0px 0px 15px 0px;">
<tbody>
<tr>
<td width="25%"><strong><?php echo trim($credit['category_title']); ?></strong></td>
<td width="25%"><strong>Title</strong></td>
<td width="25%"><strong>Role</strong></td>
<td width="25%"><strong>Director</strong></td>
</tr>
<?php endif; ?>
<tr>
<td width="25%"><?php echo $credit['credit_heading'];?></td>
<td width="25%"><?php echo $credit['credit_title']; ?></td>
<td width="25%"><?php echo $credit['credit_role']; ?></td>
<td width="25%"><?php echo $credit['credit_director']; ?></td>
</tr>
<?php endif; ?>
<?php $previous_credit = $credit; ?>
<?php endforeach; ?>
<!-- one last table close for the last record -->
</tbody></table>
(That's some ugly code and I don't have time to keep revising it, so ... community wiki in case anyone else wants to clean it up.)

plot mysql data into chart

I'm trying to use this chart generator from http://htmldrive.net/items/show/792/Rare-Accessible-charts-using-jQuery-and-HTML5.html
Here's the code which loads data from mysql database:
The query works, but I guess my interpretation of the example provided in the site was wrong.
I get an output if I do it this way(predefined data):
<tr>
<th scope="row">Profit</th>
<td>5</td>
<td>5</td>
</tr>
But when I do it this way I get a blank output:
?>
<table>
<caption> Reports</caption>
<thead>
<tr>
<td></td>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<th scope="col"><?php echo $row['Cust_Name']; ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Subtotal</th>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<td><?php echo $row['TOTAL_PUR']; ?></td>
<?php } ?>
</tr>
<tr>
<th scope="row">Profit</th>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<td><?php echo $row['TOTALPROFIT']; ?></td>
<?php } ?>
</tr>
</tbody>
</table>
Here's what I'm getting:
After the first iteration through the rows, when you display the customer names, the fetch data pointer is at the end of the dataset... you're trying to fetch the set again without resetting the pointer.
Try issuing
mysql_data_seek($query, 0);
before the while loops to display total and profit

Called hyperlink stopped showing when CSS table implemented

EDIT: Solved - was not flutter's tag stripping, should work as advertised.
I'm using Flutter (which creates custom fields) in Wordpress to display profile information entered as a Post. Before I implemented the CSS tables the link showed up and was clickable. Now I get nothing returned, even when I try to call the link outside the table.
If you know anything about this, here's my code in the index.php file and I remain available for any questions.
<?php if (in_category('Profile')) { ?>
<table id="mytable" cellspacing="0">
-snip-
<tr>
<th class="row1" valign="top">Website </td>
<td>Link: <?php echo get_post_meta($post->ID, 'FrWebsite', $single=true) ?></td>
</tr>
-snip-
</table>
Edit: #Josh - there is a foreach looping construct in the table and it is reading and displaying the code correctly, I see what you're getting at now:
<tr>
<th class="row2" valign="top">Specialities </td>
<td class="alt" valign="top"><?php $my_array = get('Expertise');
$output = "";
foreach($my_array as $check)
{
$output .= "<span>$check</span><br/> ";
}
echo $output; ?></td>
</tr>
Edit - #Josh - here's the old code as far as I can remember it, there was no major difference just a <td> tag where there now stands a <th>, there wasn't the class="" and there was no "Link:" and FrWebsite was called Website, but it still didn't work when called Website so I changed to see if that was the error.
<tr>
<td width="200" valign="top">Website </td>
<td><?php echo get_post_meta($post->ID, 'Website', $single=true) ?></td>
</tr>
Where is $post set? What does the full table look like? Could be that when you changed the table structure you accidentally removed something like (line 2 below):
<table id="mytable" cellspacing="0">
<?php foreach($posts as $post) { ?>
<tr>
<th class="row1" valign="top">Website </td>
<td>Link: <?php echo get_post_meta($post->ID, 'FrWebsite', $single=true) ?></td>
</tr>
<?php } ?>
(Just guessing here...)

Categories