Hide table row if data doesn't exist - php

I'll like to know if there is a code I could use to prevent table rows from forming if there is no data in the custom fields.
For example this is my table:
<tr>
<td><?php echo $place_1; ?></td>
<td><?php echo $person_1; ?></td>
<td><?php echo $status_1; ?></td>
<td></td>
</tr>
<tr>
<td><?php echo $place_2; ?></td>
<td><?php echo $person_2; ?></td>
<td><?php echo $status_2; ?></td>
<td></td>
</tr>
<tr>
<td><?php echo $place_3; ?></td>
<td><?php echo $person_3; ?></td>
<td><?php echo $status_3; ?></td>
<td></td>
</tr>
How can I make it that "if $place_2 is empty, hide all the table rows of 2 and 3?"
Any help is appreciated!

You could simply use PHP to only output the row when it is not empty:
...
</tr>
<?php if ($place_2 != "") { ?>
<tr>
<td><?php echo $place_2; ?></td>
<td><?php echo $person_2; ?></td>
<td><?php echo $status_2; ?></td>
<td></td>
</tr>
<?php } ?>
<tr>
...
This approach wraps the tr element in an if block. There are other many ways to achieve the same result, which may show up in other answers. Based on your question, it looks like you want to suppress $place_3 as well when $place_2 is empty. I assume $place_3 would be empty in this case, so you could apply the same approach to that tr element, substituting $place_2 with $place_3.

You can use a WHILE loop also . This will work for any number of rows. Simply replace 4 with number of rows to be checked in While condition
<?php
$i=0;
while($i<4)
{
if($place_.$i == "")
break; // if $place_ variable is empty no further rows are printed.
else
{
?>
<tr>
<td><?php echo $place_.$i; ?></td>
<td><?php echo $person_.$i; ?></td>
<td><?php echo $status_.$i; ?></td>
<td></td>
</tr>
<?php
} // closing bracket of else
$i = $i + 1;
} //end of while loop
?>

Related

Parsing XML from a URL address and converting into a table using PHP

I'm trying to take the following xml from this url http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml and make a table with the data. I've verified I can pull the data by using print_r($xml); but I can't get the data to dump into the table. This is what I've got so far, which is probably wrong. Can anyone help me out with the proper code to use?
<?php
$url = "http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml";
$xml = simplexml_load_file($url);
?>
<table>
<thead>
<tr>
<col><span style="font-weight:bold">Day</span></col>
<col><span style="font-weight:bold">Time(Eastern)</span></col>
<col><span style="font-weight:bold">Reservoir Elev. (behind dam)*</span</col>
<col><span style="font-weight:bold">Tailwater Elev. (below dam)*</span></col>
<col><span style="font-weight:bold">Avg Hourly Discharge*</span></col>
</tr>
</thead>
<tbody>
<?php foreach ($xml->RESULTSET->ROW as $obs) :?>
<tr>
<td><?php echo $obs->obs_day; ?></td>
<td><?php echo $obs->obs_hr; ?></td>
<td><?php echo $obs->upstream_elev; ?></td>
<td><?php echo $obs->downstream_elev; ?></td>
<td><?php echo $obs->avg_hourly_discharge; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As the XML has 2 <RESULTSET> elements, the RESULTSET->ROW was just picking the first one. So change that to RESULTSET[1]->ROW and you will get the data your after.
You also need to ensure that you use the same case for each element name...
<?php foreach ($xml->RESULTSET[1]->ROW as $obs) :?>
<tr>
<td><?php echo $obs->OBS_DAY; ?></td>
<td><?php echo $obs->OBS_HR; ?></td>
<td><?php echo $obs->UPSTREAM_ELEV; ?></td>
<td><?php echo $obs->DOWNSTREAM_ELEV; ?></td>
<td><?php echo $obs->AVG_HOURLY_DISCHARGE; ?></td>
</tr>
<?php endforeach; ?>

php table not displaying contents and only creating table boxes

