|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 " ". You're missing the leading ampersand. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Sun, 17 Feb 2008 09:30:46 -0500, Jerry Stuckle
<jstucklex@attglobal.net> wrote: >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. Thanks for the tip. It seems like it's only possible to loop once through the items of an array: =============== $res = $dbh->query($sql); if($res) { while ( $row = $res->fetch(PDO::FETCH_ASSOC) ) foreach ($row as $key => $val) { $val = (!$val)? " ":"$val"; //GOOD print "$key = $val\n<p>"; } //BAD : nothing shown print $row['contacts_city'] . "<p>"; //BAD: Warning: Invalid argument supplied for foreach() foreach ($row as $key => $val) { print "$key = $val\n<p>"; } } =============== I'd prefer to use an associative array so I can refer to items using their column names instead of their indexes. Is there a way to do this, but still go through the array first to check for empty items and replace them with " "? Thank you. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Sun, 17 Feb 2008 16:21:00 +0100, Gilles Ganault <nospam@nospam.com>
wrote: >I'd prefer to use an associative array so I can refer to items using >their column names instead of their indexes. Is there a way to do >this, but still go through the array first to check for empty items >and replace them with " "? I was missing a } for the while line, so it was only iterating through the first line :-/ What's strange, is that the first foreach() loop doesn't actually change the contents of the array: while ( $row = $res->fetch(PDO::FETCH_ASSOC) ) { foreach ($row as $key => $val) { $val = (!$val)? " ":$val; } foreach ($row as $key => $val) { print "$key = $val<p>\n"; } } |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Sun, 17 Feb 2008 16:40:36 +0100, Gilles Ganault <nospam@nospam.com>
wrote: (snip) Found what it was :-) When using the "$key => $val" structure, I though I could modify the contents of an associative array by just modifying "$val", but apparently you have to use "$array[$key]" instead: while ( $row = $res->fetch(PDO::FETCH_ASSOC) ) { foreach ($row as $key => $val) { $row[$key] = (!$val)? " ":$val; } print $row['contacts_city'] . "<p>\n"; } |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Sun, 17 Feb 2008 16:40:36 +0100, Gilles Ganault <nospam@nospam.com>
wrote: > On Sun, 17 Feb 2008 16:21:00 +0100, Gilles Ganault <nospam@nospam.com> > wrote: >> I'd prefer to use an associative array so I can refer to items using >> their column names instead of their indexes. Is there a way to do >> this, but still go through the array first to check for empty items >> and replace them with " "? > > I was missing a } for the while line, so it was only iterating through > the first line :-/ > > What's strange, is that the first foreach() loop doesn't actually > change the contents of the array: > > while ( $row = $res->fetch(PDO::FETCH_ASSOC) ) { > foreach ($row as $key => $val) { Then you should do a: foreach ($row as $key => &$val) { Offcourse, this is probably better solved with a css empty-cells property... -- Rik Wasmus |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Gilles Ganault wrote:
> On Sun, 17 Feb 2008 09:30:46 -0500, Jerry Stuckle > <jstucklex@attglobal.net> wrote: >> 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. > > Thanks for the tip. It seems like it's only possible to loop once > through the items of an array: > > =============== > $res = $dbh->query($sql); > if($res) { > while ( $row = $res->fetch(PDO::FETCH_ASSOC) ) > foreach ($row as $key => $val) { > $val = (!$val)? " ":"$val"; > //GOOD > print "$key = $val\n<p>"; > } > > //BAD : nothing shown > print $row['contacts_city'] . "<p>"; > > //BAD: Warning: Invalid argument supplied for foreach() foreach > ($row as $key => $val) { > print "$key = $val\n<p>"; > } > } > =============== > > I'd prefer to use an associative array so I can refer to items using > their column names instead of their indexes. Is there a way to do > this, but still go through the array first to check for empty items > and replace them with " "? > > Thank you. > What is the value of $row? It's obviously not an array, and it's not false. But remember when you're calling fetch() you're retrieving data from the database. Even if you went through this array a second time, the data retrieved would be the same as the first time. Changing the local array element $row[$key] does not change the database. If you want to do this, you need to do it just before writing the html. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On Sun, 17 Feb 2008 16:49:31 +0100, "Rik Wasmus"
<luiheidsgoeroe@hotmail.com> wrote: >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: http://img407.imageshack.us/img407/6...ptycellrz0.jpg Thanks. |
|
![]() |
| Outils de la discussion | |
|
|