PDFlib place table on bottom of defined fitbox - php

I have created a table in PDFlib PHP and defined the coordinates of the fitbox as the following:
$llx = 350;
$lly = 500;
$urx = 600;
$ury = 800;
Now I want the table to start at the bottom of these coordinates and I want the table to grow to the top if I add more cells. How can I achieve that?
The function I wrote so far (here the table starts at the top):
function createTable(pdflib $p, int $fontMedium, int $fontRegular, array $arrInput) {
$tbl=0;
$rowmax = 8;
/* ---------- table header */
$row = 1; $col = 1;
$optlist = "margin=4 fittextline={position=center font=" . $fontMedium . " fontsize=10} " .
"matchbox={fillcolor={#ffed00}} colspan=1 colwidth=200";
$tbl = $p->add_table_cell($tbl, $col, $row, $arrInput['table']['tableHeading'], $optlist);
if ($tbl == 0) {
echo("Error: " . $p->get_errmsg());
exit(1);
}
/* ---------- content rows */
foreach($arrInput['table']['tableListings'] as $listings) {
/* ----- Multi-line text with Textflow */
// $row++;
if ($row <= $rowmax) {
$row++;
} else {
echo("Error: Too many Items for table");
exit(1);
}
$font = $p->load_font("W-Regular", "unicode", "");
if ($font == 0) {
echo("Error: " . $p->get_errmsg());
exit(1);
}
$optlist = "charref fontname=W-Regular encoding=unicode fontsize=10";
$tf = $p->add_textflow(0, $listings, $optlist);
if ($tf == 0) {
echo("Error: " . $p->get_errmsg());
exit(1);
}
$optlist = "margin=4 matchbox={fillcolor={rgb 0.9 0.5 0}} textflow=" . $tf;
$tbl = $p->add_table_cell($tbl, $col, $row, "", $optlist);
if ($tbl == 0) {
echo("Error: " . $p->get_errmsg());
exit(1);
}
}
do {
$optlist = "header=1 rowheightdefault=20 " .
"stroke={{line=other}} ";
$result = $p->fit_table($tbl, $llx, $lly, $urx, $ury, $optlist);
if ($result == "_error") {
echo("Couldn't place table: " . $p->get_errmsg());
exit(1);
}
} while ($result == "_boxfull");
$p->delete_table($tbl, "");
}

I have found the answer. In the $optlist for the fit_table you can define the position of the table which (if you want it at the bottom of the fitbox) looks like this:
$optlist = "header=1 rowheightdefault=20 "
. "stroke={{line=horother linewidth=0.3}} "
. "position={bottom} "; // statement to define position of the table in the fitbox

Related

PDFLib (PHP) Couldn't place table : Calculated table height too large

