Jump to content

[SOLVED] Do not display entire divs containing empty fields


outchy

Recommended Posts

I really need some help on this one.  Right now, I have my page pulling information from my mysql database and displaying the following fields (within divs) like this:

 

Year

Name

Address

Phone

Email

 

But say, for example, if any of those fields in the database are empty, I'd like for that entire div containing that empty field to not display on the page at all.  So if the person did not fill in the year field, it would output like this:

 

Name

Address

Phone

Email

 

How could I make that work?  Thanks so much for any help...

 

<?php

include('connection.php');

$result = mysql_query("SELECT * FROM `database` order by year desc");

// Define $color=1 
$color="1";

echo '<head>';
echo '<title>Members Database</title>';
echo '<link rel="stylesheet" href="style.css" type="text/css" media="screen" />';
echo '</head>';
echo '<body>';

while($rows=mysql_fetch_array($result)){

if($color==1){

echo "<div class='bg1'>

<div class='year'>
Class of ".stripslashes($rows['year'])."
</div>

<div class='name'>
".stripslashes($rows['firstname'])." ".stripslashes($rows['nickname'])." ".stripslashes($rows['maidenname'])." ".stripslashes($rows['lastname'])."
</div> 

<div class='address'>
".stripslashes($rows['streetaddress'])."<br />
".stripslashes($rows['mailingaddress'])."<br />
".stripslashes($rows['city']).", ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])."
</div>

<div class='phone'>
(".stripslashes($rows['areacode']).") ".stripslashes($rows['prefix'])."-".stripslashes($rows['suffix'])."
</div>

<div class='email'>
".stripslashes($rows['email'])."
</div>

</div>";

// Set $color==2, for switching to other color 

$color="2";
}

else {

echo "<div class='bg2'>

<div class='year'>
Class of ".stripslashes($rows['year'])."
</div>

<div class='name'>
".stripslashes($rows['firstname'])." ".stripslashes($rows['nickname'])." ".stripslashes($rows['maidenname'])." ".stripslashes($rows['lastname'])."
</div> 

<div class='address'>
".stripslashes($rows['streetaddress'])."<br />
".stripslashes($rows['mailingaddress'])."<br />
".stripslashes($rows['city']).", ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])."
</div>

<div class='phone'>
(".stripslashes($rows['areacode']).") ".stripslashes($rows['prefix'])."-".stripslashes($rows['suffix'])."
</div>

<div class='email'>
".stripslashes($rows['email'])."
</div>

</div>";

// Set $color back to 1 
$color="1";
}

}

echo '</body>';
mysql_close($connection);
?>

Link to comment
Share on other sites

Thanks for the quick reply!  When I plug that in, I get the following:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/wareh5/public_html/database/db5.php on line 22

 

I think it has something to do with the quotation marks but I can't figure out the proper syntax...

 

have you tried something like this ?

 

<div class='year' <?php if(strlen($rows['year'])==0) { echo "style=\"display:none;\"";} ?>>
Class of ".stripslashes($rows['year'])."
</div>

 

Link to comment
Share on other sites

hmm i misread your original code, i didn't realize the whole thing was one giant echo

 

perhaps something like this would work a little better

<div class='year' ".if(strlen($rows['year'])==0) { . " style=\"display:none;\"" . } ." >
Class of ".stripslashes($rows['year'])."
</div>

Link to comment
Share on other sites

Yeah as you can see I kind of hacked it together :/  Trying to learn through trial and error...

 

Now it's saying:

 

Parse error: syntax error, unexpected T_IF in /home/wareh5/public_html/database/db5.php on line 22

 

 

hmm i misread your original code, i didn't realize the whole thing was one giant echo

 

perhaps something like this would work a little better

<div class='year' ".if(strlen($rows['year'])==0) { . " style=\"display:none;\"" . } ." >
Class of ".stripslashes($rows['year'])."
</div>

Link to comment
Share on other sites

maybe an expert can comment on it, but i think the problem is that you can't have an If statement within the echo.  I have done something similar to this

 

<?php
echo "<div class='bg2'>";
?>
<div class='year'
<?php if(strlen($rows['year'])==0) { echo " style=\"display:none;\"";  } ?> 
>
Class of <?php stripslashes($rows['year']); ?>
</div>

 

play around with it some to see if you can get it to work.  the concept is correct, its just a matter of the formatting

 

Link to comment
Share on other sites

Yeah, I'm trying it every which way but so far I can't get it to work.  The concept does seem right though, thank you for your help.  Would it work better if I rewrote the page doing it some other way than in one big echo?  If so, I'm definitely open to suggestion...

 

