Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

comma placement problem

1 view
Skip to first unread message

richard

unread,
May 20, 2013, 2:32:02 PM5/20/13
to
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;

Peter H. Coffin

unread,
May 20, 2013, 3:48:27 PM5/20/13
to
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

Salvatore

unread,
May 20, 2013, 3:57:01 PM5/20/13
to
On 2013-05-20, richard <nor...@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++

Luuk

unread,
May 20, 2013, 4:00:08 PM5/20/13
to
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;
>

richard

unread,
May 20, 2013, 4:40:22 PM5/20/13
to
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.

Christoph Becker

unread,
May 20, 2013, 5:05:40 PM5/20/13
to
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

Paul Herber

unread,
May 21, 2013, 4:51:31 AM5/21/13
to
if (($year<1969) and ($number<=10)){echo ",";}



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