FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » comma placement problem
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
comma placement problem [message #181530] Mon, 20 May 2013 18:32 Go to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
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;
Re: comma placement problem [message #181531 is a reply to message #181530] Mon, 20 May 2013 19:48 Go to previous messageGo to next message
Peter H. Coffin is currently offline  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 Go to previous messageGo to next message
Salvatore is currently offline  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 Go to previous messageGo to next message
Luuk is currently offline  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 #181534 is a reply to message #181533] Mon, 20 May 2013 20:40 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
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.
Re: comma placement problem [message #181535 is a reply to message #181530] Mon, 20 May 2013 21:05 Go to previous messageGo to next message
Christoph Becker is currently offline  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 Go to previous message
Paul Herber is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Immediate Position:::Business Analyst With BI EXP::::At Houston, TX
Next Topic: Problem with quotes in javascript output
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Dec 03 18:13:56 GMT 2024

Total time taken to generate the page: 0.02307 seconds