TCPDF - Having trouble with getting MySQL content to show in PDF - php

I am trying to make a printable version of a catalogue of products. If I display the products in HTML it works fine. But when I try and use TCPDF I loose half of it or nothing at all.
$query = "SELECT ID, Category_Name, Image FROM Categories ORDER BY Position ASC";
$result = mysqli_query($connection, $query);
confirm_query($result);
while ($cat = mysqli_fetch_row($result)) {
// add a page
$pdf->AddPage();
$query1 = "SELECT * FROM Products WHERE Category = " . $cat[0] . " AND Visibility = 1 ORDER BY Product_Name ASC";
$result1 = mysqli_query($connection, $query1);
$html = $query1;
confirm_query($result1);
//$html .= '<style>'. file_get_contents('./images/print.css').'</style>';
$html .= '<table style="margin: 0 auto;"><tbody>';
$html .= '<tr><th colspan="6"><div id="title" style="color: white;">' . $cat[1] . '</div></th></tr>';
$html .= '<tr>';
$html .= '<th></th>';
$html .= '<th>Product</th>';
$html .= '<th>Pack Size</th>';
$html .= '<th>Price(Ex VAT)</th>';
$html .= '<th>Price(Inc)</th>';
$html .= '<th>RRP</th>';
$html .= '</tr>';
// Show product prodcuts from selected category
while ( $row123 = mysqli_fetch_row($result1)) {
$html .= '<tr><td>';
$html .= '<img width="100px" height="100px" src="/images/' . $row123[6] . '"/>';
$html .= '</td><td>';
$html .= '<b><u>' . $row123[1] . '</u></b><br /><i>' . $row123[5] . '</i>';
$html .= '</td><td>';
$html .= $row123[3];
$html .= '</td><td>';
$html .= '£' . $row123[4];
//echo '<br />';
$html .= '</td><td>';
$query2 = "SELECT VAT FROM VAT WHERE ID = " . $row123[7];
$result2 = mysqli_query($connection, $query2);
confirm_query($result2);
// Put results in an accessible form
$result_array2 = array();
while ($row234 = mysqli_fetch_row($result2)) {$result_array2[] = $row234;}
$html .= '£' . number_format($row123[4] + ($result_array2[0][0] * $row123[4] / 100), 2, '.', '');
$html .= '</td><td>';
if ($row123[9] == "") {
$html .= "N/A";
} else {
$html .= '£' . number_format($row123[9], 2, '.', '');
}
$html .= '</td>';
$html .='</tr>';
}
$html .= '</tbody></table>';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
}
If I comment out the second while loop, I can get the first query ($query1) to echo out. One per page for each category. But when I uncomment it, it just creates 4 blank pages.

Related

Implode PHP array and display text strings in their own DIVs

I am using the following database query and PHP code to retrieve checkbox values and display them on the page as HTML.
$submissionId = JRequest::getInt('submissionId');
$db = JFactory::getDbo();
$db->setQuery("SELECT `FormId`, `FieldName`, `FieldValue` FROM `#__rsform_submission_values` WHERE `FormId` = '5' AND `SubmissionId` = '$submissionId' AND `FieldName` = 'documents'");
$rows = $db->loadObjectList();
foreach($rows as $row) {
$documentsArray[] = $row->FieldValue;
}
$document = implode(' ', $documentsArray);
$formLayout .= '<div class="formContainer">';
$formLayout .= '<div class="formView">';
for($i = 0; $i < count($document); $i++) {
$formLayout .= "<div>" . $document . "</div>";
}
$formLayout .= '</div>';
$formLayout .= '</div>';
The code is partially working. The problem lies in the "FieldValue" row containing one or more checkbox values. I am needing to have each checkbox value formatted and displayed in it's own DIV like I have detailed below.
<div>Checkbox value #1</div>
<div>Checkbox value #2</div>
<div>Checkbox value #3</div>
I only need to show the checkbox values and not the checkboxes themselves. Any tips on how I can select each individual string from the imploded array and display it in it's own DIV. Thank you.
Just remove a little part of your code:
$submissionId = JRequest::getInt('submissionId');
$db = JFactory::getDbo();
$db->setQuery("SELECT `FormId`, `FieldName`, `FieldValue` FROM `#__rsform_submission_values` WHERE `FormId` = '5' AND `SubmissionId` = '$submissionId' AND `FieldName` = 'documents'");
$rows = $db->loadObjectList();
$formLayout .= '<div class="formContainer">';
$formLayout .= '<div class="formView">';
foreach($rows as $row) {
$formLayout .= "<div>" . $row->FieldValue . "</div>";
}
$formLayout .= '</div>';
$formLayout .= '</div>';
Or implode in "divs":
...
foreach($rows as $row) {
$documentsArray[] = $row->FieldValue;
}
$document = implode('</div><div>', $documentsArray);
$formLayout .= '<div class="formContainer">';
$formLayout .= '<div class="formView">';
$formLayout .= '<div>' . $document . '</div>';
$formLayout .= '</div>';
$formLayout .= '</div>';

