0% found this document useful (0 votes)
123 views10 pages

Ad Rotation

The document contains code snippets for displaying product categories and items from a database on an e-commerce website. It includes PHP code to: 1. Connect to a database and retrieve category and item data from various tables 2. Display category names as links that filter the items list when clicked 3. On the item page, display the item name, image, description, price, colors and sizes from joined tables

Uploaded by

madhawaranawake
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views10 pages

Ad Rotation

The document contains code snippets for displaying product categories and items from a database on an e-commerce website. It includes PHP code to: 1. Connect to a database and retrieve category and item data from various tables 2. Display category names as links that filter the items list when clicked 3. On the item page, display the item name, image, description, price, colors and sizes from joined tables

Uploaded by

madhawaranawake
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Ad Rotation Overview 1.

Random number 1-100 - if result = 1-70 (we have opportunity to display ad 1, 70%) - if result = 71-90 (we have opportunity to display ad 2, 20%) - if result = 91-100 (we have opportunity to display ad 3, 10%)

<?php // random number 1 - 100 $result_random=rand(1, 100); // if result less than or equal 70, display ad 1 (70%) if($result_random<=70){ echo "Display ad1"; } elseif($result_random<=90){ echo "Display ad2"; } elseif($result_random<=100){ echo "Display ad3"; } ?> Overview In this tutorial create 1 file 1. counter.php Step

1. Create table "counter" in database "test". 2. Create file counter.php. CREATE TABLE `counter` ( `counter` int(9) NOT NULL default '0' ) TYPE=MyISAM;

?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect to server "); mysql_select_db("$db_name")or die("cannot select DB") $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); $counter=$rows['counter']; // if have no counter value set counter = 1 if(empty($counter)){ $counter=1; $sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')"; $result1=mysql_query($sql1); }

echo "You 're visitors No. "; echo $counter; // count more value $addcounter=$counter+1; $sql2="update $tbl_name set counter='$addcounter'"; $result2=mysql_query($sql2); mysql_close(); ?>

<HEAD> <meta http-equiv='refresh' content='2;url='file_name or URL'> </HEAD> // content = time (second) // file_name = name of file you want to refresh or redirect header("location:http://www.google.com"); or echo "<meta http-equiv='refresh' content='0;url=http://www.google.com'>";

Overview In this tutorial create 1 file 1. user_online.php Step

1. Create table "user_online" in mysql in database "test". 2. Create file user_online.php.

CREATE TABLE `user_online` ( `session` char(100) NOT NULL default '', `time` int(11) NOT NULL default '0' ) TYPE=MyISAM;

<? session_start(); $session=session_id(); $time=time(); $time_check=$time-600; //SET TIME 10 Minute $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="user_online"; // Table name // Connect to server and select databse mysql_connect("$host", "$username", "$password")or die("cannot connect to server"); mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name WHERE session='$session'"; $result=mysql_query($sql); $count=mysql_num_rows($result);

if($count=="0"){ $sql1="INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')"; $result1=mysql_query($sql1); } else { "$sql2=UPDATE $tbl_name SET time='$time' WHERE session = '$session'"; $result2=mysql_query($sql2); } $sql3="SELECT * FROM $tbl_name"; $result3=mysql_query($sql3); $count_user_online=mysql_num_rows($result3); echo "User online : $count_user_online "; // if over 10 minute, delete session $sql4="DELETE FROM $tbl_name WHERE time<$time_check"; $result4=mysql_query($sql4); mysql_close(); // Open multiple browser page for result ?>

$today=date(l); // Find what today is? using date function // If today is Monday displays message "Today is Monday" and displays image1.gif if($today==Monday){ // Compare $today with name of the day.

echo "Today is Monday"; echo "<BR>"; // Line break echo "<img src='images/image1.gif'>"; // images keep in forder "images" }

<? $today=date(l); // Find what today is? using date function

if($today==Monday){ echo "Today is Monday"; echo "<BR>"; echo "<img src='images/image1.gif'>"; } elseif($today==Tuesday){ echo "Today is Tuesday"; echo "<BR>"; echo "<img src='images/image2.gif'>"; } elseif($today==Wednesday){ echo "Today is Wednesday"; echo "<BR>"; echo "<img src='images/image3.gif'>"; } elseif($today==Thursday){

echo "Today is Thursday"; echo "<BR>"; echo "<img src='images/image4.gif'>"; } elseif($today==Friday){ echo "Today is Friday"; echo "<BR>"; echo "<img src='images/image5.gif'>"; } elseif($today==Saturday){ echo "Today is Saturday"; echo "<BR>"; echo "<img src='images/image6.gif'>"; } elseif($today==Sunday){ echo "Today is Sunday"; echo "<BR>"; echo "<img src='images/image7.gif'>"; } ?>

store_categories store_items store_item_size store_item_color

