Click to See Complete Forum and Search --> : Testing for empty data fields


dcjones
08-23-2005, 03:07 PM
Hi all,

I have a database which holds addresses.

The fields are:

address1 = (street)
address2 = (suburb)
address3 = (town)
address4 = (county)
address5 = (zip code)

What I want to do is, when the output is displayed -

if address1 is empty display address2
if address2 is empty display address3
if address3 is empty display address4
if address4 is empty display address5
if address5 is empty display black space


mysql_select_db($database_accomm, $accomm);
$query_accomm_counties = sprintf("SELECT * FROM accom WHERE name = '%s' ORDER BY pic DESC", $colname_accomm_counties);
$query_limit_accomm_counties = sprintf("%s LIMIT %d, %d", $query_accomm_counties, $startRow_accomm_counties, $maxRows_accomm_counties);
$accomm_counties = mysql_query($query_limit_accomm_counties, $accomm) or die(mysql_error());
$row_accomm_counties = mysql_fetch_assoc($accomm_counties);



I have tried to do this with:

<?php
if (strlen ($row_accomm_counties['address1']) == 0)
{
echo $row_accomm_counties['address2'];
}
else
{
echo $row_accomm_counties['address1'];
}

if (strlen ($row_accomm_counties['address2']) == 0)
{
echo $row_accomm_counties['address3'];
}
else
{
echo $row_accomm_counties['address2'];
}
?>


--- and so on.

yes I know, it don't work.

Can soneone point me in the right direction.

Many thanks in advance, keep safe and well.

Dereck
}

NogDog
08-23-2005, 03:45 PM
Do you want to display each address element which is not empty, or just the first non-empty one?

dcjones
08-23-2005, 03:53 PM
Hi,

Yes I want to display each address field if it contains data,
If it does not contain data I want the next field with data
to takes its place on the screen.

At the moment I have my output like this.

address1 (output) = "1 deadend street"
address2 (output) = *no data* so I have a blank line
address3 (output) = "somewhere suburb"
address4 (output) = "somewhere county"
address5 (output) = "zip code"

What i want to happen is:

address1 (output) = "1 deadend street"
address3 (output) = "somewhere suburb"
address4 (output) = "somewhere county"
address5 (output) = "zip code"

(address3 not displayed because the field was empty)

Thanks for looking at this.

Kind regards, keep safe and well.

Dereck

NogDog
08-23-2005, 04:13 PM
foreach(array("address1", "address2", "address3", "address4", "address5") as $value)
{
if(!empty($row_accomm_counties[$value]))
{
echo "<p>{$row_accomm_counties[$value]}</p>\n";
}
}

dcjones
08-23-2005, 04:28 PM
Hi Nogdog,

Many thanks for your reply, it works fine.

I changed the code a wee bit.


foreach(array("address1", "address2", "address3", "address4", "address5") as $value)
{
if(!empty($row_accomm_counties[$value]))
{
echo "{$row_accomm_counties[$value]}<br>";
}
}


and the output is perfect, thanks

Is there a way to mark posts as RESOLVED?

Keep safe and well

Dereck