Selecting multiple tables only working in one table

I'm trying to connect four tables. order_history, orders_list, infistall_order, and delivery_orders. When I run the code into the table of order_history, the results are as what I am expecting. But when I run the code into the webpages, it only selects the value from the table of order_history.
Note: What I mean with running the code into the table of order_history is when I enter phpMyAdmin and go to the order_history table and run SQL there with the SQL code I have.
<?php
include ("config.php");
$results = $mysqli->query
("
SELECT orders_history.transaction_id,
orders_history.items,
orders_history.quantity,
orders_history.one_product_price,
orders_list.status,
orders_list.invoices,
orders_list.payment_method,
orders_list.order_method,
infistall_order.address,
delivery_orders.address,
delivery_orders.service,
delivery_orders.cost,
delivery_orders.city
FROM orders_history
LEFT JOIN orders_list
ON orders_history.transaction_id = orders_list.transaction_id
LEFT JOIN infistall_order
ON orders_history.transaction_id = infistall_order.transaction_id
LEFT JOIN delivery_orders
ON orders_history.transaction_id = delivery_orders.transaction_id
WHERE orders_list.customer_name = 'Klaudia'"
);
Actually I am trying to collect information from all column in all table based on the transaction_id.
$orders = array();
$html = '';
if ($results) {
while($obj = $results->fetch_object()) {
$orders[$obj->transaction_id][$obj->items] = array('quantity' => $obj->quantity, 'invoices' => $obj->one_product_price);
}
$html .= '<table width="70%"><tr>';
$html .= '<td>items</td>';
$html .= '<td>quantity</td>';
$html .= '<td>one_product_price</td>';
$html .= '<td>status</td>';
$html .= '<td>invoices</td>';
$html .= '<td>payment_method</td>';
$html .= '<td>order_method</td>';
$html .= '<td>address</td>';
$html .= '<td>service</td>';
$html .= '<td>cost</td>';
$html .= '<td>city</td></tr>';
foreach ($orders AS $order_id => $order) {
$html .= '<tbody><tr><td rowspan="' . count($order) . '">' . $order_id . '</td>';
$row = 1;
foreach ($order AS $item => $data) {
if ($row > 1) { $html .= '</tr><tr>'; }
$html .= '<td>' . $item . '</td>';
$html .= '<td>' . $data['items'] . '</td>';
$html .= '<td>' . $data['quantity'] . '</td>';
$html .= '<td>' . $data['one_product_price'] . '</td>';
$html .= '<td>' . $data['status'] . '</td>';
$html .= '<td>' . $data['invoices'] . '</td>';
$html .= '<td>' . $data['payment_method'] . '</td>';
$html .= '<td>' . $data['order_method'] . '</td>';
$html .= '<td>' . $data['address'] . '</td>';
$html .= '<td>' . $data['service'] . '</td>';
$html .= '<td>' . $data['cost'] . '</td>';
$html .= '<td>' . $data['city'] . '</td>';
$row++;
}
$html .= '</tr><tbody>';
}
$html .= '</table>';
}
echo $html;
?>
On your loop:
while($obj = $results->fetch_object()) {
$orders[$obj->transaction_id][$obj->items] = array(
'quantity' => $obj->quantity,
'invoices' => $obj->one_product_price
);
}
You can change it to:
while($obj = $results->fetch_object()) {
$orders[$obj->transaction_id][$obj->items][] = array(
'quantity' => $obj->quantity,
'invoices' => $obj->one_product_price
);
}
It will prevent the data from replacing one to another, the [] sign on array is make the array to become dynamic, the key will be count from 0 and will be increment by 1 as you add more data into that array.

cant display two image links from mysql php

