PHP Snippet

Snippet Home Page Ads PHP Example

 

 

<!-- begin homepage ads -->
 <?php
 $currentpage = $_SERVER['REQUEST_URI'];
 if($currentpage=="/" || $currentpage=="/index.php" || $currentpage=="" ) {
 include('homepage-ads.php');
 }
 ?>
 <!-- end homepage ads -->



Tags:
By Jeffrey on January 19, 2010 | PHP Snippet

Snippet Dynamic Robots.txt File Example

 

.htaccess

RewriteRule ^urllist.txt$ /urllist.txt.php

robot.txt.php

<?php
header('Content-type: text/plain');
echo "User-agent: *\r\n";
echo "Disallow: \r\n";
echo "SITEMAP: http://".$_SERVER["HTTP_HOST"]."/urllist.txt\r\n";
?>



Tags:

Different php include based on URL variables

 

To have different text included in a div based on a variable in the url.

For instance, http://www.example.com/?formType=AIR-CAR-HOTEL
would include air-car-hotel.php

and

http://www.example.com/?formType=CAR-HOTEL
would include car-hotel.php

<?php
#if(!isset($_GET['formType'])) exit;
#
switch($_GET['formType']) {
case 'AIR-ONLY':
$page = 'air-only.php';
break;
case 'AIR-CAR':
$page = 'air-car.php';
break;
case 'AIR-CAR-HOTEL':
$page = 'air-car-hotel.php';
break;
case 'AIR-HOTEL':
$page = 'air-hotel.php';
break;
case 'CAR-HOTEL':
$page = 'car-hotel.php';
break;
default:
$page = 'air-only.php';
}
include('include/$page');
?> 



Tags:

Snippet Inserting an array into the database example

In order to maintain an ID to item association, your application will need to check to see if the item is already in the database and then UPDATE if so, otherwise INSERT. An example of how this might look in your record handler function would be as follows:

<?php
 function myRecordHandler($record)
 {
 $sql = "SELECT id FROM items WHERE name='".mysql_escape_string($record["NAME"])."'";
 if (mysql_num_rows(mysql_query($sql))
 {
 $sql = "UPDATE items SET
 foo='".mysql_escape_string($record["FOO"])."',
 bar='".mysql_escape_string($record["BAR"])."'
 WHERE
 name='".mysql_escape_string($record["NAME"])."'
 ";
 }
 else
 {
 $sql = "INSERT INTO items SET
 name='".mysql_escape_string($record["NAME"])."',
 foo='".mysql_escape_string($record["FOO"])."',
 bar='".mysql_escape_string($record["BAR"])."'
 ";
 }
 mysql_query($sql);
 }
?> 



Tags:
By Jeffrey on | PHP Snippet
Tags: