JSON to TABLE in PHP - php

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;
}

Related

How can I echo two items per one time by using PHP Foreach?

I'm using PHP to echo my products from the database. If we just use foreach, we will get the result one item per a loop, but actually I want it echo two items per one times as ub the below function.
This is my PHP function using foreach to fetch data from database.
I've used row item selector to wrap product selector, so I want to echo a block of product two times, and then it should echo the row item.
Example: row item = 1 then product = 2
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
foreach ($data as $k => $row) {
$out .= '<div class="row item">';
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
$out .= '</div>';
}
}
return $out;
}
This function will echo one item for a loop.
You can not print two times in one iteration of a loop. You can use conditional HTML output to do this job.
Consider this:
function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
$counter = 1;
$length = count($data);
if ($data) {
foreach ($data as $k => $row) {
if ($counter % 2 != 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if ($counter % 2 == 0 || $length == $counter) {
$out .= '</div>';
}
$counter++;
}
}
return $out;
}
You can use the modulus operator to make the check. If your iterator is a multiple of two it will output the appropriate elements (it does this by checking that the remainder is zero):
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
$i = 0;
foreach ($data as $k => $row) {
if( ($i % 2) === 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if( ($i + 1) % 2 === 0 || ($i + 1) === count($data) ) {
$out .= '</div>';
}
$i++;
}
}
return $out;
}
Note that the last bit ($i + 1) === count($data) is important in the event that your set holds an uneven number.

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

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.

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.

Prevent one data to loop

<?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++;
}

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