Entry
what is phtml? in them, query is executed w/o dbname/port etc of MySQL? while in php they are must?
Dec 26th, 2008 21:43
Rockys rainwal, Raj Aryan, Jean-Marc Molina, Imran Aziz, http://www.namaskarindiatravels.com/
"phtml" is a file extension (.phtml). A .html file is a HTML file.
A .php file is a PHP script. A .phtml is an other extension for a PHP
script that generates HTML. Using different file extensions can improve
your development framework:
- a .html file is only a plain HTML file
- a .php file is a PHP script that doesn't output HTML (a script that
handles inputs from a form...)
- a .phtml file is a PHP script that outputs HTML (a script that
displays a HTML table from MySQL record sets)
In order for your web server to handle the .phtml file correctly, you
have to configure it. Apache or IIS don't handle these files by
default. Please read the PHP manual for more information about
configuring PHP for your web server.
If you don't want or can't configure the web server (you're not your
own host, you don't want to change anything to your working
configuration...) but still want or need to use a .phtml-like extension
(what I strongly advice you to do, read beneath), you can use a double
extension:
.html.php: the file is still a PHP file, so the web server will handle
it properly, but it contains a PHP script that generates HTML.
For example, let's say that you want the user to fill in a form, handle
the form then display it. You can use 3 files, one for the form
(.html), one to handle it (.php) and an other one to display it
(.phtml). Depending of your application complexity you can decide to
integrate these 3 elements (form handle display) into 2 files or a
single file.
- 2 files: form + display (the form to fill in is above the HTML table
the displays all the record sets) in 1 file (.phtml: PHP + HTML) and an
other file that handles the form (.php)
- 1 file: as the PHP script directly redirects to the display file once
the user inputs are inserted in the MySQL database, you can use 1 file
to handle the form if it has been submitted, display the form is it
wasn't submitted (or already submitted so the user can fill in the form
again), output a HTML table to display the new entries. As the form is
handled at the beginning of the script, the new entries are correctly
displayed in the HTML table.
As I don't want to lose you in the hook, let's give you a code snippet.
Here we simply ask for the USER to fill in the FIRST and LAST NAMES
text fields of the form. The HTML table displays a list of users. So we
only need one MySQL table:
CREATE TABLE `Users` (
`FirstName` VARCHAR( 100 ) NOT NULL ,
`LastName` VARCHAR( 100 ) NOT NULL
);
note: it's a very basic example, no id nor primary key, there can be
duplicates (same names...)
//**********************************************************************
*****
// FILE : users.phtml
//**********************************************************************
*****
<html>
<body>
<h1>Users: Fill, Handle, Display</h1>
<?php
// MySQL connection and Databse selection
mysql_connect ();
mysql_select_db ("test_cat_1621_faq");
// Handle (.php)
if (isset ($_POST ['Insert']))
{
$FirstName = $_POST ['FirstName'];
$LastName = $_POST ['LastName'];
@mysql_query ("INSERT INTO Users (FirstName, LastName) VALUES
('$FirstName', '$LastName')") or die ("can't insert new user");
}
?>
<!-- Form (.html) -->
<h2>User form</h2>
<form action="<?= $_SERVER ['PHP_SELF'] ?>" method="post">
First name: <input type="text" name="FirstName"><br>
Last name: <input type="text" name="LastName"><br>
<input type="submit" name="Insert">
</form>
<?php
// Display + HTML table (.php + .html -> .phtml)
?>
<h2>Users list</h2>
<?php
$Users = @mysql_query ("SELECT * FROM Users") or die ("can't select
users");
// Only display the HTML table if there's at least 1 user
if (mysql_num_rows ($Users) > 0)
{
?>
<table border="1">
<tr>
<th>First name</th>
<th>Last name</th>
</tr>
<?php
while ($User = mysql_fetch_array ($Users))
{
?>
<tr>
<td><?= $User ['FirstName'] ?></td>
<td><?= $User ['LastName'] ?></td>
</tr>
<?php
}
?>
</table>
<?php
}
else
{
// Display a message if there're no users
?><i>empty</i><?php
}
?>
</body>
</html>
//**********************************************************************
*****
// END OF FILE : users.phtml
//**********************************************************************
*****
First we connect to MySQL and select our database (test_cat_1621_faq in
this example). Then if the form was submitted, the _POST array contains
an Insert entry, if it does, we get the names and insert the user.
We always display a simple HTML form, note that the action path is the
file itself: $_SERVER ['PHP_SELF'], the form is handled in a single
file.
Finally we display our users list using a HTML table, if there're no
users, we display a simple message.
note: as it's a very basic example, we don't validate the form nor
check for duplicates
http://www.jaipurtravelguide.com/
http://www.jaipurtravels.com/
http://www.jaipurjaipur.com/
http://www.jaipurtourismguide.com/
http://www.rajasthantravelsguide.com/
http://www.rajasthantravelguide.co.in/
http://www.travelpackagerajasthan.com/
http://www.namaskarindiatravels.com/
http://www.indianhotelsindia.com/
http://www.travelpackageindia.com/
http://www.navyatravels.com/
http://www.hillstationindiatour.com/
http://www.wildlifeindiatravel.com/
http://www.pushkartravels.com/
http://www.udaipurtravels.com/
http://www.agratravels.com/
http://indiantravelguide.co.in/
http://www.traveltoindia.biz/
http://www.info4india.com/
http://www.indiantravelsguide.com/
http://www.indiantravelguideindia.com/
http://rajasthantravelguide.wordpress.com/
http://attractioninjaipur.blogspot.com/
http://jaipurtravelsguide.blogspot.com/
http://hoteles-en-la-india.blogspot.com/
http://jaipurguiadeviajes.blogspot.com/
http://jaipurrajasthanindia.blogspot.com/
http://travelguiderajasthan.blogspot.com/
http://travelguiderajasthan.blogspot.com/
http://wildlifeindiatravel.blogspot.com/
http://indiantravelguideindia.blogspot.com/
http://indianhotelsindia.blogspot.com/
http://rajasthantravelsguide.blogspot.com/
http://pushkartravels.blogspot.com/
http://indiatravelsguide.blogspot.com/
http://goaguidedevoyage.blogspot.com/
http://goatravelsguide.blogspot.com/
http://indiaguadeviajes.blogspot.com/
http://htelseninde.blogspot.com/
http://indienguidedevoyage.blogspot.com/
http://indianhillstationsindiatravel.blogspot.com/
http://jaipurtravelguiderajasthan.blogspot.com/
http://hillstationinindia.blogspot.com/
http://jaipurcarrentals.blogspot.com/
http://indiabudgettourtravelpackage.blogspot.com/
http://romanticrajasthanhoneymoontour.blogspot.com/
http://indianadventuretourpackages.blogspot.com/
http://rajasthantraintourpackage.blogspot.com/
http://jaipurinformationguide.blogspot.com/
http://rajasthanculturaltourpackages.blogspot.com/
http://uttaranchaltourpackages.blogspot.com/
http://luxurytourspackages.blogspot.com/
http://indiahimalayatourtravelpackages.blogspot.com/
http://eastindiatravelpackages.blogspot.com/
http://indiahillstationstravelpackages.blogspot.com/
http://indiatravelpackages.blogspot.com/
http://westindiatourpackages.blogspot.com/
http://lehladakhtourpackages.blogspot.com/
http://indiabudgettourtravelpackage.blogspot.com/
http://romanticrajasthanhoneymoontour.blogspot.com/
http://worldtravelsguide.blogspot.com/
http://jaipursilverjewellery.com/
http://www.jaipurhandicrafts.com/