[PDO] Setting value to "nbsp;" if empty?

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

    [PDO] Setting value to "nbsp;" if empty?

    Hello

    Some of the columns are empty, and must be turned into "nbsp;" so
    that an empty cell is shown when displayed in HTML.

    This code, however, doesn't work (meaning: I get an empty line instead
    of "nbsp;"), but I don't know how else to loop through each column in
    the current row:

    ========
    $sql = "SELECT * FROM phones";

    foreach($dbh->query($sql) as $row) {

    foreach($row as $key =$val) {
    if(!$val)
    $val="nbsp;";
    }

    print $row['phones_name'] . "<p>";

    }
    ========

    Any idea why?

    Thank you.
  • Jerry Stuckle

    #2
    Re: [PDO] Setting value to &quot;nbsp;&quo t; if empty?

    Gilles Ganault wrote:
    Hello
    >
    Some of the columns are empty, and must be turned into "nbsp;" so
    that an empty cell is shown when displayed in HTML.
    >
    This code, however, doesn't work (meaning: I get an empty line instead
    of "nbsp;"), but I don't know how else to loop through each column in
    the current row:
    >
    ========
    $sql = "SELECT * FROM phones";
    >
    foreach($dbh->query($sql) as $row) {
    >
    foreach($row as $key =$val) {
    if(!$val)
    $val="nbsp;";
    }
    >
    print $row['phones_name'] . "<p>";
    >
    }
    ========
    >
    Any idea why?
    >
    Thank you.
    >
    Probably because $val isn't what you expect it to be. Find out what's
    in $val for an empty column and test for that.

    And BTW - the code is "&nbsp;". You're missing the leading ampersand.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Gilles Ganault

      #3
      Re: [PDO] Setting value to &quot;nbsp;&quo t; if empty?

      On Sun, 17 Feb 2008 16:49:31 +0100, "Rik Wasmus"
      <luiheidsgoeroe @hotmail.comwro te:
      >Then you should do a:
      >foreach ($row as $key =&$val) {
      I didn't know that structure. Is it refering to the value's address?
      >Offcourse, this is probably better solved with a css empty-cells
      >property...
      Nice to know, although it doesn't look nice if the cell is empty and
      the only one on the row:



      Thanks.

      Comment

      • Gilles Ganault

        #4
        Re: [PDO] Setting value to &quot;nbsp;&quo t; if empty?

        On Sun, 17 Feb 2008 14:13:12 -0500, Jerry Stuckle
        <jstucklex@attg lobal.netwrote:
        >Yes, they are two different operations. The query returns a result set
        >from the database, and the fetch gets a single row from the result set.
        I can live with that. Thanks.

        Comment

        Working...