#1
  1. No Profile Picture
    powerrush
    Guest
    Devshed Newbie (0 - 499 posts)
    Hello,

    I'm looping through a query, and inside the while result output, I am trying to loop through a second query (created from the 1st query's row results).

    I have checked that both queries return info.

    I can see that the 1st while result output works fine, but when I get to the inner while result output, it never returns any rows (nothing in the while loop evaluates).

    So, IS it possible to nest one query's output loop within another, or am I doing something wrong?

    I can post the example code, incase my description is confusing.

    TIA,
    Troy
  2. #2
  3. No Profile Picture
    rod k
    Guest
    Devshed Newbie (0 - 499 posts)
    No, it's not confusing, but it's hard to say what might be wrong w/o seeing the code. It is certainly possible to do what you want.

    Rod
  4. #3
  5. No Profile Picture
    powerrush
    Guest
    Devshed Newbie (0 - 499 posts)
    Thanks Rod.

    This page is called with the url variable
    DivisionID equal to 1 (?DivisionID=1).
    When running the script, no SQL or PHP errors occur.

    BTW, I wrote this code from memory since I don't have access to the original at this time, so please ignore any obvious syntax errors:

    <?php

    mysql_connect($host,$user,$password);

    $DivisionList = mysql_db_query($database,"select * from division order by sortvalue");

    if (isset($DivisionID))
    {

    $GetDivisionSection = mysql_db_query($database,"select section.ID, section.URL from section, sectionprop where section.id = sectionprop.sectionid and section.divisionid = $DivisionID and sectionprop.mainpage != 1 and section.void = 0");

    } else {

    $DivisionID = 0;

    }


    while($row = mysql_fetch_array($DivisionList))
    {
    echo $row["ID"];
    echo $row["Graphic"];

    if ($DivisionID == $row["ID"])
    {

    while($row2 = mysql_fetch_array($GetDivisionSection))
    {

    echo $row2["ID"];
    echo $row2["URL"];

    }

    }

    }


    ?>


    - Troy
  6. #4
  7. No Profile Picture
    moderator
    Guest
    Devshed Newbie (0 - 499 posts)
    I only took a quick look at the code, so I'm not sure if it is correct or not. But you may want to put echo mysql_error() after any queries you make to the database to ensure that the queries are correct.....normally PHP won't report MySQL errors.
  8. #5
  9. No Profile Picture
    powerrush
    Guest
    Devshed Newbie (0 - 499 posts)
    I have checked for MySQL error codes, and
    none are returned (error is null, errno is 0). Thanks for your help.
  10. #6
  11. No Profile Picture
    rod k
    Guest
    Devshed Newbie (0 - 499 posts)
    I'm at a loss. I don't see anything in the code that should be causing a problem. Since you say you wrote it from memory, is it possible there is an error in the original that you don't realize is there? I mean your logic is fine, could be something as simple as a missing bracket.

    Rod
  12. #7
  13. No Profile Picture
    powerrush
    Guest
    Devshed Newbie (0 - 499 posts)
    I will post the exact code when I get my hands on it... thanks for all your help!
  14. #8
  15. No Profile Picture
    rod k
    Guest
    Devshed Newbie (0 - 499 posts)
    Just noticed something. You need to alias the results returned in the second query.
    Try this:

    $GetDivisionSection = mysql_db_query($database,"select section.ID as ID, section.URL as URL from section, sectionprop where
    section.id = sectionprop.sectionid and section.divisionid = $DivisionID and sectionprop.mainpage != 1 and section.void
    = 0");
  16. #9
  17. No Profile Picture
    powerrush
    Guest
    Devshed Newbie (0 - 499 posts)
    Well, I tried using the SQL aliases with no luck... here is the exact code I am running:

    -----------

    <?php

    $database = "mydb";

    $cn = mysql_connect( "localhost", "root", $password);
    $db = mysql_select_db($database);

    include "intranet/common/divisionlist.sql";

    if (isset($DivisionID))
    {

    $GetDivisionSections = mysql_query("SELECT section.Name AS Name,
    section.ID AS ID, SectionProp.UseWrapper, section.URL AS URL
    FROM section, SectionProp
    WHERE section.ID = SectionProp.SectionID
    AND section.DivisionID = $DivisionID
    AND SectionProp.MainPage != 1
    AND section.Void = 0");


    } else {

    $DivisionID = 0;

    }

    ?>

    <HTML>
    <HEAD>
    <TITLE></TITLE>
    </HEAD>

    <BODY MARGINWIDTH=0 MARGINHEIGHT=0 TOPMARGIN=0 LEFTMARGIN=0
    BACKGROUND="/intranet/images/leftbg.gif" TEXT="White" LINK="WHITE"
    VLINK="WHITE" ALINK="WHITE">

    <TABLE BORDER=0 CELLSPACING="0" CELLPADDING="0" WIDTH=145>

    <?php

    if ($DivisionList)
    {

    while($row = mysql_fetch_array($DivisionList))
    {

    echo "<TR><TD><A
    HREF='left.php3?DivisionID=" . $row[ "ID"] . "'><IMG
    BORDER=0 SRC=/intranet/images/" . $row[ "Graphic"]
    . "></A></TD></TR>";

    if ($DivisionID == $row[ "ID"])
    {

    echo "<TR>
    <TD><font face='Tahoma, Arial, Helvetica' size='-2'>";

    while($row2 = mysql_fetch_array($GetDivisionSections))
    {

    echo "&nbsp;&nbsp;<A HREF='";

    if ($row2[ "UseWrapper"] == 1)
    {
    echo "wrapper.php3?SectionID=" . $row2[ "ID"] . "'";
    } else {
    echo $row2[ "URL"] . "'";
    }

    echo "TARGET='Main'>" . $row2[ "Name"] . "</A><BR>
    &nbsp;</FONT></TD>
    </TR>";

    }


    }

    }

    }

    ?>

    </TABLE>

    </BODY>
    </HTML>


    -------


    Thanx,
    Troy

Similar Threads

  1. loop question
    By soupman99 in forum PHP Development
    Replies: 13
    Last Post: July 25th, 2003, 08:29 PM

IMN logo majestic logo threadwatch logo seochat tools logo