maybe an expert can comment on it, but i think the problem is that you can't have an If statement within the echo.  I have done something similar to this

 

<?php
echo "<div class='bg2'>";
?>
<div class='year'
<?php if(strlen($rows['year'])==0) { echo " style=\"display:none;\"";  } ?> 
>
Class of <?php stripslashes($rows['year']); ?>
</div>

 

play around with it some to see if you can get it to work.  the concept is correct, its just a matter of the formatting

 

Link to comment
Share on other sites

Okay I'll try that. . Yes, sorry.  The latest piece gives this error:

 

Parse error: syntax error, unexpected '<' in /home/wareh5/public_html/database/db5.php on line 20

 

 

have you tried the latest piece that I provided ?

 

have the divs in actual HTML rather than echoing from PHP.  then have the PHP conditional within the <?php ?>

Link to comment
Share on other sites

Sure, here you go.  It says line 20 is "<?php echo "<div class='bg1'>"; ?>" but even if I take it out, it still says it's line 20 so I'm not sure...

 

<?php

include('connection.php');

$result = mysql_query("SELECT * FROM `database` order by year desc");

// Define $color=1 
$color="1";

echo '<head>';
echo '<title>Members Database</title>';
echo '<link rel="stylesheet" href="style.css" type="text/css" media="screen" />';
echo '</head>';
echo '<body>';

while($rows=mysql_fetch_array($result)){

if($color==1){

<?php echo "<div class='bg1'>"; ?>

<div class='year'
<?php if(strlen($rows['year'])==0) { echo " style=\"display:none;\"";  } ?> 
>
Class of <?php stripslashes($rows['year']); ?>
</div>

<div class='name'>
".stripslashes($rows['firstname'])." ".stripslashes($rows['nickname'])." ".stripslashes($rows['maidenname'])." ".stripslashes($rows['lastname'])."
</div> 

<div class='address'>
".stripslashes($rows['streetaddress'])."<br />
".stripslashes($rows['mailingaddress'])."<br />
".stripslashes($rows['city']).", ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])."
</div>

<div class='phone'>
(".stripslashes($rows['areacode']).") ".stripslashes($rows['prefix'])."-".stripslashes($rows['suffix'])."
</div>

<div class='email'>
".stripslashes($rows['email'])."
</div>

</div>";

// Set $color==2, for switching to other color 

$color="2";
}

else {

<?php echo "<div class='bg2'>"; ?>

<div class='year'
<?php if(strlen($rows['year'])==0) { echo " style=\"display:none;\"";  } ?> 
>
Class of <?php stripslashes($rows['year']); ?>
</div>

<div class='name'>
".stripslashes($rows['firstname'])." ".stripslashes($rows['nickname'])." ".stripslashes($rows['maidenname'])." ".stripslashes($rows['lastname'])."
</div> 

<div class='address'>
".stripslashes($rows['streetaddress'])."<br />
".stripslashes($rows['mailingaddress'])."<br />
".stripslashes($rows['city']).", ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])."
</div>

<div class='phone'>
(".stripslashes($rows['areacode']).") ".stripslashes($rows['prefix'])."-".stripslashes($rows['suffix'])."
</div>

<div class='email'>
".stripslashes($rows['email'])."
</div>

</div>";

// Set $color back to 1 
$color="1";
}

}

echo '</body>';
mysql_close($connection);
?>

 

can you show the whole chunk again (same as your original post) and say which is line 20 ?  I dont think I see anything wrong with the syntax I posted, perhaps there is something outside that conflicts

Link to comment
Share on other sites

because you put another <?php into that line that you didn't need (you copy/pasted directly from my post)

 

I only had that in there for the formatting to show correctly in my code tags.  I have made a couple changes, once again my style is only in for the first form entry.  If this works you would have to put it in for the rest and adjust your PHP tags accordingly.

<?php

include('connection.php');

$result = mysql_query("SELECT * FROM `database` order by year desc");

// Define $color=1 
$color="1";

echo '<head>';
echo '<title>Members Database</title>';
echo '<link rel="stylesheet" href="style.css" type="text/css" media="screen" />';
echo '</head>';
echo '<body>';

while($rows=mysql_fetch_array($result)){

if($color==1){

echo "<div class='bg1'>";
?>

<div class='year'
<?php if(strlen($rows['year'])==0) { echo " style=\"display:none;\"";  } ?>
>
Class of <?php stripslashes($rows['year']); ?>
</div>

<?php echo "
<div class='name'>
".stripslashes($rows['firstname'])." ".stripslashes($rows['nickname'])." ".stripslashes($rows['maidenname'])." ".stripslashes($rows['lastname'])."
</div>

<div class='address'>
".stripslashes($rows['streetaddress'])."<br />
".stripslashes($rows['mailingaddress'])."<br />
".stripslashes($rows['city']).", ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])."
</div>

