how do you deal with blanks in this example

Codes here !

Moderators: macek, egami, gesf

User avatar
Fife Club
New php-forum User
New php-forum User
Posts: 15
Joined: Wed Oct 09, 2002 7:50 pm
Location: Charlotte, NC, USA

how do you deal with blanks in this example

Postby Fife Club » Thu Oct 10, 2002 1:47 pm

Here's my page:

http://www.pstvalumni.com/directory/

And here's my code that I adjusted from tutorial examples:

Code: Select all


<html>
<body>

<?php
$db = mysql_connect("localhost", "pstvalu_director", "XXXXXXX");

mysql_select_db("pstvalu_directory",$db);
$result = mysql_query("SELECT * FROM alumni ORDER BY lastname",$db);

if ($myrow = mysql_fetch_array($result))
{
echo "<table border=1>\n";
echo "<tr>

<td>Name</td>
<td>Class</td>
<td>Location</td>

</tr>\n";
do {
printf("<tr>

<td>%s (%s), %s</td>
<td>%s</td>
<td>%s, %s</td>

</tr>\n",

$myrow[lastname],
$myrow[maidenname],
$myrow[firstname],
$myrow[classyear],
$myrow[city],
$myrow[state]);

}
while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
}
else {
echo "Sorry, no records were found!";
}
?>


</body>
</html>



In general it works. I want to put people's maiden name in parenthesis following their last name, then followed by a comma, then their first name. This works perfectly for the first item. But the other 98% of the rows do not have maiden names in the mySQL database (they're blank). Is there a way I can alter this code so that it will only print the "(" and ")" on each side of the "%s" ONLY if there is data associated with that column?

The same situation effects those people who don't have cities and states associated with them, in which case they still get a comma in that section.

Please help! Thanks.

Jay

Postby Jay » Thu Oct 10, 2002 2:45 pm

Use the print statement instead of printf

Code: Select all

if (!empty($myrow['maidenname'])) print "(".$myrow['maidenname'].")";

User avatar
Fife Club
New php-forum User
New php-forum User
Posts: 15
Joined: Wed Oct 09, 2002 7:50 pm
Location: Charlotte, NC, USA

Postby Fife Club » Thu Oct 10, 2002 7:17 pm

Jay wrote:Use the print statement instead of printf

Code: Select all

if (!empty($myrow['maidenname'])) print "(".$myrow['maidenname'].")";


Thanks, but since I'm just starting out I don't know what to do with it. I tried removing the "f" and that gave me errors. Plus I'm not sure how to implament the code you gave me. Do I leave everything else the way it is and just change

Code: Select all

$myrow[lastname],
$myrow[maidenname],
$myrow[firstname],
$myrow[classyear],
$myrow[city],
$myrow[state]);


to

Code: Select all

$myrow[lastname],
if (!empty($myrow['maidenname'])) print "(".$myrow['maidenname'].")";
$myrow[firstname],
$myrow[classyear],
$myrow[city],
$myrow[state]);


I know what I wrote probably just made you laugh, but I'm just beginning to learn this stuff. Sorry for my lack of the basics.

:oops:

(My goal is to get a simple but clean list up and running as fast as possible and adding a link to display the complete row data on a separate page. .......... then I'll have more time to properly learn how to do more stuff like adding sortable columns, user searchability, etc.)

:D

Jay

Postby Jay » Thu Oct 10, 2002 10:59 pm

This is why I never use the printf function (unless I absolutely have to).

What you want is this:

Code: Select all

if (@mysql_num_rows($result))
{
print "<table border=1>\n";
print "<tr>

<td>Name</td>
<td>Class</td>
<td>Location</td>

</tr>\n";
while($row = mysql_fetch_array($result)) {
print "<tr>\n";

print "<td>".$row['lastname'];
if (!empty($row['maidenname'])) print " ".$row['maidenname'];
print "</td>\n";

print "<td>".$row['firstname']."</td>\n";
print "<td>".$row['classyear']."</td>\n";
print "<td>".$row['city']." ".$row['state']."</td>\n";
print "</tr>\n";
}
}
else {
echo "Sorry, no records were found!";
}
?>

There is also a way around it using the printf example which is the change the appropriate variable (if set) to the value with the brackets, or leave it as nothing!

User avatar
Fife Club
New php-forum User
New php-forum User
Posts: 15
Joined: Wed Oct 09, 2002 7:50 pm
Location: Charlotte, NC, USA

Postby Fife Club » Fri Oct 11, 2002 6:42 am

Hooray!!! Thanks!!! It worked great. I then made a few very minor adjustments with spaces, commas, and parenthesis, but I was only able to do so because your example showed me how to structure it.

:D :D :D


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 6 guests