Find mismatch between variables and bound tokens in Yii - mysql

I am using prepared statement to insert data. I am getting the 'Invalid parameter number: number of bound variables does not match number of tokens.' I have tried dumping the prepared statement but I can't spot the mismatch. Is there a way to figure out the mismatch.
$i = 0 ; $j = 0 ; $k = 0 ; $l = 0 ; $str = "" ; $region_str = '' ;
$occasion_str = '' ; $flavor_str = '' ;
foreach($result->WINES->WINE as $w)
{
if($i == 0)
$str = "(:wineid$i,:acctid$i,:brand$i,:varietal$i,:descr$i,:prop$i,:color$i,:case$i,:url$i)" ;
else
$str .= ", (:wineid$i,:acctid$i,:brand$i,:varietal$i,:descr$i,:prop$i,:color$i,:case$i,:url$i)" ;
foreach($w->REGIONS->REGION as $region)
{
if($j == 0)
$region_str = "(:rwineid$i,:region$j)" ;
else
$region_str .= ", (:rwineid$i,:region$j)" ;
$j++ ;
}
foreach($w->OCCASIONS->OCCASION as $o)
{
if($k == 0)
$occasion_str = "(:owineid$i,:occasion$k)" ;
else
$occasion_str .= ", (:owineid$i,:occasion$k)" ;
$k++ ;
}
foreach($w->FLAVORS->FLAVOR as $f)
{
if($l == 0)
$flavor_str = "(:fwineid$l,:flavor$l)" ;
else
$flavor_str .= ", (:fwineid$l,:flavor$l)" ;
$l++ ;
}
$i++ ;
}
$sql = " Delete from wines ;
Insert into wines (wineid,acctid,brandname,varietal,description,propreitaryname,color,caseproduction,url)
values
$str ;
Delete from wine_regions ;
Insert into wine_regions (wineid,region) values $region_str ;
Delete from wine_occasions ;
Insert into wine_occasions (wineid,occasion) values $occasion_str ;
Delete from wine_flavors ;
Insert into wine_flavors (wineid,flavor) values $flavor_str ; " ;
if($str != "")
{
$command = Yii::app()->db->createCommand($sql) ;
// $command_2 = Yii::app()->db->createCommand($sql_2) ;
$i = 0 ; $j = 0 ; $k = 0 ; $l = 0 ;
foreach($result->WINES->WINE as $wine)
{
$command->bindValue(":wineid$i",$wine->WINEID,PDO::PARAM_STR) ;
// $command_2->bindValue(":wineid$i",$i,PDO::PARAM_STR) ;
$command->bindValue(":acctid$i",$wine->ACCTID,PDO::PARAM_STR) ;
$command->bindValue(":brand$i",$wine->BRANDNAME,PDO::PARAM_STR) ;
$command->bindValue(":varietal$i",$wine->VARIETAL,PDO::PARAM_STR) ;
$command->bindValue(":descr$i",$wine->DESCRIPTION,PDO::PARAM_STR) ;
$command->bindValue(":prop$i",$wine->PROPRIETARYNAME,PDO::PARAM_STR) ;
$command->bindValue(":color$i",$wine->COLOR,PDO::PARAM_STR) ;
$command->bindValue(":case$i",$wine->CASEPRODUCTION,PDO::PARAM_STR) ;
$command->bindValue(":url$i",$wine->URL,PDO::PARAM_STR) ;
foreach((array)$wine->REGIONS->REGION as $region)
{
$command->bindValue(":rwineid$j",$wine->WINEID,PDO::PARAM_STR) ;
$command->bindValue(":region$j",$region,PDO::PARAM_STR) ;
$j++ ;
}
foreach((array)$wine->OCCASIONS->OCCASION as $o)
{
$command->bindValue(":owineid$k",$wine->WINEID,PDO::PARAM_STR) ;
$command->bindValue(":occasion$k",$o,PDO::PARAM_STR) ;
$k++ ;
}
foreach((array)$wine->FLAVORS->FLAVOR as $f)
{
$command->bindValue(":fwineid$l",$wine->WINEID,PDO::PARAM_STR) ;
$command->bindValue(":flavor$l",$f,PDO::PARAM_STR) ;
$l++ ;
}
$i++ ;
}
var_dump($command) ;
$command->execute() ;
}

