php-general Digest 23 Mar 2011 12:20:44 -0000 Issue 7239
Topics (messages 311990 through 311998):
Re: echo?
311990 by: Al
311991 by: Jim Giner
311992 by: Tamara Temple
311993 by: Jim Giner
311994 by: Paul M Foster
311995 by: Geoff Lane
311996 by: Frank Arensmeier
311997 by: Ricardo Martinez
311998 by: Richard Quadling
Administrivia:
To subscribe to the digest, e-mail:
[email protected]
To unsubscribe from the digest, e-mail:
[email protected]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
On 3/22/2011 6:22 PM, Jim Giner wrote:
Kinda new to this, but I've been puttering/writing for about 3 weeks now and
have some good working screens up. Ran into something new while I was
debuggina script today.
Tried to echo the $i value within a for loop as part of the list of items I
was building
Something like
for ($i=0;$i<$rows;$i++)
echo $i.' '.$row['itemname'];
I expected to see :
1 item1
2 item2
...
...
but instead I got
1 item1
f item2
Yes - an 'f' and not a 2.
Tried it some more with this:
for ($i=1;$i<10;$i++)
echo $i." item".'<br>';
and got
c item
d item
e item
f item
g item
....
and so on.
It seems that I can only output the value of $i if I output a string in
front of it
echo ' '.$i;
works fine but
echo $i;
does not.
Any ideas?
If off your subject a bit; but, I suggest using
$i=0;
foreach($row as $value)
{
echo "$i $value<br />\n";
$i++;
}
--- End Message ---
--- Begin Message ---
ok - here's the code in question.
$q = 'select * from director_records ';
$qrslt = mysql_query($q);
$rows = mysql_num_rows($qrslt);
for ($i=0; $i<$rows; $i++)
{
$j = $i+1;
$row = mysql_fetch_array($qrslt);
echo $j.'-'.$row['userid'];
if ($row['user_priv']<> "")
echo ' ('.$row['user_priv'].')
';
else
echo '
';
}
The output I get is:
1-smith5
f-ginerjm (M)
g-smith8
While the alpha parts are valid, the index is only correct for the first one
(0) obviously.
--- End Message ---
--- Begin Message ---
On Mar 22, 2011, at 8:42 PM, Jim Giner wrote:
ok - here's the code in question.
$q = 'select * from director_records ';
$qrslt = mysql_query($q);
$rows = mysql_num_rows($qrslt);
for ($i=0; $i<$rows; $i++)
{
$j = $i+1;
Am i reading this correctly: the first variable is j (jay) the second
variable is i (eye) ?
This alone doesn't explain anything...
$row = mysql_fetch_array($qrslt);
echo $j.'-'.$row['userid'];
Since this is the only place $j is used, try subbing in $i+1 and see
what you get.
if ($row['user_priv']<> "")
echo ' ('.$row['user_priv'].')
';
This is really rather a strange way of getting a line break.
else
echo '
';
}
The output I get is:
1-smith5
f-ginerjm (M)
g-smith8
While the alpha parts are valid, the index is only correct for the
first one
(0) obviously.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--- End Message ---
--- Begin Message ---
Yes - it is J and I. I tried using $i+1 in the echo originally but it
wouldn't run. That's why I created $j.
And just what is wrong with the old cr/lf sequence? How would you have done
it?
What do you mean 'this alone .....'?
"Tamara Temple" <[email protected]> wrote in message
news:[email protected]...
>
> On Mar 22, 2011, at 8:42 PM, Jim Giner wrote:
>
>> ok - here's the code in question.
>> $q = 'select * from director_records ';
>> $qrslt = mysql_query($q);
>> $rows = mysql_num_rows($qrslt);
>> for ($i=0; $i<$rows; $i++)
>> {
>> $j = $i+1;
>
> Am i reading this correctly: the first variable is j (jay) the second
> variable is i (eye) ?
>
> This alone doesn't explain anything...
>
>> $row = mysql_fetch_array($qrslt);
>> echo $j.'-'.$row['userid'];
>
> Since this is the only place $j is used, try subbing in $i+1 and see what
> you get.
>
>> if ($row['user_priv']<> "")
>> echo ' ('.$row['user_priv'].')
';
>
> This is really rather a strange way of getting a line break.
>
>> else
>> echo '
';
>> }
>>
>>
>> The output I get is:
>>
>>
>> 1-smith5
>> f-ginerjm (M)
>> g-smith8
>>
>> While the alpha parts are valid, the index is only correct for the first
>> one
>> (0) obviously.
>>
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>
--- End Message ---
--- Begin Message ---
On Tue, Mar 22, 2011 at 10:50:54PM -0400, Jim Giner wrote:
> Yes - it is J and I. I tried using $i+1 in the echo originally but it
> wouldn't run. That's why I created $j.
Yes, the substitution creates a syntax error unless surrounded by
parentheses or the like.
> And just what is wrong with the old cr/lf sequence? How would you have done
> it?
You're using HTML-encoded entities for 0x0d and 0x0a. You can simply
use 0x0d and 0x0a instead. If you're running this in a web context, you
should use "<br/>" instead of CRLF. At the command line, I'm not
familiar with running PHP on Windows. In *nix environments, it's enough
to use "\n", just as they do in C. It might even work in Windows; I
don't know. If not, you should be able to use "\r\n". You can also try
the constant PHP_EOL, which is supposed to handle newlines in a
cross-platform way.
Paul
--
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com
--- End Message ---
--- Begin Message ---
Hi Jim,
On Wednesday, March 23, 2011, 1:42:18 AM, you wrote:
> ok - here's the code in question.
> $q = 'select * from director_records ';
> $qrslt = mysql_query($q);
> $rows = mysql_num_rows($qrslt);
> for ($i=0; $i<$rows; $i++)
> {
> $j = $i+1;
> $row = mysql_fetch_array($qrslt);
> echo $j.'-'.$row['userid'];
> if ($row['user_priv']<> "")
> echo ' ('.$row['user_priv'].')
';
> else
> echo '
';
> }
> The output I get is:
> 1-smith5
> f-ginerjm (M)
> g-smith8
> While the alpha parts are valid, the index is only correct for the first one
> (0) obviously.
I couldn't understand why you're getting characters, so I thought I'd
have a go myself. First, some DDL and DML to recreate your data:
create table director_records (userid char(16), user_priv char(8));
insert into director_records (userid, user_priv) values ('smith5',
''),('ginerjm','M'),('smith8','');
Now when I ran your code I got:
1-smith5
f-ginerjm (M)
g-smith8

