My New and Improved "Updating" Ad

A while ago I created a post about an image ad I created using php’s image control capabilities. Well, that one didn’t work as well as I’d hoped. It was supposed to pull information from the database to display the bestsellers in an ad automatically. However, because the bestsellers data doesn’t hang around forever, the ad ended up empty a few times.



Here’s my new one that should always have data in it. The difference is that this one pulls information from the orders data instead.



-------------------------------------------



Before (PNG file)







-------------------------------------------



After (PHP file)







-------------------------------------------



The code



```php

class database {
public $db = false;

function __construct($db_host,$db_user,$db_pass,$db_name) {
$connection = mysql_connect($db_host,$db_user,$db_pass);
if($connection) {
$dbase = mysql_select_db($db_name,$connection);
if($dbase) {
$this->db = true;
return true;
} else {
$this->db = false;
return false;
}
} else {
$this->db = false;
return false;
}
}

function select($fields, $tableName, $where="", $groupby="", $orderby="", $limit="") {
if (!$this->db) {
$data = array();
} else {
$fieldCount = count($fields);
$sql = "SELECT " . implode(",",$fields) . " FROM $tableName" . (($where == "")?"":" WHERE $where") . (($groupby == "")?"":" GROUP BY $groupby") . (($orderby == "")?"":" ORDER BY $orderby") . (($limit == "")?"":" LIMIT " . $limit);
$result = mysql_query($sql);
if (!$result) {
$data = array();
} else {
if (mysql_num_rows($result) == 0) {
$data = array();
} else {
$n = 0;
while($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
for ($j=0; $j<$fieldCount; $j++) {
$field = $fields[$j];
if(substr_count($field,"AS") > 0) {
$field = preg_replace('/.+AS[^A-Z]+([A-Z_]+)[^A-Z]{0,1}/i', '\1', $field);
}
if($fieldCount == 1) {
$data[$n] = str_replace("

\r","


\r",$row[$field]);
} else {
$data[$n][$j] = str_replace("

\r","


\r",$row[$field]);
}
}
$n = $n + 1;
}
}
}
}
return $data;
}

function non_select_query($query) {
if (!$this->db) {
return false;
} else {
$result = mysql_query($query);
if (!$result) {
return false;
} else {
return true;
}
}
}
}

$database = new database("dbhost","dbuser","dbpass","dbname");

$field = array("product_id");
$bestseller = $database->select($field,"cscart_order_details","","product_id","SUM(amount) DESC","0,3");

header("Content-type: image/png");
$im = imagecreatefrompng("/.../images/link2me/store_bestsellers.png");
$textcolor = imagecolorallocate($im, 0, 0, 0);
$arr = array();
for($i=0;$i $field = array("product");
$bsnames = $database->select($field,"cscart_product_descriptions","product_id = " . $bestseller[$i],"product_id");
if($bsnames[0] == null) {
$bestseller = array_splice($bestseller,$i,1,$arr);
}
}
for($i=0;$i $field = array("product");
$bsnames = $database->select($field,"cscart_product_descriptions","product_id = " . $bestseller[$i],"product_id");

imagestring($im, 2.5, 237, 20 + (16 * $i), $bsnames[0], $textcolor);
}
imagestring($im, 3, 237, 70, "Stop by and check them out! =)", $textcolor);
imagepng($im);
imagedestroy($im);
?>

```

Matt,



How would one integrate this php code, where should i require this file into?



Thanks,

sraza

This file itself becomes an image file so all one would need to do is to create a listmania item with the following code: