Concatenate query results while ignoring empty fields

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GazK

    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?
  • Geoff Berrow

    #2
    Re: Concatenate query results while ignoring empty fields

    Message-ID:
    <c3aff0d3-91dc-454f-a2eb-1297826b4324@j2 2g2000hsf.googl egroups.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 011000100110110 0010000000110
    001101101011011 001000110111101 100111001011
    100110001101101 111001011100111 010101101011
    http://slipperyhill.co.uk - http://4theweb.co.uk

    Comment

    • Michael Fesser

      #3
      Re: Concatenate query results while ignoring empty fields

      ..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($fie lds));
      // remove empty entries
      $address = array_filter($a ddress);
      // concatenate and print
      print implode("<br />\n", $address);

      HTH
      Micha

      Comment

      • GazK

        #4
        Re: Concatenate query results while ignoring empty fields

        On 12 Sep, 11:00, Michael Fesser <neti...@gmx.de wrote:
        .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($fie lds));
        // remove empty entries
        $address = array_filter($a ddress);
        // 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?

        Comment

        Working...