Layout Skinning Tutorial

January 30th, 2005 | Comments

Alright! For the long-awaited Skinning Tutorial!^^ Credits to my guardian angel, Maddy for the script. *hugs* I edited it to be more compatible with the latest versions of PHP.

  <HTML>

  <head>
  <title>Site Name Here</title>
  <link rel="stylesheet" type="text/css" href="style1.css" />
  </head>

  <body>

  <div align="center">
  <center>
  <br>
  <table border="1" cellpadding="2" cellspacing="0" width="750">

  <tr>
  <td colspan="3">
  <img border="0" src="topbanner1.jpg" alt="Top Banner One" />
  </td>
  </tr>
  <tr>
  <td valign="top" width="150" height="32">
  <h1>Navigation</h1>
  : : <a href="links.php">Link Here</a><br>
  : : <a href="links.php">Link Here</a><br>
  : : <a href="links.php">Link Here</a><br>
  : : <a href="links.php">Link Here</a><br><br>

  </td>

  <td valign="top" width="600" height="32" >

////main content of your site here...


  </td>

  </table>
  </center>
  </div>

  </body>

  </html>

Take note of the bold characters for they indicate the elements that are to be distinct in each layout. For the top codes, since we’re using layout1, the elements are topbanner1.jpg and style1.css. In the same way, when you create the other two layouts, layout2 should have topbanner2.jpg and style2.css, while layout3 has topbanner3.jpg and style3.css.

Now let’s separate the codes of the first layout into three parts. The first part being:

  <HTML>

  <head>
  <title>Site Name Here</title>
  <link rel="stylesheet" type="text/css" href="style1.css" />
  </head>

  <body>

  <div align="center">
  <center>
  <br>
  <table border="1" cellpadding="2" cellspacing="0" width="750">

  <tr>
  <td colspan="3">
  <img border="0" src="topbanner1.jpg" alt="Top Banner One" />
  </td>

As you can see, the top code includes all the elements that are variable in between skinning, the top banner and the style sheet. Save this file as layout1.txt. Do the same with the other two layouts, saving the top codes as layout2.txt and layout3.txt respectively. This will be the last time you will have to handle the other two layouts. From here on out the rest of the skinning process can be achieved just by using the first layout since, other than the top banner and CSS, the three layouts are identical.

Now let’s move on to the next part of your code (still using layout1). This will create a file for all the "constants" of your site, meaning all the parts that will not change throughout your site. (See the tutorial on PHP Includes for more info).

  <tr>
  <td valign="top" width="150" height="32">
  <p>Choose a SKIN</p>
  <center>
  <a href="main.php?mode=style1">Layout 1 Title</a><br>
  <a href="main.php?mode=style2">Layout 2 Title</a><br>
  <a href="main.php?mode=style3">Layout 3 Title</a><br>
  <br><br>
  <h1>Navigation</h1>
  : : <a href="links.php">Link Here</a><br>
  : : <a href="links.php">Link Here</a><br>
  : : <a href="links.php">Link Here</a><br>
  : : <a href="links.php">Link Here</a><br><br>

  </td>

  <td valign="top" width="600" height="32" >

Now save this part of the code as links.php. As you can see it’s almost identical to the original code. I’ve added the text links in bold letters that will enable visitors to choose the skin they want. You can also use small screencaps of your layouts as links to skinning if you want, but please link them in the same way.

For the rest of the code…

  <?php
  include "header.txt";
  include "links.txt";
  ?>

////main content of your site here...

  </td>

  </table>
  </center>
  </div>

  </body>

  </html>

Save this file as main.php. Notice we’ve added a PHP code up top that will call for the files header.txt and links.txt to be included? (See the PHP Includes tutorial on an in-depth explanation). This file will contain the main content of your index page and will be the format of your succeeding pages. For example, you want to create a new file, an “about me” page, so this is what you have to do:

  <?php
  include "header.txt";
  include "links.txt";
  ?>

  <p>About Me</p>
  Likes: Chocolate<br>
  Dislikes: Arrogant people<br>
  More....

  </td>

  </table>
  </center>
  </div>

  </body>

  </html>

Save that file as aboutme.php and link to it by editing links.txt . Make sure you save it with a .php extension, otherwise the PHP script wouldn’t work.

