Jump to content

spock9458

Members
  • Posts

    65
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

spock9458's Achievements

Member

Member (2/5)

0

Reputation

  1. I have a rather complicated event registration form that uses javascript to show or hide certain tables depending on radio button selections entered by users. There are basically three types of registrations: Member, Exhibitor and Partial. The script I'm using works fine to show only certain additional tables for the type of registration selected. Here is the successful script: <script> var radio; var attendValue = document.getElementsByName("rad_attend").value; function checkAttend(attendValue) { switch(attendValue) { case "1": document.getElementById("hideshow1").style.display = 'block'; document.getElementById("hideshow2").style.display = 'none'; document.getElementById("hideshow3").style.display = 'none'; break; case "2": document.getElementById("hideshow2").style.display = 'block'; document.getElementById("hideshow1").style.display = 'none'; document.getElementById("hideshow3").style.display = 'none'; break; case "3": document.getElementById("hideshow3").style.display = 'block'; document.getElementById("hideshow1").style.display = 'none'; document.getElementById("hideshow2").style.display = 'none'; break; default: document.getElementById("hideshow1").style.display = 'none'; document.getElementById("hideshow2").style.display = 'none'; document.getElementById("hideshow3").style.display = 'none'; } } </script> The script is "fired" with these radios: <td align='right'><input type='radio' name='rad_attend' value='1' onchange='UpdateTotal(true); checkAttend(this.value);' /></td> <td align='right'><input type='radio' name='rad_attend' value='2' onchange='UpdateTotal(true); checkAttend(this.value);' /></td> <td align='right'><input type='radio' name='rad_attend' value='3' onchange='UpdateTotal(true); checkAttend(this.value);' /></td> Like I said this script and functionality are all working properly. Then they asked me to add the ability for a Member registration to include additional guests, so I created three additional tables for collecting the additional information, and I am trying to use the same type of script as above in order to show either 1, 2, or 3 of the additional tables depending on the radio button selection chosen by the User. Here is the javascript I am using for this function: <script> var radio; var numAdditional = document.getElementsByName("rad_number_addl_guests").value; function checkAdditional(numAdditional) { switch(numAdditional) { case "1": document.getElementById("additionalguest1").style.display = 'block'; document.getElementById("additionalguest2").style.display = 'none'; document.getElementById("additionalguest3").style.display = 'none'; break; case "2": document.getElementById("additionalguest1").style.display = 'block'; document.getElementById("additionalguest2").style.display = 'block'; document.getElementById("additionalguest3").style.display = 'none'; break; case "3": document.getElementById("additionalguest1").style.display = 'block'; document.getElementById("additionalguest2").style.display = 'block'; document.getElementById("additionalguest2").style.display = 'block'; break; default: document.getElementById("additionalguest1").style.display = 'none'; document.getElementById("additionalguest2").style.display = 'none'; document.getElementById("additionalguest3").style.display = 'none'; } } </script> And here are the radio buttons that are supposed to fire that script: <td colspan='3'> <span class='olr-label'>How many additional guests are you brining?: <input type='radio' name='rad_number_addl_guests' value='1' onchange='checkAdditional(this.value);' /> 1 <input type='radio' name='rad_number_addl_guests' value='2' onchange='checkAdditional(this.value);' /> 2 <input type='radio' name='rad_number_addl_guests' value='3' onchange='checkAdditional(this.value);' /> 3 </span> </td> This script is not working at all. Nothing happens when either of the buttons are selected, and I must be missing something simple because it works in one situation but not in the other. I am hoping another set of eyes with more experience coding can help me figure out what is wrong with my second script. Any clues would be appreciated. Thanks.
  2. OK, here are some more details: The existing function used for the "onchange" assigned to the radio buttons is: onchange = 'UpdateTotal(True);' Here is the script code I am trying so far: <script> function checkRadios(rad_attend) { switch(rad_attend) { case 1: document.getElementById("hideshow1").style.display = 'block'; break; case 2: document.getElementById("hideshow2").style.display = 'block'; break; case 3: document.getElementById("hideshow3").style.display = 'block'; default: document.getElementById("hideshow1, hideshow2, hideshow3").style.display = 'none'; } } </script> And the code (partial) in the body of the HTML is here: <legend> Attendance Options </legend> <table border='0' cellspacing='0' cellpadding='4'> <tbody> <tr> <th align='left'>Option</th> <th align='left'>Price </th> <th align='right'>Select</th> </tr> <tr> <td>Member Registration</td> <td align='left'>$<span id='aprice1'>450</span></td> <td align='right'><input type='radio' name='rad_attend' value='1' onchange='UpdateTotal(true); checkRadios(rad_attend);' /></td> </tr> <tr id="hideshow1" style="display:none;"> <td>Member - Golf Tournament</td> <td align='left'>$<span id='mprice6'>150</span></td> <td align='right'><input type='checkbox' value='' name='chk_mqty6' onchange='UpdateTotal(true);' /></td> </tr> <tr id="hideshow1" style="display:none;"> <td>Member - Wine and Watercolor</td> <td align='left'>$<span id='mprice8'>45</span></td> <td align='right'><input type='checkbox' value='' name='chk_mqty8' onchange='UpdateTotal(true);' /></td> </tr> <tr> <td>Exhibitor Registration</td> <td align='left'>$<span id='aprice2'>1000</span></td> <td align='right'><input type='radio' name='rad_attend' value='2' onchange='UpdateTotal(true);' /></td> </tr> <tr> <td>Guest Only</td> <td align='left'>$<span id='aprice3'>0</span></td> <td align='right'><input type='radio' name='rad_attend' value='3' onchange='UpdateTotal(true);' /></td> </tr> On loading the page, the behavior is fine: None of the radio buttons are selected, and the rows with the id="hideshow1" are hidden. However, when I click on the Member radio button (value=1) nothing happens to the table rows that are supposed to be revealed. Please note that my code is not complete - I plan on adding additional "hidden" rows with id="hideshow2" and "hideshow3" for the other registration options, I just want to get the "reveal" function working first. I feel like I'm missing something pretty basic, so please advise where I'm going wrong. Thanks again.
  3. I am pretty new to Javascript, but I think it is what I need to build the functions I want into my form. I am working with a registration system that was written by someone else, that works for my needs but I want to add logic to it. The system creates an HTML page with all of the form elements I need, in a table format. Basically there are three options for my registrants to select from, in the form of radio buttons. The name of the radio button set is "rad_attend", and the available values are "1", "2" or "3". What I want to do is have the optional table rows associated with each "attend" value to be hidden initially, and only appear when the radio button is selected. In the code there is another function tied to the "onchange" condition of the radio button - it adds the registration price to a running total calculated by the existing script. So, I need a way to have the "onchange" condition fire the additional function of showing the related table rows that I want available when that radio button is selected. I think it is possible to fire two separate functions with the "onchange" condition of the radio button, but I'm getting confused as to how to handle the three options I want to "reveal" depending on the choice of the attendance buttons. To illustrate, let's call my attendance options: "Member", "Exhibitor", and "Guest". When the page loads initially, no option is selected. When the registrant chooses "Member", I want certain table rows to be displayed - if possible using the "onchange" condition. Likewise, I want different table rows to be displayed when "Exhibitor" or "Guest" radios are selected. And, if someone selects "Member" and then changes to "Exhibitor", I need the Member optional rows to disappear ("display:none") and the Exhibitor optional rows to appear ("display:block"). So it seems like some kind of "toggle" function would work, according to the different Element IDs that I can assign to the various associated table rows, but I have not been successful in finding something that works with the three different options. If anyone has any suggestions, I would really appreciate the help. Thanks
  4. Thanks everyone for your help and suggestions. I will take them to heart, and probably start over from the beginning with the newer version after studying a while. I appreciate you all trying to help me improve. Thanks!
  5. I have to admit, I am a PHP NOVICE, and don't get that much practice. A few years ago I created some code that worked for my purpose, but now my server has upgraded PHP to version 5.3, and the old code won't work at all. Here is the old code that used to work: <?php // Connection to DB mysql_connect("server", "user", "password") or die('Cannot connect to the database because: ' . mysql_error()); mysql_select_db ("database"); // Retrieve all Region 6 Legislators from the "contact_info" table $result = mysql_query("SELECT * FROM contact_info WHERE branch = 'House' AND district = '11' ORDER BY district") or die(mysql_error()); // Start building the table for showing results using "while" loop echo "<table width='75%' border='0' cellpadding='10' cellspacing='0' bgcolor='#f0f0f0' style='margin-left:100px; margin-bottom:20px'>"; // Here is the "while" loop while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table row echo "<tr valign='top'><td width='45%'><b>"; echo "House District: </b><font color='red'>",$row['district'],"</font><br>"; echo $row['first_name']," ",$row['last_name'],"<br>"; echo $row['address'],"<br>"; echo $row['csz'],"</td>"; echo "<td width='55%'><b>County(ies): </b><font color='red'>",$row['county'],"</font><br>"; echo "Capitol Phone: <font color='green'>",$row['cap_phone'],"</font><br>"; echo "Office Phone: <font color='green'>",$row['bus_phone'],"</font><br>"; echo "Home Phone: <font color='green'>",$row['home_phone'],"</font><br>"; echo "Email: <a href='mailto:",$row['email'],"'>",$row['email'],"</a></td></tr>"; } echo "</table>"; ?> So, I've been Googling and trying to adapt my code to the new version using some other scripts that I have updated, but I'm not having any luck at all. Here is the modified code I'm trying to use now, which does NOTHING. I have even tried to get it to print out the number of rows in my result, but it prints NOTHING. <?php // Connection to DB $mysqli = new mysqli("localhost", "user", "password", "database"); if($mysqli->connect_errno > 0){ die('Unable to connect to database [' . $db->connect_error . ']'); } // Retrieve all matching legislators $query = 'SELECT * FROM contact_info WHERE branch = 'House' AND district = '10' ORDER BY district'; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); /* determine number of rows result set */ $row_cnt = mysqli_num_rows($result); echo "There are ",$row_cnt," rows in the result."; // Start building the table for showing results using "while" loop echo "<table width='75%' border='0' cellpadding='10' cellspacing='0' bgcolor='#f0f0f0' style='margin-left:100px; margin-bottom:20px'>"; // Here is the "while" loop while($row = $result->fetch_array(MYSQLI_ASSOC)) { // Print out the contents of each row into a table row echo "<tr valign='top'><td width='45%'><b>"; echo "House District: </b><font color='red'>",$row['district'],"</font><br>"; echo $row['first_name']," ",$row['last_name'],"<br>"; echo $row['address'],"<br>"; echo $row['csz'],"</td>"; echo "<td width='55%'><b>County(ies): </b><font color='red'>",$row['county'],"</font><br>"; echo "Capitol Phone: <font color='green'>",$row['cap_phone'],"</font><br>"; echo "Office Phone: <font color='green'>",$row['bus_phone'],"</font><br>"; echo "Home Phone: <font color='green'>",$row['home_phone'],"</font><br>"; echo "Email: <a href='mailto:",$row['email'],"'>",$row['email'],"</a></td></tr>"; } echo "</table>"; ?> I have a feeling that I'm missing a thing or two, or something I have in there is not correct. Any help will be so greatly appreciated. Thanks!
  6. @Barand - That works! You have the best answer for Bonus Question...
  7. @kicken - Right on the money!! I thought I had tried that but I must not have had it quite right. Thanks for sorting it out for me. Bonus Question.... Is there a way to direct the output of my code to a Word doc, so that it keeps my formatting and everything? I have tried "copy and paste" from my browser, but the indentations in the output (which I set using <span> tags) don't stick, along with a couple of other spacing quirks. I have it looking really good here: http://www.nmlta.org/MemberDirectory-New/all_agent_list1.php Thanks to all for help!
  8. I have apparently solved the problem... I have replaced my "if" conditional with an "if/else" conditional. On the "if" conditional, if the company being processed is in a different County than the last company, the header row of the table is printed, the $current_county variable is updated, and then the company information is listed. for the "else" conditional, the header is omitted, but I included the entire code again that updates the $current_county variable and lists the company information. This correct output can now be seen here: http://www.nmlta.org/MemberDirectory-New/all_agent_list-test5.php Unless someone has a shorter, more preferable solution, I'll just go with this one... Thanks again.
  9. I apologize for the three sets of code, but this one is really close... It appears to be checking for the County change at the right moment, and the heading is changed at the right record, but it is still only printing the first company information in each County... you can check the results here: http://www.nmlta.org/MemberDirectory-New/all_agent_list-test4.php And the new code is here: // Retrieve ALL the "company" specific data (including contacts) $query = "SELECT company.*, co_contact.cont_name, co_contact.cont_title, co_contact.cont_email FROM company LEFT JOIN co_contact ON company.company_id = co_contact.company_id WHERE company.comp_type = 'Agent' ORDER BY company.comp_county, company.comp_name, co_contact.cont_rank"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); // Build the $company_array $company_array = array(); while($row = $result->fetch_assoc()) { if(!isset($company_array[$row['company_id']])) { //Add the company specific data here $company_array[$row['company_id']] = array( 'name' => $row['comp_name'], 'uw' => $row['comp_uw'], 'street' => $row['comp_street'], 'pobox' => $row['comp_pobox'], 'csz' => $row['comp_csz'], 'phone' => $row['comp_ph'], 'fax' => $row['comp_fx'], 'web' => $row['comp_web'], 'email' => $row['comp_email'], 'county' => $row['comp_county'], 'branches' => array(), 'br_contacts' => array() ); } //Add the company contact info here $company_array[$row['company_id']]['co_contacts'][] = array( 'name' => $row['cont_name'], 'title' => $row['cont_title'], 'email' => $row['cont_email'] ); } //Run query to get branch info - for ALL companies $query = "SELECT branch.*, br_contact.cont_name, br_contact.cont_title, br_contact.cont_email FROM branch JOIN company ON branch.company_id = company.company_id LEFT JOIN br_contact ON branch.branch_id = br_contact.branch_id WHERE company.comp_type = 'Agent' ORDER BY company.comp_county, branch.br_street, br_contact.cont_rank"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); //Add branch (and branch contacts) results to $company_array while($row = $result->fetch_assoc()) { $compID = $row['company_id']; $branchID = $row['branch_id']; if(!isset($company_array[$compID]['branches'][$row['branch_id']])) { $company_array[$compID]['branches'][$branchID] = array( 'branch_name' => $row['branch_name'], 'street' => $row['br_street'], 'csz' => $row['br_csz'], 'phone' => $row['br_ph'], 'fax' => $row['br_fx'] ); } $company_array[$compID]['branches'][$branchID]['br_contacts'][] = array( 'name' => $row['cont_name'], 'title' => $row['cont_title'], 'email' => $row['cont_email'] ); } echo "<span class='heading'>"; echo "2014-15 Agent Members of the New Mexico Land Title Association<br><br>"; echo "</span>"; $current_county = 'xxx'; // Start building the table for showing results echo "<table border='1' cellpadding='10' cellspacing='0' style='width:750px; margin-left:100px; margin-bottom:20px'>"; // Begin the "foreach" loop for rows in the $company_array foreach($company_array as $company) { echo "<font color='red'>Checking County Match</font><br>\n"; // Enter County IF Statement Here if ($company['county'] != $current_county) { echo "<tr><td><b><font class='county'>"; echo $company['county']," County</font><br><hr></b></td></tr>"; echo "<tr><td>\n"; $current_county = $company['county']; echo "This company is in $current_county County<br>\n"; echo "<b>{$company['name']}</b><br>\n"; if(!empty($company['uw'])) { echo "<b>{$company['uw']}</b><br>\n";} if(!empty($company['street'])) { echo "{$company['street']}<br>\n";} if(!empty($company['pobox'])) { echo "{$company['pobox']}<br>\n";} echo "{$company['csz']}<br>\n"; echo "{$company['phone']} Fax: {$company['fax']}<br>\n"; if(!empty($company['web'])) { echo "<a href='http://{$company['web']}' target='_blank'>{$company['web']}</a><br>\n";} if(!empty($company['email'])) { echo "Email: <a href='mailto:{$company['email']}'>{$company['email']}</a><br>\n";} // if(!empty($company['county'])) { // echo "Serving <font color='green'>{$company['county']} </font>County<br>\n";} echo "<br>"; foreach($company['co_contacts'] as $co_contact) { if(!empty($co_contact['name'])) { echo "&nbsp&nbsp&nbsp <b>{$co_contact['name']}</b>, {$co_contact['title']}<br>\n";} if(!empty($co_contact['email'])) { echo "&nbsp&nbsp&nbsp Email: <a href='mailto:{$co_contact['email']}'>{$co_contact['email']}</a><br>\n";} echo "<br>"; } foreach($company['branches'] as $branch) { echo "&nbsp&nbsp&nbsp <b>{$branch['branch_name']}</b><br>\n"; echo "&nbsp&nbsp&nbsp {$branch['street']}<br>\n"; echo "&nbsp&nbsp&nbsp {$branch['csz']}<br>\n"; echo "&nbsp&nbsp&nbsp {$branch['phone']} Fax: {$branch['fax']}><br>\n"; if(!empty($branch['email'])) { echo "&nbsp&nbsp&nbsp Email: <a href='mailto:{$branch['email']}'>{$branch['email']}</a><br>\n";} echo "<br>"; foreach($branch['br_contacts'] as $br_contact) { if(!empty($br_contact['name'])) { echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <b>{$br_contact['name']}</b>, {$br_contact['title']}<br>\n";} if(!empty($br_contact['email'])) { echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Email: <a href='mailto:{$br_contact['email']}'>{$br_contact['email']}</a><br>\n";} echo "<br>"; } } } } echo "</td></tr>\n"; echo "</table>"; ?> I think the only change I made was the location of the closing brace(s)... again - I appreciate the help.
  10. This is probably a better attempt, it does change the heading, but only one company prints out for each County - it does not print all of the companies in each County: // Retrieve ALL the "company" specific data (including contacts) $query = "SELECT company.*, co_contact.cont_name, co_contact.cont_title, co_contact.cont_email FROM company LEFT JOIN co_contact ON company.company_id = co_contact.company_id WHERE company.comp_type = 'Agent' ORDER BY company.comp_county, company.comp_name, co_contact.cont_rank"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); // Build the $company_array $company_array = array(); while($row = $result->fetch_assoc()) { if(!isset($company_array[$row['company_id']])) { //Add the company specific data here $company_array[$row['company_id']] = array( 'name' => $row['comp_name'], 'uw' => $row['comp_uw'], 'street' => $row['comp_street'], 'pobox' => $row['comp_pobox'], 'csz' => $row['comp_csz'], 'phone' => $row['comp_ph'], 'fax' => $row['comp_fx'], 'web' => $row['comp_web'], 'email' => $row['comp_email'], 'county' => $row['comp_county'], 'branches' => array(), 'br_contacts' => array() ); } //Add the company contact info here $company_array[$row['company_id']]['co_contacts'][] = array( 'name' => $row['cont_name'], 'title' => $row['cont_title'], 'email' => $row['cont_email'] ); } //Run query to get branch info - for ALL companies $query = "SELECT branch.*, br_contact.cont_name, br_contact.cont_title, br_contact.cont_email FROM branch JOIN company ON branch.company_id = company.company_id LEFT JOIN br_contact ON branch.branch_id = br_contact.branch_id WHERE company.comp_type = 'Agent' ORDER BY company.comp_county, branch.br_street, br_contact.cont_rank"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); //Add branch (and branch contacts) results to $company_array while($row = $result->fetch_assoc()) { $compID = $row['company_id']; $branchID = $row['branch_id']; if(!isset($company_array[$compID]['branches'][$row['branch_id']])) { $company_array[$compID]['branches'][$branchID] = array( 'branch_name' => $row['branch_name'], 'street' => $row['br_street'], 'csz' => $row['br_csz'], 'phone' => $row['br_ph'], 'fax' => $row['br_fx'] ); } $company_array[$compID]['branches'][$branchID]['br_contacts'][] = array( 'name' => $row['cont_name'], 'title' => $row['cont_title'], 'email' => $row['cont_email'] ); } echo "<span class='heading'>"; echo "2014-15 Agent Members of the New Mexico Land Title Association<br><br>"; echo "</span>"; $current_county = 'xxx'; // Start building the table for showing results echo "<table border='1' cellpadding='10' cellspacing='0' style='width:750px; margin-left:100px; margin-bottom:20px'>"; // Begin the "foreach" loop for rows in the $company_array foreach($company_array as $company) { echo "<font color='red'>Checking County Match</font><br>\n"; // Enter County IF Statement Here if ($company['county'] != $current_county) { echo "<tr><td><b><font class='county'>"; echo $company['county']," County</font><br><hr></b></td></tr>"; echo "<tr><td>\n"; $current_county = $company['county']; echo "This company is in $current_county County<br>\n"; echo "<b>{$company['name']}</b><br>\n"; if(!empty($company['uw'])) { echo "<b>{$company['uw']}</b><br>\n";} if(!empty($company['street'])) { echo "{$company['street']}<br>\n";} if(!empty($company['pobox'])) { echo "{$company['pobox']}<br>\n";} echo "{$company['csz']}<br>\n"; echo "{$company['phone']} Fax: {$company['fax']}<br>\n"; if(!empty($company['web'])) { echo "<a href='http://{$company['web']}' target='_blank'>{$company['web']}</a><br>\n";} if(!empty($company['email'])) { echo "Email: <a href='mailto:{$company['email']}'>{$company['email']}</a><br>\n";} // if(!empty($company['county'])) { // echo "Serving <font color='green'>{$company['county']} </font>County<br>\n";} echo "<br>"; foreach($company['co_contacts'] as $co_contact) { if(!empty($co_contact['name'])) { echo "&nbsp&nbsp&nbsp <b>{$co_contact['name']}</b>, {$co_contact['title']}<br>\n";} if(!empty($co_contact['email'])) { echo "&nbsp&nbsp&nbsp Email: <a href='mailto:{$co_contact['email']}'>{$co_contact['email']}</a><br>\n";} echo "<br>"; } foreach($company['branches'] as $branch) { echo "&nbsp&nbsp&nbsp <b>{$branch['branch_name']}</b><br>\n"; echo "&nbsp&nbsp&nbsp {$branch['street']}<br>\n"; echo "&nbsp&nbsp&nbsp {$branch['csz']}<br>\n"; echo "&nbsp&nbsp&nbsp {$branch['phone']} Fax: {$branch['fax']}><br>\n"; if(!empty($branch['email'])) { echo "&nbsp&nbsp&nbsp Email: <a href='mailto:{$branch['email']}'>{$branch['email']}</a><br>\n";} echo "<br>"; foreach($branch['br_contacts'] as $br_contact) { if(!empty($br_contact['name'])) { echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <b>{$br_contact['name']}</b>, {$br_contact['title']}<br>\n";} if(!empty($br_contact['email'])) { echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Email: <a href='mailto:{$br_contact['email']}'>{$br_contact['email']}</a><br>\n";} echo "<br>"; } } } echo "</td></tr>\n"; } echo "</table>"; I know I'm getting close, but I've been trying for the entire weekend, so I'm hoping someone can spot my error quicker... thanks again. Forgot to add - the results of this code are here: http://www.nmlta.org/MemberDirectory-New/all_agent_list-test3.php
  11. I have to give credit to user "Psycho" here, who came up with the code I'm using for my Membership Directory. I have even completed all of the code that will allow companies to update their own information in the database when they need to. The last problem I'm having is with my "master" list of members. It is all working with the exception of - I can't get the County heading to show up when the actual County location in the database record is different from the last company. I get the heading properly on the first Company, but after that, even though I know the County data is changing, because I'm echoing out the data as it goes (for testing), and I'm just baffled - I have tried every placement of the IF conditional that I can think of. The current code results can be seen here: http://www.nmlta.org/MemberDirectory-New/all_agent_list-test2.php I'm sure there is a big clue because my flag that says "Checking County Match" is all occurring before the companies even print out, but I can't find the right change to make in my code, which follows: // Retrieve ALL the "company" specific data (including contacts) $query = "SELECT company.*, co_contact.cont_name, co_contact.cont_title, co_contact.cont_email FROM company LEFT JOIN co_contact ON company.company_id = co_contact.company_id WHERE company.comp_type = 'Agent' ORDER BY company.comp_county, company.comp_name, co_contact.cont_rank"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); // Build the $company_array $company_array = array(); while($row = $result->fetch_assoc()) { if(!isset($company_array[$row['company_id']])) { //Add the company specific data here $company_array[$row['company_id']] = array( 'name' => $row['comp_name'], 'uw' => $row['comp_uw'], 'street' => $row['comp_street'], 'pobox' => $row['comp_pobox'], 'csz' => $row['comp_csz'], 'phone' => $row['comp_ph'], 'fax' => $row['comp_fx'], 'web' => $row['comp_web'], 'email' => $row['comp_email'], 'county' => $row['comp_county'], 'branches' => array(), 'br_contacts' => array() ); } //Add the company contact info here $company_array[$row['company_id']]['co_contacts'][] = array( 'name' => $row['cont_name'], 'title' => $row['cont_title'], 'email' => $row['cont_email'] ); } //Run query to get branch info - for ALL companies $query = "SELECT branch.*, br_contact.cont_name, br_contact.cont_title, br_contact.cont_email FROM branch JOIN company ON branch.company_id = company.company_id LEFT JOIN br_contact ON branch.branch_id = br_contact.branch_id WHERE company.comp_type = 'Agent' ORDER BY company.comp_county, branch.br_street, br_contact.cont_rank"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); //Add branch (and branch contacts) results to $company_array while($row = $result->fetch_assoc()) { $compID = $row['company_id']; $branchID = $row['branch_id']; if(!isset($company_array[$compID]['branches'][$row['branch_id']])) { $company_array[$compID]['branches'][$branchID] = array( 'branch_name' => $row['branch_name'], 'street' => $row['br_street'], 'csz' => $row['br_csz'], 'phone' => $row['br_ph'], 'fax' => $row['br_fx'] ); } $company_array[$compID]['branches'][$branchID]['br_contacts'][] = array( 'name' => $row['cont_name'], 'title' => $row['cont_title'], 'email' => $row['cont_email'] ); } echo "<span class='heading'>"; echo "2014-15 Agent Members of the New Mexico Land Title Association<br><br>"; echo "</span>"; $current_county = 'xxx'; // Start building the table for showing results echo "<table border='1' cellpadding='10' cellspacing='0' style='width:750px; margin-left:100px; margin-bottom:20px'>"; // Begin the "foreach" loop to check for County change foreach($company_array as $company) { echo "<font color='red'>Checking County Match</font><br>\n"; // Enter County IF Statement Here if ($company['county'] != $current_county) { echo "<tr><td><b><font class='county'>"; echo $company['county']," County</font><br><hr></b></td></tr>"; } // Begin the "foreach" loop for rows in the $company_array foreach($company_array as $company) { echo "<tr><td>\n"; $current_county = $company['county']; echo "This company is in $current_county County<br>\n"; echo "<b>{$company['name']}</b><br>\n"; if(!empty($company['uw'])) { echo "<b>{$company['uw']}</b><br>\n";} if(!empty($company['street'])) { echo "{$company['street']}<br>\n";} if(!empty($company['pobox'])) { echo "{$company['pobox']}<br>\n";} echo "{$company['csz']}<br>\n"; echo "{$company['phone']} Fax: {$company['fax']}<br>\n"; if(!empty($company['web'])) { echo "<a href='http://{$company['web']}' target='_blank'>{$company['web']}</a><br>\n";} if(!empty($company['email'])) { echo "Email: <a href='mailto:{$company['email']}'>{$company['email']}</a><br>\n";} // if(!empty($company['county'])) { // echo "Serving <font color='green'>{$company['county']} </font>County<br>\n";} echo "<br>"; foreach($company['co_contacts'] as $co_contact) { if(!empty($co_contact['name'])) { echo "&nbsp&nbsp&nbsp <b>{$co_contact['name']}</b>, {$co_contact['title']}<br>\n";} if(!empty($co_contact['email'])) { echo "&nbsp&nbsp&nbsp Email: <a href='mailto:{$co_contact['email']}'>{$co_contact['email']}</a><br>\n";} echo "<br>"; } foreach($company['branches'] as $branch) { echo "&nbsp&nbsp&nbsp <b>{$branch['branch_name']}</b><br>\n"; echo "&nbsp&nbsp&nbsp {$branch['street']}<br>\n"; echo "&nbsp&nbsp&nbsp {$branch['csz']}<br>\n"; echo "&nbsp&nbsp&nbsp {$branch['phone']} Fax: {$branch['fax']}><br>\n"; if(!empty($branch['email'])) { echo "&nbsp&nbsp&nbsp Email: <a href='mailto:{$branch['email']}'>{$branch['email']}</a><br>\n";} echo "<br>"; foreach($branch['br_contacts'] as $br_contact) { if(!empty($br_contact['name'])) { echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <b>{$br_contact['name']}</b>, {$br_contact['title']}<br>\n";} if(!empty($br_contact['email'])) { echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Email: <a href='mailto:{$br_contact['email']}'>{$br_contact['email']}</a><br>\n";} echo "<br>"; } } } echo "</td></tr>\n"; } echo "</table>"; ?> I will greatly appreciate someone pointing out my mistake, which I'm sure is obvious to code gurus - I am just baffled. Thanks!
  12. That does it - perfectly! Thanks again for all your help...
  13. @ch0cu3r - Thank you so much for your patience and explanations. The code is indeed working, and when I changed the queries to select only the three fields from both co_contact and br_contact tables, and did a little more formatting of the output, it looks like this: http://www.nmlta.org/NewMemDir/bern_psycho-test2.php There is one caveat, though - there is a Warning after the first company listing, I assume that is because there is no branch associated with the first company. Is there any way to get rid of that warning? Here is the complete working code now: // Retrieve ALL the "company" specific data (including contacts) $query = "SELECT company.*, co_contact.cont_name, co_contact.cont_title, co_contact.cont_email FROM company LEFT JOIN co_contact ON company.company_id = co_contact.company_id WHERE comp_county = 'BERNALILLO' ORDER BY company.comp_name, co_contact.cont_rank"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); // Build the $company_array $company_array = array(); while($row = $result->fetch_assoc()) { if(!isset($company_array[$row['company_id']])) { //Add the company specific data here $company_array[$row['company_id']] = array( 'name' => $row['comp_name'], 'street' => $row['comp_street'], 'csz' => $row['comp_csz'], 'phone' => $row['comp_ph'], 'fax' => $row['comp_fx'], 'web' => $row['comp_web'], 'email' => $row['comp_email'], //'branches' => array(), //'br_contacts' => array() ); } //Add the company contact info here $company_array[$row['company_id']]['co_contacts'][] = array( 'name' => $row['cont_name'], 'title' => $row['cont_title'], 'email' => $row['cont_email'] ); } //Run query to get branch info - for ALL companies $query = "SELECT branch.*, br_contact.cont_name, br_contact.cont_title, br_contact.cont_email FROM branch JOIN company ON branch.company_id = company.company_id LEFT JOIN br_contact ON branch.branch_id = br_contact.branch_id WHERE company.comp_county = 'BERNALILLO' ORDER BY company.comp_name, branch.br_street, br_contact.cont_rank"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); //Add branch (and branch contacts) results to $company_array while($row = $result->fetch_assoc()) { $compID = $row['company_id']; $branchID = $row['branch_id']; if(!isset($company_array[$compID]['branches'][$row['branch_id']])) { $company_array[$compID]['branches'][$branchID] = array( 'branch_name' => $row['branch_name'], 'street' => $row['br_street'], 'csz' => $row['br_csz'], 'phone' => $row['br_ph'], 'fax' => $row['br_fx'] ); } $company_array[$compID]['branches'][$branchID]['br_contacts'][] = array( 'name' => $row['cont_name'], 'title' => $row['cont_title'], 'email' => $row['cont_email'] ); } // 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 $company) { echo "<tr><td>\n"; echo "<b>{$company['name']}</b><br>\n"; echo "{$company['street']}<br>\n"; echo "{$company['csz']}<br>\n"; echo "{$company['phone']} Fax: {$company['fax']}<br>\n"; if(!empty($company['web'])) { echo "{$company['web']}<br>\n";} if(!empty($company['email'])) { echo "Email: <a href='mailto:{$company['email']}'>{$company['email']}</a><br><br>\n";} foreach($company['co_contacts'] as $co_contact) { echo "&nbsp&nbsp&nbsp <b>{$co_contact['name']}</b>, {$co_contact['title']}<br>\n"; if(!empty($co_contact['email'])) { echo "&nbsp&nbsp&nbsp Email: <a href='mailto:{$co_contact['email']}'>{$co_contact['email']}</a><br>\n";} echo "<br>"; } foreach($company['branches'] as $branch) { echo "&nbsp&nbsp&nbsp <b>{$branch['branch_name']}</b><br>\n"; echo "&nbsp&nbsp&nbsp {$branch['street']}<br>\n"; echo "&nbsp&nbsp&nbsp {$branch['csz']}<br>\n"; echo "&nbsp&nbsp&nbsp {$branch['phone']} Fax: {$branch['fax']}><br>\n"; if(!empty($branch['email'])) { echo "&nbsp&nbsp&nbsp Email: <a href='mailto:{$branch['email']}'>{$branch['email']}</a><br>\n";} echo "<br>"; foreach($branch['br_contacts'] as $br_contact) { if(!empty($br_contact['name'])) { echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <b>{$br_contact['name']}</b>, {$br_contact['title']}<br>\n";} if(!empty($br_contact['email'])) { echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Email: <a href='mailto:{$br_contact['email']}'>{$br_contact['email']}</a><br>\n";} echo "<br>"; } } echo "</td></tr>\n"; } echo "</table>";
  14. OK, I'm working on this code, and it's getting confusing because I've changed my data structure a bit. I have it partially working with this new code: // Retrieve ALL the "company" specific data (including contacts) $query = "SELECT * FROM company LEFT JOIN co_contact ON company.company_id = co_contact.company_id WHERE comp_county = 'BERNALILLO' ORDER BY company.comp_name, co_contact.cont_rank"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); // Build the $company_array $company_array = array(); while($row = $result->fetch_assoc()) { if(!isset($company_array[$row['company_id']])) { //Add the company specific data here $company_array[$row['company_id']] = array( 'name' => $row['comp_name'], 'street' => $row['comp_street'], 'csz' => $row['comp_csz'], 'phone' => $row['comp_ph'], 'fax' => $row['comp_fx'], 'web' => $row['comp_web'], 'email' => $row['comp_email'], //'branches' => array(), //'br_contacts' => array() ); } //Add the company contact info here $company_array[$row['company_id']]['co_contacts'][] = array( 'name' => $row['cont_name'], 'title' => $row['cont_title'], 'email' => $row['cont_email'] ); } //Run query to get branch info - for ALL companies $query = "SELECT branch.*, br_contact.* FROM branch JOIN company ON branch.company_id = company.company_id LEFT JOIN br_contact ON branch.branch_id = br_contact.branch_id WHERE company.comp_county = 'BERNALILLO' ORDER BY company.comp_name, branch.br_street, br_contact.cont_rank"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); //Add branch (and branch contacts) results to $company_array while($row = $result->fetch_assoc()) { $compID = $row['company_id']; $branchID = $row['branch_id']; if(!isset($company_array[$compID]['branches'][$row['branch_id']])) { $company_array[$compID]['branches'][$branchID] = array( 'branch_name' => $row['branch_name'], 'street' => $row['br_street'], 'csz' => $row['br_csz'], 'phone' => $row['br_ph'], 'fax' => $row['br_fx'] ); } $company_array[$compID]['branches'][$branchID]['br_contacts'][] = array( 'name' => $row['cont_name'], 'title' => $row['cont_title'], 'email' => $row['cont_email'] ); } // 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 $company) { echo "<tr><td>\n"; echo "<b>{$company['name']}</b><br>\n"; echo "{$company['street']}<br>\n"; echo "{$company['csz']}<br>\n"; echo "{$company['phone']} Fax: {$company['fax']}<br>\n"; if(!empty($company['web'])) { echo "{$company['web']}<br>\n";} if(!empty($company['email'])) { echo "Email: {$company['email']}<br><br>\n";} foreach($company['co_contacts'] as $co_contact) { echo "<b>{$co_contact['name']}</b>, {$co_contact['title']}<br>\n"; if(!empty($co_contact['email'])) { echo "Email: {$co_contact['email']}<br>\n";} echo "<br>"; } foreach($company['branches'] as $branch) { echo "<b>{$branch['branch_name']}</b><br>\n"; echo "{$branch['street']}<br>\n"; echo "{$branch['csz']}<br>\n"; echo "{$branch['phone']} Fax: {$branch['fax']}><br>\n"; echo "Email: {$branch['email']}<br>\n"; foreach($branch['br_contacts'] as $br_contact) { echo "<b>{$br_contact['name']}</b>, {$br_contact['title']}<br>\n"; echo "Email: {$br_contact['email']}<br>\n"; } } echo "</td></tr>\n"; } echo "</table>"; Which produces the listing shown here: http://www.nmlta.org/NewMemDir/bern_psycho-test2.php It is showing "Invalid Arguments" on lines 118 and 128, which are: foreach($company['co_contacts'] as $co_contact) and foreach($company['branches'] as $branch) I think I can work it out, but I'm not sure what is the purpose behind the following two statements: if(!isset($company_array[$compID]['branches'][$row['branch_id']])) and if(!isset($company_array[$row['company_id']])) I can't really understand why I'm getting the invalid argument errors... I will appreciate any help, and apologize for my ignorance. Thanks.
  15. Yes, I had forgotten that he suggested a solution - I tried to get his code to work now (after revising it to modern 'mysqli' format) but I am running into problems - probably because I don't understand the "loading" of the array. Here is my edited code: <?php // Connection to DB $mysqli = new mysqli("localhost", "username", "password", "database"); if($mysqli->connect_errno > 0){ die('Unable to connect to database [' . $db->connect_error . ']'); } // Retrieve ALL the "company" specific data (including contacts) $query = "SELECT * FROM company LEFT JOIN co_contact ON company.company_id = co_contact.company_id WHERE comp_county = 'BERNALILLO' ORDER BY company.comp_name, co_contact.cont_rank"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); // Build the $company_array $company_array = array(); while($row = $result->fetch_assoc()) { if(!isset($company_array[$row['company_id']])) { //Add the company specific data here $company_array[$row['company_id']] = array( 'name' => $row['comp_name'], 'address' => $row['address'], 'phone' => $row['phone'], 'fax' => $row['fax'], 'web' => $row['web'], 'email' => $row['email'] ); } //Add the company contact info here $company_array[$row['company_id']]['contacts'][] = array( 'name' => $row['contact_name'], 'title' => $row['title'], 'email' => $row['email'] ); } //Run query to get branch info - for ALL companies $query = "SELECT branch.*, br_contact.* FROM branch JOIN company ON branch.company_id = company.company_id LEFT JOIN br_contact ON branch.branch_id = br_contact.branch_id WHERE company.comp_county = 'BERNALILLO' ORDER BY company.comp_name, branch.br_street, br_contact.cont_rank"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); //Add branch (and branch contacts) results to $company_array while($row = $result->fetch_assoc()) { $compID = $row['company_id']; $branchID = $row['branch_id']; if(!isset($company_array['']['branches'][$row['branch_id']])) { $company_array[$compID]['branches'][$branchID] = array( 'branch_name' => $row['branch_name'], 'address' => $row['address'], 'phone' => $row['phone'], 'fax' => $row['fax'], 'web' => $row['web'], 'email' => $row['email'] ); } $company_array[$compID]['branches'][$branchID]['contacts'][] = array( 'name' => $row['contact_name'], 'title' => $row['title'], 'email' => $row['email'] ); } // 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 $company) { echo "<tr><td>\n"; echo "<b>{$company['company_name']}</b><br>\n"; echo "{$company['address']}<br>\n"; echo "{$company['phone']} Fax: {$company['phone']}><br>\n"; echo "Email: {$company['email']}<br>\n"; foreach($company['contacts'] as $contact) { echo "<b>{$contact['contact_name']}</b>, {$contact['title']}<br>\n"; echo "Email: {$contact['email']}<br>\n"; } foreach($company['branches'] as $branch) { echo "<b>Branch {$branch['branch_name']}</b><br>\n"; echo "{$branch['address']}<br>\n"; echo "{$branch['phone']} Fax: {$branch['phone']}><br>\n"; echo "Email: {$branch['email']}<br>\n"; foreach($branch['contacts'] as $contact) { echo "<b>{$contact['contact_name']}</b>, {$contact['title']}<br>\n"; echo "Email: {$contact['email']}<br>\n"; } } echo "</td></tr>\n"; } echo "</table>"; mysqli_close(); ?> I'm getting warnings on these two lines: foreach($company['contacts'] as $contact) and foreach($company['branches'] as $branch) I know it's probably because I don't understand everything that is happening, so if anyone can suggest corrections, I would appreciate. Thanks!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.