By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,824 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

Concatenate query results while ignoring empty fields

P: n/a
I am hoping someone can steer me towards an elegant solution to a
simple problem.

I have a query result which returns an array of details about a
physical building, including its address. The relevant fields are:

$row[houseNo]
$row[street]
$row[town]
$row[city]
$row[county]
$row[postcode]

I want to print the address in the usual html format. i.e.

houseNo<br/>
street<br/>
town<br/>
etc

*but* if the field is empty (e.g. the address is in London - no town
field required) I do not want a blank row in my html.

I can think of various ways to do this, but they are all very
inelegant, e.g. 6 if clauses, each adding $row[] . "<br/>" if $row <>
"". Euch.

I guess what I want is a for loop, but how do I only loop through the
relevant $row[] values and ignore the non-address fields?
Sep 12 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Message-ID:
<c3**********************************@j22g2000hsf. googlegroups.comfrom
GazK contained the following:
>$row[houseNo]
$row[street]
$row[town]
$row[city]
$row[county]
$row[postcode]

I want to print the address in the usual html format. i.e.

houseNo<br/>
street<br/>
town<br/>
etc
The usual format is

houseNo street<br/>
etc
>
*but* if the field is empty (e.g. the address is in London - no town
field required) I do not want a blank row in my html.

I can think of various ways to do this, but they are all very
inelegant, e.g. 6 if clauses, each adding $row[] . "<br/>" if $row <>
"". Euch.

I guess what I want is a for loop, but how do I only loop through the
relevant $row[] values and ignore the non-address fields?
You could easily use a loop but given that the first line is different
(and perhaps you may want town and postcode on the same line too) I
think you are stuck with the method you describe. But the ternary
operator makes it a little more elegant

$address="";
$address.= (empty($row['houseNo']))?"":$row['houseNo']."&nbsp;";
$address.= (empty($row['street']))?"</br>":$row['street']."</br>";
$address.= (empty($row['town']))?"":$row['town']."</br>";
etc..

You could write a function taking two arguments to do it, but it hardly
seems worth the effort.
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk - http://4theweb.co.uk
Sep 12 '08 #2

P: n/a
..oO(GazK)
>I am hoping someone can steer me towards an elegant solution to a
simple problem.

I have a query result which returns an array of details about a
physical building, including its address. The relevant fields are:

$row[houseNo]
$row[street]
$row[town]
$row[city]
$row[county]
$row[postcode]

I want to print the address in the usual html format. i.e.

houseNo<br/>
street<br/>
town<br/>
etc

*but* if the field is empty (e.g. the address is in London - no town
field required) I do not want a blank row in my html.

I can think of various ways to do this, but they are all very
inelegant, e.g. 6 if clauses, each adding $row[] . "<br/>" if $row <>
"". Euch.

I guess what I want is a for loop, but how do I only loop through the
relevant $row[] values and ignore the non-address fields?
$fields = array('houseNo', 'street', 'town', ...);
foreach ($fields as $field) {
if (!empty($row[$field])) {
...
}
}

You could also try it with array functions:

// get only address fields from the row
$fields = array('houseNo', 'street', 'town', ...);
$address = array_intersect_key($row, array_flip($fields));
// remove empty entries
$address = array_filter($address);
// concatenate and print
print implode("<br />\n", $address);

HTH
Micha
Sep 12 '08 #3

P: n/a
On 12 Sep, 11:00, Michael Fesser <neti...@gmx.dewrote:
.oO(GazK)
I am hoping someone can steer me towards an elegant solution to a
simple problem.
I have a query result which returns an array of details about a
physical building, including its address. The relevant fields are:
$row[houseNo]
$row[street]
$row[town]
$row[city]
$row[county]
$row[postcode]
I want to print the address in the usual html format. i.e.
houseNo<br/>
street<br/>
town<br/>
etc
*but* if the field is empty (e.g. the address is in London - no town
field required) I do not want a blank row in my html.
I can think of various ways to do this, but they are all very
inelegant, e.g. 6 if clauses, each adding $row[] . "<br/>" if $row <>
"". Euch.
I guess what I want is a for loop, but how do I only loop through the
relevant $row[] values and ignore the non-address fields?

$fields = array('houseNo', 'street', 'town', ...);
foreach ($fields as $field) {
* if (!empty($row[$field])) {
* * ...
* }

}

You could also try it with array functions:

// get only address fields from the row
$fields = array('houseNo', 'street', 'town', ...);
$address = array_intersect_key($row, array_flip($fields));
// remove empty entries
$address = array_filter($address);
// concatenate and print
print implode("<br />\n", $address);

HTH
Micha
Thanks to everybody for the helpful replies. I went with Micha's
solution, which works a treat. Who'da thought there could be so many
useful array functions?
Sep 12 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.