RE: [PHP] Problem With Inner Loop

2005-08-30 Thread Murray @ PlanetThoughtful
> Hi,
> 
> The following code is attempting to display a list of work types for all
> the
> users in my database. However it only loops through the inner loop once
> and
> I can't work out why, can anyone help here please?
> 
> Thanks for your help
> 
> 
>  include('application.php');
> $staff_qid = mysql_query('SELECT
>   U.User_ID,
>   CONCAT_WS(" ", U.user_Firstname, U.User_Lastname) AS "User_Name"
>   FROM Users U, Allocations A, Projects P
>   WHERE U.User_ID = A.User_ID
>   AND A.Project_ID = P.Project_ID
>   AND P.Project_ID = 38
>   AND (U.User_Type = "Staff"
>OR U.User_Type = "Manager"
>OR U.User_Type = "Administrator")
>   ORDER By User_Name');
> $work_type_qid = mysql_query('SELECT
>Work_Type_ID,
>Work_Type
>FROM Work_Types
>WHERE Project_ID = 38
>ORDER BY Day_Type');
> ?>
> 
> 
>  
>  
>   Work_Type; ?>
>  
>  
> 
> 

Hi Shaun,

Looking at your code, it doesn't appear that there's an actual relationship
between your two queries? Ie the select statement for the 'inner' query
doesn't reference any value returned by the select statement from the
'outer' query.

This means that for every record returned by the outer query, the same
record or records will be returned by the inner query, which doesn't seem to
match the intention of the summary you gave at the top of your email. This
could be an error of interpretation on my part. It may well be that every
record returned by the inner query is relevant to each and every record
returned by the outer query. It's hard to tell from the details you've
included.

Having said which, I'm personally more used to situations where the
following occurs (warning, pseudo-code alert!):

$outerrs = select_query;
while ($outerdata = get_result_from_outerrs){
$innerrs = select_query_using_value_in_where_clause_from_$outerdata;
while ($innerdata = get_result_from_innerrs){
display_information_from_outerdata_and_or_innerdata;
}
}

In the above situation, the inner query is called for each record in the
outer query, indicating a relationship between the two resultsets.

As another thought, have you run the inner select statement against your db
(ie using PHPMyAdmin / MySQL Query Browser / etc) to confirm that under
normal circumstances that query actually returns more than one record?

Regards,

Murray
---
"Lost in thought..."
http://www.planetthoughtful.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Problem With Inner Loop

2005-08-29 Thread Jasper Bryant-Greene

Shaun wrote:

Hi,

The following code is attempting to display a list of work types for all the 
users in my database. However it only loops through the inner loop once and 
I can't work out why, can anyone help here please?






 
 
  Work_Type; ?>
 
 





Well, if Project_ID is a primary key or unique key of some sort, as I 
would assume it was if it's an ID, then the $work_type_qid query will 
only return one row.


And because the query is performed outside the outer while loop, it will 
only execute once, at the start of the request. If you want the same 
SELECT query to be executed over and over (why?) then you would need to 
move the mysql_query() inside the outer while loop.


HTH
--
Jasper Bryant-Greene
Freelance web developer
http://jasper.bryant-greene.name/

If you find my advice useful, please consider donating to a poor
student! You can choose whatever amount you think my advice was
worth to you. http://tinyurl.com/7oa5s

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php