bchandel Posted August 26, 2009 Share Posted August 26, 2009 I have the following script a display of which can be seen at http://chandel.com/try/try3.php How can it be modified so the records which does not have particular field values does not print that field name. Like if a record has only First Name, Last Name then it should only print is as below: First Name:Jagrup Last Name:Parihar and not like: First Name: Jagrup Last Name: Parihar Home Phone: Mobile: Email: Web Site: State: Country: UK <html> <head> <style type="text/css"> body{ background-color:#b0c4de; border-top:blue solid 50px; border-left:blue solid 50px; } </style> </head> <body> <?php $db_connect = mysql_connect("XXXX.com", "XXXX_user", "password"); mysql_select_db("XXXX_database"); $query="SELECT * FROM Directory"; $result=mysql_query($query); $num=mysql_num_rows($result); mysql_close(); Print "<h1>My Directory</h1>"; echo "<h3>Search Results: $num records found</h3>"; $i=0; while ($i < $num) { $Name=mysql_result($result,$i,"Name"); $LastName=mysql_result($result,$i,"LastName"); $FirstName=mysql_result($result,$i,"FirstName"); $LastName=mysql_result($result,$i,"LastName"); $Phone=mysql_result($result,$i,"Phone"); $Mobile=mysql_result($result,$i,"Mobile"); $Email=mysql_result($result,$i,"Email"); $Web=mysql_result($result,$i,"Web"); $State=mysql_result($result,$i,"State"); $Country=mysql_result($result,$i,"Country"); echo "<b>$Name</b><hr> First Name: $FirstName<br> Last Name: $LastName<br> Home Phone: $Phone<br> Mobile: $Mobile<br> Email: $Email<br> Web Site: $Web<br> State: $State<br> Country: $Country<br><hr>"; $i++; } ?> </body> </head> Link to comment https://forums.phpfreaks.com/topic/171901-help-needed-to-make-this-code-work-thank-you/ Share on other sites More sharing options...
guyfromfl Posted August 26, 2009 Share Posted August 26, 2009 i am assuming this is what you want: $Mobile=mysql_result($result,$i,"Mobile"); // other vars..... //Make an if for each if ($Mobile) { echo "Mobile: " . $Mobile; } Link to comment https://forums.phpfreaks.com/topic/171901-help-needed-to-make-this-code-work-thank-you/#findComment-906447 Share on other sites More sharing options...
bchandel Posted August 26, 2009 Author Share Posted August 26, 2009 echo "<b>$Name</b><hr> First Name: $FirstName<br> Would it work if for above I change as below: if ($Name) { echo "Name: " . $Name; }<br> if ($FirstName) { echo "First Name: " . $FirstName; }<br> Link to comment https://forums.phpfreaks.com/topic/171901-help-needed-to-make-this-code-work-thank-you/#findComment-906453 Share on other sites More sharing options...
guyfromfl Posted August 27, 2009 Share Posted August 27, 2009 You're mixing html and php code all over the place. record 1 Name= FirstName=Joe Bob record 2 Name=Billy Bob FirstName= record3 Name=Peggy Sue FirstName=Peggy echo "<b>Database Contents</b>"; foreach ($rec as $record) { if ($Name) { echo "Name: " . $Name[$rec] . <br />"; } if ($FirstName) { echo "First Name: " . $FirstName[$rec]; } echo "<br />"; } Is that what you are looking for? Link to comment https://forums.phpfreaks.com/topic/171901-help-needed-to-make-this-code-work-thank-you/#findComment-907840 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.