PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > [PDO] Setting value to "nbsp;" if empty?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
[PDO] Setting value to "nbsp;" if empty?

Réponse
 
LinkBack Outils de la discussion
Vieux 17/02/2008, 11h44   #1
Gilles Ganault
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut [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.
  Réponse avec citation
Vieux 17/02/2008, 14h30   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PDO] Setting value to "nbsp;" 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@attglobal.net
==================

  Réponse avec citation
Vieux 17/02/2008, 15h21   #3
Gilles Ganault
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PDO] Setting value to "nbsp;" if empty?

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)? "&nbsp;":"$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 "&nbsp;"?

Thank you.
  Réponse avec citation
Vieux 17/02/2008, 15h40   #4
Gilles Ganault
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PDO] Setting value to "nbsp;" if empty?

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 "&nbsp;"?


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)? "&nbsp;":$val;
}

foreach ($row as $key => $val) {
print "$key = $val<p>\n";
}
}
  Réponse avec citation
Vieux 17/02/2008, 15h48   #5
Gilles Ganault
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PDO] Setting value to "nbsp;" if empty?

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)? "&nbsp;":$val;
}

print $row['contacts_city'] . "<p>\n";

}
  Réponse avec citation
Vieux 17/02/2008, 15h49   #6
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PDO] Setting value to "nbsp;" if empty?

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 "&nbsp;"?

>
> 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
  Réponse avec citation
Vieux 17/02/2008, 15h50   #7
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PDO] Setting value to "nbsp;" if empty?

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)? "&nbsp;":"$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 "&nbsp;"?
>
> 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
==================

  Réponse avec citation
Vieux 17/02/2008, 15h59   #8
Gilles Ganault
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PDO] Setting value to "nbsp;" if empty?

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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h03.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15645 seconds with 16 queries