<div class='phone'>
(".stripslashes($rows['areacode']).") ".stripslashes($rows['prefix'])."-".stripslashes($rows['suffix'])."
</div>

<div class='email'>
".stripslashes($rows['email'])."
</div>

</div>";

// Set $color==2, for switching to other color

$color="2";
}

else {

<?php echo "<div class='bg2'>"; ?>

<div class='year'
<?php if(strlen($rows['year'])==0) { echo " style=\"display:none;\"";  } ?>
>
Class of <?php stripslashes($rows['year']); ?>
</div>

<div class='name'>
".stripslashes($rows['firstname'])." ".stripslashes($rows['nickname'])." ".stripslashes($rows['maidenname'])." ".stripslashes($rows['lastname'])."
</div>

<div class='address'>
".stripslashes($rows['streetaddress'])."<br />
".stripslashes($rows['mailingaddress'])."<br />
".stripslashes($rows['city']).", ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])."
</div>

<div class='phone'>
(".stripslashes($rows['areacode']).") ".stripslashes($rows['prefix'])."-".stripslashes($rows['suffix'])."
</div>

<div class='email'>
".stripslashes($rows['email'])."
</div>

</div>";

// Set $color back to 1
$color="1";
}

}

echo '</body>';
mysql_close($connection);
?>

Link to comment
Share on other sites

Okay, that did something... now it seems to be hiding the "year" field regardless of whether or not it actually contains data.  So as before, it would say "Class of 1958", now it says "Class of".

 

What I really want to do is hide the entire div that contains the empty field, so the if the field itself is blank, the entire div/line containing "Class of 1958" won't show up at all... does that make sense?

 

because you put another <?php into that line that you didn't need (you copy/pasted directly from my post)

 

I only had that in there for the formatting to show correctly in my code tags.  I have made a couple changes, once again my style is only in for the first form entry.  If this works you would have to put it in for the rest and adjust your PHP tags accordingly.

<?php

include('connection.php');

$result = mysql_query("SELECT * FROM `database` order by year desc");

// Define $color=1 
$color="1";

echo '<head>';
echo '<title>Members Database</title>';
echo '<link rel="stylesheet" href="style.css" type="text/css" media="screen" />';
echo '</head>';
echo '<body>';

while($rows=mysql_fetch_array($result)){

if($color==1){

echo "<div class='bg1'>";
?>

<div class='year'
<?php if(strlen($rows['year'])==0) { echo " style=\"display:none;\"";  } ?>
>
Class of <?php stripslashes($rows['year']); ?>
</div>

<?php echo "
<div class='name'>
".stripslashes($rows['firstname'])." ".stripslashes($rows['nickname'])." ".stripslashes($rows['maidenname'])." ".stripslashes($rows['lastname'])."
</div>

<div class='address'>
".stripslashes($rows['streetaddress'])."<br />
".stripslashes($rows['mailingaddress'])."<br />
".stripslashes($rows['city']).", ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])."
</div>

<div class='phone'>
(".stripslashes($rows['areacode']).") ".stripslashes($rows['prefix'])."-".stripslashes($rows['suffix'])."
</div>

<div class='email'>
".stripslashes($rows['email'])."
</div>

</div>";

// Set $color==2, for switching to other color

$color="2";
}

else {

<?php echo "<div class='bg2'>"; ?>

<div class='year'
<?php if(strlen($rows['year'])==0) { echo " style=\"display:none;\"";  } ?>
>
Class of <?php stripslashes($rows['year']); ?>
</div>

<div class='name'>
".stripslashes($rows['firstname'])." ".stripslashes($rows['nickname'])." ".stripslashes($rows['maidenname'])." ".stripslashes($rows['lastname'])."
</div>

<div class='address'>
".stripslashes($rows['streetaddress'])."<br />
".stripslashes($rows['mailingaddress'])."<br />
".stripslashes($rows['city']).", ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])."
</div>

<div class='phone'>
(".stripslashes($rows['areacode']).") ".stripslashes($rows['prefix'])."-".stripslashes($rows['suffix'])."
</div>

<div class='email'>
".stripslashes($rows['email'])."
</div>

</div>";

// Set $color back to 1
$color="1";
}

}

echo '</body>';
mysql_close($connection);
?>

Link to comment
Share on other sites

no, it just means that your year field is "blank" but not empty

 

if your year is always going to be 4 digits, can you try to use strlen > 3 instead of ==0 ? 

 

Perhaps when inserting it into the database it puts something into the field that counts as a character which is why my check wasn't working