Now for the magic! ^^ Copy the following code and save it as header.txt.

  <?php

  session_start();

  if(!isset($_GET['mode'])){

  $_GET['mode']='default'; //set default sheet

  }

  switch($_GET['mode']){

  case 'default':

  if(empty($styled)){

  include "layout1.txt";

  }

  elseif(!empty($styled)){

  if($styled=="style1"){

  include "layout1.txt";

  }

  if($styled=="style2"){

  include "layout2.txt";

  }

  if($styled=="style3"){

  include "layout3.txt";

  }

  }

  break;

  //====================================

  case 'style1':

  session_register("styled");
  $styled="style1";

  include "layout1.txt";
  break;

  //====================================

  case 'style2':

  session_register("styled");
  $styled="style2";

  include "layout2.txt";
  break;

  //====================================

  case 'style3':

  session_register("styled");
  $styled="style3";

  include "layout3.txt";
  break;

  //====================================

  }

  ?>

And we’re done! Now you can start skinning your site. You can skin as many layouts as you want as long as you keep adding a new style and case in your header.txt. Also, make sure that the paths to your files are correct, especially in header.txt so it can switch to the right header when necessary. To be safe just put all your files in the same folder.

That’s all and have fun!^^

  • Share/Bookmark

Category: PHP, Web Design

PHP CHMOD

January 16th, 2005 | Comments

This PHP CHMODing tutorial was contributed by Azekeal.

Note that mode is not automatically assumed to be an octal value, so strings (such as “g+w”) will not work properly. To ensure the expected operation, you need to prefix mode with a zero (0): IE, not 666, but 0666 .

bool chmod ( string filename, int mode);

example:

chmod("somefile.php", 0666);
  • Share/Bookmark

Category: PHP, Web Design

Connecting to MySQL

January 15th, 2005 | Comments

MySQL Tutorial contributed by Azekeal.

MySQL? Right then, firstly, SQL stands for structured query language

Anyway, here’s a quick PHP class for mysql connections:

<?php
// helper mysql function

class DB
{
// fill in these values for your s
var $host = "localhost";
var $db = "database";
var $user = "username";
var $pass = "p4Ssw0rD";

var $link;
var $sys;

function DB()
{
// calls the shutdown function at the end
register_shutdown_function( array( &$this, "_DB" ) );
$this->connected = false;
}
function connect()
{
$this->connected = true;

// connect to mysql
$this->link = mysql_connect($this->host, $this->user, $this->pass)
or die("Could not connect : ($this->host, $this->user) " . mysql_error());
mysql_select_db($this->db) or die("Could not select database");
}

function query($SQL, $singleResult = false, $silent = false)
{
global $DB;

// if not already connected, connect to mysql
if(!$DB->connected)
$DB->connect();

// run the query
$result = mysql_query($SQL);
if($result === false)
{
$returnError = mysql_error();
if(!$silent) error("Query failed : $returnError ($SQL)", __LINE__, __FILE__);
return array();
}

// make an array of data
$data = array();
while ($line = @mysql_fetch_array($result, MYSQL_ASSOC))
$data[] = $line;

@mysql_free_result($result);

// return the data
return ($singleResult===true && isset($data[0]) ? $data[0] : $data);
}
function _DB() // destructor
{
// close the connection
if($this->connected)
@mysql_close($this->link);
}
}

$DB = new DB();
?>

Save this as db.php

4 basic commands you need for sql:

SELECT, INSERT, DELETE, CREATE TABLE

key:
<word> – replace with name of something relevent (IE <table_name> -> mytable)
[ blah....] – stuff between square brackets is optional (IE select … [WHERE a=b])
( x | y ) – must have one of the options specified (IE where a=b (AND|OR) c=d..)

SELECT (*|<column_name>) FROM <table_name> [ WHERE var1=value1 [ (AND|OR)
var2=value2 ] ] [ORDER BY <column_name>] [LIMIT [<offset>,] <num_items> ]

To make a query – first include the file that you paste the above code into (include “db.php”, and then just us the following:

$results_array = DB::query("SELECT stuff FROM Tables_OR_whatever");

*data is returned in the array as follows:

  $results_array =
  Array(
  [0] => Array(
  "stuff" => "value1"

  ),

  [1] => Array(...),
  ...
  )

i.e. the first result from that query would be in $results_array[0]['stuff'];

* the number of results can be found by using count($results_array)

* if the query has no return data, the $results_array will be an empty array

  • Share/Bookmark

Category: MySQL, Web Design