I would like the table to display all the values inside of it, but it is currently not displaying the values and only creating an empty table
[what the table is displayed as (image)][1]
$result = mysqli_query( $conn,'SELECT * FROM Pictures ');
$conn->close();
Html
<html>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;"
<thead>
<tr>
<th>id</th>
<th>Name</th>
<th>image</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
if( $result != null){
Result is not empty
while( $row1 = mysqli_fetch_assoc( $result ) ){
foreach ($row1 as $row){
?>
<tr>
<td><?php $row['id'] ?></td>
<td><?php $row['hname'] ?></td>
<td><?php $row['himage'] ?></td>
<td><?php $row['hdesc'] ?></td>
</tr>
<?php
}
}
}else{
echo "Something went wrong with the result";
}
?>
</tbody>
</table>
<?php //mysqli_close($conn); ?>
</body>
</html>
changed the out put to match the answer you gave but the output came out as picture 2 while my table is actually picture 3 any ideas
output:
table im trying to display
Display the data in this way:
<td><?php echo $row['id']; ?></td>
Try changing
<td><?php $row['id'] ?></td>
<td><?php $row['hname'] ?></td>
<td><?php $row['himage'] ?></td>
<td><?php $row['hdesc'] ?></td>
to
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['hname']; ?></td>
<td><?php echo $row['himage']; ?></td>
<td><?php echo $row['hdesc']; ?></td>
or the shorthand variant
<td><?= $row['id']; ?></td>
<td><?= $row['hname']; ?></td>
<td><?= $row['himage']; ?></td>
<td><?= $row['hdesc']; ?></td>
And as Samuel pointed out in a comment, are you sure there is a need for the extra foreach considering you're already looping with the while?
Update: OP have you tried the following?
while( $row = mysqli_fetch_assoc( $result ) ){
//removed foreach()
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['hname']; ?></td>
<td><?php echo $row['himage']; ?></td>
<td><?php echo $row['hdesc']; ?></td>
</tr>
<?php
}
}else{
Update 2 OP wishes to have the image load instead of showing the raw URL.
To do this, we need to use an actual image tag and insert the url into the src.
I assume that this line is the image URL
<td><?php echo $row['himage']; ?></td>
So change it to
<td> <img src="<?php echo $row['himage']; ?>" > </td>

Loop php values from an echo in HTML

I have a little issue when I try to to loop my php values in HTML. So far this is what I tried but I have not excpected result.
If I remove the loop I only get the first entry. I would like to echo all the possibles entries from my research.
This is my code ( from an SQL request).
<html>
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Number</th>
<th>Adress</th>
<th>link</th>
<!--<th class="glyphicon glyphicon-pencil"></th>-->
</tr>
<tr>
<?php $rowG = oci_fetch_array($stid, OCI_RETURN_NULLS);?>
<?php foreach($array as $rowG=>$value): ?> <tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
</tr>
</table>
</html>
Do you know where I made my mistake ?
Thank you for your help
Edit : Finally I managed to do it by using a do{}while loop.
Thank you all for your help
RFlow
It's hard to guess what you are trying to do or even what actually happens, since I don't know what is assigned to $rowG, so I tried to hack meaning out of this from the code's errors and came up with that :
<?php
while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {
?>
<tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]; ?></td>
<td><?php echo ' Link '; ?></td>
</tr>
<?php
}
?>
If it doesn't work with you, you'll have to provide the informations that should have been included in your question since the begining.
This is basic iteration over query results:
while ($rowG = oci_fetch_array($stid, OCI_RETURN_NULLS)) {?>
<tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
</tr>
<?php
}
<?php foreach($array as $rowG=>$value): ?> <tr>
<td><?php echo $rowG[2]; ?></td>
<td><?php echo $rowG[1]; ?></td>
<td><?php echo $rowG[0]; ?></td>
<td><?php echo $rowG[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$rowG[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
Would be :
<?php foreach($rowG as $arr): ?> <tr>
<td><?php echo $arr[2]; ?></td>
<td><?php echo $arr[1]; ?></td>
<td><?php echo $arr[0]; ?></td>
<td><?php echo $arr[3]?></td>
<td><?php echo "<a href='./consultation.php?Login=$arr[2]'> Link </a>" ; ?></td>
<?php endforeach;}} ?>
Note the use of $arr instead of $rowG.
In the original code $array is not used.

Change row color of a table based on mysqli value using materializecss