I don't check, if will be error - inform me in the comments. Try my code:
if(!empty($result->WINES->WINE))
{
$i = 0 ; $j = 0 ; $k = 0 ; $l = 0 ;
$arr = $region_arr = $occasion_arr = $flavor_arr = array();
$str = $region_str = $occasion_str = $flavor_str = array();
foreach($result->WINES->WINE as $w)
{
$t = array(
":wineid$i" => $w->WINEID,
":acctid$i" => $w->ACCTID,
":brand$i" => $w->BRANDNAME,
":varietal$i" => $w->VARIETAL,
":descr$i" => $w->DESCRIPTION,
":prop$i" => $w->PROPRIETARYNAME,
":color$i" => $w->COLOR,
":case$i" => $w->CASEPRODUCTION,
":url$i" => $w->URL,
);
$arr = array_merge($arr, $t);
$str[] = implode(',', array_keys($t));
foreach($w->REGIONS->REGION as $region)
{
$t = array(
":rwineid$j" => $w->WINEID,
":region$j" => $region,
);
$region_arr = array_merge($region_arr, $t);
$region_str[] = implode(',', array_keys($t));
$j++ ;
}
foreach($w->OCCASIONS->OCCASION as $o)
{
$t = array(
":owineid$k" => $w->WINEID,
":occasion$k" => $o,
);
$occasion_arr = array_merge($occasion_arr, $t);
$occasion_str[] = implode(',', array_keys($t));
$k++ ;
}
foreach($w->FLAVORS->FLAVOR as $f)
{
$t = array(
":fwineid$l" => $w->WINEID,
":flavor$l" => $f,
);
$flavor_arr = array_merge($flavor_arr, $t);
$flavor_str[] = implode(',', array_keys($t));
$l++ ;
}
$i++ ;
}
$sql = " Delete from wines ;
Insert into wines (wineid,acctid,brandname,varietal,description,propreitaryname,color,caseproduction,url)
values
(".implode('), (', $str).") ;
Delete from wine_regions ;
Insert into wine_regions (wineid,region) values (".implode('), (', $region_str).") ;
Delete from wine_occasions ;
Insert into wine_occasions (wineid,occasion) values (".implode('), (', $occasion_str).") ;
Delete from wine_flavors ;
Insert into wine_flavors (wineid,flavor) values (".implode('), (', $flavor_str).") ; " ;
$command = Yii::app()->db->createCommand($sql) ;
$command->bindValues($arr);
$command->bindValues($region_arr);
$command->bindValues($occasion_arr);
$command->bindValues($flavor_arr);
$command->execute() ;
}

Related

Hi i need to add multiple minimum minimum_order_amount for other postcodes

i'm using this code that i found here! the problem is i need to add multiple minimum minimum_order_amount for other postcodes! Help!
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
$minimum = 20; // Set the minimum order value
$postcodes = array('26224', '26222', '26442', '26443'); // Define your targeted postcodes in the array
$postcode = ''; // Initializing
if ( isset($_POST['shipping_postcode']) && ! empty($_POST['shipping_postcode']) ) {
$postcode = $_POST['shipping_postcode'];
if ( empty($postcode) && isset($_POST['billing_postcode']) && ! empty($_POST['billing_postcode']) ) {
$postcode = $_POST['billing_postcode'];
}
} elseif ( $postcode = WC()->customer->get_shipping_postcode() ) {
if ( empty($postcode) ) {
$postcode = WC()->customer->get_billing_postcode();
}
}
if ( WC()->cart->total < $minimum && ! empty($postcode) && in_array( $postcode, $postcodes ) ) {
$error_notice = sprintf( 'Η παραγγελία σου είναι %s - Ελάχιστη Παραγγελία %s ' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
);
if( is_cart() ) {
wc_print_notice( $error_notice, 'error' );
} else {
wc_add_notice( $error_notice, 'error' );
}
}
}

Print HTML Table from an array with condition with Perl CGI

