Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #9478 > unrolled thread

comma placement problem

Started byrichard <noreply@example.com>
First post2013-05-20 14:32 -0400
Last post2013-05-21 09:51 +0100
Articles 7 — 6 participants

Back to article view | Back to comp.lang.php


Contents

  comma placement problem richard <noreply@example.com> - 2013-05-20 14:32 -0400
    Re: comma placement problem "Peter H. Coffin" <hellsop@ninehells.com> - 2013-05-20 14:48 -0500
    Re: comma placement problem Salvatore <sal@yojimbo.hack.invalid> - 2013-05-20 19:57 +0000
    Re: comma placement problem Luuk <luuk@invalid.lan> - 2013-05-20 22:00 +0200
      Re: comma placement problem richard <noreply@example.com> - 2013-05-20 16:40 -0400
    Re: comma placement problem Christoph Becker <cmbecker69@gmx.de> - 2013-05-20 23:05 +0200
    Re: comma placement problem Paul Herber <paul@pherber.com> - 2013-05-21 09:51 +0100

#9478 — comma placement problem

Fromrichard <noreply@example.com>
Date2013-05-20 14:32 -0400
Subjectcomma placement problem
Message-ID<bgnwqrwpql3u$.1hv0a1p6pymz3.dlg@40tude.net>
This does exactly what I want except that when 1969 is reached, the commas
don't get placed where I want them.
I need the commas on every line except for the last line.
which is why I put the conditional just before the \n.





$year=1960;
$number=1;
$number=(int)$number;
$year=(int)$year;


while ($year<=1969):


