|
Re: comma placement problem [message #181531 is a reply to message #181530] |
Mon, 20 May 2013 19:48 |
Peter H. Coffin
Messages: 245 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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
|
|
|
Re: comma placement problem [message #181532 is a reply to message #181530] |
Mon, 20 May 2013 19:57 |
Salvatore
Messages: 38 Registered: September 2012
Karma: 0
|
Member |
|
|
On 2013-05-20, richard <noreply(at)example(dot)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++
|
|
|
Re: comma placement problem [message #181533 is a reply to message #181530] |
Mon, 20 May 2013 20:00 |
Luuk
Messages: 329 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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;
>
|
|
|
|
Re: comma placement problem [message #181535 is a reply to message #181530] |
Mon, 20 May 2013 21:05 |
Christoph Becker
Messages: 91 Registered: June 2012
Karma: 0
|
Member |
|
|
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
|
|
|
Re: comma placement problem [message #181542 is a reply to message #181530] |
Tue, 21 May 2013 08:51 |
Paul Herber
Messages: 26 Registered: February 2011
Karma: 0
|
Junior Member |
|
|
On Mon, 20 May 2013 14:32:02 -0400, richard <noreply(at)example(dot)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
|
|
|