i have made a script that returns me an array with several lines like :
DATA:VALUE:VALUE_MAX
I need to fill a table with those value like :
NAME | Status
--------------------------
DATA | OK/minor/warning...
.... | .........
.... | .........
with VALUE and VALUE_MAX i calculate the percent wich give me the status.
here is my code for print the table :
my #i = my_status();
print <<END;
<div class="container">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
END
my $inc = 0;
while (#i) {
my #temp = split /:/, #i[$inc];
my $name = $temp[0];
my $percent = ($temp[1] * $temp[2] / 100);
my $status = undef;
if ($percent <= 24 ) {
print "<tr class='info'>";
$status = "Critical !";
}
elsif ($percent <= 49 ) {
print "<tr class='danger'>";
$status = "Danger !";
}
elsif ($percent <= 74 ) {
print "<tr class='warning'>";
$status = "Warning";
}
elsif ($percent <= 99 ) {
print "<tr class='active'>";
$status = "Minor";
}
elsif ($percent == 100 ) {
print "<tr class='success'>";
$status = "OK";
}
print "<td>$name</td>";
print "<td>$status</td>";
print "</tr>";
$inc++;
}
print <<END;
</tbody>
</table>
</div>
END
My script "my_status" is a bit long to execute, it's full of server request...
but the thing is, on the HTML page, everything is a mess, i get wrong value, and an endless loop who print only "Critical !" in status colomns
what is wrong with my script ?
You are not iterating #i in your while loop. Your line
while (#i) {
means that it will stay in the loop as long as #i is true. Because that's an array, that means that as long as there are items in #i, it will stay in the loop.
You do not remove anything from #i inside of the loop. There are no shift or pop commands, and you also do not overwrite #i. So it will stay indefinitely. You've got yourself an infinite loop.
What you want instead is probably a foreach loop. Then you also don't need $inc. It will put each element inside of #i into $elem and run the loop.
foreach my $elem (#i) {
my #temp = split /:/, $elem;
my $name = $temp[0];
my $percent = ( $temp[1] * $temp[2] / 100 );
my $status = undef;
if ( $percent <= 24 ) {
print "<tr class='info'>";
$status = "Critical !";
}
elsif ( $percent <= 49 ) {
print "<tr class='danger'>";
$status = "Danger !";
}
elsif ( $percent <= 74 ) {
print "<tr class='warning'>";
$status = "Warning";
}
elsif ( $percent <= 99 ) {
print "<tr class='active'>";
$status = "Minor";
}
elsif ( $percent == 100 ) {
print "<tr class='success'>";
$status = "OK";
}
print "<td>$name</td>";
print "<td>$status</td>";
print "</tr>";
}
You can read up on loops in perlsyn starting from for loops.

pdo update using bindParam show the error number of bound variables does not match number of tokens

I have a simple form for update image name into mysql database, but unfortunately it always throw the error SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens. The code seems OK, but I don't know it cannot update the database. Any help is appreciated.
Here is the image name and the SQL that I have printed out.
Array
(
[0] => 20141027103534_0.jpg
[1] => 20141027103534_1.jpg
[2] =>
[3] =>
[4] =>
[5] => 20141027103534_5.jpg
[6] => 20141027103534_6.jpg
[7] =>
[8] =>
[9] =>
)
UPDATE `image`
SET
`propertyPictureCate1` = :propertyPictureCate1,
`propertyPictureCate2` = :propertyPictureCate2,
`propertyPictureCate3` = :propertyPictureCate3,
`propertyPictureCate4` = :propertyPictureCate4,
`propertyPictureCate5` = :propertyPictureCate5,
`memo1` = :memo1,
`memo2` = :memo2,
`memo3` = :memo3,
`memo4` = :memo4,
`memo5` = :memo5,
`spotPictureCate1` = :spotPictureCate1,
`spotPictureCate2` = :spotPictureCate2,
`spotPictureCate3` = :spotPictureCate3,
`spotPictureCate4` = :spotPictureCate4,
`spotPictureCate5` = :spotPictureCate5,
`spotName1` = :spotName1,
`spotName2` = :spotName2,
`spotName3` = :spotName3,
`spotName4` = :spotName4,
`spotName5` = :spotName5,
`distance1` = :distance1,
`distance2` = :distance2,
`distance3` = :distance3,
`distance4` = :distance4,
`distance5` = :distance5,
`img1` = :img1,
`img2` = :img2,
`img6` = :img6,
`img7` = :img7
WHERE `buildingID` = :buildingID
the image name within bindParam that I tested to print out.
img1 20141027103534_0.jpg
img2 20141027103534_1.jpg
img6 20141027103534_5.jpg
img7 20141027103534_6.jpg
and below is my code for above result
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$file_name = [];
for ($i = 0; $i < count($_FILES['file']['tmp_name']); $i++) {
if($_FILES['file']['error'][$i] != 4) {
$validextensions = array("jpeg", "jpg", "png","gif");
$ext = explode('.', basename($_FILES['file']['name'][$i]));
$file_extension = end($ext);
$file_name[$i] = date("Ymdhis") . "_". $i . "." . $ext[count($ext) - 1];
$target_path = $_SERVER['DOCUMENT_ROOT'] . "/".PROJ_DIR."/uploads/" . date("Ymdhis") . "_". $i . "." . $ext[count($ext) - 1];
move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path);
} else {
$file_name[$i] = "";
}
}
try {
$sql = "
UPDATE `image`
SET
`propertyPictureCate1` = :propertyPictureCate1,
`propertyPictureCate2` = :propertyPictureCate2,
`propertyPictureCate3` = :propertyPictureCate3,
`propertyPictureCate4` = :propertyPictureCate4,
`propertyPictureCate5` = :propertyPictureCate5,
`memo1` = :memo1,
`memo2` = :memo2,
`memo3` = :memo3,
`memo4` = :memo4,
`memo5` = :memo5,
`spotPictureCate1` = :spotPictureCate1,
`spotPictureCate2` = :spotPictureCate2,
`spotPictureCate3` = :spotPictureCate3,
`spotPictureCate4` = :spotPictureCate4,
`spotPictureCate5` = :spotPictureCate5,
`spotName1` = :spotName1,
`spotName2` = :spotName2,
`spotName3` = :spotName3,
`spotName4` = :spotName4,
`spotName5` = :spotName5,
`distance1` = :distance1,
`distance2` = :distance2,
`distance3` = :distance3,
`distance4` = :distance4,
`distance5` = :distance5".
$s1 = (($file_name[0] != "") ? ",\n`img1` = :img1" : NULL).
$s2 = (($file_name[1] != "") ? ",\n`img2` = :img2" : NULL).
$s3 = (($file_name[2] != "") ? ",\n`img3` = :img3" : NULL).
$s4 = (($file_name[3] != "") ? ",\n`img4` = :img4" : NULL).
$s5 = (($file_name[4] != "") ? ",\n`img5` = :img5" : NULL).
$s6 = (($file_name[5] != "") ? ",\n`img6` = :img6" : NULL).
$s7 = (($file_name[6] != "") ? ",\n`img7` = :img7" : NULL).
$s8 = (($file_name[7] != "") ? ",\n`img8` = :img8" : NULL).
$s9 = (($file_name[8] != "") ? ",\n`img9` = :img9" : NULL).
$s10 = (($file_name[9] != "") ? ",\n`img10` = :img10" : NULL).
" \nWHERE `buildingID` = :buildingID
";
echo '<pre>';
print_r($file_name);
echo '</pre>';
echo '<pre>';
print_r($sql);
echo '</pre>';
$stmt = $con->prepare($sql);
$stmt->bindParam(":propertyPictureCate1",$_POST['propertyPictureCate1']);
$stmt->bindParam(":propertyPictureCate2",$_POST['propertyPictureCate2']);
$stmt->bindParam(":propertyPictureCate3",$_POST['propertyPictureCate3']);
$stmt->bindParam(":propertyPictureCate4",$_POST['propertyPictureCate4']);
$stmt->bindParam(":propertyPictureCate5",$_POST['propertyPictureCate5']);
$stmt->bindParam(":memo1",$_POST['memo1']);
$stmt->bindParam(":memo2",$_POST['memo2']);
$stmt->bindParam(":memo3",$_POST['memo3']);
$stmt->bindParam(":memo4",$_POST['memo4']);
$stmt->bindParam(":memo5",$_POST['memo5']);
$stmt->bindParam(":spotPictureCate1",$_POST['spotPictureCate1']);
$stmt->bindParam(":spotPictureCate2",$_POST['spotPictureCate2']);
$stmt->bindParam(":spotPictureCate3",$_POST['spotPictureCate3']);
$stmt->bindParam(":spotPictureCate4",$_POST['spotPictureCate4']);
$stmt->bindParam(":spotPictureCate5",$_POST['spotPictureCate5']);
$stmt->bindParam(":spotName1",$_POST['spotName1']);
$stmt->bindParam(":spotName2",$_POST['spotName2']);
$stmt->bindParam(":spotName3",$_POST['spotName3']);
$stmt->bindParam(":spotName4",$_POST['spotName4']);
$stmt->bindParam(":spotName5",$_POST['spotName5']);
$stmt->bindParam(":buildingID",$_POST['buildingID']);
if($file_name[0] !== ""){
echo 'img1 '.$file_name[0].'<br>';
$stmt->bindParam(":img1", $file_name[0]);
}
if($file_name[1] !== ""){
echo 'img2 '.$file_name[1].'<br>';
$stmt->bindParam(":img2", $file_name[1]);
}
if($file_name[2] !== ""){
echo 'img3 '.$file_name[2].'<br>';
$stmt->bindParam(":img3", $file_name[2]);
}
if($file_name[3] !== ""){
echo 'img4 '.$file_name[3].'<br>';
$stmt->bindParam(":img4", $file_name[3]);
}
if($file_name[4] !== ""){
echo 'img5 '.$file_name[4].'<br>';
$stmt->bindParam(":img5", $file_name[4]);
}
if($file_name[5] !== ""){
echo 'img6 '.$file_name[5].'<br>';
$stmt->bindParam(":img6", $file_name[5]);
}
if($file_name[6] !== ""){
echo 'img7 '.$file_name[6].'<br>';
$stmt->bindParam(":img7", $file_name[6]);
}
if($file_name[7] !== ""){
echo 'img8 '.$file_name[7].'<br>';
$stmt->bindParam(":img8", $file_name[7]);
}
if($file_name[8] !== ""){
echo 'img9 '.$file_name[8].'<br>';
$stmt->bindParam(":img9", $file_name[8]);
}
if($file_name[9] !== ""){
echo 'img10 '.$file_name[9].'<br>';
$stmt->bindParam(":img10", $file_name[9]);
}
//$stmt->debugDumpParams();
$stmt->execute();
$con = null;
} catch(PDOException $e) {
echo $e->getMessage();
}
}
You forgot to bind distance parameters to your statement.
Before calling $stmt->execute(), you should either add these lines for binding to $stmt
$stmt->bindParam(":distance1",$_POST['distance1']);
$stmt->bindParam(":distance2",$_POST['distance2']);
$stmt->bindParam(":distance3",$_POST['distance3']);
$stmt->bindParam(":distance4",$_POST['distance4']);
$stmt->bindParam(":distance5",$_POST['distance5']);
or remove these lines from you $sql string variable
`distance1` = :distance1,
`distance2` = :distance2,
`distance3` = :distance3,
`distance4` = :distance4,
`distance5` = :distance5

