Re: nested for loops
| From: | Rob Adams | Date: | Wed, 09 Jul 2003 19:17:46 +0000 |
| Subject: | Re: nested for loops | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-154620@lists.php.net to get a copy of this message | ||
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...
> 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.net...
> > your syntax is correct, just need to change for ($j=0: $j < 5; $j++) to
> > for ($j=0; $j < 5; $j++)
> >
> > >>> "Micah Montoy" <montmica@isu.edu> 07/09/03 12:56PM
> > >>> >>>
> > 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");
> > }
> > }
> >
> >
> >
>
>