Ask a Question related to PHP Development, Design and Development.
-
Matt Matijevich #1
Re: [PHP] nested for loops
your syntax is correct, just need to change for ($j=0: $j < 5; $j++) to
for ($j=0; $j < 5; $j++)
Anyone ever do a nested for loop?>>> "Micah Montoy" <montmica@isu.edu> 07/09/03 12:56PM >>>
$i =0;
$j =0;
for ($x=0; $x < 50; $x++){
echo ("1 to 50");
for ($j=0: $j < 5; $j++) {
echo ("less than 5");
}
}
Matt Matijevich Guest
-
Nested loops?
Hi, I've been adapting a script which parses HTML and creates an RSS feed (http://www.perl.com/lpt/a/2001/11/15/creatingrss.html). Now I want... -
HTML::Template arbitraryily nested recursive loops
Can anyone suggest a way to display an arbitrarily deep nested loop structure using HTML::Template? Below is what I've tried. I have a data... -
Possible Bug w/ nested loops and queries
May have found a possible bug... can someone please verify? In a database I have two tables, temp1 and temp2 These tables can contain anything,... -
Using 'my' within nested loops
I am curious about the amount of overhead created with the first example below as compared to the second example below: Example 1: my $this;... -
nested for loops
Anyone ever do a nested for loop? $i =0; $j =0; for ($x=0; $x < 50; $x++){ echo ("1 to 50"); for ($j=0: $j < 5; $j++) { echo ("less than... -
Micah Montoy #2
Re: [PHP] nested for loops
I did it similar but calling some variables and it would never work. I'll
give it a shot again and see what happens and if same thing, I'll post it.
thanks
"Matt Matijevich" <matijevich@alliancetechnologies.net> wrote in message
news:sf0c1185.079@nw0102dsm.alliancetechnologies.n et...> your syntax is correct, just need to change for ($j=0: $j < 5; $j++) to
> for ($j=0; $j < 5; $j++)
>> Anyone ever do a nested for loop?> >>> "Micah Montoy" <montmica@isu.edu> 07/09/03 12:56PM >>>
>
> $i =0;
> $j =0;
> for ($x=0; $x < 50; $x++){
> echo ("1 to 50");
>
> for ($j=0: $j < 5; $j++) {
> echo ("less than 5");
> }
> }
>
>
>
Micah Montoy Guest
-
Dan Joseph #3
RE: [PHP] nested for loops
Hi,
I did one once. It was similar to what you typed out here. Are you having
troubles with this?
-Dan Joseph
> -----Original Message-----
> From: Micah Montoy [mailto:montmica@isu.edu]
> Sent: Wednesday, July 09, 2003 1:56 PM
> To: [email]php-general@lists.php.net[/email]
> Subject: [PHP] nested for loops
>
>
> Anyone ever do a nested for loop?
>
> $i =0;
> $j =0;
> for ($x=0; $x < 50; $x++){
> echo ("1 to 50");
>
> for ($j=0: $j < 5; $j++) {
> echo ("less than 5");
> }
> }
>
> I haven't seen a nested loop in PHP, so I'm not sure of the exact syntax.
>
> thanks
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>Dan Joseph Guest
-
Micah Montoy #4
Re: [PHP] nested for loops
Well, the for loop is now working but its not displaying what I want. I am
pulling data from a DB and for every new <td> I would like to get the next
result and if it reaches more then 2 columns, to start a new row. This I am
not initally having a problem with. That is the looping. The problem I am
running into, is getting the the DB stuff to show. Right now, I only one
record and it shows for every single one. Here is a snippet of the code.
$result = mssql_query("SELECT * FROM files WHERE file_id = '$cat_name' ORDER
BY file_name ASC");
$i = $col_num;
$j = 0;
$filename= mssql_result($result,0,"file_name");
$fileID = mssql_result($result,0,"file_id");
for ($k = 0; $k < mssql_num_rows($result); $k++) {
//changes row color
if (fmod($k, 2)){$rowColor = "$oddcolor";}else{$rowColor =
"$evencolor";}
echo ("<TR bgcolor=$rowColor>");
for ($j = 0; $j < $i; $j++) {
echo ("<td><a href=index.php&ID=$fileID_id>$filename</a>".
"Name: <strong>$getimage_name</strong>".
"<a href='index.php&ID=$fileID">Delete</a></td>");
}
echo ("</TR>");
}
Anyone see what I need to do to make it loop through and pull out the next
file name. I thought about a foreach but it never wanted to work with the
above.
thanks
"Matt Matijevich" <matijevich@alliancetechnologies.net> wrote in message
news:sf0c1185.079@nw0102dsm.alliancetechnologies.n et...> your syntax is correct, just need to change for ($j=0: $j < 5; $j++) to
> for ($j=0; $j < 5; $j++)
>> Anyone ever do a nested for loop?> >>> "Micah Montoy" <montmica@isu.edu> 07/09/03 12:56PM >>>
>
> $i =0;
> $j =0;
> for ($x=0; $x < 50; $x++){
> echo ("1 to 50");
>
> for ($j=0: $j < 5; $j++) {
> echo ("less than 5");
> }
> }
>
>
>
Micah Montoy Guest
-
Matt Matijevich #5
Re: [PHP] nested for loops
<snip>
$result = mssql_query("SELECT * FROM files WHERE file_id = '$cat_name'
ORDER
BY file_name ASC");
$i = $col_num;
$j = 0;
$filename= mssql_result($result,0,"file_name");
$fileID = mssql_result($result,0,"file_id");
for ($k = 0; $k < mssql_num_rows($result); $k++) {
//changes row color
if (fmod($k, 2)){$rowColor = "$oddcolor";}else{$rowColor =
"$evencolor";}
echo ("<TR bgcolor=$rowColor>");
for ($j = 0; $j < $i; $j++) {
echo ("<td><a href=index.php&ID=$fileID_id>$filename</a>".
"Name: <strong>$getimage_name</strong>".
"<a href='index.php&ID=$fileID">Delete</a></td>");
}
echo ("</TR>");
}
</snip>
Unless I am misuderstainding what you want to do, you need to reassign
$filename and $fileID_id variables in your loop somewhere using the
mssql_result function. The way the loop is working now they will be the
same throught the entire loop.
Matt Matijevich Guest
-
Rob Adams #6
Re: [PHP] nested for loops
You're getting the data from your query outside of your loop. That needs to
be part of your loop, otherwise, as you've seen, it just displays the first
record. You could try something like this:
$cnt = 0;
while ($row = mysql_fetch_object($result))
{
echo "<td>$row->file_name</td><td>$row->file_id</td>";
$cnt++;
if ($cnt % 2)
echo "</tr><tr>";
}
HTH.
-- Rob
"Micah Montoy" <montmica@isu.edu> wrote in message
news:20030709190208.75714.qmail@pb1.pair.com...am> Well, the for loop is now working but its not displaying what I want. Iam> pulling data from a DB and for every new <td> I would like to get the next
> result and if it reaches more then 2 columns, to start a new row. This Iam> not initally having a problem with. That is the looping. The problem IORDER> running into, is getting the the DB stuff to show. Right now, I only one
> record and it shows for every single one. Here is a snippet of the code.
>
> $result = mssql_query("SELECT * FROM files WHERE file_id = '$cat_name'> BY file_name ASC");
>
> $i = $col_num;
> $j = 0;
>
> $filename= mssql_result($result,0,"file_name");
> $fileID = mssql_result($result,0,"file_id");
>
> for ($k = 0; $k < mssql_num_rows($result); $k++) {
> //changes row color
> if (fmod($k, 2)){$rowColor = "$oddcolor";}else{$rowColor =
> "$evencolor";}
>
> echo ("<TR bgcolor=$rowColor>");
>
> for ($j = 0; $j < $i; $j++) {
> echo ("<td><a href=index.php&ID=$fileID_id>$filename</a>".
> "Name: <strong>$getimage_name</strong>".
> "<a href='index.php&ID=$fileID">Delete</a></td>");
> }
> echo ("</TR>");
> }
>
>
> Anyone see what I need to do to make it loop through and pull out the next
> file name. I thought about a foreach but it never wanted to work with the
> above.
>
> thanks
>
>
> "Matt Matijevich" <matijevich@alliancetechnologies.net> wrote in message
> news:sf0c1185.079@nw0102dsm.alliancetechnologies.n et...>> > your syntax is correct, just need to change for ($j=0: $j < 5; $j++) to
> > for ($j=0; $j < 5; $j++)
> >> > Anyone ever do a nested for loop?> > >>> "Micah Montoy" <montmica@isu.edu> 07/09/03 12:56PM >>>
> >
> > $i =0;
> > $j =0;
> > for ($x=0; $x < 50; $x++){
> > echo ("1 to 50");
> >
> > for ($j=0: $j < 5; $j++) {
> > echo ("less than 5");
> > }
> > }
> >
> >
> >
>
Rob Adams Guest



Reply With Quote

