Nested loops help please?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tzuriel

    #1

    Nested loops help please?

    Hello all,

    I think nested loops will do what I want, but I can't seem to get them
    right.

    I have two tables, members and casts. I run a query as follows:

    $sql_query="SEL ECT DISTINCT casts.yearID, members.memberN ame"
    . " FROM members INNER JOIN casts ON members.memberI D =
    casts.memberID"
    . " WHERE (((casts.yearID ) In (SELECT DISTINCT yearID FROM
    casts WHERE memberID='$id') ) AND ((casts.memberI D)<>'$id'));";


    Which gets me the data I want to display using the following php:

    $result=mysql_q uery($sql_query );

    $num=mysql_numr ows($result);

    echo "Cast data for: <b>$castmembe r</b>"; ID:$id<br>";
    $year =mysql_result($ result,$i,"year ID");

    echo " 1st season: <b>$year</b><p>";

    //set up table for $result
    print "<table width=200 border=0>\n";
    print "\t<td><fon t face=arial size=1/><b>Cast Member</font></td>\n";
    print "\t<td><fon t face=arial size=1/><b>Season</font></b></td>\n";

    //loop through $result
    $i=0;
    while ($i < $num) {

    $member=mysql_r esult($result,$ i,"memberName") ;
    $year =mysql_result($ result,$i,"year ID");
    print "<tr>\n";
    print "\t<td><fon t face=arial size=1/>$member</font></td>\n";
    print "\t<td><fon t face=arial size=1/>$year</font></td>\n";
    print "</tr>\n";


    $i++;

    }
    print "</table>\n";
    ?>


    <?php echo "Cast data for: <b>",$castmembe r; ?><br></b>

    However, the data is just one long list in the table with the two
    columns Cast Member and Season. What I want to do is have a separate
    table for each Season so that when yearID changes from, for example,
    1980 to 1981, the rows that show the cast members for 1980 are in one
    table and those members from 1981 are in another.

    Can this be done with nested loops? Do I need another query? Please
    help if you can. I've struggled with this for a couple days and being
    very new, have run out of stuff to try!

    Thanks!

    Tzuriel

  • frothpoker

    #2
    Re: Nested loops help please?

    two queries would be easier but obviously has a bigger overhead.

    You could do the following:

    Set $CurrentYear as the yearID from the first row

    on each time through the loop, check YearID against $CurrentYear and if
    it is not the same then end the table and start a new one before
    setting $CurrentYear to the yearID of the current row.



    tzuriel wrote:
    Hello all,
    >
    I think nested loops will do what I want, but I can't seem to get them
    right.
    >
    I have two tables, members and casts. I run a query as follows:
    >
    $sql_query="SEL ECT DISTINCT casts.yearID, members.memberN ame"
    . " FROM members INNER JOIN casts ON members.memberI D =
    casts.memberID"
    . " WHERE (((casts.yearID ) In (SELECT DISTINCT yearID FROM
    casts WHERE memberID='$id') ) AND ((casts.memberI D)<>'$id'));";
    >
    >
    Which gets me the data I want to display using the following php:
    >
    $result=mysql_q uery($sql_query );
    >
    $num=mysql_numr ows($result);
    >
    echo "Cast data for: <b>$castmembe r</b>"; ID:$id<br>";
    $year =mysql_result($ result,$i,"year ID");
    >
    echo " 1st season: <b>$year</b><p>";
    >
    //set up table for $result
    print "<table width=200 border=0>\n";
    print "\t<td><fon t face=arial size=1/><b>Cast Member</font></td>\n";
    print "\t<td><fon t face=arial size=1/><b>Season</font></b></td>\n";
    >
    //loop through $result
    $i=0;
    while ($i < $num) {
    >
    $member=mysql_r esult($result,$ i,"memberName") ;
    $year =mysql_result($ result,$i,"year ID");
    print "<tr>\n";
    print "\t<td><fon t face=arial size=1/>$member</font></td>\n";
    print "\t<td><fon t face=arial size=1/>$year</font></td>\n";
    print "</tr>\n";
    >
    >
    $i++;
    >
    }
    print "</table>\n";
    ?>
    >
    >
    <?php echo "Cast data for: <b>",$castmembe r; ?><br></b>
    >
    However, the data is just one long list in the table with the two
    columns Cast Member and Season. What I want to do is have a separate
    table for each Season so that when yearID changes from, for example,
    1980 to 1981, the rows that show the cast members for 1980 are in one
    table and those members from 1981 are in another.
    >
    Can this be done with nested loops? Do I need another query? Please
    help if you can. I've struggled with this for a couple days and being
    very new, have run out of stuff to try!
    >
    Thanks!
    >
    Tzuriel

    Comment

    • tzuriel

      #3
      Re: Nested loops help please?

      Thanks for the suggestion. Would you be able to offer some code for
      consideration? I tried something like this at one point but couldn't
      get it to work.

      Thanks,

      Tzuriel
      frothpoker wrote:
      two queries would be easier but obviously has a bigger overhead.
      >
      You could do the following:
      >
      Set $CurrentYear as the yearID from the first row
      >
      on each time through the loop, check YearID against $CurrentYear and if
      it is not the same then end the table and start a new one before
      setting $CurrentYear to the yearID of the current row.
      >
      >
      >

      Comment

      • frothpoker

        #4
        Re: Nested loops help please?

        // your query
        $sql_query="SEL ECT DISTINCT casts.yearID, members.memberN ame"
        . " FROM members INNER JOIN casts ON members.memberI D =
        casts.memberID"
        . " WHERE (((casts.yearID ) In (SELECT DISTINCT yearID FROM
        casts WHERE memberID='$id') ) AND ((casts.memberI D)<>'$id'));";

        $CurrentYear = 0;

        while($result=m ysql_query($sql _query); )
        {
        if ($CurrentYear == $result[1])
        {
        $string = $string .
        "</TABLE><P><TABLE ><TR><TD>$resul t[1]</TD></TR>";
        $CurrentYear = $result[1]; // sets $CurrentYear to the same as
        this record
        } //end if statement
        $string = $string . "<TR><TD>$resul t[2]</TD></TR>";
        } // end while statement

        print $string


        -- code ends --

        Obviously you will need to output table headers before the first record
        and put some checking in so that you don't close the table if this is
        the first record in the loop, but if i did it all for you....

        Please note that I have not checked this for spelling or syntax (i am
        notorious for leaving semi-colons off...)

        Obiron

        tzuriel wrote:
        Thanks for the suggestion. Would you be able to offer some code for
        consideration? I tried something like this at one point but couldn't
        get it to work.
        >
        Thanks,
        >
        Tzuriel
        frothpoker wrote:
        two queries would be easier but obviously has a bigger overhead.

        You could do the following:

        Set $CurrentYear as the yearID from the first row

        on each time through the loop, check YearID against $CurrentYear and if
        it is not the same then end the table and start a new one before
        setting $CurrentYear to the yearID of the current row.

        Comment

        Working...