while ($number<=10):
$result = mysql_query("SELECT atitle,artist,avid FROM A$year WHERE id =
$number");
if (!$result) { echo 'Could not run query: ' . mysql_error();  exit; }
$vid = mysql_fetch_row($result);


echo "{";

echo 'image:';
echo '""';
echo ",";

echo "title:";
echo '"';
echo $vid[0];
echo '",';

echo 'file:"http://www.youtube.com/watch?v=';
echo $vid[2];
echo '",';

echo 'description:';
echo '"';
echo $year." # ".$number." - ".$vid[1];
echo '"';

echo "}";
if ($year<1969 and $number<=10){echo ",";}
echo "\n";
$number++;
endwhile;

$number=1;
$year++;

endwhile;

[toc] | [next] | [standalone]


#9479

From"Peter H. Coffin" <hellsop@ninehells.com>
Date2013-05-20 14:48 -0500
Message-ID<slrnkpkvgb.msg.hellsop@nibelheim.ninehells.com>
In reply to#9478
On Mon, 20 May 2013 14:32:02 -0400, richard wrote:
> This does exactly what I want except that when 1969 is reached, the commas
> don't get placed where I want them.
> I need the commas on every line except for the last line.
> which is why I put the conditional just before the \n.
>
>
>
>
>
> $year=1960;
> $number=1;
> $number=(int)$number;
> $year=(int)$year;
>
>
> while ($year<=1969):
>
....
> if ($year<1969 and $number<=10){echo ",";}
> echo "\n";
> $number++;
> endwhile;

Off-by-one problems usually stem from mismatching "<" and "<=".
Experiment there.

-- 
7. When I've captured my adversary and he says, "Look, before you kill 
   me, will you at least tell me what this is all about?" I'll say, 
   "No." and shoot him. On second thought I'll shoot him then say "No."
        --Peter Anspach's list of things to do as an Evil Overlord

[toc] | [prev] | [next] | [standalone]


#9480

FromSalvatore <sal@yojimbo.hack.invalid>
Date2013-05-20 19:57 +0000
Message-ID<kndv6d$rc8$1@yojimbo.hack>
In reply to#9478
On 2013-05-20, richard <noreply@example.com> wrote:
> This does exactly what I want except that when 1969 is reached, the commas
> don't get placed where I want them.
> I need the commas on every line except for the last line.
> which is why I put the conditional just before the \n.
> [snip]

Rewrite the two while loops to use curly brackets instead of the colon
and "endwhile" keyword.

-- 
Blah blah bleh...
GCS/CM d(-)@>-- s+:- !a C++$ UBL++++$ L+$ W+++$ w M++ Y++ b++

[toc] | [prev] | [next] | [standalone]


#9481

FromLuuk <luuk@invalid.lan>
Date2013-05-20 22:00 +0200
Message-ID<8vbq6a-lv5.ln1@luuk.invalid.lan>
In reply to#9478
On 20-05-2013 20:32, richard wrote:
> This does exactly what I want except that when 1969 is reached, the commas
> don't get placed where I want them.
> I need the commas on every line except for the last line.
> which is why I put the conditional just before the \n.
>

> $number=1;
> $number=(int)$number;

?? why this second assignment?

> while ($year<=1969):

i think an assignment to $number should be placed here....

> while ($number<=10):

...
> if ($year<1969 and $number<=10){echo ",";}

and a check to print "," based on "$number<=10" why?
this 'if' is placed in a 'while' loop with condition "$number<=10", 
there should be no need to check if $number is <= 10....

> echo "\n";
> $number++;
> endwhile;
>
> $number=1;
aaah here is is, a better place would be just before the 'while'

> $year++;
>
> endwhile;
>

[toc] | [prev] | [next] | [standalone]


#9482

Fromrichard <noreply@example.com>
Date2013-05-20 16:40 -0400
Message-ID<vgqcz35hvahf$.13owazhjsbwlm$.dlg@40tude.net>
In reply to#9481
On Mon, 20 May 2013 22:00:08 +0200, Luuk wrote:

> On 20-05-2013 20:32, richard wrote:
>> This does exactly what I want except that when 1969 is reached, the commas
>> don't get placed where I want them.
>> I need the commas on every line except for the last line.
>> which is why I put the conditional just before the \n.
>>
> 
>> $number=1;
>> $number=(int)$number;
> 
> ?? why this second assignment?
> 
>> while ($year<=1969):
> 
> i think an assignment to $number should be placed here....
> 
>> while ($number<=10):
> 
> ...
>> if ($year<1969 and $number<=10){echo ",";}
> 
> and a check to print "," based on "$number<=10" why?
> this 'if' is placed in a 'while' loop with condition "$number<=10", 
> there should be no need to check if $number is <= 10....
> 

I tried that. What I got was no comma when $year=1969.
Apparently, the last comma doesn't seem to make any difference.
the script works just fine with it.

[toc] | [prev] | [next] | [standalone]


#9483

FromChristoph Becker <cmbecker69@gmx.de>
Date2013-05-20 23:05 +0200
Message-ID<kne36t$ec0$1@speranza.aioe.org>
In reply to#9478
richard wrote:
> This does exactly what I want except that when 1969 is reached, the commas
> don't get placed where I want them.
> I need the commas on every line except for the last line.
> which is why I put the conditional just before the \n.

I usually prefer to build lists, which are *separated* by the same
string, by building a stack (array) of the items and letting the stack
implode() afterwards:

  $list = array('One');
  $list[] = 'Two';
  $list[] = 'Three';
  echo implode(', ' $list);

In this particular case I would build up the JSON as nested PHP array,
and then stringify it with json_encode().

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#9490

FromPaul Herber <paul@pherber.com>
Date2013-05-21 09:51 +0100
Message-ID<89dmp89d5h4dikkp160u3rmd1rnf8tbbb3@news.eternal-september.org>
In reply to#9478
On Mon, 20 May 2013 14:32:02 -0400, richard <noreply@example.com> wrote:

>This does exactly what I want except that when 1969 is reached, the commas
>don't get placed where I want them.
>I need the commas on every line except for the last line.
>which is why I put the conditional just before the \n.
>
>
>
>
>
>$year=1960;
>$number=1;
>$number=(int)$number;
>$year=(int)$year;
>
>
>while ($year<=1969):
>
>
>while ($number<=10):
>$result = mysql_query("SELECT atitle,artist,avid FROM A$year WHERE id =
>$number");
>if (!$result) { echo 'Could not run query: ' . mysql_error();  exit; }
>$vid = mysql_fetch_row($result);
>
>
>echo "{";
>
>echo 'image:';
>echo '""';
>echo ",";
>
>echo "title:";
>echo '"';
>echo $vid[0];
>echo '",';
>
>echo 'file:"http://www.youtube.com/watch?v=';
>echo $vid[2];
>echo '",';
>
>echo 'description:';
>echo '"';
>echo $year." # ".$number." - ".$vid[1];
>echo '"';
>
>echo "}";
>if ($year<1969 and $number<=10){echo ",";}

if (($year<1969) and ($number<=10)){echo ",";}



-- 
Regards, Paul Herber, Sandrila Ltd.
http://www.sandrila.co.uk/    twitter: @sandrilaLtd      

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web