<?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,
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'"
);
$orders = array();
$html = '';
if ($results) {
while($obj = $results->fetch_object()) {
$orders[$obj->transaction_id][$obj->items] = array(
'invoices' => $obj->invoices,
'status' => $obj->status,
'payment_method' => $obj->payment_method,
'service' => $obj->service,
'cost' => $obj->cost,
'quantity' => $obj->quantity,
'one_product_price' => $obj->one_product_price,
'city' => $obj->city);
}
$html .= '<table width="70%"><tr>';
$html .= '<td>transaction_id</td>';
$html .= '<td>items</td>';
$html .= '<td>quantity</td>';
$html .= '<td>one_product_price</td>';
$html .= '<td>status</td>';
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['quantity'] . '</td>';
$html .= '<td>' . $data['one_product_price'] . '</td>';
$row++;
$html .= '<td rowspan="' . count($order) . '">' . $data['status'] . '</td>';
}
$html .= '</tr><tbody>';
}
$html .= '</table>';
}
echo $html;
?>
The code above will result like this:
-----------------------------------------------------------------------
id_cart products quantity invoices status
-----------------------------------------------------------------------
this 2 $20
-------------------------------------------------------
0001 that 1 $20 pending
-------------------------------------------------------
those 2 $20 pending pending
-----------------------------------------------------------------------
Total Invoices: $60
-----------------------------------------------------------------------
this 2 $20
-------------------------------------------------------
0002 that 1 $20 approved
-------------------------------------------------------
those 2 $20 approved approved
-----------------------------------------------------------------------
Total Invoices: $60
Have a look at the column of status, where the result is looping.
What I want is it shouldn't be looping and only get one result as how the column of id_cart looks like. This is the code for the column of status
$html .= '<td rowspan="' . count($order) . '">' . $data['status'] . '</td>';
I need to solve it!
[EDIT]
When I move the code like this:
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['quantity'] . '</td>';
$html .= '<td>' . $data['one_product_price'] . '</td>';
$row++;
}
$html .= '<td rowspan="' . count($order) . '">' . $data['status'] . '</td>';
$html .= '</tr><tbody>';
-----------------------------------------------------------------------
id_cart products quantity invoices status
-----------------------------------------------------------------------
this 2 $20
-------------------------------------------------------
0001 that 1 $20
-------------------------------------------------------
those 2 $20 pending
-----------------------------------------------------------------------
Total Invoices: $60
-----------------------------------------------------------------------
this 2 $20
-------------------------------------------------------
0002 that 1 $20
-------------------------------------------------------
those 2 $20 approved
-----------------------------------------------------------------------
Total Invoices: $60
The resultin the column of status in at the very bottom of the tbody. I want it as how the column of id_cart is.
You need 1 value of status per tbody, so put it outside your loop. Also, because you're reusing $order, I've put count($order) in a seperate variable. Also, close your </tbody> tag.
foreach ($orders AS $order_id => $order) {
$orderCount = count($order);
$html .= '<tbody><tr><td rowspan="' . $orderCount . '">' . $order_id . '</td>';
$row = 1;
foreach ($order AS $item => $data) {
if ($row > 1) { $html .= '</tr><tr>'; }
$html .= '<td>' . $item . '</td>';
$html .= '<td>' . $data['quantity'] . '</td>';
$html .= '<td>' . $data['one_product_price'] . '</td>';
if ($row == 1)
$html .= '<td rowspan="' . $orderCount . '">' . $data['status'] . '</td>';
$row++;
}
$html .= '</tr></tbody>';
}
Use valign attribute for td element to get the value aligned center vertically.
$html .= '<td rowspan="' . $orderCount . '" valign="middle">' . $data['status'] . '</td>';
Hope it will work for you.
Check this out:
foreach ($order AS $item => $data) {
if ($row > 1) { $html .= '</tr><tr>'; }
$html .= '<td>' . $item . '</td>';
$html .= '<td>' . $data['quantity'] . '</td>';
$html .= '<td>' . $data['one_product_price'] . '</td>';
if( $row == 1 )
{
$html .= '<td style="vertical-align: middle" rowspan="' . count($order) . '">'. $data['status'] . '</td>';
}
$row++;
}
Related
for anyone who can give a hand. Thanks a lot.
I can't get the JSON data to get into the table. I don't know if it's a problem with the data request path...
When I do // var_dump ($ results); I check that the results of the api (url) are loaded in the page but I don't know what error I made when trying to introduce them in the table
add_shortcode( 'external_data', 'callback_function_name');
function callback_function_name() {
$url = 'https:
$arguments = array(
'method' => 'GET',
);
$response = wp_remote_get( $url, $arguments );
$results = json_decode( wp_remote_retrieve_body( $response ) );
// var_dump($results);
$html = '';
$html .= '<table>';
$html .= '<tr>';
$html .= '<td>name</td>';
$html .= '<td>suite</td>';
$html .= '<td>display_name</td>';
$html .= '<td>id</td>';
$html .= '<td>conference</td>';
$html .= '<td>division</td>';
$html .= '</tr>';
foreach( $results as $result ) {
$html .= '<tr>';
$html .= '<td>' . $result->name . '</td>';
$html .= '<td>' . $result->nickname . '</td>';
$html .= '<td>' . $result->display_name . '</td>';
$html .= '<td>' . $result->id . '</td>';
$html .= '<td>' . $result->conference . '</td>';
$html .= '<td>' . $result->division . '</td>';
$html .= '</tr>';
}
$html .= '</table>';
return $html;
}
fgYou can use file_get_contents
function callback_function_name() {
$data=file_get_contents('https://https://delivery.oddsandstats.co/team_list/NFL.JSON?api_key=your api key');
$data=(array)json_decode($data);
$result=(array)$data['results'];
$teams=$result['data']->team;
$html = '';
$html .= '<table>';
$html .= '<tr>';
$html .= '<td>name</td>';
$html .= '<td>suite</td>';
$html .= '<td>display_name</td>';
$html .= '<td>id</td>';
$html .= '<td>conference</td>';
$html .= '<td>division</td>';
$html .= '</tr>';
foreach( $teams as $result ) {
$html .= '<tr>';
$html .= '<td>' . $result->name . '</td>';
$html .= '<td>' . $result->nickname . '</td>';
$html .= '<td>' . $result->display_name . '</td>';
$html .= '<td>' . $result->id . '</td>';
$html .= '<td>' . $result->conference . '</td>';
$html .= '<td>' . $result->division . '</td>';
$html .= '</tr>';
}
$html .= '</table>';
return $html;
}
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.
I have a web page where i am able to export a .csv file that is readable on excel. The invoice pulls the data from my database to calculate the total and grand total using the following columns:
quantity, packing_price, courier_price
I have noticed that my code doesn't output the correct answer when the prices contains a '£' sound in front of it. Is there a way that i could make this work for both number and currency data types?
CODE:
$output2= "";
$sql2 = mysql_query("
SELECT j.date
, j.order_ref
, j.quantity
, j.packing_price
, j.dispatch_type
, j.courier_price
FROM Jobs j
WHERE j.order_type = 'Retail International'
AND j.confirmed = 'Yes';
");
$columns_total2 = mysql_num_fields($sql2);
$total3 = "Total";
for ($i = 0; $i < $columns_total2; $i++)
{
$heading2 = mysql_field_name($sql2, $i);
$output2 .= '"'.$heading2.'",';
}
$output2 .= $total3;
$output2 .="\n";
$sum2 = 0;
while ($row = mysql_fetch_array($sql2)) {
for ($i = 0; $i < $columns_total2; $i++) {
$output2 .='"'.$row["$i"].'",';
}
$qty2 = $row['quantity'];
$pack_price2 = $row['packing_price'];
$dispatch2 = $row['courier_price'];
$total2 = (($qty2*$pack_price2) + $dispatch2);
$total3 = $total2*1.2;
$output2 .= $total3;
$sum2 += $total3; // Add to overall total
$output2 .="\n";
}
Output:
http://i754.photobucket.com/albums/xx182/rache_R/Screenshot2014-07-03at113133_zpsbcc09900.png
Use this Code....
$output= "<table border='1' width='60%'><tr>";
$sql = " SELECT j.date ,
j.order_ref ,
j.quantity ,
j.packing_price ,
j.dispatch_type ,
j.courier_price
FROM Jobs j
WHERE j.order_type = 'Retail International'
AND j.confirmed = 'Yes' ";
$query = mysql_query($sql);
$total_columns = mysql_num_fields($query);
for ($i = 0; $i < $total_columns; $i++){
$heading = mysql_field_name($query, $i);
$output .= '<td>' . $heading . '</td>';
if(($i+1) == $total_columns) $output .= '<td>Total</td></tr>';
}
while ($row = mysql_fetch_array($query)) {
$total_price = 0;
$total_price =( ( $row['quantity'] * $row['packing_price'] ) +
$row['courier_price'] );
$total_price = $total_price * 1.2;
$timestamp = DateTime::createFromFormat('Y-m-d h:i:s',
$row['date'])->getTimestamp();
$output .= '<tr>';
$output .= '<td>' . date("d/m/Y", $timestamp) . '</td>';
$output .= '<tr>';
$output .= '<td>' . date("d/m/Y", strtotime($row['date']) . '</td>';
$output .= '<td>' . $row['order_ref'] . '</td>';
$output .= '<td>' . $row['quantity']. '</td>';
$output .= '<td>' . $row['packing_price'] . '</td>';
$output .= '<td>' . $row['dispatch_type'] . '</td>';
$output .= '<td>' . $row['courier_price'] . '</td>';
$output .= '<td>' . number_format($total_price, 2) . '</td>';
$output .= '</tr>';
}
$output .= "</table>";
echo '<br>' . $output;
The past couple of hours I am trying to generate an xml file like this
<?xml version="1.0" encoding="UTF-8"?>
<mywebstore>
<created_at>2010-04-08 12:32</created_at>
<products>
<product>
<id>322233</id>
<name><![CDATA[MadBiker 600 Black Polarized]]></name>
<link><![CDATA[http://www.mywebstore.gr/product/322233]]></link>
<image><![CDATA[http://www.mywebstore.gr/product/322233.jpg]]></image>
<category id="23"><![CDATA[Sports > Extreme Sports]]></category>
<price_with_vat>322.33</price_with_vat>
<manufacturer><![CDATA[SuperGlasses]]></manufacturer>
<description><![CDATA[This is the description.....]]></description>
<weight>350</weight>
<mpn>ZHD332</mpn>
<instock>N</instock>
<availability>Pre-order</availability>
</product>
<product>
...
</product>
</products>
</mywebstore>
from opencart.
I have written this piece of code
<?php
class ControllerFeedSkroutzXml extends Controller {
public function index() {
$this->language->load('feed/skroutz_xml');
if ($this->config->get('skroutz_xml_status')) {
$output = '<?xml version="1.0" encoding="UTF-8"?>';
$output .= '<mywebstore>';
$output .= '<created_at>' . date('Y-m-d H:i') . '</created_at>';
$output .= '<products>';
$this->load->model('catalog/product');
$products = $this->model_catalog_product->getProducts();
foreach ($products as $product) {
$attribute_groups = $this->model_catalog_product->getProductAttributes($product['product_id']);
//print_r($attribute_groups);
if (!empty($attribute_groups)) {
foreach ($attribute_groups as $attribute_group) {
if (!empty($attribute_group)) {
foreach ($attribute_group['attribute'] as $attribute) {
$attribute = array_filter($attribute);
if (!empty($attribute)) {
// [attribute_id] => 13, Color
if ($attribute['attribute_id'] == 13 && $attribute['text'] != '') {
$attribute_color = $attribute['text'];
}
// [attribute_id] => 16, Lens Technology
if ($attribute['attribute_id'] == 16 && $attribute['text'] != '') {
$attribute_lens_technology = $attribute['text'];
}
}
}
}
}
}
if ($product['special']) {
$final_price = number_format((float)$product['special'], 2, '.', '');
} else {
$final_price = number_format((float)$product['price'], 2, '.', '');
}
if ($product['quantity'] > 0) {
$instock = $this->language->get('instock_Y');
} else {
$instock = $this->language->get('instock_N');
}
$output .= '<product>';
$output .= '<id>' . $product['product_id'] . '</id>';
$output .= '<name><![CDATA[' . $this->language->get('category_name') . ' ' . $product['name'] . ' ' . $attribute_color . ' ' . $attribute_lens_technology . ']]></name>';
$output .= '<link><![CDATA[' . $this->url->link('product/product', 'product_id=' . $product['product_id']) . ']]></link>';
$output .= '<image><![CDATA['. HTTP_IMAGE . $product['image'] . ']]></image>';
$output .= '<category id="' . $product['manufacturer_id'] . '"><![CDATA[ ' . $this->language->get('category_name') . ' > ' . $product['manufacturer'] . ' ]]></category>';
$output .= '<price_with_vat>' . $final_price . '</price_with_vat>';
$output .= '<manufacturer><![CDATA[' . $product['manufacturer'] . ']]></manufacturer>';
$output .= '<description><![CDATA[' . $product['meta_description'] . ']]></description>';
$output .= '<instock>' . $instock . '</instock>';
$output .= '<availability>' . $product['stock_status'] . '</availability>';
$output .= '</product>';
}
$output .= '</products>';
$output .= '</mywebstore>';
$this->response->addHeader('Content-Type: application/xml');
$this->response->setOutput($output);
}
}
}
?>
But the block of code that generates the attributes it doesn't work as expected.
A lot of my products don't have attributes (at least not yet), so what I want to accomplish is to show attributes right next to the name of the product
Example
Name: MadBiker 600
Attribute - Color: Black
Attribute - Lens Technology : Polarized
All together <name>MadBiker 600 Black Polarized</name>
Only if a product has attributes!
The above php code generates the <name>MadBiker 600 Black Polarized</name> to all empty of attributes products until it finds the next product with an attribute!
Could someone please point out where is the problem?
Thank you!
You aren't resetting the $attribute_lens_technology and $attribute_color with each iteration of the foreach. You need to reset these after the foreach loop definition
New foreach loop:
foreach ($products as $product) {
$attribute_lens_technology = false;
$attribute_color = false;
$attribute_groups = $this->model_catalog_product->getProductAttributes($product['product_id']);
//print_r($attribute_groups);
if (!empty($attribute_groups)) {
foreach ($attribute_groups as $attribute_group) {
if (!empty($attribute_group)) {
foreach ($attribute_group['attribute'] as $attribute) {
$attribute = array_filter($attribute);
if (!empty($attribute)) {
// [attribute_id] => 13, Color
if ($attribute['attribute_id'] == 13 && $attribute['text'] != '') {
$attribute_color = $attribute['text'];
}
// [attribute_id] => 16, Lens Technology
if ($attribute['attribute_id'] == 16 && $attribute['text'] != '') {
$attribute_lens_technology = $attribute['text'];
}
}
}
}
}
}
if ($attribute_lens_technology === false || $attribute_color === false) {
// Code here such as continue; if you want to skip products without both attributes
}
if ($product['special']) {
$final_price = number_format((float)$product['special'], 2, '.', '');
} else {
$final_price = number_format((float)$product['price'], 2, '.', '');
}
if ($product['quantity'] > 0) {
$instock = $this->language->get('instock_Y');
} else {
$instock = $this->language->get('instock_N');
}
$output .= '<product>';
$output .= '<id>' . $product['product_id'] . '</id>';
$output .= '<name><![CDATA[' . $this->language->get('category_name') . ' ' . $product['name'] . ' ' . $attribute_color . ' ' . $attribute_lens_technology . ']]></name>';
$output .= '<link><![CDATA[' . $this->url->link('product/product', 'product_id=' . $product['product_id']) . ']]></link>';
$output .= '<image><![CDATA['. HTTP_IMAGE . $product['image'] . ']]></image>';
$output .= '<category id="' . $product['manufacturer_id'] . '"><![CDATA[ ' . $this->language->get('category_name') . ' > ' . $product['manufacturer'] . ' ]]></category>';
$output .= '<price_with_vat>' . $final_price . '</price_with_vat>';
$output .= '<manufacturer><![CDATA[' . $product['manufacturer'] . ']]></manufacturer>';
$output .= '<description><![CDATA[' . $product['meta_description'] . ']]></description>';
$output .= '<instock>' . $instock . '</instock>';
$output .= '<availability>' . $product['stock_status'] . '</availability>';
$output .= '</product>';
}
It's easier to write an xml file using simplexml than it is to manually try and output your own.
Nevertheless, here's a simple shorthand if statement to fix to your problem though (if attribute color is empty, it will append an empty string instead:
$output .= !empty($attribute_color) ? '<name><![CDATA[' . $this->language->get('category_name') . ' ' . $product['name'] . ' ' . $attribute_color . ' ' . $attribute_lens_technology . ']]></name>' : '';
I wonder how can i find out last in table with PHP, aka if that is last then i want to apply different style and content to it.
This is part of my code generating table content.
$content .= '</tr>';
$totalcols = count ($columns);
if (is_array ($tabledata))
{
foreach ($tabledata as $tablevalues)
{
if ($tablevalues[0] == 'dividingline')
{
$content .= '<tr><td colspan="' . $totalcols . '" style="background-color:#efefef;"><div align="left"><b>' . $tablevalues[1] . '</b></div></td></tr>';
continue;
}
else
{
$content .= '<tr>';
foreach ($tablevalues as $tablevalue)
{
$content .= '<td>' . $tablevalue . '</td>';
}
$content .= '</tr>';
continue;
}
}
}
else
{
$content .= '<tr><td align="center" style="border-bottom: none;" colspan="' . $totalcols . '">No Records Found</td></tr>';
}
I know there is option to do something like that with jQuery later on, but I don't want to complicate it and just solve it with php at time when i generate table.
Keep a count:
$count = 1;
foreach ($tablevalues as $tablevalue)
{
$count++;
if( $count > count( $tablevalues)) {
echo "Last td tag is up next! ";
}
$content .= '<td>' . $tablevalue . '</td>';
}
you can execute any code in last loop:
for($i=0;$i<count($tablevalues);$i++)
{
$tablevalue=$tablevalues[$i];
$content .= '<td>' . $tablevalue . '</td>';
if($i==count($tablevalues)-1){
// last loop execution
}
}
here is a somehow trick
foreach ($tablevalues as $k => $tablevalue)
{
if ($k==count($tablevalues)-1)
{
// last td of current row
$content .= '<td>' . $tablevalue . '</td>';
}
else
{
$content .= '<td>' . $tablevalue . '</td>';
}
}
I just hope that your keys of your $tablevalues set match this code.
You should use a for($i = 0; i < count($tablevalues); $i++) {} loop and consider count($tablevalues)-1 to be the last key of your array.
So that $tablevalues[count($tablevalues)-1] is your last <td>.
Why dont you use jQuery?It has provisions to do something like that.