PHP Classes

File: get_banner_data.php

Recommend this page to a friend!
  Classes of Olaf Lederer   Import templates   get_banner_data.php   Download  
File: get_banner_data.php
Role: Application script
Content type: text/plain
Description: example file to handle TM data for banners
Class: Import templates
Import data about TemplateMonster.com templates
Author: By
Last change:
Date: 19 years ago
Size: 8,318 bytes
 

Contents

Class file image Download
<?php
include($_SERVER['DOCUMENT_ROOT']."/classes/import_templates/import_templates.php");

function
microtime_float() {
   list(
$usec, $sec) = explode(" ", microtime());
   return ((float)
$usec + (float)$sec);
}
$time_start = microtime_float();

class
Banner_builder extends Import_templates {
   
    var
$thumbs = array();
    var
$num_records;
   
    function
get_latest_template_id() {
       
$last = $this->last_template_id();
       
$this->settings['delim'] = DELIMITER;
       
$this->settings['order'] = "desc";
       
$this->settings['from'] = $last - 1;
       
$options = $this->build_options($this->settings);
        if (
$this->get_records(REMOTE_HOST."templates_screenshots4.php".$options)) {
           
$tpl_num = substr($this->all_data[0], 0, strpos($this->all_data[0], DELIMITER));
            unset(
$this->all_data);
            return
$tpl_num;
        } else {
           
$this->messages[] = $this->error_list(20);
            return;
        }
    }
   
// filter = 1 (website templates only) check the webapi specs for more info
   
function get_banner_thumbdata($start, $end = "", $filter = 1) {
       
$this->settings['delim'] = DELIMITER;
       
$this->settings['from'] = $start;
        if (
$end != "") $this->settings['to'] = $end;
       
$this->settings['filter'] = $filter;
       
$this->settings['list_begin'] = LIST_BEGIN;
       
$this->settings['list_delim'] = LIST_DELIM;
       
$this->settings['list_end'] = LIST_END;
       
$this->settings['full_path'] = "false";
       
$this->settings['currency'] = CURRENCY;
       
$this->import_data(SS_TABLE, "templates_screenshots4.php", "yes");
    }
    function
copy_banner_thumbs($first) {
       
$last = $this->last_template_id();
       
$tpl_ids = $this->get_new_tpl_ids($first, $last);
        if (
mysql_num_rows($tpl_ids) > 0) {
           
$counter = 0;
            while (
$tpl = mysql_fetch_object($tpl_ids)) {
                if (!
$this->copy_thumbs($tpl->id, true)) $counter++;
            }
            if (
$counter == 0) {
               
$this->messages[] = $this->error_list(50);
            } else {
               
$this->messages[] = $this->error_list(24, $counter);
            }
        }
    }
    function
get_rand_thumb_ids($limit) {
       
$thumb_ids = array();
       
$sql= sprintf("SELECT id FROM %s ORDER BY RAND()", SS_TABLE);
       
$result = mysql_query($sql);
        while (
$obj = mysql_fetch_object($result)) {
           
$thumb_ids[] = $obj->id;
        }
       
$thumb_rand_array = array_rand($thumb_ids, $limit);
       
$id_string = "";
        foreach (
$thumb_rand_array as $val) {
           
$id_string .= "'".$thumb_ids[$val]."', ";
        }
       
$id_string = rtrim($id_string, ", ");
        return
$id_string;
    }
    function
create_thumb_data($limit = 3) {
       
$search_in = $this->get_rand_thumb_ids($limit);
       
$sql = sprintf("SELECT id, price FROM %s WHERE id IN (%s) ORDER BY date_added", SS_TABLE, $search_in);
       
$result = mysql_query($sql);
        if (!
$result) {
           
$this->messages[] = $this->error_list(13);
        } else {
           
$this->num_records = mysql_num_rows($result);
            while(
$obj = mysql_fetch_object($result)) {
               
$this->thumbs['thumb'][] = $obj->id."-m.jpg";
               
$this->thumbs['price'][] = $obj->price;
               
$this->thumbs['id'][] = $obj->id;
            }
           
mysql_free_result($result);
        }
    }
}