Link to comment
Share on other sites

Okay, I'll use year as an example and you can do the rest in the same manner:

 

...";


if (!empty($rows['year'])) {
echo "<div class='year'>
Class of \".stripslashes($rows['year']).\"
</div>";
}


echo "...

 

Would something like that do what you need?

edit: the '...' isn't literal, just showing you that I closed the echo tag and re-opened it after.

Link to comment
Share on other sites

I tried the last suggestion and this is what I'm getting now:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/wareh5/public_html/database/db7.php on line 24

 

Line 24 claims to be:

Class of \".stripslashes($rows['year']).\"

 

This is how I plugged it in, does this look right?

 

echo "<div class='bg1'>;"

if (!empty($rows['year'])) {
echo "<div class='year'>
Class of \".stripslashes($rows['year']).\"
</div>";
}

echo "

<div class='name'>
".stripslashes($rows['firstname'])." ".stripslashes($rows['nickname'])." ".stripslashes($rows['maidenname'])." ".stripslashes($rows['lastname'])."
</div> 
...etc.

 

Okay, I'll use year as an example and you can do the rest in the same manner:

 

...";


if (!empty($rows['year'])) {
echo "<div class='year'>
Class of \".stripslashes($rows['year']).\"
</div>";
}


echo "...

 

Would something like that do what you need?

edit: the '...' isn't literal, just showing you that I closed the echo tag and re-opened it after.

Link to comment
Share on other sites

couple problems I see

 

semicolon should be outside the quotes of your first echo

 

inside the first div you should use " and not \"

 

echo "<div class='bg1'>";

if (!empty($rows['year'])) {
echo "<div class='year'>
Class of ".stripslashes($rows['year'])."
</div>";
}

 

thats all i saw at first glance

Link to comment
Share on other sites

IT WORKS!  Thank you guys so much, wow.  Here is the final working code:

 

<?php

include('connection.php');

$result = mysql_query("SELECT * FROM `database` order by lastname desc");

// Define $color=1 
$color="1";

echo '<head>';
echo '<title>Members Database</title>';
echo '<link rel="stylesheet" href="style.css" type="text/css" media="screen" />';
echo '</head>';
echo '<body>';

while($rows=mysql_fetch_array($result)){

if($color==1){

echo "<div class='bg1'>";

if (!empty($rows['year'])) {
echo "<div class='year'>
Class of ".stripslashes($rows['year'])."
</div>";
}

echo "

<div class='name'>
".stripslashes($rows['firstname'])." ".stripslashes($rows['nickname'])." ".stripslashes($rows['maidenname'])." ".stripslashes($rows['lastname'])."
</div> 

<div class='address'>
".stripslashes($rows['streetaddress'])."<br />
".stripslashes($rows['mailingaddress'])."<br />
".stripslashes($rows['city']).", ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])."
</div>

<div class='phone'>
(".stripslashes($rows['areacode']).") ".stripslashes($rows['prefix'])."-".stripslashes($rows['suffix'])."
</div>

<div class='email'>
".stripslashes($rows['email'])."
</div>

<div class='id'>
ID: ".stripslashes($rows['id'])." 
</div> 

</div>";

// Set $color==2, for switching to other color 

$color="2";
}

else {

echo "<div class='bg2'>";

if (!empty($rows['year'])) {
echo "<div class='year'>
Class of ".stripslashes($rows['year'])."
</div>";
}

echo "

<div class='name'>
".stripslashes($rows['firstname'])." ".stripslashes($rows['nickname'])." ".stripslashes($rows['maidenname'])." ".stripslashes($rows['lastname'])."
</div> 

<div class='address'>
".stripslashes($rows['streetaddress'])."<br />
".stripslashes($rows['mailingaddress'])."<br />
".stripslashes($rows['city']).", ".stripslashes($rows['state'])." ".stripslashes($rows['zipcode'])."
</div>

<div class='phone'>
(".stripslashes($rows['areacode']).") ".stripslashes($rows['prefix'])."-".stripslashes($rows['suffix'])."
</div>

<div class='email'>
".stripslashes($rows['email'])."
</div>

<div class='id'>
ID: ".stripslashes($rows['id'])." 
</div> 

</div>";

// Set $color back to 1 
$color="1";
}

}

echo '</body>';
mysql_close($connection);
?>

 

couple problems I see

 

semicolon should be outside the quotes of your first echo

 

inside the first div you should use " and not \"

 

echo "<div class='bg1'>";

if (!empty($rows['year'])) {
echo "<div class='year'>
Class of ".stripslashes($rows['year'])."
</div>";
}

 

thats all i saw at first glance

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.