Forum Moderators: coopster

Message Too Old, No Replies

can I hide contents of a div if another div is not being populated?

         

hoom

4:28 pm on Mar 8, 2010 (gmt 0)

10+ Year Member



Hi - I have a table where these are the first two rows:

<tr>
<td><span class="style2">Artist</span></td>
</tr>
<tr>
<td><span class="style8">
<?php if ($totalRows_rsRecRes > 0) { // Show if recordset not empty ?>
<?php echo $row_rsRecordings['recordingartistName']; ?>
<?php } // Show if recordset not empty ?></span></td>
</tr>

basically what I want to be able to do is to hide the header (Artist) if the database field that should populate the second row is empty. Is this possible, does anyone know?

Matthew1980

4:34 pm on Mar 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Hoom,

WRT the results, are you asking what is the best way to see if the results set is empty and if so display a message to say something like "Sorry no results to show" OR are you just wanting to show hide different headings depending on results that you get back?

if(mysql_num_rows($result) < 0)
{
echo "no results to show";
exit;
}
else{
//build the table
}

Just as en example of the syntax that could be used...

Cheers,

MRb

EDIT: corrected typo!

[edited by: Matthew1980 at 4:39 pm (utc) on Mar 8, 2010]

eatspinach

4:35 pm on Mar 8, 2010 (gmt 0)

10+ Year Member






try this?

<tr <?php if(if ($totalRows_rsRecRes > 0)){print "style='display:none;'"}> ?>
<td><span class="style2">Artist</span></td>
</tr>
<tr>
<td><span class="style8">
<?php if ($totalRows_rsRecRes > 0) { // Show if recordset not empty ?>
<?php echo $row_rsRecordings['recordingartistName']; ?>
<?php } // Show if recordset not empty ?></span></td>
</tr>

hoom

5:24 pm on Mar 8, 2010 (gmt 0)

10+ Year Member



Hi - Matthew1980 - what I want is to be able to not have a page with orphaned headings showing if the results fields that they relate to are empty - using the code example above, this would be ok:

Artist:
Michael Jackson

but Artist:
(blank space) wouldn't.

eatspinach - I tried your code suggestion but it gave me an error:

Parse error: syntax error, unexpected T_IF in......

thanks anyway

Matthew1980

8:37 pm on Mar 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Hoom,

Is there likely to be more than 1 row returned at any time, because if there is going to be more than 1, you will need to loop through the data returned using a while loop (always recommended when iterating through a database/sql_query array).

If there is only ever going to be one row returned, the best way to 'hide' a header would be with a ternary:-


<tr>
<td><?php echo(isset($totalRows_rsRecRes) && ($totalRows_rsRecRes < 0) ? 'nothing in the db': '<span class="style2">Artist</span>'); ?></td>
</tr>


Not the best way to do it, but it will do as you ask of it.

Also you could do the same with:-


<tr>
<td><span class="style8">
<?php echo (isset($totalRows_rsRecRes) && ($totalRows_rsRecRes < 0) ? 'No data in the database' : $row_rsRecordings['recordingartistName'] ); ?>
</span></td>
</tr>


Basically what's being said is: if the $var is set and it has nothing in it '?' evaluate the statement then if its true 'No data to display' ':' other wise, echo the $rowVar, as the entire statement is prefixed with the echo.

Worst case scenario is that you will have a table with two rows (header & content) that say 'No data in the db' or two rows populated with what you want, but a table there either way...

Hope that makes things a little easier to follow & hopefully I have understood what you have asked about ;-p

Good luck with the rest of the project.

Cheers,

MRb

eatspinach

9:12 am on Mar 9, 2010 (gmt 0)

10+ Year Member



sorry i should of checked it before i posted it. what i meant was

<tr <?php if($totalRows_rsRecRes > 0){ print "style='display:none;'";} ?>
<td><span class="style2">Artist</span></td>
</tr>
<tr>
<td><span class="style8">
<?php if ($totalRows_rsRecRes > 0) { // Show if recordset not empty ?>
<?php echo $row_rsRecordings['recordingartistName']; ?>
<?php } // Show if recordset not empty ?></span></td>
</tr>


but it looks like you got it all figured out anyways so good luck.