$test_banner = new Banner_builder;
$max_records = 20;
$last_id = $test_banner->get_latest_template_id();
if (isset(
$_POST['Submit'])) {
    if (
intval($_POST['start_num']) == 0 || $_POST['start_num'] > $last_id) {
       
$test_banner->messages[] = "The value in the field <b>Starting template ID</b> is not valid.";
    } else {
        if (
intval($_POST['end_num'] > 0) && $_POST['end_num'] < $last_id) {
            if (
$_POST['end_num'] > $_POST['start_num']) {
                if ((
$_POST['end_num'] - $_POST['start_num']) <= $max_records) {
                   
$start = $_POST['start_num'];
                   
$end = $_POST['end_num'];
                   
$test_banner->get_banner_thumbdata($start, $end); // use this function to update the template data
                   
$test_banner->delete_old_thumbs(); // first remove the old thumbs
                   
$test_banner->copy_banner_thumbs($start); // this function will copy the thumbbails.
               
} else {
                   
$test_banner->messages[] = "You're above the maximum of templates that the script at once.";
                }
            } else {
               
$test_banner->messages[] = "The ending ID must be smaller the starting ID number.";
            }
        } elseif (
$_POST['end_num'] == "") {
           
$start = $_POST['start_num'];
           
$end = "";
           
$test_banner->get_banner_thumbdata($start, $end);
           
$test_banner->delete_old_thumbs(); // first remove the old thumbs
           
$test_banner->copy_banner_thumbs($start);
        } else {
           
$test_banner->messages[] = "The value in the field <b>Ending template ID</b> is not valid.";
        }
    }
}
   
$test_banner->create_thumb_data(4); // create 4 thumbnails

$time_end = microtime_float();
$time = $time_end - $time_start;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Example page: Build banners with data from templateMonster.com</title>
<style type="text/css">
<!--
body {
    font:0.9em Verdana, Arial, Helvetica, sans-serif;
    margin:10px;
    text-align:center;
}
#thumb {
    width: 160px;
    height:200px;
    border: 1px solid #333333;
    padding:0;
    float: left;
    margin-right:10px;
    text-align:center;
}
#thumb p {
    background-color:#CCCCCC;
    border-bottom:2px solid #333333;
    margin:0 0 10px 0;
    line-height:25px;
}
#container {
    width: 740px;
    margin:0 auto;
    text-align:left;
    border:1px solid #999999;
    padding:10px;
}
label {
    display:block;
    float:left;
    width:155px;
    text-align:right;
    margin-right:5px;
}
-->
</style>
<script type="text/javascript">
<!--
function window_open(page, name, x, y) {
    newWindow = window.open(page,name,'scrollbars=yes,width='+x+',height='+y+',left=50,top=100');
    newWindow.focus();
}
if (opener.location.href == document.referrer) {
    window.resizeTo(800,800);
}
-->
</script>
</head>

<body>
<div id="container">
<h2 style="margin-top:15px;">Processing template data for a random banner</h2>
<p>This example show some random templates based on some selected templates stored in the database. <br>
The thumbnail location is local and the preview data is on a remote location. </p>
<p><i>Use this form to update some template data. </i><br>
Enter a template ID as start number and an end number, the end number must be smaller then the start number and the the last one. <br>
The maximum number of templates which can be handled by executing this script is: <b><?php echo $max_records; ?></b> templates</p>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <p><label>Starting template ID</label>
  <input name="start_num" type="text" size="5" value="<?php if (isset($_POST['start_num'])) echo $_POST['start_num']; ?>"></p>
  <p><label>Ending template ID</label>
  <input name="end_num" type="text" size="5" value="<?php if (isset($_POST['end_num'])) echo $_POST['end_num']; ?>">
  <input type="submit" name="Submit" value="Submit">
  (last template ID is <b><?php echo $last_id; ?></b>)</p>
</form>
<?php
echo "<p style=\"color:red;\">".$test_banner->show_messages("<br>\n")."&nbsp;</p>\n";
if (isset(
$test_banner->num_records)) {
    echo
"<h3>Example output:</h3>\n";
    for (
$i = 0; $i < $test_banner->num_records; $i++) {
       
$image = THUMB_FOLDER.$test_banner->thumbs['thumb'][$i];
       
$img_size = getimagesize($_SERVER['DOCUMENT_ROOT'].$image);
       
$tpl_price = $test_banner->thumbs['price'][$i];
       
$tpl_id = $test_banner->thumbs['id'][$i];
       
$url = htmlspecialchars("http://www.templatehelp.com/aff/preview.php?aff=olaf&apr=yes&src=yes&nt=yes&help=yes&sample=yes&faq=yes&nmg=yes&auth=yes&down=yes&hide_flash=0&i=".$tpl_id);
        echo
"
        <div id=\"thumb\">
          <p><b>Price: $"
.$tpl_price."</b></p>
          <a href=\"#\" onClick=\"window_open('"
.$url."', 'preview', 780, 540)\"><img src=\"".$image."\" ".$img_size[3]." border=\"0\"></a>
        </div>\n"
;
    }
}
// end output example
echo "<br clear=\"all\">\n";
echo
"<p><br><br>The script's execution time was ".number_format($time, 4, ".", "")." seconds...<br></p>\n";
?>
<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Refresh this page!</a> (to notice the random effect)</p>
</div>
<p style="margin-top:20px;">More classes and scripts on: <a href="http://www.finalwebsites.com/snippets.php">www.finalwebsites.com</a></p>

</body>
</html>