function find_image_by_id() {
global $connection;
$query = "SELECT * ";
$query .= "FROM images ";
$query .= "WHERE page_id={$_GET["page"]}";
$image_set = mysqli_query($connection, $query);
confirm_query($image_set);
return $image_set;
}
function display_image_by_id(){
$current_image = find_image_by_id();
while($image=mysqli_fetch_assoc($current_image)){
$output = "<div class=\"images\">";
$output .= "<img src=\"images/";
$output .= $image["ilink"];
$output .= "\" width=\"72\" height=\"72\" />";
$output .= $image["phone_name"];
$output .= "</div><br />";
}
mysqli_free_result($current_image);
return $output;
}
This is the code I'm using to show the images stored as links in mysql
and the images are in a folder. But what happens after this code is executed only the second
value is displayed. I want both value/ images to be displayed.
Try something like that-
All you need to do is just initialize this variable outside the loop.
$output =''; //initialize before
SO your function look like this -
function display_image_by_id(){
$current_image = find_image_by_id();
$output =''; //initialize before
while($image=mysqli_fetch_assoc($current_image)){
$output .= "<div class=\"images\">";
$output .= "<img src=\"images/";
$output .= $image["ilink"];
$output .= "\" width=\"72\" height=\"72\" />";
$output .= $image["phone_name"];
$output .= "</div><br />";
}
mysqli_free_result($current_image);
return $output;
}

Display a table in while loop

I am trying to display a table in while loop with 3 rows and 3 td for each row. But I am confusing how to archived thit.
This is my code so far:
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output = "<table>\n";
$output .= " <tr>\n";
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
$output .= " </tr>\n";
$output .= "</table>\n";
echo $output;
}
The above code given me a table with multiple rows and 1 td for each row. But it is not my expecting result.
Can anybody tell me how can I display such a table in my While loop?
The current code creates a new table for every single entry. What you want is to move the <table> and </table> pieces outside of the loop, then you want a counter to keep track of how many cells you've handled. If it's even, start a new <tr>. Otherwise, end the current </tr>. Make sure you check at the end to see if you've finished a </tr>, and optionally add an empty <td></td> if needed.
this aught to do it
<?php
echo "<table>\n";
$cells = $total = 0;
$total_cells = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result))
{
$testimonial = nl2br($row['testimonial']);
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
if ($cells === 0)
echo "<tr>\n";
//Create new testimonial output
$output = " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
$cells++;
$total++;
if ($cells === 3 || $total === $total_cells)
{
echo "</tr>\n";
$cells = 0;
}
}
echo "</table>\n";
?>
Try this
$i = 0;
echo "<table>\n<tr>";
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
if( $i %2 == 1 )
echo "</tr><tr>\n";
$i++;
}
echo "</tr>\n</table>\n";
EDIT
In general, for displaying n cells per row, change the if statement to
if( $i % n == (n - 1) )

Writing out the category of a product from Duka Press Plugin

I'm trying to modify a wordpress plugin called Duka Press, and I'm a complete noobie to PHP. It uses shortcodes which produces the template for the product grid, and I basically just need to write out all the categories the product has to create jQuery filter function.
To better understand what I need read the HTML comment in the code underneath
CODE:
<ul>
I need this for each category inside the active category set i the shortcode:
<li>[CategoryNameHere]</li>
</ul>
$products = get_posts($order_string . 'numberposts=' . $per_page . '&post_type=' . $type . '&meta_key=price&category=' . $category . $offset);
if (is_array($products) && count($products) > 0) {
$content .= '<div class="dpsc_grid_display">';
$count = 1;
$all_count = 0;
foreach ($products as $product) {
$output = dpsc_get_product_details($product->ID, $p_b_n, $direct_checkout);
if ($output) {
$attachment_images = &get_children('post_type=attachment&post_status=inherit&post_mime_type=image&post_parent=' . $product->ID);
$main_image = '';
foreach ($attachment_images as $image) {
$main_image = $image->guid;
break;
}
$prod_permalink = get_permalink($product->ID);
$content .= '<div class="dpsc_grid_product">';
$content .= '<div class="dpsc_grid_product_image">';
if ($main_image != '') {
$content .= '<img src="' . DP_PLUGIN_URL . '/lib/timthumb.php?src=' . $main_image . '&w=' . $dp_shopping_cart_settings['g_w'] . '&h=' . $dp_shopping_cart_settings['g_h'] . '&zc=1" >';
}
$content .= '</div>';
<!-- In the following line i need to write out all the categories for the current product -->
$content .= '<div class="dpsc_grid_product_detail [Categories here]">';
$content .= '<p class="title">' . __($product->post_title) . '</p>';
$content .= '<p class="detail">' . $product->post_excerpt . '</p>';
$content .= '<p class="price">' . $output['price'] . '</p>';
$content .= $output['start'];
$content .= $output['add_to_cart'];
$content .= $output['end'];
$content .= '</div>';
$content .= '</div>';
if ($count === intval($column)) {
$content .= '<div class="clear"></div>';
$count = 0;
}
$count++;
$all_count++;
}
}
$content .= '<div class="clear"></div>' . $page_links . '<div class="clear"></div>';
$content .= '</div>';
$content .= '<div class="clear"></div>';
}
return $content;
Shortcode that uses the template above:
[dpsc_grid_display category="22" total="100" column="3" per_page="100" type="duka"]

Categories