Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Need Help With Logic

$
0
0

I have read and learned a lot in the last couple of weeks, working on my membership directory listings.  I am close to the final solution, but can't seem to work out the right logic for the final product.  Here is what happens: my sample query selects all members from the 'company' table that match a certain county location or alphabet letter, and uses LEFT JOIN to attach all people associated with the company from the 'contact' table.  The results of the first query are then loaded into an array, called $company_array, for processing by "list functions" that I have created for the formatted output on the screen... If all I had were company and contact information, I would be done - but that's not the case.

 

Some (but not all) companies have one or more branch locations associated with them, and each branch location may have one or more people associated with them.  At some point in my listing process, I have to pause and run a second query to find matching entries from the 'branch' table (using a company_id key) and LEFT JOIN to attach all people associated with the branch from the 'contact' table, and load those results into another array, called $branch_array.  I am successful when running the queries, as I have tested a couple of different ways... but I can't get the final result I want, which I'll describe now.

 

The order of events should be: 1) start listing the first company info; 2) following the company info, list all contact info for the people associated with the company;3) check to see if there are one or more branches associated with the company, and if so, list each branch, followed by each contact person associated with the branch;  4) Repeat the process to go through all of the companies found in the first query.

 

My most recent version of the code is as follows:

// Retrieve all the data from the "company" table
$query = "SELECT * FROM company LEFT JOIN contact ON company.company_id = contact.company_id WHERE comp_county = 'BERNALILLO' ORDER BY company.comp_name, contact.cont_rank"; 

$result = mysql_query($query)
or die(mysql_error());

// Build the $company_array
$company_array = array();
while($row = mysql_fetch_assoc($result)) {
    $company_array[] = $row;
}

$lastCompany = '';
$lastBranch = '';

// Start building the table for showing results
echo "<table border='1' cellpadding='10' cellspacing='0' bgcolor='#f0f0f0' style='margin-left:100px; margin-bottom:20px'>";

// Begin the "foreach" loop for rows in the $company_array

foreach($company_array as $row) {
    // Check if this is a different company than the last one
    if ($lastCompany != $row['company_id']) { 
    // If this is a different company - change the $lastCompany variable   
    $lastCompany = $row['company_id'];
    
    echo "<tr><td><p><b>";
    // List the company info only if it is not the $lastCompany
    listCompany($row);
    }
    
    listContact($row);
    
    }

// Retrieve all the matching data from the "branch" table

$query2 = "SELECT * FROM branch LEFT JOIN contact ON branch.branch_id = contact.branch_id WHERE branch.company_id = '".$row['company_id']."' ORDER BY branch.br_name, contact.cont_rank";
	
$result2 = mysql_query($query2)
or die(mysql_error());

// Check to see if there is a Branch to list
if (mysql_num_rows($result2) != 0); {

// Build the $branch_array
$branch_array = array();
while($row2 = mysql_fetch_assoc($result2)) {
    $branch_array[] = $row2;
}

foreach($branch_array as $row2) {
    if ($lastBranch != $row2['branch_id']) {
	$lastBranch = $row2['branch_id'];
	
	listBranch($row2);
	
    }
    
    listBranchContact($row2);
}

    echo "</td></tr>";
}

echo "</table>";

mysql_close();

The result of running this code can be seen here: http://www.nmlta.org/NewMemDir/testlist2.php which, as you can see, does not get to the branch listing until the last company is listed... I can't seem to figure out where or how to insert my second query, or whether I should use some kind of if/else logic to get it to work the way I need.  Any suggestions will be appreciated... thanks.


Viewing all articles
Browse latest Browse all 13200

Trending Articles