Jump to content
Sign in to follow this  
lynkfs

php

Recommended Posts

A web application ends up being encoded in html, js and css.
php is a language which can be used to create these components, but more importantly can also be used within all 3 of these types of files.
That gives some interesting possibilities.

To make that happen, php needs to be activated serverside. Many server hosting environments come with php pre-installed.
Activating php so that it can be used at the time of serving regular .html, .js or .css files by the browser depends on the type of server.
Regular unix based servers make use of a .htaccess file which needs a line like

AddHandler application/x-httpd-php5 .html .htm

Windows servers have another mechanism

1 - Using php within Smart

Php can be invoked like this, where the php statements are inclosed in a <?php block.

asm
    var i = 0;
    i = <?php echo 35; ?>;
    alert(i);
end;

A practical use would be to include server side scripts normally accessed through a POST request (which eliminates a server round trip as well).
The below code reads a MySqL database at form init

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components

var response : variant := new JObject;

asm
@response =
<?php
$link = mysql_pconnect("...domain...", "...user...", "...password...") or die("Could not connect");
mysql_select_db("...database...") or die("Could not select database");

$sql_statement = 'Select * from FishFacts';

$arr = array();
$rs = mysql_query($sql_statement);
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
echo '{"rows":'.json_encode($arr).'}';

mysql_close($link);
?>;
end;  

writeln(response.rows[0].Common_Name);   //Clown Triggerfish


2 - Using php within Html files
A (possibly) usefull example scenario would be to separate web-pages into a header.html, a regular Smart index.html and a footer file.
php has an include mechanism, which can be used like so

<body>
<?php include "header.html" ?>

  <script type="text/javascript">    /* This prevents the window being moved by touches,
      to give the impression of a native app */
    document.ontouchmove = function(e) { e.preventDefault(); }
      </script>

    <script type="text/javascript" src="main.js.php"></script>

</body>


3 - Using php within css files
Similarly stylesheets can be scripted, something like this snippet where, within the stylesheet, the current date is checked which sets an image url

<?php 
 $month = date('m');
 $day = date('d');
 if($month=='12' && $day=='25') {
    $logoSrc = 'images/holidayLogo.png';
 } else {
    $logoSrc = 'images/logo.png';
 }
?>
h1 {
 background: url(<?=$logoSrc?>) no-repeat;
}

Demo (of 1 and 2)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×