id, cat_title, cat_desc id, cat_id, item_title, item_price, item_desc, item_image item_ id, item_size item_id, item_color

Script to View Categories <?php //connect to database $conn = mysql_connect("localhost", "joeuser", "somepass"); mysql_select_db("testDB",$conn) or die(mysql_error()); $display_block = "<h1>My Categories</h1> <P>Select a category to see its items.</p>"; //show categories first $get_cats = "select id, cat_title, cat_desc from store_categories order by cat_title"; $get_cats_res = mysql_query($get_cats) or die(mysql_error()); if (mysql_num_rows($get_cats_res) < 1) { $display_block = "<P><em>Sorry, no categories to browse.</em></p>"; } else { while ($cats = mysql_fetch_array($get_cats_res)) { $cat_id = $cats[id]; $cat_title = strtoupper(stripslashes($cats[cat_title])); $cat_desc = stripslashes($cats[cat_desc]); $display_block .= "<p><strong><a href=\"$_SERVER[PHP_SELF]?cat_id=$cat_id\">$cat_title</a></strong> <br>$cat_desc</p>"; if ($_GET[cat_id] == $cat_id) { //get items $get_items = "select id, item_title, item_price from store_items where cat_id = $cat_id order by item_title"; $get_items_res = mysql_query($get_items) or die(mysql_error()); if (mysql_num_rows($get_items_res) < 1) { $display_block = "<P><em>Sorry, no items in this category.</em></p>"; } else { $display_block .= "<ul>"; while ($items = mysql_fetch_array($get_items_res)) { $item_id = $items[id]; $item_title = stripslashes($items[item_title]); $item_price = $items[item_price];

$display_block .= "<li><a href=\"showitem.php?item_id=$item_id\">$item_title</a> </strong> (\$$item_price)"; } $display_block .= "</ul>"; } } } } ?> <HTML> <HEAD> <TITLE>My Categories</TITLE> </HEAD> <BODY> <? print $display_block; ?> </BODY> </HTML>

Script to View Item Information 1: <?php 2: //connect to database 3: $conn = mysql_connect("localhost", "joeuser", "somepass") 4: or die(mysql_error()); 5: mysql_select_db("testDB",$conn) or die(mysql_error()); 6: 7: $display_block = "<h1>My Store - Item Detail</h1>"; 8: 9: //validate item 10: $get_item = "select c.cat_title, si.item_title, si.item_price, 11: si.item_desc, si.item_image from store_items as si left join 12: store_categories as c on c.id = si.cat_id where si.id = $_GET[item_id]"; 13: 14: $get_item_res = mysql_query($get_item) or die (mysql_error()); 15: 16: if (mysql_num_rows($get_item_res) < 1) { 17: //invalid item 18: $display_block .= "<P><em>Invalid item selection.</em></p>"; 19: } else { 20: //valid item, get info 21: $cat_title = strtoupper(stripslashes( 22: mysql_result($get_item_res,0,'cat_title'))); 23: $item_title = stripslashes(mysql_result($get_item_res,0,'item_title')); 24: $item_price = mysql_result($get_item_res,0,'item_price'); 25: $item_desc = stripslashes(mysql_result($get_item_res,0,'item_desc')); 26: $item_image = mysql_result($get_item_res,0,'item_image'); 27: 28: //make breadcrumb trail 29: $display_block .= "<P><strong><em>You are viewing:</em><br> 30: <a href=\"seestore.php?cat_id=$cat_id\">$cat_title</a> 31: &gt; $item_title</strong></p> 32:

33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: }

<table cellpadding=3 cellspacing=3> <tr> <td valign=middle align=center><img src=\"$item_image\"></td> <td valign=middle><P><strong>Description:</strong><br>$item_desc</p> <P><strong>Price:</strong> \$$item_price</p>"; //get colors $get_colors = "select item_color from store_item_color where item_id = $item_id order by item_color"; $get_colors_res = mysql_query($get_colors) or die(mysql_error()); if (mysql_num_rows($get_colors_res) > 0) { $display_block .= "<P><strong>Available Colors:</strong><br>"; while ($colors = mysql_fetch_array($get_colors_res)) { $item_color = $colors['item_color']; $display_block .= "$item_color<br>"; } } //get sizes $get_sizes = "select item_size from store_item_size where item_id = $item_id order by item_size"; $get_sizes_res = mysql_query($get_sizes) or die(mysql_error()); if (mysql_num_rows($get_sizes_res) > 0) { $display_block .= "<P><strong>Available Sizes:</strong><br>"; while ($sizes = mysql_fetch_array($get_sizes_res)) { $item_size = $sizes['item_size']; $display_block .= "$item_size<br>"; }

$display_block .= " </td> </tr> </table>"; } ?> <HTML> <HEAD> <TITLE>My Store</TITLE> </HEAD> <BODY> <? print $display_block; ?> </BODY> </HTML>

You might also like