What I want if the result is not found from the query select, I want to show else function like:
else {
$html .= 'You have not added stock at all';
}
Because in this case I used string of html, I don't know how to echo the else statement.
More or less my codes is looking like this right now (There are many parts I have removed since it's too long)
<?php
include("../actions/config.php");
$resultsClaim = $mysqli->query("SELECT");
$orders = array();
$html = '';
if ($resultsClaim) {
while($obj = $resultsClaim->fetch_object()) {
$orders[$obj->id_cart][$obj->items] = array('status' => $obj->status ...);
}
foreach ($orders AS $order_id => $order) {
$orderCount = count($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>';
$row++;
}
$html .= '<div>
<div class="member-popUpeStock'.$data['id'].' member-PopUp">
<div class="member-PopUp-box">
X
<div class="tablePopUp">
<div class="table-row">
<div class="col">: '.$data['method'].' </div>';
///HERE WHERE I WANT TO DO THAT////
else {
echo 'Nothing';
}
echo $html;
?>
Can anyone help me, please! Thanks in Advance.
Since you are taking the no of orders into a variable, you can make use of that directly to see if the orders exists or not.
$orderCount = 0; //Define orderCount in the beginning.
//Rest of the code, till foreach
foreach(){
//Rest of the code here
}
if(intval($orderCount) <= 0) //Using intval, for the case the for loop is not executed at all
{
$html = "Nothing";
}
Here you dont need to concatenate it to $html since you are displaying the table content if order exists or else you displaying "Nothing".
So $html will either have the table content or else "Nothing".
Hope this helps.
First get count of $orders
if (! empty($orders)) {
foreach ($orders AS $order_id => $order) {
// YOUR CODE HERE
}
}
else {
$html .= 'Nothing'; // Append your `no records found` message to the `$html` variable:
}
foreach ($orders AS $order_id => $order) { } else {}
So "}" is missing for foreach loop. Hope it will work for you. Let me know if you need further help.
Give it a try
<?php
include("../actions/config.php");
$resultsClaim = $mysqli->query("SELECT");
$orders = array();
$html = '';
if (!empty($resultsClaim)) {
while ($obj = $resultsClaim->fetch_object()) {
$orders [$obj->id_cart][$obj->items] = array('status' => $obj->status);
}
foreach ($orders AS $order_id => $order) {
$orderCount = count($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>';
$row++;
}
$html .= '<div>
<div class="member-popUpeStock' . $data['id'] . ' member-PopUp">
<div class="member-PopUp-box">
X
<div class="tablePopUp">
<div class="table-row">
<div class="col">: ' . $data['method'] . ' </div>';
}
}
///HERE WHERE I WANT TO DO THAT////
else {
echo 'Nothing';
}
echo $html;
?>
Related
I have written a function to display the data fetched from MySQL database using PHP mysqli_query( $db_conn, $sql ); when I am calling the following display function first row of the data is not displayed. Please suggest how can I get the first row.
function display_data($retval) {
$output = '<table border="1">';
foreach($retval as $key => $var) {
$output .= '<tr>';
foreach($var as $k => $v) {
if ($key === 0) {
$output .= '<td><strong>' . $k . '</strong></td>';
} else {
$output .= '<td>' . $v . '</td>';
}
}
$output .= '</tr>';
}
$output .= '</table>';
echo $output;
}
Code for display data
if (isset( $_POST['submit'])){
$_date = $_POST["date"];
//echo $_date;
$resultArray = array();
$sql = "SELECT * FROM outstanding WHERE ondate='$_date' ";
};
/*echo json_encode($retval);*/
$retval = mysqli_query( $db_conn, $sql );
display_data($retval);
}
EDIT 2:
I was able to achieve this by the following code, please suggest if there are any better methods to display in HTML/web page.
//code to display data in table form
function display_data($retval)
{
$output = '<table border="1">';
foreach ($retval as $key => $var)
{
echo $key;
if ($key === 0)
{
$output .= '<tr>';
foreach ($var as $k => $v)
{
$output .= '<th><strong>' . $k . '</strong></th>';
};
$output .= '</tr>';
};
$output .= '<tr>';
foreach ($var as $k => $v)
{
if ($key === 0)
{
$output .= '<td>' . $v . '</td>';
echo $v;
}
else
{
$output .= '<td>' . $v . '</td>';
};
};
$output .= '</tr>';
}
$output .= '</table>';
echo $output;
}
The reason why you cannot see the first row is that you never display it. If the iteration is on the first row, you only display the header, but you skip the data.
foreach ($var as $k => $v) {
if ($key === 0) {
// If this is the first row, you display the key, but not the value.
$output .= '<td><strong>' . $k . '</strong></td>';
} else {
$output .= '<td>' . $v . '</td>';
}
}
Your suggested solution is the correct one. You need to loop the first row twice. Once to display the header and then once to display the data.
function display_data1($retval) {
$output = '<table border="1">';
foreach ($retval as $key => $var) {
if ($key === 0) {
// If key is 0, meaning first row...
$output .= '<thead><tr>';
// create new HTML row and loop the first MySQL row
foreach ($var as $k => $v) {
$output .= '<th>' . $k . '</th>';
}
$output .= '</tr></thead>';
}
// Then loop as normal all rows including the first one.
$output .= '<tr>';
foreach ($var as $k => $v) {
$output .= '<td>' . $v . '</td>';
}
$output .= '</tr>';
}
$output .= '</table>';
echo $output;
}
Warning: You are wide open to SQL Injections and should use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input! Even when your queries are executed only by trusted users, you are still in risk of corrupting your data. Escaping is not enough!
I have an array that i pull from a database that looks like this
id - name - shortlink - downloadurl - count - date
Now I want to display that array as a table so the administartor of the site can se the content of the database. For that, I'm using this code:
function build_table($array){
// start table
$html = '<table id="usertable">';
// header row
$html .= '<tr class="header">';
foreach($array[0] as $key=>$value){
$html .= '<th>' . $key . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $array as $key=>$value){
$html .= '<tr>';
foreach($value as $key2=>$value2){
$html .= '<td>' . $value2 . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
And it works great.
The problem is that i wan't do display some of the columns with a different code. E.g. 'downloadurl' which is an webadress, i want to make clickable. I just can't figure out how to split up the function so that i can write the code for the individual columns.
You could try something similar to the following:
using conditionals
This is probably the most straight forward way to go about handling different attributes per column - not without its drawbacks (noted below)
// data rows
foreach( $array as $key => $value) {
$html .= '<tr>';
foreach($value as $key2 => $value2) {
if ($key2 == 'downloadurl') {
$html .= '<td>Download</td>';
} else {
$html .= '<td>' . $value2 . '</td>';
}
}
$html .= '</tr>';
}
using switch statement
If you decide on this route, it may be a bit easier to manage in the long run as if () elseif () else() can become difficult to read over time.
foreach($value as $key2 => $value2) {
switch($key2) {
case 'downloadurl':
$html .= '<td>Download</td>';
break;
default:
$html .= '<td>' . $value2 . '</td>';
}
}
use this function to generate urls:
function getURL($downloadURL)
{
$html = "<a href = '$downloadURL'>Download</a>";
return $html;
}
And your function build_table():
function build_table($array){
// start table
$html = '<table id = "usertable">';
// header row
$html .= '<tr class = "header">';
foreach($array[0] as $key => $value){
$html .= '<th>' . $key . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $array as $key => $value){
$html .= '<tr>';
foreach($value as $key2 => $value2){
$value2 = ($key2 == 'downloadurl') ? getURL($value2) : $value2;
$html .= '<td>' . $value2 . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
}
I have this php code which takes in elements from an array and arranges the values into divs (the code starts a new row after every 3 items):
<?php
function display_products($product_id, $product){
$count = 0;
$output = "";
foreach($products as $product_id => $product) {
if (++$count % 3 == 1){
$output = $output . '<div class="offset1 span3">';
}
else {
$output = $output . '<div class="span3">';
}
$output = $output . '<div class="centre">';
$output = $output . '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';
$output = $output . "</div>";
$output = $output . "</div>";
if (++$count % 3 == 0) {
$output = $output . "</div> <div class='row-fluid'>";
}
}
return $output;
}
$products = array();
$products[101] = array(
"name" => "My Product",
"img" => "img/product1.jpg"
);
?>
and have linked to this file in my index.php file like this:
<div class="row-fluid">
<?php
echo display_products($product_id, $product);
?>
</div>
my problem is, the function doesn't work. The code worked fine before I pasted it into the function.
You're defining the second variable passed into your function to be named $product, then using $products to loop through (which won't exist).
Should be:
function display_products($product_id, $products){
...
foreach($products as $product_id => $product) {
ARRAY
$array['1'] = null;
$array['2']['21'] = null;
$array['3']['31'] = null;
$array['3']['32'] = null;
$array['4']['41'] = null;
$array['4']['42']['421'] = null;
$array['4']['42']['422'] = null;
$array['5']['51'] = null;
$array['5']['52'] = null;
$array['5']['53']['531'] = null;
$array['5']['53']['532'] = null;
$array['5']['53']['533'] = null;
$array['6']['61']['611'] = null;
$array['6']['62'] = null;
$array['6']['63']['631'] = null;
$array['6']['63']['632'] = null;
$array['6']['63']['633'] = null;
$array['6']['63']['634'] = null;
$array['7']['71']['711'] = null;
$array['8']['81']['811'] = null;
$array['8']['81']['812'] = null;
$array['9']['91']['911'] = null;
$array['9']['91']['912'] = null;
$array['9']['92']['921'] = null;
$array['9']['92']['922'] = null;
I'm trying to convert array above into a HTML table. I need to add rowspan to get output like shown as image below but run out of juice.
PHP
//If condition is satistied
if (isset($array) && count($array) > 0)
{
//echo '<pre>'; print_r($array); exit;
//Initiate variables
$row = null;
$level_one_array = $array;
//Iterate through menu level one
foreach ($level_one_array as $level_one_name => $level_two_array)
{
//If level two is an array
if (is_array($level_two_array))
{
//Iterate through menu level two
foreach ($level_two_array as $level_two_name => $level_three_array)
{
//If level two is an array
if (is_array($level_three_array))
{
//Iterate through menu level three
foreach ($level_three_array as $level_three_name => $null)
{
$row .= '<tr>';
$row .= '<td class="td_data">' . $level_one_name . '</td>';
$row .= '<td class="td_data">' . $level_two_name . '</td>';
$row .= '<td class="td_data">' . $level_three_name . '</td>';
$row .= '</tr>';
}
}
//If level two is not an array
else
{
$row .= '<tr>';
$row .= '<td class="td_data">' . $level_one_name . '</td>';
$row .= '<td class="td_data">' . $level_two_name . '</td>';
$row .= '<td class="td_data"> </td>';
$row .= '</tr>';
}
}
}
//If level two is not an array
else
{
$row .= '<tr>';
$row .= '<td class="td_data">' . $level_one_name . '</td>';
$row .= '<td class="td_data"> </td>';
$row .= '<td class="td_data"> </td>';
$row .= '</tr>';
}
}
$table = '<table>';
$table .= '<tr><td class="td_title">Menus</td><td class="td_title">Submenus/Items</td><td class="td_title">Items</td></tr>';
$table .= $row;
$table .= '</table>';
echo $table;
}
else
{
echo '<center>No record found</center>';
}
WHAT I NEED IS THIS:
I wrote new code that will do the trick. Working with rowspan is very tricky. The key elements are calculate_rowspan() and the flag $on_level_X_row to determine whether or not it is necessary to add <tr>.
It was necessary to detach the output from the most inner foreach because you have to handle multiple rows with it. Also I added "\n" after each </tr> to make the output a little more human readable.
function calculate_rowspan($array)
{
if (!is_array($array) || count($array) == 0)
return 1;
$rowspan = 0;
foreach ($array as $key=>$value)
{
$rowspan++;
if (is_array($value) && count($value) > 0)
{
$rowspan += count($value);
// -1 because first element is one same row
$rowspan--;
}
}
return $rowspan;
}
if (is_array($array) && count($array) > 0)
{
$output = '';
foreach ($array as $level_one_name => $level_two_array)
{
if (!is_array($level_two_array) || count($level_two_array) == 0)
{
$output .= '<tr><td>';
$output .= $level_one_name;
$output .= '</td><td></td><td></td></tr>'."\n";
}
else
{
$output .= '<tr>';
$output .= '<td rowspan="'.calculate_rowspan($level_two_array).'">';
$output .= $level_one_name;
$output .= '</td>';
$on_level_one_row = TRUE;
foreach ($level_two_array as $level_two_name => $level_three_array)
{
if ( ! $on_level_one_row)
$output .= '<tr>';
else
$on_level_one_row = FALSE;
if (!is_array($level_three_array) || count($level_three_array) == 0)
{
$output .= '<td>';
$output .= $level_two_name;
$output .= '</td><td></td></tr>'."\n";
}
else
{
$output .= '<td rowspan="'.calculate_rowspan($level_three_array).'">';
$output .= $level_two_name;
$output .= '</td>';
$on_level_two_row = TRUE;
foreach ($level_three_array as $level_three_name => $null)
{
if ( ! $on_level_two_row)
$output .= '<tr>';
else
$on_level_two_row = FALSE;
$output .= '<td>';
$output .= $level_three_name;
$output .= '</td></tr>'."\n";
}
}
}
}
}
print '<table>'."\n";
print $output;
print '</table>';
}
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.