Home  |  About  | Last |  Submit  |  Contact
AllQuests.com




Previous Question:  Blank page displaying  PHPNext Question:  [ubuntu] Firefox menu disappears when using most Themes  x86 64 bit Users
Question PHP script needs minor tweeking if it is possible ( SitePoint Forums PHP )
Updated: 2010-07-21 05:55:05 (4)
PHP script needs minor tweeking if it is possible

Is there a way to modify this script so that if a value returned is null that it won't print that particular field? Basically, this script grabs each user, address, and phone numbers in my family. If someone doesn't have a work number listed, or cel-phone listed, I would prefer it not add anything (like a "Work:" or "Cell:" field) for that user.

For example:

First Name Last Name
555 Some drive
Some City, CA 12345
Home: 555-555-5555
Work:
Cell: 444-444-4444

Would look like:

First Name Last Name
555 Some drive
Some City, CA 12345
Home: 555-555-5555
Cell: 444-444-4444

But would not affect other users like:

First Name Last Name
444 Some Other drive
Some Other City, WY 98765
Home: 555-555-5555
Work: 333-333-3333
Cell: 444-444-4444

Thanks in advance:

PHP Code:

<?php
include 'config2.php';
include
'opendb.php';

// here's a quickie function to print the address from
// an associative array. We'll use it in a moment
function printaddr($addr){
   echo
"<b>{$addr['NameF']} {$addr['NameL']}</b><br />\n";
   echo
"<b>" . nl2br($addr['Address']) . "</b><br />\n";
   echo
"Home: {$addr['Home']}<br />\n";
   echo
"Work: {$addr['Work']}<br />\n";
   echo
"Cell: {$addr['Cell']}<br />\n<br />\n";
}

// get the data from DB
$query  = "SELECT NameF, NameL, Address, Home, Work, Cell FROM Contacts WHERE  Affiliation = 'family' ORDER BY NameL";
$result = mysql_query($query);

// now put everything in one big array
$BigAddressArray = array();
while(
$row = mysql_fetch_array($result)){
   
$BigAddressArray[] = $row;
}

// chunk the array for three column output
// note that if you wanted 2 or 4 or 12 column
// output you only need to change the last
// argument in array_chunk(). Everything else
// stays the same.
$AddressTable = array_chunk($BigAddressArray, 3);

echo
"<table>\n";
foreach(
$AddressTable as $TableRow){
   echo
"<tr>";
   foreach(
$TableRow as $TableCell){
      echo
'<td valign="top" width="250px">';
      
printaddr($TableCell);
      echo
'</td>';
   }
   echo
"</tr>\n";
}
echo
"</table>\n";

include
'closedb.php';
?>
<form><center>
<input type="button" value="Print This Page" onClick="window.print()" /></center>
</form>

Answers: PHP script needs minor tweeking if it is possible ( SitePoint Forums PHP )
PHP script needs minor tweeking if it is possible

Don't echo the text if the value isn't there. eg. Change
PHP Code:

echo "Home: {$addr['Home']}<br />\n"; 

to something like
PHP Code:

if (!empty({$addr['Home']})) echo "Home: {$addr['Home']}<br />\n"; 


Mittineague

PHP script needs minor tweeking if it is possible

Well, here are the changes made, but the page now comes up blank. Any ideas as to what I am doing wrong? BTW: Thanks for the help.

PHP Code:

<


function
printaddr($addr){
   echo
"<b>{$addr['NameF']} {$addr['NameL']}</b><br />\n";
   if (!empty({
$addr['Address']})) echo "Address: {$addr['Address']}<br />\n";
   if (!empty({
$addr['Home']})) echo "Home: {$addr['Home']}<br />\n";
   if (!empty({
$addr['Work']})) echo "Work: {$addr['Work']}<br />\n";
   if (!empty({
$addr['Cell']})) echo "Cell: {$addr['Cell']}<br />\n";
}

jdh239

PHP script needs minor tweeking if it is possible

Don't put any bracket around the array parameter for empty function:
PHP Code:

function printaddr($addr){
   echo
"<b>{$addr['NameF']} {$addr['NameL']}</b><br />\n";
   if (!empty(
$addr['Address'])) echo "Address: {$addr['Address']}<br />\n";
   if (!empty(
$addr['Home'])) echo "Home: {$addr['Home']}<br />\n";
   if (!empty(
$addr['Work'])) echo "Work: {$addr['Work']}<br />\n";
   if (!empty(
$addr['Cell'])) echo "Cell: {$addr['Cell']}<br />\n";
}

kefeso

PHP script needs minor tweeking if it is possible

Awesome! Thanks for the info. That was the fix. Working perfectly now. Thank you for your help.

jdh239

Previous Question:  Blank page displaying  SitePoint Forums  PHPNext Question:  [ubuntu] Firefox menu disappears when using most Themes  Ubuntu Forums  x86 64 bit Users

- Source: PHP script needs minor tweeking if it is possible SitePoint Forums PHP
- Previous Question: Blank page displaying SitePoint Forums PHP
- Next Question: [ubuntu] Firefox menu disappears when using most Themes Ubuntu Forums x86 64 bit Users