I get this Exception:
Couldn't place table : Calculated table height 657.375 too large (> 492, shrinking 74.8432%)
The content is dynamic, and It seems sometimes it exeeds the table capacity but I am not sure, any ideas?
Version of PDFLibary 9.2.0 / Version of PHP 7.2.13 MSVC15
Portion of code where the exception is raised:
$result = $p->fit_table($tbl, $llx, $lly, $urx, $y, $fit_opts);
if ($result == "_error")
throw new Exception("Couldn't place table : " .
$p->get_errmsg());
Entire code below:
<?php
$outfile = "";
$title = "Table Invoice";
$infile = "stationery.pdf";
$tf = 0;
$tbl = 0;
$sum = 0;
$total = 0;
$subtotal = 0;
$tabheight = 0;
$pagewidth = 792;
$pageheight = 612;
$fontsize = 12;
$capheight = 7;
$rowheight = 16;
$margin = 4;
$leading = "120%";
$ystart = $pageheight - 40;
$yoffset = 15;
$ycontinued = 40;
$nfooters = 1;
$nheaders = 1;
/* The table coordinates are fixed; only the height of the table may differ*/
$llx = 25;
$urx = 770;
$lly = 80;
/* The widths of the individual columns is fixed */
$maxcol = 5;
$c1 = 20;
$c2 = 300;
$c3 = 30;
$c4 = 30;
$c5 = 30;
$c6 = 30;
$c7 = 30;
$c8 = 30;
$c9 = 30;
$c10 = 30;
$c11 = 30;
/* Get the current date */
setlocale(LC_TIME, "C");
date_default_timezone_set("Europe/Berlin");
$fulldate = date("F j, Y");
/* Text to output after the table */
$closingtext =
"Terms of payment: 30 days net. " .
"90 days warranty starting at the day of sale. " .
"This warranty covers defects in workmanship only. " .
"Kraxi Systems, Inc. will, at its option, repair or replace the " .
"product under the warranty. This warranty is not transferable. " .
"No returns or exchanges will be accepted for wet products.";
//Executing query for table rows
if (!$rst) {
print "Error " . $conn->ErrorMsg();
exit;
} else {
if ($rst->RecordCount() > 0) {
$address = array(
"John Q. Doe", "255 Customer Lane", "Suite B",
"12345 User Town", "Everland"
);
try {
$searchpath = $PDFSOURCEPATH;
$p = new PDFlib();
$p->set_option("errorpolicy=return");
$p->set_option("textformat=bytes");
$p->set_option("SearchPath={{" . $searchpath . "}}");
if ($p->begin_document("", "") == -1) {
die("Error: " . $p->get_errmsg());
}
$boldfont = $p->load_font("Helvetica-Bold", "unicode", "");
if ($boldfont == 0)
throw new Exception("Error: " . $p->get_errmsg());
$regularfont = $p->load_font("Helvetica", "unicode", "");
if ($regularfont == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* Start the output page */
$p->begin_page_ext($pagewidth, $pageheight, "");
/* Output the customer's address */
$y = $ystart;
$p->setfont($regularfont, $fontsize);
for ($i = 0; $i < count($address); $i++) {
$p->fit_textline($address[$i], $llx, $y, "");
$y -= $yoffset;
}
/* Print the header and the date */
$y -= 3 * $yoffset;
$p->setfont($boldfont, $fontsize);
$p->fit_textline("INVOICE", $llx, $y, "position {left top}");
$p->fit_textline($fulldate, $urx, $y, "position {right top}");
$y -= 3 * $yoffset;
$head_opts_right = "fittextline={position={right top} " .
" font=" . $boldfont . " fontsize={capheight=" . $capheight . "}} " .
" rowheight=" . $rowheight . " margin=" . $margin;
$head_opts_left = "fittextline={position={left top} " .
" font=" . $boldfont . " fontsize={capheight=" . $capheight . "}} " .
" rowheight=" . $rowheight . " margin=" . $margin;
$col = 1;
$row = 1;
/* Add each heading cell with the option list defined above;
* in addition, supply a fixed column width
*/
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
"Col 1",
$head_opts_right . " colwidth=" . $c1
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
"Col 2",
$head_opts_left . " colwidth=" . $c2
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
"Col 3",
$head_opts_left . " colwidth=" . $c3
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
"Col 4",
$head_opts_right . " colwidth=" . $c4
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
"Col 5",
$head_opts_right . " colwidth=" . $c5
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
"Col 6",
$head_opts_right . " colwidth=" . $c6
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
"Col 7",
$head_opts_right . " colwidth=" . $c7
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
"Col 8",
$head_opts_right . " colwidth=" . $c8
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
"Col 9",
$head_opts_right . " colwidth=" . $c9
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
"Col 10",
$head_opts_right . " colwidth=" . $c10
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
"Col 11",
$head_opts_right . " colwidth=" . $c11
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$row++;
$body_opts = "fittextline={position={right top} " .
" font=" . $regularfont .
" fontsize={capheight=" . $capheight . "}} " .
" rowheight=" . $rowheight . " margin=" . $margin;
$itemno = 1;
$masterRows = array();
while (!$rst->EOF) {
$col = 1;
$masterRows[] = $row;
/* ---------------------------------------------------------------
* Add the text line cell containing the Item in the first column,
* with the options defined for table body cells above
* ---------------------------------------------------------------
*/
$tbl = $p->add_table_cell($tbl, $col++, $row, $rst->fields["col1"], $body_opts);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tf_opts = "font=" . $regularfont .
" fontsize={capheight=" . $capheight . "} leading=" . $leading;
$bodytf_opts = "fittextflow={firstlinedist=capheight}" .
" colwidth=" . $c2 . " margin=" . $margin;
/* Add the Textflow with the options defined above */
$tf = $p->add_textflow(0, $rst->fields["customername"], $tf_opts);
if ($tf == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* Add the Textflow table cell with the options defined above */
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
"",
$bodytf_opts . " textflow=" . $tf
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tf = 0;
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
date("m/d/Y", strtotime($rst->fields["col2"])),
$body_opts
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tbl = $p->add_table_cell(
$tbl,
$col++,
$row,
date("m/d/Y", strtotime($rst->fields["col3"])),
$body_opts
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
if ($result == "_error")
throw new Exception("Couldn't place table : " .
$p->get_errmsg());
/* Calculate the overall sum */
$total += $sum;
$itemno++;
$row++;
//Executing query for table rows comments
while (!$rstComments->EOF) {
$tf_opts = "font=" . $regularfont .
" fontsize={capheight=" . $capheight . "} alignment=left leading=" . $leading;
$tf = $p->add_textflow(0, $rstComments->fields["comment"], $tf_opts);
if ($tf == 0)
throw new Exception("Error: " . $p->get_errmsg());
$bodytf_opts = "fittextflow={firstlinedist=capheight} colspan=10" .
" colwidth= 700 margin=" . $margin;
$tbl = $p->add_table_cell(
$tbl,
2,
$row,
"",
$bodytf_opts . " textflow=" . $tf
);
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$tf = 0;
///////////////End comments///////////////
$row++;
$rstComments->MoveNext();
} /* for */
$rst->MoveNext();
}
$footer_opts =
"rowheight=" . $rowheight . " colspan=2 margin =" . $margin .
" matchbox={name=subtotal}";
$tbl = $p->add_table_cell($tbl, $maxcol - 1, $row, "", $footer_opts . "");
if ($tbl == 0)
throw new Exception("Error adding cell: " . $p->get_errmsg());
$fill_opts =
" fill={";
foreach ($masterRows as $rowNumber) {
$fill_opts .=
"{area=row" . $rowNumber . " fillcolor={gray 0.9}} ";
}
do {
$fit_opts =
"header=" . $nheaders . " footer=" . $nfooters .
$fill_opts .
"{area=header fillcolor={rgb 0.90 0.90 0.98}} " .
"{area=footer fillcolor={rgb 0.98 0.92 0.84}}}";
/* Place the table instance */
$result = $p->fit_table($tbl, $llx, $lly, $urx, $y, $fit_opts);
/* An error occurred or the table's fitbox is too small to keep any
* contents
*/
if ($result == "_error")
throw new Exception("Couldn't place table : " .
$p->get_errmsg());
if ($result != "_boxfull") {
/* Format the total to a maximum of two fraction digits */
$roundedValue = sprintf("%.2f", $total);
$contents = "total: " . $roundedValue;
/* Retrieve the coordinates of the third (upper right) corner of
* the "subtotal" matchbox. The parameter "1" indicates the
* first instance of the matchbox.
*/
$x3 = 0;
$y3 = 0;
if ($p->info_matchbox("subtotal", 1, "exists") == 1) {
$x3 = $p->info_matchbox("subtotal", 1, "x3");
$y3 = $p->info_matchbox("subtotal", 1, "y3");
} else {
throw new Exception("Error: " . $p->get_errmsg());
}
/* Start the text line at the corner coordinates retrieved
* (x2, y2) with a small margin. Right-align the text.
*/
$p->setfont($boldfont, $fontsize);
$p->fit_textline(
$contents,
$x3 - $margin,
$y3 - $margin,
"position={right top}"
);
} else if ($result == "_boxfull") {
/* Get the last body row output in the table instance */
$lastrow = $p->info_table($tbl, "lastbodyrow");
/* Calculate the subtotal */
$subtotal = 0;
for ($i = 0; $i < $lastrow - $nfooters; $i++) {
$subtotal += $items[$i][1] * $items[$i][2];
}
/* Format the subtotal to a maximum of two fraction digits*/
$roundedValue = sprintf("%.2f", $subtotal);
$contents = "subtotal: " . $roundedValue;
/* Retrieve the coordinates of the third (upper right) corner of
* the "subtotal" matchbox. The parameter "1" indicates the
* first instance of the matchbox.
*/
$x3 = 0;
$y3 = 0;
if ($p->info_matchbox("subtotal", 1, "exists") == 1) {
$x3 = $p->info_matchbox("subtotal", 1, "x3");
$y3 = $p->info_matchbox("subtotal", 1, "y3");
} else {
throw new Exception("Error: " . $p->get_errmsg());
}
/* Start the text line at the corner coordinates retrieved in
* (x3, y3) with a small margin. Right-align the text.
*/
$p->setfont($boldfont, $fontsize);
$p->fit_textline(
$contents,
$x3 - $margin,
$y3 - $margin,
"position={right top}"
);
/* Output the "Continued" remark */
$p->setfont($regularfont, $fontsize);
$p->fit_textline(
"-- Continued --",
$urx,
$ycontinued,
"position {right top}"
);
$p->end_page_ext("");
$p->begin_page_ext($pagewidth, $pageheight, "");
$y = $ystart;
}
} while ($result == "_boxfull");
/* Get the table height of the current table instance */
$tabheight = $p->info_table($tbl, "height");
$y = $y - (int) $tabheight - $yoffset;
/* Add the closing Textflow to be placed after the table */
$tf_opts = "font=" . $regularfont . " fontsize=" . $fontsize .
" leading=" . $leading . " alignment=justify";
$tf = $p->add_textflow(0, $closingtext, $tf_opts);
if ($tf == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* Loop until all text has been fit which is indicated by the "_stop"
* return value of fit_textflow()
*/
do {
/* Place the Textflow */
$result = $p->fit_textflow($tf, $llx, $lly, $urx, $y, "");
if ($result == "_error")
throw new Exception("Couldn't place table : " .
$p->get_errmsg());
if ($result == "_boxfull" || $result == "_boxempty") {
$p->setfont($regularfont, $fontsize);
$p->fit_textline(
"-- Continued --",
$urx,
$ycontinued,
"position {right top}"
);
$p->end_page_ext("");
$p->begin_page_ext($pagewidth, $pageheight, "");
$y = $ystart;
}
} while (!$result == "_stop");
$p->end_page_ext("");
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
if ($email == 1) {
$filename = $filename . ".pdf";
#unlink($filename);
$file = fopen($filename, "w");
fputs($file, $buf, $len);
fclose($file);
PDF_delete($p);
} else {
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=$report_filename");
print $buf;
}
} catch (PDFlibException $e) {
die("PDFlib exception occurred in hello sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n");
} catch (Exception $e) {
die($e);
}
} else {
if ($email == 1) {
try {
$p = new PDFlib();
if ($p->begin_document("", "") == -1) {
die("Error: " . $p->get_errmsg());
}
$p->begin_page_ext($pagewidth, $pageheight, "");
$p->set_text_pos(20, 600);
$p->end_page_ext("");
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
$filename = $filename . ".pdf";
#unlink($filename);
$file = fopen($filename, "w");
fputs($file, $buf, $len);
fclose($file);
} catch (PDFlibException $e) {
die("PDFlib exception occurred in hello sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n");
} catch (Exception $e) {
die($e);
}
} else {
print "No records matched search criteria";
exit;
}
}
}
?>

Pdflib: how to create drop cap using add_flowtext()

I took the cookbook example and modified it to use add_textflow() instead of create_textflow() but I can't get it to work. Instead of interpreting the macros it just writes it as plain text. What am I doing wrong?
From documentation I got the understanding that I should define the macros inline - or did I misunderstand it? Have tried everything I could think of...
$tf = 0;
$optlist = "";
$llx = 100; $lly = 50; $urx = 450; $ury = 800;
$t_fontsize = 16; // font size of the text
$t_leading = 20; // leading of the text
$c_num = 3; // no. of lines for the drop cap to cover
$c_textrise = -(($c_num - 1) * $t_leading); // text rise of the drop cap
$c_fontsize = -($c_textrise * 1.8); // font size of the drop cap
try {
$p = new pdflib();
$p->set_option("errorpolicy=exception");
$p->set_option("stringformat=utf8");
$p->begin_document("", "");
$optlist = "fontname=Helvetica fontsize=" . $t_fontsize . " encoding=unicode ";
$text = "<macro " .
"{cap_start {fontsize=" . $c_fontsize . " leading=" . $t_leading .
" textrise=" . $c_textrise .
" matchbox={createwrapbox boxheight={leading textrise}}} " .
"cap_end {matchbox=end fontsize=" . $t_fontsize . " textrise=0}}>";
$text .= "<&cap_start>O<&cap_end>ver the mountains... ";
$tf = $p->add_textflow($tf, $text, $optlist);
$text =
"Paper Planes are the ideal way of " .
"passing the time. We offer revolutionary " .
"new developĀ­ments of the traditional comĀ­mon " .
"paper planes. If your lesson, conference, or lecture " .
"turn out to be deadly boring, you can have a wonderful time " .
"with our planes. ";
$tf = $p->add_textflow($tf, $text, $optlist);
do {
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
$result = $p->fit_textflow($tf, $llx, $lly, $urx, $ury, "");
$p->end_page_ext("");
} while ($result == "_boxfull" || $result == "_nextpage");
/* Check for errors */
if (!$result == "_stop") {
if ($result == "_boxempty")
throw new Exception ("Error: Textflow box too small");
else {
throw new Exception ("User return '" . $result .
"' found in Textflow");
}
}
$p->delete_textflow($tf);
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=drop_caps.pdf");
print $buf;
} catch (PDFlibException $e) {
die("PDFlib exception occurred:\n".
"[" . $e->get_errnum() . "] " . $e->get_apiname() .
": " . $e->get_errmsg() . "\n");
} catch (Exception $e) {
die($e->getMessage());
}
$p = 0;
this is simple: only create_texflow() support inline options (because you must specify the data in a single chunk). With add_textflow() you have the possibility to "build" the textflow handle in multiple steps, for each text fragment with the related options.
In your case, it might look like the following way:
$optlistmacro = " macro " .
"{cap_start {fontsize=" . $c_fontsize . " leading=" . $t_leading .
" textrise=" . $c_textrise .
" matchbox={createwrapbox boxheight={leading textrise}}} " .
"cap_end {matchbox=end fontsize=" . $t_fontsize . " textrise=0}}";
$tf = $p->add_textflow($tf, "", $optlist . $optlistmacro);
$tf = $p->add_textflow($tf, "O", "&cap_start");
$tf = $p->add_textflow($tf, "ver the mountains... ", "&cap_end");
In this code fragment, I do the first add_textflow() without text but all the
macros settings. Then it place the "O" with the macro (&cap_start) in
the option list. The following text "ver the mountains... " is done
in the next add_textflow() and as option I place the &cap_end.

How retour on line on cell of table pdflib

How I do do retour on line retour chariot on cell of table with pdflib
I do that on cell
$tbl = $p->add_table_cell($tbl, $col, $row, $num."\r\n".$quote, $optlist);
but not do retour on line on cell of table
for a multiline table cell, you have to use a textflow cell. You find a simple sample in the PDFlib cookbook starter_table.php:
/* ----- Multi-line text with Textflow */
$col++;
$font = $p->load_font("Times-Roman", "unicode", "");
if ($font == 0) {
die("Error: " . $p->get_errmsg());
}
$optlist = "charref fontname=Times-Roman encoding=unicode fontsize=8 ";
$tf = $p->add_textflow($tf, $tf_text, $optlist);
if ($tf == 0) {
die("Error: " . $p->get_errmsg());
}
$optlist = "margin=2 textflow=" . $tf;
$tbl = $p->add_table_cell($tbl, $col, $row, "", $optlist);
if ($tbl == 0) {
die("Error: " . $p->get_errmsg());
}
You fill find a detailed introduction into PDFlib Table and textflow within PDFlib 9 Tutorial, chapter 8.3 and 8.2.

How To Change Numbers Based On Results

I have a follow up question on something I got help with here the other day (No Table Three Column Category Layout).
The script is as follows:
$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$parent_node = mysql_fetch_assoc($res);
$id = (isset($parent_node['cat_id'])) ? $parent_node['cat_id'] : $id;
$catalist = '';
if ($parent_node['left_id'] != 1)
{
$children = $catscontrol->get_children_list($parent_node['left_id'], $parent_node['right_id']);
$childarray = array($id);
foreach ($children as $k => $v)
{
$childarray[] = $v['cat_id'];
}
$catalist = '(';
$catalist .= implode(',', $childarray);
$catalist .= ')';
$all_items = false;
}
$NOW = time();
/*
specified category number
look into table - and if we don't have such category - redirect to full list
*/
$query = "SELECT * FROM " . $DBPrefix . "categories WHERE cat_id = " . $id;
$result = mysql_query($query);
$system->check_mysql($result, $query, __LINE__, __FILE__);
$category = mysql_fetch_assoc($result);
if (mysql_num_rows($result) == 0)
{
// redirect to global categories list
header ('location: browse.php?id=0');
exit;
}
else
{
// Retrieve the translated category name
$par_id = $category['parent_id'];
$TPL_categories_string = '';
$crumbs = $catscontrol->get_bread_crumbs($category['left_id'], $category['right_id']);
for ($i = 0; $i < count($crumbs); $i++)
{
if ($crumbs[$i]['cat_id'] > 0)
{
if ($i > 0)
{
$TPL_categories_string .= ' > ';
}
$TPL_categories_string .= '' . $category_names[$crumbs[$i]['cat_id']] . '';
}
}
// get list of subcategories of this category
$subcat_count = 0;
$query = "SELECT * FROM " . $DBPrefix . "categories WHERE parent_id = " . $id . " ORDER BY cat_name";
$result = mysql_query($query);
$system->check_mysql($result, $query, __LINE__, __FILE__);
$need_to_continue = 1;
$cycle = 1;
$column = 1;
$TPL_main_value = '';
while ($row = mysql_fetch_array($result))
{
++$subcat_count;
if ($cycle == 1)
{
$TPL_main_value .= '<div class="col'.$column.'"><ul>' . "\n";
}
$sub_counter = $row['sub_counter'];
$cat_counter = $row['counter'];
if ($sub_counter != 0)
{
$count_string = ' (' . $sub_counter . ')';
}
else
{
if ($cat_counter != 0)
{
$count_string = ' (' . $cat_counter . ')';
}
else
{
$count_string = '';
}
}
if ($row['cat_colour'] != '')
{
$BG = 'bgcolor=' . $row['cat_colour'];
}
else
{
$BG = '';
}
// Retrieve the translated category name
$row['cat_name'] = $category_names[$row['cat_id']];
$catimage = (!empty($row['cat_image'])) ? '<img src="' . $row['cat_image'] . '" border=0>' : '';
$TPL_main_value .= "\t" . '<li>' . $catimage . '' . $row['cat_name'] . $count_string . '</li>' . "\n";
++$cycle;
if ($cycle == 7) // <---- here
{
$cycle = 1;
$TPL_main_value .= '</ul></div>' . "\n";
++$column;
}
}
if ($cycle >= 2 && $cycle <= 6) // <---- here minus 1
{
while ($cycle < 7) // <---- and here
{
$TPL_main_value .= ' <p> </p>' . "\n";
++$cycle;
}
$TPL_main_value .= '</ul></div>'.$number.'
' . "\n";
}
I was needing to divide the resulting links into three columns to fit my html layout.
We accomplished this by changing the numbers in the code marked with "// <---- here".
Because the amount of links returned could be different each time, I am trying to figure out how to change those numbers on the fly. I tried using
$number_a = mysql_num_rows($result);
$number_b = $number_a / 3;
$number_b = ceil($number_b);
$number_c = $number_b - 1;
and then replacing the numbers with $number_b or $number_c but that doesn't work. Any ideas?
As mentioned before, you can use the mod (%) function to do that.
Basically what it does is to get the remainder after division. So, if you say 11 % 3, you will get 2 since that is the remainder after division. You can then make use of this to check when a number is divisible by 3 (the remainder will be zero), and insert an end </div> in your code.
Here is a simplified example on how to use it to insert a newline after every 3 columns:
$cycle = 1;
$arr = range (1, 20);
$len = sizeof ($arr);
for ( ; $cycle <= $len; $cycle++)
{
echo "{$arr[$cycle - 1]} ";
if ($cycle % 3 == 0)
{
echo "\n";
}
}
echo "\n\n";

foreach loop prevous data in export file

I have a export script that export items to a csv file. I want to export the additional images for each item on each product row in the csv file.
That part looks like this:
$img_query=tep_db_query("SELECT additional_images_id, products_id, popup_images
FROM " . TABLE_ADDITIONAL_IMAGES . "
WHERE products_id=" . $products['products_id'] ."");
if (!tep_db_num_rows($img_query)) {
$imageextra2 = " ";
} else {
$img_rows=tep_db_num_rows($img_query);
while ($img = tep_db_fetch_array($img_query)) {
$img2 = $img['popup_images'];
$pid = $img['products_id'];
$a = array($img2);
foreach($a as &$img){
}
foreach($a as $imageextra) {
$imageextra = "http://www.URL.com/images/". $img2.";";
$imageextra2 .= $imageextra;
}
} }
But when I export more than one product that have additional images the images from the line abowe follows to the next line in the csv file.
Heres an exampe of the result. Each item has one additional image:
Item-A;AdditionalImage-A.jpg;AdditionalImage-A2.jpg
Item-B;AdditionalImage-A.jpg;AdditionalImage-A2.jpg;AdditionalImage-B.jpg;AdditionalImage-B2.jpg
Item-C;AdditionalImage-A.jpg;AdditionalImage-A2.jpg;AdditionalImage-B.jpg;AdditionalImage-B2.jpg;AdditionalImage-C.jpg;AdditionalImage-C2.jpg
What can I do to get this working?
Cheers,
Fredrik
Edit
Below is the complete section of php that grab the info and generate the export file:
<?php
require('includes/application_top.php');
if (isset($_POST['create'])) {
if (isset($_POST['product']) && is_array($_POST['product'])) {
foreach ($_POST['product'] as $product_id) {
$products_query_raw = "select p.products_id, p.products_image, p.products_model, products_image_pop, p.products_price, p.products_quantity, p.products_tax_class_id, pd.products_name, pd.products_description from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = $product_id and pd.products_id = p.products_id and pd.language_id = $languages_id";
$products_query = tep_db_query($products_query_raw);
if ($products = tep_db_fetch_array($products_query)) {
//++++ QT Pro: End Changed Code
$products_attributes_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . $products['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "'");
$products_attributes = tep_db_fetch_array($products_attributes_query);
if ($products_attributes['total'] > 0) {
//++++ QT Pro: Begin Changed code
$products_id = $products['products_id'];
require_once(DIR_WS_CLASSES . 'pad_single_radioset_print.php');
$class = 'pad_single_radioset';
$pad = new $class($products_id);
$attribs .= $pad->draw();
//++++ QT Pro: End Changed Code
}
$product_desc = (isset($_POST['strip_tags']) && ($_POST['strip_tags'] == '1'))
? strip_tags($products['products_description'],'<br>, <li>, </li>, <ul>, </ul>') //ORG
: $products['products_description'];
$tax_rate = 0;
$taxes_query_raw = "select tax_rate from " . TABLE_TAX_RATES . " where tax_class_id = " . $products['products_tax_class_id'];
$taxes_query = tep_db_query($taxes_query_raw);
while ($taxes = tep_db_fetch_array($taxes_query)) {
$tax_rate += $taxes['tax_rate'];
}
$tax = ($products['products_price'] * $tax_rate) / 100;
$stock = $products['products_quantity'];
$product_desc=preg_replace("/([[:space:]]{2,})/",' ',$product_desc);
// Categories - just show the sub-category
$categories_list = Array();
$categories_query_raw = "select cd.categories_name, cd.categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " ptc, " . TABLE_CATEGORIES_DESCRIPTION . " cd where ptc.products_id = " . $products['products_id'] . " and cd.categories_id = ptc.categories_id and cd.language_id = $languages_id";
$categories_query = tep_db_query($categories_query_raw);
while ($categories = tep_db_fetch_array($categories_query)) {
$categories_list[] = $categories['categories_name'];
$categories_id = $categories['categories_id'];
}
$category_name = implode(' / ', $categories_list);
// Additional images
$img_query=tep_db_query("SELECT additional_images_id, products_id, popup_images
FROM " . TABLE_ADDITIONAL_IMAGES . "
WHERE products_id=" . $products['products_id'] ."");
if (!tep_db_num_rows($img_query)) {
$imageextra2 = "; ; ";
} else {
$img_rows=tep_db_num_rows($img_query);
while ($img = tep_db_fetch_array($img_query)) {
$img2 = $img['popup_images'];
$pid = $img['products_id'];
$a = array($img2);
$imageextra2 = " "; # This avoid letting the additional images from lines abowe follow.
foreach($a as $imageextra) {
$imageextra = "http://www.URL.com/images/". $img2.";";
$imageextra2 .= $imageextra;
}
}
}
$export .=
$products['products_model'].';'.
$products['products_name'].';'.
$product_price.';'.
$products['products_quantity'].';'.
$product_desc.';'.
$categories_id.';'.
$shipping_cost.';'.
'http://www.URL.com/images/'.basename($products['products_image_pop']).';'.
$imageextra2.
"\n";
}
}
if ($fp = #fopen($_SERVER['DOCUMENT_ROOT'] . '/feed/t-butik.csv', 'w')) {
$utf = iconv("windows-1252","ISO-8859-1",$export);
$out = 'variable_name='.$utf;
fwrite($fp, $utf);
fclose($fp);
$messageStack->add("Feed generates successfully!!!", 'success');
} else {
$messageStack->add("ERROR: Permissions error trying to write feed to disk.", 'error');
}
}
}
?>
There doesn't seem to be enough of the code to give you an absolute answer but my guess would be that the variable $imageextra2 is not being reset on each iteration.
When you change a product you should reset the variable thats holding the images, which I assume is $imageextra2
Change This
if (!tep_db_num_rows($img_query)) {
$imageextra2 = "; ; ";
} else {
$img_rows=tep_db_num_rows($img_query);
while ($img = tep_db_fetch_array($img_query)) {
$img2 = $img['popup_images'];
$pid = $img['products_id'];
$a = array($img2);
$imageextra2 = " "; # This avoid letting the additional images from lines abowe follow.
foreach($a as $imageextra) {
$imageextra = "http://www.URL.com/images/". $img2.";";
$imageextra2 .= $imageextra;
}
}
}
To This
$imageextra2 = " "; # resets images
if (tep_db_num_rows($img_query)) {
$img_rows=tep_db_num_rows($img_query);
while ($img = tep_db_fetch_array($img_query)) {
$img2 = $img['popup_images'];
$pid = $img['products_id'];
$a = array($img2);
foreach($a as $imageextra) {
$imageextra = "http://www.URL.com/images/". $img2.";";
$imageextra2 .= $imageextra;
}
}
}
Best Regards,
Jason

Categories