I am trying to echo values from mysql database into a table but some values may be empty and I don't want the cell to output empty. Is there a way for me to show a message e.g "N/A" if a particular value is empty in the database?
<table><tr><td>Friends Name:</td><td><b>".$row['first_name']." ".$row['last_name']."</b></td></tr></table>
Something like
<table>
<tr>
<td>Friends Name:</td>
<?php if($row['first_name']): ?>
<td><b>".$row['first_name']." ".$row['last_name']."</b></td>
<?php else: ?>
<td><b>N/A</b></td>
<?php endif; ?>
</tr>
</table>
Related
noob problem: i have some issues with a loop in php...here is the code (i used the same methodology for other pages and it works); the code it is supposed to display the names of the products from a order, it works, but it is not showing the very first product , i don't know why :
<?php $i=1; while($row_selectOrderItems = mysqli_fetch_array($result_selectOrderItems)){ ?>
<tr>
<td> <?php echo $i; ?> </td>
<td> <?php echo $row_selectOrderItems['pro_name']; ?> </td>
<td> <?php echo $row_selectOrderItems['pro_price']; ?> </td>
<td> <?php echo $row_selectOrderItems['q']; ?> </td>
<td> <?php echo $row_selectOrderItems['q']*$row_selectOrderItems['pro_price']; ?> </td>
</tr>
<?php $i++; } ?>
and here is the code where i used mysqli_fetch_array before the loop
$query_selectOrderItems = "SELECT *,order_items.quantity AS q FROM orders,order_items,products WHERE order_items.order_id='$order_id' AND order_items.pro_id=products.pro_id AND order_items.order_id=orders.order_id";
$result_selectOrderItems = mysqli_query($con,$query_selectOrderItems);
$row_selectOrderItems=mysqli_fetch_array($result_selectOrderItems);
Does anyone have any idea how should i modify this code? Thank you!
You're reading and ignoring the first record in the results. Consider how your loop works:
while($row_selectOrderItems = mysqli_fetch_array($result_selectOrderItems))
Each iteration calls mysqli_fetch_array, stores the record in $row_selectOrderItems, then uses that to display the record. Then consider what you do before the loop:
$row_selectOrderItems = mysqli_fetch_array($result_selectOrderItems);
You're doing exactly that same thing, but not displaying that first record.
Simply remove that first call to mysqli_fetch_array before the loop.
$row_selectOrderItems=mysqli_fetch_array($result_selectOrderItems);
remove this line, so that, it will not read the 1st result at starting.
Now, when you use it in the while loop, it reads the first line
may be you used mysqli_fetch_array($result_selectOrderItems) before this for loop.
check once
Thank you for any assistance that you may offer with this...
I'm creating a page with a bunch of weather reports from various weather instruments. The issue that I'm having is that sometimes these instruments do not send the certain pieces of data, for various reasons. I'm using a foreach loop to display all of the weather conditions...but if a station didn't submit, for example a wind gust value, then I get a warning within the list. Obviously, I would rather have the list just say "no data" or something...rather than the big red warning box.
Here is the code that I've been working on...
<table>
<tbody>
<tr>
<th>Site</th>
<th>Temperature°F</th>
<th>Dew Point°F</th>
<th>Humidity%</th>
<th>Wind Direction</th>
<th>Wind Speed MPH</th>
<th>Wind Gust MPH</th>
<th>Pressure (mb)</th>
</tr>
<?php foreach($data->STATION as $site): ?>
<tr>
<td> <?php echo($site->NAME); ?> </td>
<td> <?php echo($site->OBSERVATIONS->air_temp_value_1)->value; ?> </td>
<td> <?php echo($site->OBSERVATIONS->dew_point_temperature_value_1d)->value; ?> </td>
<td> <?php echo($site->OBSERVATIONS->relative_humidity_value_1)->value; ?> </td>
<td> <?php echo($site->OBSERVATIONS->wind_cardinal_direction_value_1d)->value; ?> </td>
<td> <?php echo($site->OBSERVATIONS->wind_speed_value_1)->value; ?> </td>
<td> <?php echo($site->OBSERVATIONS->wind_gust_value_1)->value; ?> </td>
<td> <?php echo($site->OBSERVATIONS->sea_level_pressure_value_1d)->value; ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
These all come from JSON data from an API. The issue is that if a new observation from weather instrument does not send a wind gust value...then there is nothing in the JSON data for that variable. But, my code is still searching for it in the JSON data. Therefore, I get the warning:
Notice: Undefined property: stdClass::$wind_gust_value_1 in C:\wamp64\www\mesowesttable.php on line 48
Is there some way to have the table populated with "No data" rather than getting the warning? Some type of "if/then" statement?
Thank you all,
Justin
you can check if is value set for that variable:
$wind_gust_value = isset($site->OBSERVATIONS->wind_gust_value_1->value) ? $site->OBSERVATIONS->wind_gust_value_1->value : 'empty';
echo $wind_gust_value;
or first initialize with some data
You could use PHPs null coalescing operator (??) to check if the value is set or not.
<?php echo $site->OBSERVATIONS->wind_gust_value_1->value ?? 'no data'; ?>
I have an array of options that is used in a form's drop down select input.
DATA.php file that holds the array info...
'options' => array("Car", "SUV", "Pickup", "Van", "Bus", "Motorcycle");
Edit page that displays the form and calls the options for the drop down input..
echo '<option '.$selected.' value="'.$optionValue.'">'.$optionLabel.'</option>';
when the form is saved the array option position value is saved to my database. I have a table on another page that echos some of the information the user submitted. But because the information is being pulled from the database as is, the information shows up as either, 0, 1, 2, 3, etc. which is the arrays position value (example: 0=car, 1=Suv, 2=pickup, etc.)
this displays the table...
<tr>
<td><span class="nobr"><?php echo $_sublogin->getData('vehicle_type') ?>
</span></td>
</tr>
How do i display the information back, so the table knows if the value is 1 echo SUV, etc.
I'm pretty new at php and i feel this is something i should know how to do but i dont. please help!
thank you.
That is probably because $optionValue contains 0,1,2... Page submits value part of the dropdown list (value="'.$optionValue.'"). I think $optionLabel has the values car, SUV... etc. So what you can do is replace $optionValue with $optionLabel.
echo '<option '.$selected.' value="'.$optionLabel.'">'.$optionLabel.'</option>';
That will save the label value to the database.
if i understood your problem then you can use if/else condition for showing name against their value. like this-
<?php
if($_sublogin->getData('vehicle_type') == 1):
?>
<tr>
<td><span class="nobr"> SUB </span></td>
</tr>
<?php
elseif($_sublogin->getData('vehicle_type') == 2):
?>
<tr>
<td><span class="nobr"> CAR </span></td>
</tr>
.
.
.
.
<?php endif;?>
are you looking for this:
$arr['options']=array("Car", "SUV", "Pickup", "Van", "Bus", "Motorcycle");
echo $arr['options'][1];
//output: SUV
I am trying to display a list of all entries in the table people. I have the list of entries generating correctly as a html table but I want to ad a row at the top of the table with the table field names. I could do this manually but figure there must be a way to do it dynamically in case I add or remove fields later on.
here is the controller function
public function peopleDisplay() {
$this->set('people', $this->people->find('all'));
}
Are the field names already in the array that generates? If so how do I reference them? If not how do I get them to be?
Here is the view so far
<table>
<tr>
***were I want the row of field names to go***
</tr>
<?php foreach($people as $people): ?>
<tr>
<td>
<?php echo $people['people']['id'] ?>
</td>
<td>
<?php echo $people['people']['firstName'] ?>
</td>
<td>
<?php echo $people['people']['secondName'] ?>
</td>
</tr>
<?php endforeach; ?>
</table>
The column names are the keys in your array. For instance, 'id', 'firstName', and 'secondName' are the column names.
Another way to approach would be to get the column names as a separate array in your controller and then output them in your view.
$people = $this->People->find('all');
$colNames = array_keys($this->People->getColumnTypes());
$this->set(compact('colNames', 'people'));
Then in your view:
foreach($colNames as $col){ //Output column names }
foreach($people as $person){ //Output people }
I am using this fantastic example of a jQuery editable invoice as a template for creating dynamic invoices for my users.
It's working quite well and I am successfully generating the items but I now need to save the values entered into the various text fields and enter them into the MySQL database.
I am confident in doing the MySQL entering with PHP but what makes this trickier is that the amount of 'invoice items' is completely dynamic and I am unsure how I can get PHP to 'check' through the pages text fields and find new ones, group them and then add them to my DB.
Here is an example of my code that I am using to generate the items:
<?php if($invoice_items->result_array()) { ?>
<?php foreach($invoice_items->result_array() as $invoice_Row): ?>
<tr class="item-row">
<td class="item-name">
<div class="delete-wpr">
<textarea><?php echo $invoice_Row['item_name']; ?> Facility Booking</textarea>
<a class="delete" href="javascript:;" title="Remove row">X</a>
</div>
</td>
<td class="description">
<textarea><?php echo $invoice_Row['description']; ?></textarea>
</td>
<td><textarea class="cost">$<?php echo $invoice_Row['hourly_cost']; ?>.00</textarea></td>
<td><textarea class="qty"><?php echo $total_time_hours; ?></textarea></td>
<td><span class="price">$<?php $unit_total = $invoice_Row['hourly_cost']* $total_time_hours; echo $unit_total;?>.00</span></td>
</tr>
<?php endforeach; ?>
<?php } ?>
I am thinking that I need to perhaps generate unique ID's for each invoice items text field, ie item-1-desc, item-1-cost etc, but that involves writing javascript which I know almost nothing about. Also I would still have to get PHP to loop through the ID's somehow until it reached the end...
If anyone has attempted something similar before or you can see a solution to my problem I would greatly appreciate your help.
Thanks,
Tim
Use the php form array syntax name="item-desc[<?php echo $id?>]"
You can then iterate them on the backend with foreach to store the data. You have the id's as keys to the arrays so it should be fairly trivial to update the db.