I'm using Materialize css on my project, i have a table that shows mysql field called "status" and in this table i want change the color of the row if i change the "status" like "1=blue, 2=red..." Someone here knows how i can make a function to do this? Thank you.
table extample:
table class="striped bordered responsive-table">
<thead>
<tr>
<th>ID</th>
<th>Cliente</th>
<th>Objeto</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php while($row_os = mysqli_fetch_assoc($result_user)){?>
<tr>
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
</tbody>
One simple way would be:
<?php
$colorMap = [
1 => 'blue',
2 => 'red',
// add more
];
while ($row_os = mysqli_fetch_assoc($result_user)) { ?>
<tr style="background:<?php echo $colorMap[$row_os['status']] ?>">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
Of course you could also add a class depending on status the same way and do the styling in CSS.
Simple as Doing this on the row , only if the status must contain a value of either 1 or 2
<?php
while($row_os = mysqli_fetch_assoc($result_user)){?>
<tr class="<?php echo $row_os["status"]==1?'blue':'red'?> lighten-2">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>
or if status varies (then follow #colburton answers ) or this
<?php
while($row_os = mysqli_fetch_assoc($result_user)){
$color="";
switch($row_os["status"]){
case 1:
$color="blue";
break;
case 2:
$color="red";
break;
//and so on
}
?>
<tr class="<?php echo $color;?> lighten-2">
<td><?php echo $row_os["num"]; ?></td>
<td><?php echo $row_os["cliente"]; ?></td>
<td><?php echo $row_os["object"]; ?></td>
<td><?php echo $row_os["status"]; ?></td>
</tr>
<?php } ?>

Create looping table with variable from result

I have pulled some information from a site API and it is stored in $result. I would like to create a table with this information that loops x number of times (constant). Here is how I currently have it repeating by hand and it works. I just imagine there must be a better/easier way to do this then manually repeating it each time.
<tr>
<td><?php echo strtoupper($result['weapons']['0']['stat']['id']); ?></td>
<td><?php echo round($result['weapons']['0']['extra']['accuracy'],2); ?>%</td>
<td><?php echo round($result['weapons']['0']['extra']['kpm'],2); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['shots']); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['hits']); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['kills']); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['hs']); ?></td>
<td><?php echo round($result['weapons']['0']['extra']['hkp'],2); ?>%</td>
</tr>
<tr>
<td><?php echo strtoupper($result['weapons']['1']['stat']['id']); ?></td>
<td><?php echo round($result['weapons']['1']['extra']['accuracy'],2); ?>%</td>
<td><?php echo round($result['weapons']['1']['extra']['kpm'],2); ?></td>
<td><?php echo number_format($result['weapons']['1']['stat']['shots']); ?></td>
<td><?php echo number_format($result['weapons']['1']['stat']['hits']); ?></td>
<td><?php echo number_format($result['weapons']['1']['stat']['kills']); ?></td>
<td><?php echo number_format($result['weapons']['1']['stat']['hs']); ?></td>
<td><?php echo round($result['weapons']['1']['extra']['hkp'],2); ?>%</td>
</tr>
I just can't wrap my head around how I would loop this? I have searched and looked at many articles, I just can't seem to get anything to work.
If you'd like to run through every result the easiest way would be to use foreach
e.g.
foreach($result['weapons'] as $tr){ ?>
<tr>
<td><?= strtoupper($tr['stat']['id']) ?></td>
<td><?= round($tr['extra']['accuracy'],2) ?>%</td>
//... etc.
</tr>
<?php } //...
If you'd prefer to define the amount of loops just use "for"
for ($i = 0; $i <= 10; $i++) { ?>
<tr>
<td><?= strtoupper($result['weapons'][$i]['stat']['id']) ?></td>
<td><?= round($result['weapons'][$i]['extra']['accuracy'],2) ?>%</td>
<td><?= round($result['weapons'][$i]['extra']['kpm'],2) ?></td>
//.... etc.
</tr>
<?php }
it's very easy to use foreach loop lets assume you got your value in variable $result_api then loop through this array
$i=0;
foreach($result_api as $result)
{ ?>
<tr>
<td><?php echo strtoupper($result['weapons'][$i]['stat']['id']); ?></td>
<td><?php echo round($result['weapons'][$i]['extra']['accuracy'],2); ?>%</td>
<td><?php echo round($result['weapons'][$i]['extra']['kpm'],2); ?></td>
<td><?php echo number_format($result['weapons'][$i]['stat']['shots']); ?></td>
<td><?php echo number_format($result['weapons'][$i]['stat']['hits']); ?></td>
<td><?php echo number_format($result['weapons']['0']['stat']['kills']); ?></td>
<td><?php echo number_format($result['weapons'][$i]['stat']['hs']); ?></td>
<td><?php echo round($result['weapons'][$i]['extra']['hkp'],2); ?>%</td>
</tr>
<?php
$i++;
}

Categories