Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with nested conditional

0 views
Skip to first unread message

kenoli

unread,
Jun 15, 2010, 8:29:17 PM6/15/10
to
I am trying to retrieve a list of enum values for a specific column
(stakeholder_func) in a mysql table and use it to organize data
retrieved from the same table.

The array that holds the enum values is $enum_fields (the code for
retrieving it is not here, but works fine). I am then selecting all
rows and columns from the table. I want to organize the rows in lists
under each enum value according to the value entered for that row in
the column (stakeholder_func) from which the enum values were derived.
Something like this:

enum value [1] ($enum_fields[0])
name 1 with $row['stakeholder_func'] == enum value 1
name 2 with $row['stakeholder_func'] == enum value 1
name 3 with $row['stakeholder_func'] == enum value 1
name 4 with $row['stakeholder_func'] == enum value 1

enum value [2] ($enum_fields[1])
name 1 with $row['stakeholder_func'] == enum value 2
name 2 with $row['stakeholder_func'] == enum value 2
name 3 with $row['stakeholder_func'] == enum value 2

etc.

Here's my code:

$query = "SELECT * FROM tbl_pub_mtg ORDER BY stakeholder_func";
$result = $db->query($query) or die('Initial query screwed
up'); //
From a DB class I created that has already been instantiated as $db.

foreach ($enum_fields as $enum_value) { // The
$enum_fields array
contains the enum values for the stakeholder_func column.

echo "$enum_value<br/>";

while ($row = mysql_fetch_array
($result, MYSQL_ASSOC)) {

if( $row['stakeholder_func']
== $enum_value ) {
echo "&nbsp;&nbsp;&nbsp;&nbsp;
{$row['first_name']}<br/>";
} // End if . . .

} //End while . . .

echo "<br/><br/>";

} // End foreach . . .

What I get is the following. The "if . . ." conditional fires on the
first "while" but not the rest. What am I doing wrong?
Incidentally, there is data in the database that would return rows
that should show up under the other stakeholder categories.

Business
Kenoli
Henry

Education

Gardens

Sports

Neighborhood Groups

Thanks,

--Kenoli

Kenoli Oleari
Institute of the Commons
510-717-1706
kenol...@gmail.com
www.iotc-hub.org

Jerry Stuckle

unread,
Jun 15, 2010, 8:41:43 PM6/15/10
to

First of all, do NOT do a SELECT *. Itemize the exact columns you want.
If there is ever a change in your database later, the SELECT * could
break (best case) or cause problems much later in the code (much harder
to locate).

Your problem is you are retrieving the information from the database
(mysql_fetch_array()) within your foreach() loop. The first time
through your foreach() loop will exhaust all of the items in the result set.

A much better way would be to get the proper sequence in the SQL
request. Ask in comp.databases.mysql for help in doing it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

0 new messages