On Monday, April 1, 2002, at 05:09 PM, Jay Fitzgerald wrote:
> Ok....Thanks to all those who are helping me (sorry for > cross-posting)... That's okay. This script looks a lot better -- because you can actually -do- something with the output! :) > My scripts are working great except for one thing....when it queries > the db there are some fields that have a NULL value, thus when it > writes the XML file it does this: > <null_field></null_field> Yep. > What I would like to do is either place an in the XML tags for > all of these NULL fields - can I do an if / else statement in the code > I am using below?? Yep. But look at the point where you have placed your pseudo-if/else statement. Read through this script as though you were the PHP processor -- what would happen? Well, you'd open the file for appending, you'd write your XML declaration and stylesheet PI, you'd write a few element names, and then you'd write a name pulled from your database. Fine, everything looks good up to this point... (continued below) > $phone_list = fopen("/usr/local/apache2/htdocs/test/phone_list.xml", > "w+"); > > fwrite($phone_list, "<?xml version=\"1.0\" > encoding=\"ISO-8859-1\"?>\n"); > fwrite($phone_list, "<?xml-stylesheet type=\"text/xsl\" > href=\"phone_list.xsl\"?>\n\n"); > > fwrite($phone_list, "<test>\n"); > fwrite($phone_list, "\t<phone_list>\n"); > > while ($row = mysql_fetch_array($sql_result)) > { > $name = $row["name"]; > $ext = $row["ext"]; > $title = $row["title"]; > $home = $row["home"]; > $pager = $row["pager"]; > $cell = $row["cell"]; > $email = $row["email"]; > > fwrite($phone_list, "\t\t<employee>\n"); > fwrite($phone_list, "\t\t\t<name>"); > fwrite($phone_list, "$name"); > fwrite($phone_list, "</name>\n"); Here is where you're going to run into a problem. Take a close look at what would happen if there is an $ext, and what would happen if there is no $ext (in other words, simulate the if/else statement in your mind): > fwrite($phone_list, "\t\t\t<ext>"); > // would need an if / else statement here > fwrite($phone_list, "$ext"); > // end if / else statement here > fwrite($phone_list, "</ext>\n"); See what's wrong? Regardless of whether or not there is an if/else statement, you are still going to do the following two lines of code: fwrite($phone_list, "\t\t\t<ext>"); fwrite($phone_list, "</ext>\n"); Because your if/else statement does not encompass these lines. What will the effect of this be? I'll show you: if there is an $ext <ext>123</ext> if there is no $ext <ext></ext> So, even if you use the pseudo if/else statement, you will still end up with a possibly empty <ext> element. If you changed your if/else statement to encompass this entire section of the code, however, then you can write <ext>123</ext> if there is an $ext variable or you can have nothing happen if there is no $ext. Here is what it would look like in pseudocode: // would need an if / else statement here fwrite($phone_list, "\t\t\t<ext>"); fwrite($phone_list, "$ext"); fwrite($phone_list, "</ext>\n"); // end if / else statement here Does that make sense? This way, if there's no $ext variable, you don't end up writing the <ext> and </ext> element tags. So how do you construct the actual statement? Well, to be honest with you, I don't think you want to use an if/else statement at all -- a simple if statement will do the trick. It works just like this: if (!empty($ext)) { fwrite($phone_list, "\t\t\t<ext>"); fwrite($phone_list, "$ext"); fwrite($phone_list, "</ext>\n"); } The empty() "function" (actually a language construct, but the difference isn't important) tests to see if there is actually any value attached to the $ext variable. And the exclamation point inverts the boolean value of the expression, meaning "if this is NOT true". So the if statement above simply says "if the value held by $ext is not empty, write three tabs + <ext> + $ext + </ext> to the file pointed to by $phone_list." The rest of your code looks good (closing the element tags and the file pointer). HTH, Erik ---- Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php