a FOREACH Loop outputs data from a booking array. Within this loop an IF condition filters the several bookings. If the booking date is after today, it outputs the booking information into a table. Works properly.
If there is no booking in future, there should a message printed "No bookings in future". In case that there is never made a booking, this works fine, but if bookings in past already exist, the message repeats for every booking in the past.
How can I prevent this?
Thanks in advance
Thorsten
<tbody>
<?php if ($tpl['booking_arr']) { ?>
<?php foreach ($tpl['booking_arr'] as $item) { ?>
<?php if (date('Ymd', strtotime($item['dt'])) > date('Ymd')) { ?>
<tr>
<td>
<?php echo date($tpl['option_arr']['o_date_format'], strtotime($item['dt']));?>
</td>
<td style="text-align: center;"><?php echo (int)$item['people'] + (int)$item['children']; ?></td>
</tr><?php } else { ?>
<tr>
<td colspan="2" style="font-size: 10px;">
keine bevorstehenden Reservierungen
</td>
</tr>
<?php } } } ?>
<tr><td style="border-bottom: 1px solid #000;" colspan="2"></td></tr>
<tr>
<td class="reg-seit">REG: <?php echo date($tpl['option_arr']['o_date_format'], strtotime($tpl['arr']['created']));?></td>
<td class="res-seit">RES: <?php $array = $tpl['booking_arr']; $getCount = count($array); echo ($getCount);?> </td>
</tr>
I don't know much about PHP but here's an idea: don't put the "no upcoming bookings" message inside the foreach loop without having some way to break from the loop (provided it is ordered by date). Use a boolean to check if there are any future bookings, which is set to false by default, and set to true if a future booking is found. After the loop, print the message if there are no future bookings. In pseudocode,
boolean futureBookingsExist = false;
foreach (date in bookingsArray) {
if (date > currentDate) {
outputBookingDate();
futureBookingsExist = true;
}
}
if (!futureBookingsExist) {
print("no future bookings exist");
}
You can just break out of the foreach block once you print out that message with break. This is based on the assumption that the content of $tpl['booking_arr'] is ordered by date. In case it's not, you will have to sort it.
Related
I want to create a PHP page to display the results of a query in a MySQL database under the format of a table. By spending quite some time on different forums, I ended with something that is somehow satisfying me but that is strongly affecting the design and the layout of my webpage. Due to the fact that I wrote the code by a test-fail strategy, it is far from being straightforward and I am sure it is possible to shorten and simplify it and, therefore, make it more compatible with the format of my webpage. Could anybody have a look at it and give some suggestions of general interest about how to solve this kind of issues?
<div id="main">
<?php
require_once('../mysqli_connect.php');
$response = $db->query("SELECT * FROM metabolite");
echo '<table align="center" cellspacing="2" cellpadding="5" border = "1">
<tr><td align="center"><b>Metabolites</b></td>
<td align="center"><b>KEGG Id</b></td>
<td align="center"><b>Synonyms</b></td></tr>';
while ($data = $response->fetch())
{
?>
<tr><td align="left">
<?php echo $data['Metabolite_name']; ?></td>
<td align="left">
KEGG: <?php echo $data['Synonyms']; ?></td>
<td align="left">
<?php echo $data['Synonyms']; ?></td>
</tr>
<?php
}
$response->closeCursor();
?>
</div>
I thank you in advance for all your effort and your help.
Tom.
There's no way we can improve the design and layout of your webpage with the code you've given us. What I can do is write 'better' readable code.
<?php
function tableCell($content)
{
echo '<td align="left">'.$content.'</td>';
}
// database access
require_once('../mysqli_connect.php');
// get all records from the metabolite table
$response = $db->query("SELECT * FROM metabolite");
// start main division
echo '<div id="main">';
// start the table
echo '<table align="center" cellspacing="2" cellpadding="5" border = "1">';
// walk through all the metabolite records
while ($data = $response->fetch())
{
// start a row
echo '<tr>';
// create the cells
tableCell($data['Metabolite_name']);
tableCell('KEGG: '.$data['Synonyms']);
tableCell($data['Synonyms']);
// finish a row
echo '</tr>';
}
// close the table
echo '</table>';
// close main division
echo '</div>';
// close query
$response->closeCursor();
But this is not worth much, the output should remain the same.
if ($response->num_rows > 0) {
while($data = $response->fetch_assoc()) {
echo "<tr><td>" . $data["Metabolite_name"]. "</td></tr>" . ;
}
}
else {
echo "0 results";
}
I'm currently modifying open source PHP POS, I would like to amend the code instead of showing warning and display the 0 stock items to don't display the result if the quantity is 0.
I manage to change the message from normal message to a javascript popup however I still prefer to not show the item. Because my staff who use this system always ignores the popup and when the month end it showed quite a number of negative items and creates headache for me to check and alter. Therefore I'm seeking professional helps here to advise and assist me. I look high and low and manage to get a bunch of code which I believe it might be the one. Please correct me if I'm wrong.
The code:
function add_item($item_id,$quantity=1,$discount=0,$price=null,$description=null,$serialnumber=null)
{
//make sure item exists
if(!$this->CI->Item->exists($item_id))
{
//try to get item id given an item_number
$item_id = $this->CI->Item->get_item_id($item_id);
if(!$item_id)
return false;
}
//Alain Serialization and Description
//Get all items in the cart so far...
$items = $this->get_cart();
//We need to loop through all items in the cart.
//If the item is already there, get it's key($updatekey).
//We also need to get the next key that we are going to use in case we need to add the
//item to the cart. Since items can be deleted, we can't use a count. we use the highest key + 1.
$maxkey=0; //Highest key so far
$itemalreadyinsale=FALSE; //We did not find the item yet.
$insertkey=0; //Key to use for new entry.
$updatekey=0; //Key to use to update(quantity)
foreach ($items as $item)
{
//We primed the loop so maxkey is 0 the first time.
//Also, we have stored the key in the element itself so we can compare.
if($maxkey <= $item['line'])
{
$maxkey = $item['line'];
}
if($item['item_id']==$item_id)
{
$itemalreadyinsale=TRUE;
$updatekey=$item['line'];
}
}
$insertkey=$maxkey+1;
//array/cart records are identified by $insertkey and item_id is just another field.
$item = array(($insertkey)=>
array(
'item_id'=>$item_id,
'line'=>$insertkey,
'name'=>$this->CI->Item->get_info($item_id)->name,
'item_number'=>$this->CI->Item->get_info($item_id)->item_number,
'description'=>$description!=null ? $description: $this->CI->Item->get_info($item_id)->description,
'serialnumber'=>$serialnumber!=null ? $serialnumber: '',
'allow_alt_description'=>$this->CI->Item->get_info($item_id)->allow_alt_description,
'is_serialized'=>$this->CI->Item->get_info($item_id)->is_serialized,
'quantity'=>$quantity,
'discount'=>$discount,
'price'=>$price!=null ? $price: $this->CI->Item->get_info($item_id)->unit_price
)
);
//Item already exists and is not serialized, add to quantity
if($itemalreadyinsale && ($this->CI->Item->get_info($item_id)->is_serialized ==0) )
{
$items[$updatekey]['quantity']+=$quantity;
}
else
{
//add to existing array
$items+=$item;
}
$this->set_cart($items);
return true;
}
function out_of_stock($item_id)
{
//make sure item exists
if(!$this->CI->Item->exists($item_id))
{
//try to get item id given an item_number
$item_id = $this->CI->Item->get_item_id($item_id);
if(!$item_id)
return false;
}
$item = $this->CI->Item->get_info($item_id);
$quanity_added = $this->get_quantity_already_added($item_id);
if ($item->quantity - $quanity_added < 0)
{
return true;
}
return false;
}
Thank you in advance!
Jeff
You only show the code of the frontend (the "template"), and unfortunately this code is obviously missing some parts at the end - at least the table row end should be there.
However I assumed that the missing </tr> element would come just after the code snippet.
What I did here is simply placing the <tr> containing the product in a condition so that it will only be shown if the in-stock quantity is > 0.
<div id="register_wrapper">
<?php echo form_open("sales/change_mode",array('id'=>'mode_form')); ?>
<span><?php echo $this->lang->line('sales_mode') ?></span>
<?php echo form_dropdown('mode',$modes,$mode,'onchange="$(\'#mode_form\').submit();"'); ?>
<div id="show_suspended_sales_button">
<?php echo anchor("sales/suspended/width:425",
"<div class='small_button'><span style='font-size:73%;'>".$this->lang->line('sales_suspended_sales')."</span></div>",
array('class'=>'thickbox none','title'=>$this->lang->line('sales_suspended_sales')));
?>
</div>
</form>
<?php echo form_open("sales/add",array('id'=>'add_item_form')); ?>
<label id="item_label" for="item">
<?php
if($mode=='sale')
{
echo $this->lang->line('sales_find_or_scan_item');
}
else
{
echo $this->lang->line('sales_find_or_scan_item_or_receipt');
}
?>
</label>
<?php echo form_input(array('name'=>'item','id'=>'item','size'=>'40'));?>
<div id="new_item_button_register" >
<?php echo anchor("items/view/-1/width:360",
"<div class='small_button'><span>".$this->lang->line('sales_new_item') </span></div>",
array('class'=>'thickbox none','title'=>$this->lang- >line('sales_new_item')));
?>
</div>
</form>
<table id="register">
<thead>
<tr>
<th style="width:11%;"><?php echo $this->lang->line('common_delete'); ?></th>
<th style="width:30%;"><?php echo $this->lang->line('sales_item_number'); ?></th>
<th style="width:30%;"><?php echo $this->lang->line('sales_item_name'); ?></th>
<th style="width:11%;"><?php echo $this->lang->line('sales_price'); ?></th>
<th style="width:11%;"><?php echo $this->lang->line('sales_quantity'); ?></th>
<th style="width:11%;"><?php echo $this->lang->line('sales_discount'); ?></th>
<th style="width:15%;"><?php echo $this->lang->line('sales_total'); ?></th>
<th style="width:11%;"><?php echo $this->lang->line('sales_edit'); ?></th>
</tr>
</thead>
<tbody id="cart_contents">
<?php
if(count($cart)==0)
{
?>
<tr><td colspan='8'>
<div class='warning_message' style='padding:7px;'><?php echo $this->lang- >line('sales_no_items_in_cart'); ?></div>
</tr></tr>
<?php
}
else
{
foreach(array_reverse($cart, true) as $line=>$item)
{
$cur_item_info = $this->Item->get_info($item['item_id']);
// THIS IS THE CONDITION WHERE I CHECK THE AVAILABILITY STATE
if ($cur_item_info->quantity > 0) {
echo form_open("sales/edit_item/$line");
?>
<tr>
<td><?php echo anchor("sales/delete_item/$line",'['.$this->lang->line('common_delete').']');?></td>
<td><?php echo $item['item_number']; ?></td>
<td style="align:center;"><?php echo $item['name']; ?><br /> [<?php echo $cur_item_info->quantity; ?> in stock]</td>
<?php if ($items_module_allowed)
{
?>
<td><?php echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6'));?></td>
<?php
}
else
{
?>
<td><?php echo $item['price']; ?></td>
<?php echo form_hidden('price',$item['price']); ?>
<?php
}
?>
<td>
<?php
if($item['is_serialized']==1)
{
echo $item['quantity'];
echo form_hidden('quantity',$item['quantity']);
}
else
{
echo form_input(array('name'=>'quantity','value'=>$item['quantity'],'size'=>'2'));
}
?>
</td>
</tr>
<?php
// I assumed that there must be a table row closer (above)
} // end if quantity > 0
?>
HTH
Regarding the new code - you have a function named "out_of_stock", I'd try to put a call to this function just at the beginning of add_item. Please note that this is just a guess as I don't know the intrinsics of your shop system:
function add_item($item_id,$quantity=1,$discount=0,$price=null,$description=null,$serialnumber=null)
{
//make sure item exists
if(!$this->CI->Item->exists($item_id))
{
//try to get item id given an item_number
$item_id = $this->CI->Item->get_item_id($item_id);
if(!$item_id)
return false;
}
// add the out-of-stock check here
if (out_ot_stock($item_id)) {
return false;
}
// continue with the rest of code
I need to display specific value of key 'pasdiz_alus' from array $form->data into a table cell. And I need to display this table row only if value of key 'pasdiz_alus' is greater than '0'.
The code for this is below, but the problem is that output displays also the value of key 'pasdiz_alus' above my table row and there it is displayed as many times as number of keys of array.
How can I get rid of this display of value of " 'pasdiz_alus' x times of number of keys in array (in my case 29 times - there are 29 keys in the array)"? In this case it is: 5454545454545454......
My code is:
<table style="width: 800px;">
<tbody>
<?php
if ($form->data['pasdiz_alus'] > 0){
echo '<tr><td style="width: 100px;">Bilde šeit</td><td style="width: 500px;"> <strong>Pašdizainēts alus</strong></td>';
foreach($form->data as $key => $value) {
if($key === 'pasdiz_alus')
echo '<td style="width: 100px;">';
echo $form->data['pasdiz_alus'];
echo '</td>';
}
echo '<td style="width: 100px;">Cena šeit</td></tr>';
}
?>
</tbody>
</table>
And this is the output display, in this case the value of 'pasdiz_alus' is 54
The first row is the "wrong" one that I need to get rid off, and the second row is the "right" one.
5454545454545454545454545454545454545454545454545454545454
Bilde šeit Pašdizainēts alus 54 Cena šeit
Thanks for helping!
Brgds, Raivis
The problem in your script is here:
if($key === 'pasdiz_alus') // <-- missing opening "{"
echo '<td style="width: 100px;">'; // <-- inside the "if"
echo $form->data['pasdiz_alus']; // <-- OUTSIDE the "if"
echo '</td>'; // <-- OUTSIDE the "if"
} // <-- this matches the foreach "{"
Why do you cycle all the array keys, instead of directly accessing it?
<table style="width: 800px;">
<tbody>
<?php if ($form->data['pasdiz_alus'] > 0) { ?>
<tr>
<td style="width: 100px;">Bilde šeit</td>
<td style="width: 500px;"><strong>Pašdizainēts alus</strong></td>
<td style="width: 100px;"><?=$form->data['pasdiz_alus']?></td>
<td style="width: 100px;">Cena šeit</td>
</tr>
<?php } ?>
</tbody>
</table>
This should solve your problem, but I'd recommend also to move the if part before even opening the table:
<?php if ($form->data['pasdiz_alus'] > 0) { ?>
<table style="width: 800px;">
<tbody>
<tr>
<td style="width: 100px;">Bilde šeit</td>
<td style="width: 500px;"><strong>Pašdizainēts alus</strong></td>
<td style="width: 100px;"><?=$form->data['pasdiz_alus']?></td>
<td style="width: 100px;">Cena šeit</td>
</tr>
</tbody>
</table>
<?php } ?>
foreach($array as $k => $v)
{
if($k == 'pasdiz_alus' && $v > 0)
{
echo $v;
}
}
you need that addition in your if to make sure its only printed if it is the right key and its greater 0
I'm trying to use if-statement to change <td>-s color.
Basically, I have a simple query to retrieve information from the database.
Additionaly, I have a column there which keeps the information like if the task is accomplished or not. When I retrieve the information I get all of them, but I need the accomplished tasks to be green, and others without any color.
I've searhed for the answer, but I couldn't find anything that satisfies me.
For example:
$qry = mysql_query("select * from table");
$recs = array();
while($row = mysql_fetch_array($qry))
$recs[]=$row;
mysql_free_result($qry);
I've tried to add while statement to the code above, but I was confused and it didnt work :(
I'm printing the results using heredoc:
How to give them color here?
<?php
$last_id=0;
foreach($recs as $rec)
{
$html=<<<HTML
<tr>
<td><b>Номер</b></td>
<td>$rec[0]</td>
</tr>
<tr>
<td><b>Номер документа</b></td>
<td>$rec[1]</td>
</tr>
<tr>
<td><b>Дата регистрации</b></td>
<td>$rec[8]</td>
</tr>
<tr>
<td><b>От кого</b></td>
<td>$rec[2]</td>
</tr>
<tr>
<td><b>По</b></td>
<td>$rec[4]</td>
</tr>
<tr>
<td><b>Краткое содержание</b></td>
<td>$rec[3]</td>
</tr>
<tr>
<td><b>Исполнитель</b></td>
<td>$rec[5]</td>
</tr>
<tr>
<td><b>Срок исполнения</b></td>
<td>$rec[6]</td>
</tr>
<tr>
<td><b>Срок исполнения продлен до</b></td>
<td><b>$rec[10]</b></td>
</tr>
<tr>
<td><b>Прислан</b></td>
<td>$rec[9]</td>
</tr>
<tr>
<td><b>Примечание</b></td>
<td>$rec[7]</td>
</tr>
<tr>
<td bgcolor="#838B83"> </td>
<td bgcolor="#838B83"> </td>
</tr>
HTML;
print $html;
if($rec[0]>$last_id)
$last_id=$rec[0];
};
$new_id=$last_id+1;
?>
rather than colour use a class, so you can change it in CSS
<td<?php if($row['complete']) echo ' class="complete"'; ?>>data</td>
<table>
<tr>
<td>column heading</td>
</tr>
<?php
$qry=mysql_query("select * from table");
while($row=mysql_fetch_array($qry)) {
if($row['urcolumnn']==1)
{
echo "<tr bgcolor=green>";
}
else
{
echo "<tr>";
}
?>
<td>
<?php echo $row['urcolumn']; ?>
</td>
</tr>
<?php } ?>
</table>
This is an example code. think this will help you. here i give the background color to <tr> like this if u want to give color to <td> use this <td style="background-color:green;">
I would suggest you to use ternary operator, rather than using IF statement, or your could use another workaround to use arrays to define colors, this would help you to define various colors for each status value globally, please find an example below:
$aryColor = array(
'complete' => 'green',
'incomplete' => "red",
'pending' => 'orange'
.....
);
//you can specify values for all of your status, or leave them blank for no color, the keys in the array are the possible values from your status field
foreach($recs as $rec) {
echo '<tr bgcolor="'.$aryColor[$rec['status_field']].'">
<td>'.$rec['title_field'].'</td>
</tr>';
}
I hope this helps you out, and you can easily edit this for HEREDOC.
first you should change your column values change its structure to int and set default value as "0".so when your task is complete the field value should be change to "1".
now come to your code:
$qry = mysql_query("select * from table");
while($row = mysql_fetch_assoc($qry)){
if($row['status']==1){
echo "<td color="green">Data</td>"
}
else{
echo "<td color="white">Data</td>";
}
}
Hence you can get the rows in green which status is==1 means complete and white for incomplete.
I use mysql_fetch_array to fetch data from a mysql results set:
while($row = mysql_fetch_array($qry_result)){
Next I would like to place the data into columns in a table, but maximum of two columns at each row.
So using a table:
<table>
<tr>
<td>Record Here</td>
<td>Record Here</td>
</tr>
<tr>
<td>Record Here</td>
<td>Record Here</td>
</tr>
<tr>
<td colspan="2">Record Here</td>
</tr>
As you see above, I want to loop the results and create table columns in the loop.
This so that the records line up two and two on a results page.
Remember, if there is an odd number of records, then the last table column would need a colspan of 2, or perhaps just use an empty column?
Anybody know how to do this?
If I use a for loop inside the while loop, I would just be lining up the same records x times for each while loop. Confusing...
Any ideas?
Thanks
Implement a (1-indexed) counter within the while loop. Then add (after the loop):
if ($counter%2)
echo '<td></td>';
This will leave an additional blank cell in your table if the last column contains only one row.
Something like this should work...
<table>
<?php
while(true) {
$row1 = mysql_fetch_array($qry_result);
if($row1 === false) break;
$row2 = mysql_fetch_array($qry_result);
if($row2 !== false) {
echo "<tr><td>$row1</td><td>$row2</td></tr>";
} else {
echo "<tr><td coslspan=\"2\">$row1</td></tr>";
break; // output the final row and then stop looping
}
}
?>
</table>
<table>
<tr>
<?
$i=1;
$num = mysql_num_rows($qry_result);
while($row = mysql_fetch_array($qry_result)){
if($i%2==1)
?>
<tr>
<?
?>
<td <? if($i==$num && $i%2==1)echo 'colspan="2"';?>><?=$row['keyName']?></td>
<?
if($i%2==0)
{
<?
</tr>
?>
}
$i++;
}
?>