outchy Posted March 11, 2009 Share Posted March 11, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/ Share on other sites More sharing options...
lonewolf217 Posted March 11, 2009 Share Posted March 11, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782040 Share on other sites More sharing options...
outchy Posted March 11, 2009 Author Share Posted March 11, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782053 Share on other sites More sharing options...
lonewolf217 Posted March 11, 2009 Share Posted March 11, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782064 Share on other sites More sharing options...
outchy Posted March 11, 2009 Author Share Posted March 11, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782065 Share on other sites More sharing options...
lonewolf217 Posted March 11, 2009 Share Posted March 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782078 Share on other sites More sharing options...
outchy Posted March 11, 2009 Author Share Posted March 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782089 Share on other sites More sharing options...
lonewolf217 Posted March 11, 2009 Share Posted March 11, 2009 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782092 Share on other sites More sharing options...
outchy Posted March 11, 2009 Author Share Posted March 11, 2009 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782097 Share on other sites More sharing options...
lonewolf217 Posted March 11, 2009 Share Posted March 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782107 Share on other sites More sharing options...
outchy Posted March 11, 2009 Author Share Posted March 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782112 Share on other sites More sharing options...
lonewolf217 Posted March 11, 2009 Share Posted March 11, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782119 Share on other sites More sharing options...
outchy Posted March 11, 2009 Author Share Posted March 11, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782135 Share on other sites More sharing options...
lonewolf217 Posted March 11, 2009 Share Posted March 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782140 Share on other sites More sharing options...
aebstract Posted March 11, 2009 Share Posted March 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782141 Share on other sites More sharing options...
outchy Posted March 11, 2009 Author Share Posted March 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782155 Share on other sites More sharing options...
lonewolf217 Posted March 11, 2009 Share Posted March 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782157 Share on other sites More sharing options...
outchy Posted March 11, 2009 Author Share Posted March 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148937-solved-do-not-display-entire-divs-containing-empty-fields/#findComment-782171 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.