I am new to PHP and am running into a small problem with this code. I am trying to make a layout for a page that uses more PHP to fill in the blanks.
When I view the source <? include $navbar ?> is commented out but <?=$pagetitle?> works, why is that?
For reference:
$navbar = "navbar.php";
and navbar.php:
<?php echo "Select Car Change Profile"; ?>
Layout.php:
<?php
echo "
<html>
<head>
<title>Race Data. <?=$pagetitle?></title>
</head>
<body>
<div id='page'>
<table border='1'>
<tbody>
<tr>
<td colspan='3'>Banner goes here.<?=$pagetitle?></td>
</tr>
<tr>
<td rowspan='2'>Left menu</td>
<td colspan='2'><? include $navbar; ?></td>
</tr>
<tr>
<td>Content</td>
<td>Right menu</td>
</tr>
<tr>
<td colspan='3'>Footer</td>
</tr>
</tbody>
</table>
</div>
</html>
";
?>
I'm sure knowing this will help amny future problems I run into.
Also, what are the diferences in using <? ?> vs <?php ?>?
In php, you always need to use <?php /*code*/ ?>
The shorthand version to echo something is <?= /*string*/ ?>, but to run code, such as an include you would need to start with <?php. In your example, this would be:
<tr>
<td rowspan='2'>Left menu</td>
<td colspan='2'><?php include $navbar; ?></td>
</tr>
Correction
I only just noticed that you placed the PHP tags inside another set of PHP tags. You're kind of doing it the hard way. In a PHP file, anything is regarded as an echo, except for content inside <?php ?> tags. So this should work perfectly for you:
<html>
<head>
<title>Race Data. <?=$pagetitle?></title>
</head>
<body>
<div id='page'>
<table border='1'>
<tbody>
<tr>
<td colspan='3'>Banner goes here.<?=$pagetitle?></td>
</tr>
<tr>
<td rowspan='2'>Left menu</td>
<td colspan='2'><?php include $navbar; ?></td>
</tr>
<tr>
<td>Content</td>
<td>Right menu</td>
</tr>
<tr>
<td colspan='3'>Footer</td>
</tr>
</tbody>
</table>
</div>
</html>
The difference is that I didn't put <?php ?> tags around the whole thing.
Try
short_open_tag=On;
in php.ini
And restart your Apache server.
Related
View table<-- this is my example. And code is provided too. I need to separate HTML from PHP, moving HTML to another file but my PHP code still be able to link with it. Is there any idea? I am trying to make something like View model controller.
<html>
<head>
<meta charset="utf-8">
<title>View Records</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="form">
<p>
Dashboard
| View Records
| Add Admin
| Logout
</p>
<table width="100%" border="1" style="border-collapse:collapse;">
<thead>
<tr>
<th><strong>ID</strong></th>
<th><strong>Username</strong></th>
<th><strong>User Password</strong></th>
<th><strong>Full Name</strong></th>
<th><strong>Edit</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
</body>
<?php
$count=1;
$sel_query="Select * from admin ORDER BY id ASC;";
$result = mysqli_query($con,$sel_query);
while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td align="center"><?php echo $row["ID"]; ?></td>
<td align="center"><?php echo $row["username"]; ?></td>
<td align="center"><?php echo $row["user_pass"]; ?></td>
<td align="center"><?php echo $row["fullname"]; ?></td>
<td align="center">
Edit</td>
<td align="center">
Delete</td>
</tr>
<?php $count++; } ?>
</tbody>
</table>
</div>
</body>
</html>```
Separating the php and html is a good start, and helps you see the next step in converting to OOP and then MVC.
MVC at this point is too broad for a simple answer here, but I would recommend this as a first step as it has the underlying principles:
PHP is always at the top; never output anything until all your logic is finished
Load configuration
Work with user input and redirect if POST
Execute business logic
Exit PHP and output HTML. Remember, PHP is essentially a templating language, might as well use it as such
Your code would then look something like this:
<?php
// load database connection, etc
$url = 'your url';
// deal with user input. Always use POST if data will be changed!
if($_POST['action'] == 'delete') {
// delete from admin where id=?
header('location: '.$url);
die;
}
// end "controller" section
// begin "model" section
$sel_query="Select * from admin ORDER BY id ASC;";
$result = mysqli_query($con,$sel_query);
// end "model" section
// begin "view" section.
// Note, you could simply put the html in a separate file and just include it here.
?>
<html>
<head>
<meta charset="utf-8">
<title>View Records</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="form">
<p>
Dashboard
| View Records
| Add Admin
| Logout
</p>
<table width="100%" border="1" style="border-collapse:collapse;">
<thead>
<tr>
<th><strong>ID</strong></th>
<th><strong>Username</strong></th>
<th><strong>User Password</strong></th>
<th><strong>Full Name</strong></th>
<th><strong>Edit</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
</tbody>
<?php while($row = mysqli_fetch_assoc($result)): ?>
<tr>
<td align="center"><?= $row["ID"] ?></td>
<td align="center"><?= $row["username"] ?></td>
<td align="center"><?= $row["user_pass"] ?></td>
<td align="center"><?= $row["fullname"] ?></td>
<td align="center">
<form method="post">
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<?= $row["ID"] ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</body>
</html>
Notice that following this pattern is laying the groundwork for moving to mvc. But first, start working on your oop chops and move all the business logic into a model object.
After that, you can move the template to its own file, and have the model injected into the view, which lets the view have access to the info it needs, and then render the output.
Meanwhile, you'll need a router (all traffic is rerouted to index.php) and controller, and figure out which interpretation of MVC you will use ;)
Here's a very basic solution.
Make an HTML file as a template. E.g. "template.html". Use HTML comments as placeholders for the data. (I went with comments as it means the HTML remains compliant)
I've left out some of the non-relevant bits, so hopefully you get the idea:
<html>
...
<table width="100%" border="1" style="border-collapse:collapse;">
<thead>
<tr>
<th><strong>ID</strong></th>
<th><strong>Username</strong></th>
<th><strong>User Password</strong></th>
<th><strong>Full Name</strong></th>
<th><strong>Edit</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
<tbody>
<!--ROW-->
<tr>
<td align="center"><!--ID--></td>
<td align="center"><!--username--></td>
<td align="center"><!--user_pass--></td>
<td align="center"><!--fullname--></td>
<td align="center">
Edit</td>
<td align="center">
Delete</td>
</tr>
<!--ENDROW-->
</tbody>
</table>
</div>
</body>
</html>
Then, in your PHP code, you read in the html, find the row template, and replace the fields as needed:
<?php
// Read the template
$html = file_get_contents('template.html');
// Find the row template
$regRowTemplate = '/<!--ROW-->(.*)<!--ENDROW-->/i';
preg_match($regRowTemplate, $html, $m);
$rowTemplate = $m[1];
// Start building our replacement rows
$htmlRows = '';
$count=1;
$sel_query="Select * from admin ORDER BY id ASC;";
$result = mysqli_query($con,$sel_query);
while ($row = mysqli_fetch_assoc($result)) {
// Start with a fresh copy of the template
$htmlRow = $rowTemplate;
// Replace comment placeholders with values
foreach ($row as $key => $value) {
$htmlRow .= str_replace('<!--' . $key . '-->', $value, $htmlRow);
}
// Append to our rows
$htmlRows .= $htmlRow;
$count++;
}
// Replace the row template with our expanded rows
$html = preg_replace(regRowTemplate, $htmlRows, $html);
// Do something with the html
Source untested, but should give you a good starting point. I kept it pretty raw. If I was doing this for real, I'd allow for the possibility of spaces in the comment placeholders by using a regular expression instead, but for now it's good enough.
Hai everyone iam trying to add manual page break in tcpdf i tried , but it doesn't works, how to break this..? in the location where i need to include coding
$content = '
<style>
.chead
{
...
</style>
<table class="body">
<tr>
<td>
<table style="width:595px;">
<tr>
'.$myhead.'
</tr>
</table>
</td>
</tr>
//how to add manual page break here..?
<tcpdf method="AddPage" />
<tr>
<td>
<table style="width:595px;">
<tr>
'.$mybody.'
</tr>
</table>
</td>
</tr>
</table>';
Try use:
<br pagebreak="true">
Result:
I'm trying to create a simple form to show a basic table and a delete query...
I can get the delete query working but when I use an include it does not work, if i run the 'display.php' file alone in browser it runs perfectly and shows the database...
Can anyone advise what would be best way incorporate the display.php file in this html file? Or explain the issue?
<!DOCTYPE html>
<div id="content">
<?php include('display.php')?>
<div id="sidebar"></div>
<div id="footer">
<h5></h5>
</div>
<table>
<tr>
<td>
<form action="delete.php" method="post"></form>
<table border="0" cellpadding="3" cellspacing="1" style="background-color: #536977" width="100%">
<tr>
<td width="150">Enter Username to be deleted</td>
<td width="6">:</td>
<td width="294"><input name="delusername" type="text"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" value="submit"></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</div>
<?php require_once('display.php'); ?>
Presuming they are in the same folder.
I'm new in this language. I'm working on a website. I use HTML file like this:
<html lang="en">
...
<div class="" id="temperatura" name="temp">
<?php require 'php/staticsTemp.php'; ?>
<h3 class="centered">Temperature</h3>
<hr>
<br>
<table class="tg" border="5">
<tr>
<th class="tg-031e">Temperature ºC</th>
<th class="tg-031e">Date & Time</th>
</tr>
<tr>
<td class="tg-031e">33</td>
<td class="tg-031e">44</td>
</tr>
</table>
</div>
...
</html>
And I want to substitute the value 33 and 44 in the table to values that are inside the PHP file.
My PHP looks like this:
<?php
include("ligacaobd.php");
$sql="SELECT * FROM Valores ORDER BY Momento DESC LIMIT 20";
$result = mysql_query($sql, $ligacaobd) or die(mysql_error());
$rowValor = mysql_fetch_assoc($result);
do
{
$data[date('d/m/Y H:i:s', $rowValor['Momento'])]=$rowValor['Temperatura'];
}
while ($rowValor= mysql_fetch_assoc($result));
?>
Any thoughts? I tried with function POST, but in HTML doesn't work.
You must convert your html in .php as it is and include this php code, either directly in the page or via another php page.
You will then be able to manipulate your variables and do something such as:
<td class="tg-031e"><?php echo $myVariable1; ?></td>
<td class="tg-031e"><?php echo $myVariable2; ?></td>
I'm not quite certain what you mean, but I will try to help you.
First of all don't* use mysql functions anymore. These functions are no longer maintained. Use **mysqli or PDO instead.
If you want to show variables in a HTML document you can do the following thing.
<tr>
<td class="tg-031e"><?php echo $var1; ?></td>
<td class="tg-031e"><?php echo $var2; ?></td>
</tr>
Also I would recommend you separating your HTML files from the PHP or at least place your PHP code at the top of your document. For example:
<?php
$var1 = 'Example Variable';
?>
<html lang="en">
<head>
<title>Example</title>
</head>
<body>
<?php echo $var1; ?>
</body>
<html>
Yet the best practice is separating HTML from PHP.
Since you are new to the PHP language, I have some great tutorials for you.
Take a look at http://www.w3schools.com. They have some basic PHP tutorials for you to start with.
Good luck.
Don't use mysql_* functions anymore! They are deprecated in PHP 5.5 and should be removed in 5.6. Use PDO instead.
Not tested yet, but should work. You just need to change your <table /> code to this:
<table class="tg" border="5">
<tr>
<th class="tg-031e">Temperature ºC</th>
<th class="tg-031e">Date & Time</th>
</tr>
<?php foreach ($data as $datetime => $temperature): ?>
<tr>
<td class="tg-031e"><?php echo $temperature; ?></td>
<td class="tg-031e"><?php echo $datetime; ?></td>
</tr>
<?php endforeach; ?>
</table>
Hope it helps :)
I am trying to calculate ( by a PHP file ) the diameter, area and circumference of a circle whose radius is given by the user in a html file. User gives the values of the radius and PHP script calculates the above mentioned quantities. The problem is that when I am executing the program, the html takes the input but the PHP script isn't showing the output.
The code is as follows:-
circle.html:-
<html>
<head>
<title>Circle</title>
</head>
<body>
<form method="GET" action="circle1.php">
<center>
<h1>Circle</h1>
<table border= 2 width=200>
<tr align=center>
<td>Radius</td>
<td><input type="text" name=radius size=5></td>
</tr>
<tr align=center>
<td colspan=2>
<input type="submit">
<input type="reset">
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
circle1.php:-
<html>
<head>
<title>Circle</title>
</head>
<body>
<center>
<h1>Circle</h1>
<?php
define("PI",3.14159,TRUE);
GLOBAL $d,$c,$a,$r;
$r=$_GET['radius'];
$d=2*$r;
$c=2*PI*$r;
$a=PI*$r*$r;
?>
<table border = 2 width=200>
<tr>
<td>Diameter</td>
<td><?php echo $d; ?></td>
</tr>
<tr>
<td>Circumference</td>
<td><?php echo $c; ?></td>
</tr>
<tr>
<td>Area</td>
<td><?php echo $a; ?></td>
</tr>
</table>
<h2>Back</h2>
</center>
</form>
</body>
</html>
sorry for such a long post. Any help would be appreciated.
Some server setups such as various Amazon Linux AMIs require you to output HTML through php print functions such as echo to properly execute. If you have this type of setup, then you'll need to execute your server-side PHP code before you output all your client-side HTML.
Try this for circle1.php:
<?php
define("PI",3.14159,TRUE);
GLOBAL $d,$c,$a,$r;
$r=$_GET['radius'];
$d=2*$r;
$c=2*PI*$r;
$a=PI*$r*$r;
echo'
<html>
<head>
<title>Circle</title>
</head>
<body>
<center>
<h1>Circle</h1>
<table border="2" width="200">
<tr>
<td>Diameter</td>
<td>'.$d.'</td>
</tr>
<tr>
<td>Circumference</td>
<td>'.$c.'</td>
</tr>
<tr>
<td>Area</td>
<td>'.$a.'</td>
</tr>
</table>
<h2>Back</h2>
</center>
</form>
</body>
</html>';
?>
Your code could be cleaned up a bit, but it should work. There isn't anything intrinsically wrong with it. I literally cut and pasted it as a test and it works fine.
If the first page (pure HTML) works, but you get nothing for the second (circle1.php) then the first thing to check is if your webserver is configured to use PHP and if the path to the php.ini is accurate because the bottom line is that your PHP parser is out of the loop.
Your code is correct. You must have some problem related to php.ini or apache . Check weather your <?php code is escaping or not by checking source code. . If it is , put this code on the top error_reporting(E_ALL); and try to debug it.
You need to encapsulate your input properties with quotes.
Replace
<input type="text" name=radius size=5>
With
<input type="text" name="radius" size="5">