View Full Version : No data - no display table cell
sticks
12-08-2002, 10:06 PM
Hi. I have a viewproduct page that displays lots of info about travel products. Some of these fields are displayed in a table. If there has been no data entered for one field then obviously no data will show. However, I was hoping to not have the table cell/row show either if there is no data in the corresponding field. Is this possible or am I dreaming ??
I'm new to this so excuse my ignorance.
Thanks, Sheryll.
mordred
12-08-2002, 10:32 PM
Yes, that's possible.
mordred
12-08-2002, 10:36 PM
If you found my answer ignorant, I'll have to explain: The functionality you want to implement is doable, but we have really no information what your code looks like, and believe me, there are a *lot* of viewproduct.php pages out there.
So if you can provide us with the code you're struggling with, we can give better and more precise help. ;)
sticks
12-08-2002, 11:58 PM
Hi mordred and thanks for your reply. I have posted some of the code below as requested. Basically, if there is only one roomtype and/or say only one adult price for each roomtype etc then we don't want that second one to show. Not good at explaining but if there's no data for a whole table row then I would like it if the table row didn't show at all.
<table border=1 cellpadding=0 cellspacing=0 align="center" bgcolor="#336699" bordercolor="#FFDE00">
<tr bgcolor="#FFDE00" bordercolor="#FFDE00">
<td class="text2" width="232" align="center"><font color="#336699">Date</font></td>
<td class="text" width="131" align="center"><font color="#336699"><b>Adult
Price </b></font></td>
<td class="text" width="119" align="center"><font color="#336699"><b>Child
Price </b></font></td>
</tr>
<tr bgcolor="#FFDE00" bordercolor="#FFDE00" align="center">
<td class="label" colspan="3" bgcolor="#FFFFFF">
<? echo($row['roomtypea']); ?>
</td>
</tr>
<tr>
<td valign="top" class="text2">
<? echo($row['date1']); ?>
<br />
<? echo($row['date2']); ?>
<br>
<? echo($row['date3']); ?>
<br>
<? echo($row['date4']); ?>
<br>
</td>
<td valign="top" class="text">
<? echo($row['adult1']); ?>
<br>
<br>
<? echo($row['adult2']); ?>
<br>
</td>
<td class="text" valign="top">
<? echo($row['child1']); ?>
<br>
<br>
<? echo($row['child2']); ?>
</td>
</tr>
<tr>
<td class="text2"> Extra Night</td>
<td class="text">
<? echo($row['extadult']); ?>
</td>
<td class="text">
<? echo($row['extchild']); ?>
</td>
</tr>
<tr bgcolor="#FFDE00" align="center">
<td class="label" colspan="3" bgcolor="#FFFFFF">
<? echo($row['roomtypeb']); ?>
</td>
</tr>
<tr>
<td class="text2" valign="top">
<? echo($row['date5']); ?>
<br />
<? echo($row['date6']); ?>
<br>
<? echo($row['date7']); ?>
<br>
<? echo($row['date8']); ?>
<br>
</td>
<td class="text" valign="top">
<? echo($row['adult3']); ?>
<br>
<br>
<? echo($row['adult4']); ?>
<br>
</td>
<td class="text" valign="top">
<? echo($row['child3']); ?>
<br>
<br>
<? echo($row['child4']); ?>
</td>
</tr>
<tr>
<td class="text2">Extra Night<br>
</td>
<td class="text">
<? echo($row['extadult2']); ?>
</td>
<td class="text">
<? echo($row['extchild2']); ?>
</td>
</tr>
<tr>
<td class="text" colspan="3"> <b>Includes:</b>
<? echo(nl2br($row['includes'])); ?>
</td>
</tr>
</table>
Thank you for replying, Sheryll.
mordred
12-09-2002, 12:41 AM
Originally posted by sticks
Basically, if there is only one roomtype and/or say only one adult price for each roomtype etc then we don't want that second one to show.
Let's try to transform this english sentence into PHP. It's nearly perfectly suited to employ the "if" control structure to, well, structure the program flow.
if ( isset($row['roomtypea']) && !empty($row['roomtypea']) ) {
echo '<tr bgcolor="#FFDE00" bordercolor="#FFDE00" align="center">';
echo '<td class="label" colspan="3" bgcolor="#FFFFFF">';
echo $row['roomtypea'];
echo '</td>';
echo '</tr>';
}
isset() is a function that returns true if the variable tested does exist, and empty() returns true if the value (e.g. the content) of the variable is empty. So in this case, I'm only echoing the whole table row if the row variable does exist and if it is not empty (the not comes from the !, which inverts the boolean value - true becomes false and vice versa).
I think if you try this out it should be quite self-explaining. Of course, you have to adjust the above code to suit your needs specifically, because I did not understand which <tr> rows you wanted to show and which and based on what circumstances.
sticks
12-14-2002, 12:05 AM
Hi mordred - you are wonderful :))
Sorry for taking so long to reply but have two kids with broken legs (and no I don't beat them up :) )
Your advice was perfect and I'm extremely happy so thank you very much.
Thanks, Sheryll.
whammy
12-14-2002, 01:18 AM
I'm no PHP guru, but if the second condition (the variable had a value) wouldn't that make the first check (isset) unnecessary? I'm trying to learn this by bits and pieces in between learning C# etc. ;)
mordred
12-14-2002, 01:47 AM
Good point whammy.
I was under the impression that empty() would also trigger a warning message if it were applied to a non-existent variable, because that's the case if your error reporting level is set to high level. *
After reading through the manual section for empty(), it seems that this function leads to some confusion because which values are seen as empty and which not isn't that intuitive. So I propose I minor rewrite of my suggested code above to
if ( isset($row['roomtypea']) && strlen($row['roomtypea']) > 0) {
Now you need the isset() function to make sure you work with a defined variable.
* PHP knows different levels of error reporting. These levels define which kind of errors are reported with a short error message. Setting this level to the constant E_ALL, you also see "notices" (just another form of error messages). Those "notices" don't cause your script to terminate, but they inform you of minor glitches and ambiguieties (spelling?) in your code. They are very helpful for debugging purposes. And since you can't always control what level of error reporting is set on a different host, it's good to prepare for the case that it's very high and the user doesn't get upset by so many strange "notice" messages.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.