list all records from one custom taxonomy starting with one letter i.e A

I want to list all records in one custom taxonomy start with only A or B. Below code is to list all record with all letters.
Here is the code to list all record with one custom taxonomy with all letters in groups. Example,
A
Aajkacatch (0)
Aajkiitem (0)
Aamzie (0)
Aaneri (0)
Aaramshop (0)
Abaaya (0)
B
B.LAB (0)
Baby-Republic (0)
Babyoye (45)
Bank-Bazaar (1)
<?php
// Template Name: Store Template
// get all the stores
$stores = get_terms(APP_TAX_STORE, array('hide_empty' => 0, 'child_of' => 0, 'pad_counts' => 0, 'app_pad_counts' => 1));
// get ids of all hidden stores
$hidden_stores = clpr_hidden_stores();
$list = '';
$groups = array();
if ($stores && is_array($stores) ) {
// unset child stores
foreach($stores as $key => $value)
if($value->parent != 0)
unset($stores[$key]);
foreach($stores as $store)
$groups[mb_strtoupper(mb_substr($store->name, 0, 1))][] = $store;
if (!empty($groups)) :
foreach($groups as $letter => $stores) {
$old_list = $list;
$letter_items = false;
$list .= "\n\t" . '<h2 class="stores">' . apply_filters( 'the_title', $letter ) . '</h2>';
$list .= "\n\t" . '<ul class="stores">';
foreach($stores as $store) {
if (!in_array($store->term_id, $hidden_stores)) {
$list .= "\n\t\t" . '<li>' . apply_filters('the_title', $store->name). ' (' . intval($store->count) . ')</li>';
$letter_items = true;
}
}
$list .= "\n\t" . '</ul>';
if(!$letter_items)
$list = $old_list;
}
endif;
} else {
$list .= "\n\t" . '<p>' . __('Sorry, but no stores were found.', 'appthemes') .'</p>';
}
?>
While it wouldn't be the most efficient way, you could easily just check the first letter of the store name.
change:
if (!in_array($store->term_id, $hidden_stores)) {
to:
if (!in_array($store->term_id, $hidden_stores) ) {
$first_letter = substr( $store->name, 0, 1 );
if ( $first_letter == 'A' || && $first_letter == 'B' )
That would ensure you're only listing A and B items.

FPDF performance worse on live server than localhost

I have the following php script below that reads in about 2300 products with images etc from 20 categories using WAMP; the problem is that it take over 3 mins to create a pdf of 213 pages; based on the code is there a way of improving performance. I have already implemented these changes:
In php.ini
Find:
post_max_size = 8M
upload_max_filesize = 2M
max_execution_time = 30
max_input_time = 60
memory_limit = 8M
Change to:
post_max_size = 750M
upload_max_filesize = 750M
max_execution_time = 5000
max_input_time = 5000
memory_limit = 1000M
And add this to C:\wamp\bin\mysql\mysql5.0.45\my.ini:
max_allowed_packet = 200M
this has stopped the outOfMemory exception I was getting & also when I run it on my VPS cloud server with the same settings it can up to over 10 minutes! any help please?(I know it's a big script file)(there is also a business rule of 12 products per page, 4 rows by 3)
<?php
// Database connection information
require ('fpdf/fpdf.php');
class catalogue {
var $pdf;
var $x;
var $y;
var $catName;
var $pageAdded;
var $headerAdded;
var $myFont = "futuram";
var $bgColor;
var $fontColor;
var $headerImage;
var $aTozArray;
var $pageAlreadyAdded;
// Constructor to generate a PDF of the catalogue
function catalogue() {
global $pdf, $y;
define ( 'EURO', chr ( 128 ) );
$pdf = new FPDF ();
$y = 0;
$connect = mysql_connect ( 'localhost', 'xxxxxx', 'xxxxx' );
if (! $connect)
die ( 'Unable to connect to database' );
$select = mysql_select_db ( 'XXXXXXXXXXXX' );
if (! $select)
die ( 'Unable to select database' );
$pdf->SetFillColor ( 255, 255, 255 );
$pdf->SetMargins ( 0, 0, 0 );
$this->addPage ();
$pdf->AddFont ( 'futuram', '', 'futuram.php' );
$pdf->AddFont ( 'futurastdbold', '', 'futurastdbold.php' );
$pdf->AddFont ( 'futurastdboldoblique', '', 'futurastdboldoblique.php' );
$pdf->AddFont ( 'futurastdbook', '', 'futurastdbook.php' );
$pdf->AddFont ( 'futurastdbookoblique', '', 'futurastdbookoblique.php' );
$pdf->AddFont ( 'futurastdcondensed', '', 'futurastdcondensed.php' );
$pdf->AddFont ( 'futurastdcondensedbold', '', 'futurastdcondensedbold.php' );
$pdf->AddFont ( 'futurastdcondensedboldobl', '', 'futurastdcondensedboldobl.php' );
$pdf->AddFont ( 'futurastdcondensedextrabd', '', 'futurastdcondensedextrabd.php' );
$pdf->AddFont ( 'futurastdcondensedlight.php', '', 'futurastdcondensedlight.php' );
$pdf->AddFont ( 'futurastdcondensedlightobl', '', 'futurastdcondensedlightobl.php' );
$pdf->AddFont ( 'futurastdcondensedoblique', '', 'futurastdcondensedoblique.php' );
$pdf->AddFont ( 'futurastdcondensedboldobl', '', 'futurastdcondensedboldobl.php' );
$pdf->AddFont ( 'futurastdcondextraboldobl', '', 'futurastdcondextraboldobl.php' );
$pdf->AddFont ( 'futurastdheavy', '', 'futurastdheavy.php' );
$pdf->AddFont ( 'futurastdheavyoblique', '', 'futurastdheavyoblique.php' );
$pdf->AddFont ( 'futurastdlight', '', 'futurastdlight.php' );
$pdf->AddFont ( 'futurastdlightoblique', '', 'futurastdlightoblique.php' );
$pdf->AddFont ( 'futurastdmedium', '', 'futurastdmedium.php' );
$pdf->AddFont ( 'futurastdmediumoblique', '', 'futurastdmediumoblique.php' );
/*
$pdf->AddFont ( 'Georgia', '', 'georgia.php' );
$pdf->AddFont ( 'Georgia', 'I', 'georgiai.php' );
$pdf->AddFont ( 'Georgia', 'B', 'georgiab.php' );
*/
$pdf->SetAutoPageBreak ( true, 0 );
// add the front pages
$filename = 'http://localhost/images/Welcome_page_pic_fin-01.jpg';
$pdf->Image ( $filename, 0, 0, 210, 297 );
$this->addPage ();
$this->getParent ();
$pdf->Output ();
// actually outputs the PDF. Can be automatically output to a file on
// the server.
}
// Get parent categories
function getParent() {
global $pdf, $x, $y, $page, $catName, $bgColor, $fontColor, $headerImage, $pageAlreadyAdded;
$query = "SELECT * FROM categories";
// This query returns all categories, but not sub categories
$result = mysql_query ( $query );
$numrows = mysql_num_rows ( $result );
// For each category...
for($i = 0; $i < $numrows; $i ++) {
$pdf->SetFont ( 'futuram', '', 40 );
if ($page == 2) {
$x = 15;
$y = 20;
$pdf->setX ( $x );
$pdf->setY ( $y );
// setY defines the y coordinate
$page ++;
// adds a page
} else {
//$this->addPage ();
}
$array = mysql_fetch_array ( $result );
// puts the actual category names into an $array
$id = $array ['category_id'];
$catName = $array ['category_name'];
$image = $array ['category_icon'];
$sql = "SELECT category_colour1, category_colour2, category_icon FROM categories WHERE category_id=$id";
$coloursResult = mysql_query ( $sql );
$coloursArray = mysql_fetch_array ( $coloursResult );
$fontColor = $coloursArray ['category_colour1'];
$bgColor = $coloursArray ['category_colour2'];
$leaderImage = $coloursArray ['category_icon'];
$pdf->setX ( 18.5 );
// setX defines the x coordinate
$pdf->SetFillColor ( 255, 255, 255 );
$pdf->setTextColor ( 0, 71, 53 );
// $pdf->Cell ( 15, 20, $catName );
if (#GetImageSize ( $image )) {
//$filename = $leaderImage;
// add in for test
$filename = $image;
} else {
$filename = 'http://localhost/images/blank.jpg';
}
$pdf->Image ( $filename, 0, 0, 210, 297 );
// creates a cell in the page. Notes:
// http://www.fpdf.org/en/doc/cell.htm
// If automatic page breaking is enabled and the cell goes beyond
// the limit, a page break is done BEFORE outputting.
$this->addPage ();
$filename = 'http://localhost/images/blank.jpg';
$this->getChildren ( $id, $catName);
// calls the getChildren() function to gather the sub categories of
// the given category being used
}
}
// Get children
function getChildren($parent, $catName) {
global $pdf, $y;
$query = "SELECT * FROM shop_categories where parent_id = $parent ORDER BY category_id";
$result = mysql_query ( $query );
$numrows = mysql_num_rows ( $result );
// $array = mysql_fetch_array($result);
// fix the case with $numrows = 0
$catList = array ();
while ( $array = mysql_fetch_array ( $result ) ) {
array_push ( $catList, array (
$array ['name'],
$array ['category_id']
) );
}
$this->getProducts ( $catList,$catName );
}
// Get products in categories
function getProducts($catList,$catName) {
global $pdf;
$prodList = array ();
$numSubCategories = count ( $catList );
for($i = 0; $i < $numSubCategories; $i ++) {
$catid = $catList [$i] [1];
$query = "SELECT product_id,ProductName, WeightQuantity, PriceUnit, Euro, Pound, Code, Image FROM products where category_id = $catid order by product_id asc";
$result = mysql_query ( $query );
$numResults = mysql_num_rows($result);
if($numResults > 0) {
array_push ( $prodList, $result );
}
}
// If category is a parent send it back to getChildren
if (count ( $prodList ) != 0) {
$this->processPage ( $catList, $prodList,$catName );
}
}
function processPage($catList, $prodList, $catName) {
global $pdf, $catName, $page, $pageAdded, $myFont, $fontColor, $pageAlreadyAdded;
$pdf->SetFillColor ( 255, 255, 255 );
$itemsPerRow = 3;
$rowsPerPage = 4;
$currentRow = 0;
$numItems = 0;
$totalRows = 0;
$numberRowsUsed = 0;
for($i = 0; $i < count ( $prodList ); $i ++) {
$items = mysql_num_rows ( $prodList [$i] );
$totalRows += ceil ( $items / $itemsPerRow );
}
// changed this from $prodList[i] to $prodList
for($i = 0; $i < count ( $prodList ); $i ++) {
$subCatHeadName = $catList [$i] [0];
if ($i > 0) {
$numberOfitems = mysql_num_rows ( $prodList [$i - 1] );
$numberRowsUsed += ( int ) ceil ( $numberOfitems / $itemsPerRow );
}
$pdf->SetFont ( 'futuram', '', 28 );
$fonts = explode ( ",", $fontColor );
$pdf->SetTextColor ( 122, 114, 102 ); // grey
$p = 0;
$q = 0;
$pdf->SetX ( 0 );
$pdf->SetY ( 10 );
$pdf->SetLeftMargin ( 0 );
$pdf->SetRightMargin ( 0 );
$pdf->cMargin = 100;
$p1 = 0;
$q1 = 0;
$pdf->SetX ( 0 );
$numItems = mysql_num_rows ( $prodList [$i] ); // 5
$numRows = ceil ( $numItems / $itemsPerRow ); // 2
$remItems = $numItems % $itemsPerRow; // 2
if ($numItems > 0) {
for($j = 0; $j < $numRows; $j ++) {
$this->processRow ( $itemsPerRow, $prodList [$i], $j, $currentRow,$subCatHeadName,$catName );
$currentRow ++;
if ($currentRow % $rowsPerPage == 0) {
$this->addPage ();
$pageAlreadyAdded = true;
}
}
}
}
}
function processRow($numItems, $result, $rowNum, $currentRow,$subCatHeadName,$catName) {
global $pdf;
$pdf->SetX ( 0 );
$pdf->SetY ( 20 );
$this->header ();
$this->footer ();
if ($numItems > 0) {
for($i = 0; $i < $numItems; $i ++) {
$this->processItem ( $result, $rowNum, $i, $currentRow,$subCatHeadName,$catName );
}
}
}
function processItem($result, $rowNum, $itemNum, $currentRow,$subCatHeadName, $catName) {
global $pdf, $x, $y;
$p1 = 0;
$q1 = 0;
$pdf->SetX ( 0 );
$pdf->SetY ( $q1 + 10 );
$pdf->SetFont ( 'futurastdmedium', '', 40 );
$pdf->Cell ( 200, 16, $catName, 0, 5, 'C', true );
$pdf->SetX ( 0 );
$pdf->SetY ( $q1 + 25 );
$pdf->SetFont ( 'futurastdmedium', '', 10 );
$pdf->Cell ( 200, 8, $subCatHeadName, 0, 10, 'C', true );
$a = 18.5;
$b = 0;
if ($currentRow % 4 == 0) {
$rowNum = 0;
} else if ($currentRow % 4 == 1) {
$rowNum = 1;
} else if ($currentRow % 4 == 2) {
$rowNum = 2;
} else if ($currentRow % 4 == 3) {
$rowNum = 3;
}
$pdf->SetX ( $a );
$pdf->SetY ( $b );
$marginX = 0;
$marginY = 30;
$pdf->SetFont ( 'futuram', '', 6 );
$array = mysql_fetch_array ( $result );
$id = $array ['product_id'];
$name = $array ['ProductName'];
$price = $array ['PriceUnit'];
$UKprice = $array ['Pound'];
$Europrice = $array ['Euro'];
$code = $array ['Code'];
$weight = $array ['WeightQuantity'];
$deltaX = 62.5;
$deltaY = 64;
$a = $marginX + ($itemNum * $deltaX);
$b = $marginY + ($rowNum * $deltaY);
$image = $array ['Image'];
$imageName = ltrim ($image,':');
if( $imageName == "" || $imageName == null) {
$imageName = 'blank.jpg';
}
$filename = 'http://localhost/images/'.$imageName;
// check if the image exists on the server; replaces it with a
// placeholder otherwise
if (#GetImageSize ( 'http://localhost/images/'.$imageName )) {
$filename = 'http://localhost/images/'.$imageName;
} else {
$filename = 'http://localhost/images/blank.jpg';
}
if ($name != null) {
$pdf->Image ( $filename, $a + 20, ($b), 55, 25 );
}
$pdf->setY ( $b + 30 );
$pdf->setX ( $a + 5 ); // price is placed where coordinate x=150
$pdf->setTextColor ( 122, 114, 102 );
$pdf->SetFont ( 'futurastdheavy', '', 8.5 );
$line1 = "";
$line2 = "";
if ($name != null) {
if (strlen ( trim ( $name ) ) > 30) {
$name = substr ( $name, 0, 30 );
$pdf->setX ( $a + 35 );
$pdf->Cell ( 10, 10, trim ( $name ), 0, 0, 'C' );
} else {
$pdf->setX ( $a + 35 );
$pdf->Cell ( 10, 10, trim ( $name ), 0, 0, 'C' );
}
}
$pdf->setY ( $b + 33 );
$pdf->setX ( $a + 35 ); // price is placed where coordinate x=150
//$pdf->setTextColor ( 0, 0, 0, 0 );
$pdf->SetFont ( 'futurastdlightoblique', '', 7 );
if ($name != null) {
$pdf->Cell ( 10, 10, 'Weight/Quantity '.$weight." ".$price, 0, 0, 'C' );
}
$pdf->setY ( $b + 36 );
$pdf->setX ( $a + 35 ); // price is placed where coordinate x=150
//$pdf->setTextColor ( 0, 0, 0, 0 );
$pdf->SetFont ( 'futurastdlightoblique', '', 7 );
if ($name != null) {
$pdf->Cell ( 10, 10, 'Price Unit <Euro> <Pound>', 0, 0, 'C' );
}
$pdf->setY ( $b + 39 );
$pdf->setX ( $a + 35 ); // price is placed where coordinate x=150
// $pdf->setTextColor ( 0, 0, 0, 0 );
$pdf->SetFont ( 'futurastdbook', '', 7 );
$UKprice = utf8_decode("£" . number_format($UKprice, 2));
$Europrice = utf8_decode("EUR" . number_format($Europrice, 2));
if ($name != null) {
// $this->cell(10,10,iconv("UTF-8", "ISO-8859-1", "£"). $money,0);
$pdf->Cell ( 10, 10, "PRICE: ".$Europrice ." ".$UKprice, 0, 0, 'C' );
}
$pdf->setY ( $b + 42 );
$pdf->setX ( $a + 35 ); // price is placed where coordinate x=150
//$pdf->setTextColor ( 0, 0, 0, 0 );
$pdf->SetFont ( 'futurastdbold', '', 8.5 );
if ($code != null) {
$pdf->Cell ( 10, 10, "CODE: ".trim($code), 0, 0, 'C' ); // the product code
// goes
// here, in
}
//$pdf->setTextColor ( 0, 0, 0, 0 );
$pdf->SetFont ( 'futurastdbold', '', 8.5 );
// $y += 10; //move the y location down 10 (pixels...?)
$pdf->setXY ( $a, $b + 32.5 ); // place the 'cursor' at these coordinates
$pdf->SetLeftMargin ( 0 );
$pdf->SetRightMargin ( 0 );
$pdf->setY ( 0 );
$pdf->setX ( 0 ); // price is placed where coordinate x=150
}
// Function to add a page to the PDF
function addPage() {
global $pdf, $page, $y, $pageAdded;
$pdf->AddPage ();
// NOTE: this could have been $pdf->AddPage(P, A4) where P=portrait and
// A4 is page size.
// See http://www.fpdf.org/en/doc/addpage.htm
$pdf->Rect ( 0, 0, 250, 350, 'F' ); // The 'F' here means 'filled'. could
// also be
// 'D' for drawn border-only or DF for
// drawn and
// filled
$y = 5;
$pdf->setY ( $y );
$page ++;
$pageAdded = true;
}
function header() {
global $pdf, $catName, $pageAdded, $headerAdded, $myFont, $fontColor, $bgColor, $headerImage;
if ($pageAdded == true) {
$pdf->SetLeftMargin ( 68.5 );
$pdf->SetRightMargin ( 0 );
$pdf->SetFont ( 'futuram', '', 10 );
$pdf->SetX ( 0 );
$pdf->SetY ( 0 );
$pdf->SetFillColor ( 255, 0, 0 ); // grey
$pdf->Cell ( 77, 3, '', 0, 5, 'C', true );
$pdf->SetLeftMargin ( 0 );
$pdf->SetRightMargin ( 0 );
$title = $catName;
$image = $headerImage;
$pdf->SetFont ( 'futuram', '', 40 );
$pdf->SetX ( 0 );
$pdf->SetY ( 3 );
$fonts = explode ( ",", $fontColor );
$bg = explode ( ",", $bgColor );
$pdf->SetFillColor ( 255, 255, 255 ); // pure white
$pdf->SetLineWidth ( 1 );
$pdf->cMargin = 15;
$pdf->Ln ( 10 );
$headerAdded = true;
}
$pageAdded = false;
$pdf->SetFont ( 'futuram', '', 14 );
$pdf->SetFillColor ( 255, 255, 255 ); // white
}
function footer() {
global $pdf, $headerAdded, $myFont, $fontColor, $bgColor;
if ($headerAdded == true) {
$pdf->SetLeftMargin ( 0 );
$pdf->SetRightMargin ( 0 );
$pdf->SetFont ( 'futurastdlight', '', 9 );
$pdf->SetX ( 0 );
$pdf->SetY ( - 18 );
$fonts = explode ( ",", $fontColor );
$bg = explode ( ",", $bgColor );
$pdf->SetFillColor ( 255, 255, 255 ); // grey
$pdf->SetTextColor ( 122,114,102 ); // whitesmoke
// $pdf->Cell ( 0, 3, 'PRICES SUBJECT TO FLUCTUATION', 0, 1, 'C', true );
$pdf->SetX ( 18.5 );
$pdf->SetLeftMargin ( 0 );
$pdf->SetRightMargin ( 0 );
$pdf->SetX ( 4 );
$pdf->SetY ( -0.05 );
$pdf->SetFillColor ( 255, 0, 0 ); // grey
$pdf->Cell ( 28, -4, $pdf->PageNo (), 0, 0, 'C', true );
$pdf->SetFillColor ( 255, 255, 255 ); // grey
$pdf->SetLeftMargin ( 68.5 );
$pdf->SetRightMargin ( 0 );
$pdf->SetX ( 50 );
$pdf->SetY ( - 3 );
$pdf->SetFillColor ( 255, 0, 0 ); // grey
$pdf->Cell ( 77, 3, '', 0, 5, 'C', true );
$pdf->SetFillColor ( 255, 255, 255 ); // grey
$pdf->SetLeftMargin ( 184 );
$pdf->SetRightMargin ( 0 );
$pdf->SetX ( 4 );
$pdf->SetY ( -0.05 );
$pdf->SetFillColor ( 255, 0, 0 ); // grey
$pdf->Cell ( 28, -4, $pdf->PageNo (), 0, 0, 'C', true );
$pdf->SetFillColor ( 255, 255, 255 ); // grey
}
$headerAdded = false;
$pdf->SetFont ( $myFont, '', 14 );
$pdf->SetFillColor ( 255, 255, 255 );
}
}
$catalogue = new catalogue ();
?>