I'm coding a very simple address book-style database and accompanying PHP pages, an am hitting a minor stumbling block which should be a cinch to solve.
In the table, I have the fields "Surname" and "Forenames", and on the PHP script I am developing, I basically want to report all of the names in alphabetical order, in a 'Surname, Forenames' format... i.e. 'Bloggs, Joe' and so on. That's easy enough to do, but for some of the records in the table, I have entries with just surnames. On my basic reporting script, I am thus left with me 'Bloggs, ' on these type of entries, which is not what I want.
I am sure there is a very easy way around this with an IF / ELSE set of statements in the results script. The way I see it in fairly literal terms is like this: IF field Forenames is blank, then print nothing, ELSE print ", Forenames". But how do I code this in PHP?
Below is the result of my attempts. Can anyone help me clear this code up and fix the problem? Thanks.
-------BEGIN CODE-------
$result = mysql_query ("SELECT PersonID,Surname,Forenames from tblPeople");
$forenames = mysql_result($result,"Forenames");
if($row = mysql_fetch_array($result)) {
do {
print ("<a href=\"viewperson.php?id=");
print $row["PersonID"];
print ("\">");
print $row["Surname"];
if ($forenames="") {
print ("");
}
else
{
echo ', ',$forenames,'';
}
}
-----END CODE-----