473,387 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Concatenate query results while ignoring empty fields

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
3 4039
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
..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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Andy | last post by:
Hello, I am having many problems with setting up a parameter query that searches by the criteria entered or returns all records if nothing is entered. I have designed an unbound form with 3...
1
by: Big Time | last post by:
I've got a crosstab query that counts the number of values in one of my tables. However, rather than giving me the number of instances of each record, what I would like is for each value to...
8
by: Dixie | last post by:
I have the results of a query to send to a mailmerge with Word 2000. The query produces say 6 to 8 records, where only 1 of the fields is different from record to record. I can only have one...
2
by: exapplerep | last post by:
I've seen how to use VBA code to concatenate two fields into a third by using an expression in the "After Update" property in fields 1 & 2. field3 = field1 + field2 The above code would go...
5
by: Sam | last post by:
Hi, I have one table like : MyTable {field1, field2, startdate, enddate} I want to have the count of field1 between startdate and enddate, and the count of field2 where field2 = 1 between...
3
by: MusicMogul | last post by:
Hey Y'All, I'm trying to do the following. I have create a query to retrieve values from 4 tables. In table employee, has the employee first name, middle name and last name. In my query i...
5
by: DeanL | last post by:
Hi all, I'm trying to set up a query that runs from a command button on a form (simple enough so far), what I want the query to do is take values from the fields on the form (seven fields in...
3
by: seagullino | last post by:
Hello, I've developed my first Form, a simple affair that enables the user to search text in the memo fields in our database. When the "search" button is pressed, it runs a macro which runs the...
10
by: Aaron Hoffman | last post by:
Hello, I'm hoping someone might be able to offer some guidance to my problem. I have one query in MS Access which consists of 2 tables joined by a SEQUENCE_ID. By joining the two tables I am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.