That is, all but the first result has 
x in front of it. These are
HTML entities that display as characters and it so happens that f
is 'j' and g is 'g'. Strictly, these entities should be terminated
with a semi-colon (i.e. f and g), but your browser is
'obligingly' making sense of the 'bad formatting' and this is why
you're getting characters.
BTW, an alternative to your for construct would be to use a while loop
to iterate through a data table. e.g. in your case, I'd have used:
$q = 'select * from director_records ';
$qrslt = mysql_query($q);
$i = 1;
while ($row = mysql_fetch_array($qrslt)){
echo $i++ . '-' . $row['userid'];
if ($row['user_priv']<>""){
echo " (" . $row['user_priv'] . ")";
}
echo "<br>\n";
}
HTH,
--
Geoff Lane
Cornwall, UK
[email protected]
--- End Message ---
--- Begin Message ---
23 mar 2011 kl. 02.42 skrev Jim Giner:
> ok - here's the code in question.
> $q = 'select * from director_records ';
> $qrslt = mysql_query($q);
> $rows = mysql_num_rows($qrslt);
> for ($i=0; $i<$rows; $i++)
> {
> $j = $i+1;
> $row = mysql_fetch_array($qrslt);
> echo $j.'-'.$row['userid'];
> if ($row['user_priv']<> "")
> echo ' ('.$row['user_priv'].')
';
> else
> echo '
';
> }
>
>
> The output I get is:
>
>
> 1-smith5
> f-ginerjm (M)
> g-smith8
>
> While the alpha parts are valid, the index is only correct for the first one
> (0) obviously.
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
Why not try some basic debugging strategies and see what you get?
Try:
for ($i=0; $i<$rows; $i++)
{
var_dump($i);
$j = $i+1;
$row = mysql_fetch_array($qrslt);
echo $j.'-'.$row['userid'];
var_dump($j);
if ($row['user_priv']<> "")
echo ' ('.$row['user_priv'].')
';
else
echo '
';
}
The output you've posted, that's rendered output, right? What's the raw output?
By the way, the code snippet you gave us is not complete. Is there anything
else? As Dan noticed earlier, judging from that code snippet only, there must
be something else funky going on.
/frank
--- End Message ---
--- Begin Message ---
Hi
after of the for, u can use
it shoulds back the class of variable, by example "its is string" "its is
int" etc
for ($i=0;$i<$rows;$i++)
echo $i.' '.$row['itemname'];
echo gettype($i);
Can be that you must define before the class of this variable, because, the
system is thinking this is a string class data or hexa etc.
grettings and sorry for my written english ;)
--
Ricardo
_______________________________________________
IT Architect
website: http://www.pulsarinara.com
--- End Message ---
--- Begin Message ---
On 23 March 2011 07:46, Geoff Lane <[email protected]> wrote:
> Hi Jim,
>
> On Wednesday, March 23, 2011, 1:42:18 AM, you wrote:
>
>> ok - here's the code in question.
>> $q = 'select * from director_records ';
>> $qrslt = mysql_query($q);
>> $rows = mysql_num_rows($qrslt);
>> for ($i=0; $i<$rows; $i++)
>> {
>> $j = $i+1;
>> $row = mysql_fetch_array($qrslt);
>> echo $j.'-'.$row['userid'];
>> if ($row['user_priv']<> "")
>> echo ' ('.$row['user_priv'].')
';
>> else
>> echo '
';
>> }
>
>
>> The output I get is:
>
>
>> 1-smith5
>> f-ginerjm (M)
>> g-smith8
>
>> While the alpha parts are valid, the index is only correct for the first one
>> (0) obviously.
>
>
> I couldn't understand why you're getting characters, so I thought I'd
> have a go myself. First, some DDL and DML to recreate your data:
>
> create table director_records (userid char(16), user_priv char(8));
> insert into director_records (userid, user_priv) values ('smith5',
> ''),('ginerjm','M'),('smith8','');
>
> Now when I ran your code I got:
>
> 1-smith5
f-ginerjm (M)
g-smith8

>
> That is, all but the first result has 
x in front of it. These are
> HTML entities that display as characters and it so happens that f
> is 'j' and g is 'g'. Strictly, these entities should be terminated
> with a semi-colon (i.e. f and g), but your browser is
> 'obligingly' making sense of the 'bad formatting' and this is why
> you're getting characters.
>
> BTW, an alternative to your for construct would be to use a while loop
> to iterate through a data table. e.g. in your case, I'd have used:
>
> $q = 'select * from director_records ';
> $qrslt = mysql_query($q);
> $i = 1;
> while ($row = mysql_fetch_array($qrslt)){
> echo $i++ . '-' . $row['userid'];
> if ($row['user_priv']<>""){
> echo " (" . $row['user_priv'] . ")";
> }
> echo "<br>\n";
> }
>
> HTH,
I use ...
while(False !== ($row = mysql_fetch_array($qrslt)){
}
just so that if I have a query with 1 cell which is 0, or '', I don't
abort the loop.
--
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY
--- End Message ---