SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with a nested repeat region

    Hopefully someone can help me out with this one.

    I basically have some SQL returning results of a search page, where you can search on various keywords by checking boxes, and it returns Employers that match those keywords :

    Code:
    mysql_select_db($database_myDatabase, $myDatabase);
    
    if (isset($_GET['ckbox'])){
    
    // get profile keys
    
    $ckbox = array_keys($_GET['ckbox']);
    
    // sql string
    $sql = 'SELECT Employers.*, EmployerContacts.*  FROM EmployerContacts 
    INNER JOIN Employers ON EmployerContacts.EmployerID = Employers.EmployerID 
    INNER JOIN EmployerProfiles ON EmployerProfiles.EmployerID = EmployerContacts.EmployerID 
    WHERE EmployerProfiles.ProfileID IN(' . implode(',', $ckbox).')
    GROUP BY Employers.EmployerID 
    ORDER BY Employers.EmployerID DESC';
    
    $rsContacts = mysql_query($sql) or die(mysql_error());
    $row_rsContacts = mysql_fetch_assoc($rsContacts);
    @$totalRows = mysql_num_rows($rsContacts);
    }
    else
    {
    echo 'You did not check any profiles.';
    }
    ?>
    The results are drawing fields from the Employers table and EmployerContacts table, hopefully to look like :

    Employer1
    Employer1.Contact 1
    Employer1.Contact 2
    Employer1.Contact 3
    Employer2
    Employer2.Contact 1
    Employer2.Contact 2
    Employer2.Contact 3

    etc

    However, I can only seem to get it to repeat the Employers, and show just the first Contact for each, like this :

    Employer1
    Employer1.Contact 1
    Employer2
    Employer2.Contact 1

    etc

    So I guess I'm looking for help with looping through the Contacts for each Employer.

    The code currently looks like this :

    Code:
    <form action="" method="post">
    
    <table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td colspan="3" class="employerheader">Employer / Contacts</td>
        </tr>
        <?php
    	// RepeatSelectionCounter_1 Begin Loop
    	$RepeatSelectionCounter_1_IterationsRemaining = $RepeatSelectionCounter_1_Iterations;
    	while($RepeatSelectionCounter_1_IterationsRemaining--){
    		if($RepeatSelectionCounterBasedLooping_1 || $row_rsContacts){
    ?>
        <tr>
          <td class="employercell" colspan="2"><?php echo $row_rsContacts['Employer']; ?></td>
          <td width="61" class="employercell">Email ?</td>
        </tr>
        <tr>
          <td class="fieldcell"><?php echo $row_rsContacts['FirstName']; ?> <?php echo $row_rsContacts['LastName']; ?></td>
          <td width="302" class="fieldcell"><?php echo $row_rsContacts['JobTitle']; ?></td>
          <td width="61"><input name="EmailList_<?php echo $RepeatSelectionCounter_1; ?>" type="checkbox" id="EmailList_<?php echo $RepeatSelectionCounter_1; ?>" value="Y" <?php if (!(strcmp($row_rsContacts['EmailList'],"Y"))) {echo "checked=\"checked\"";} ?> />
              <script type="text/javascript">
          	check_box[boxes] = "EmailList_<?php echo $RepeatSelectionCounter_1; ?>";
    		boxes += 1;		  	
          </script></td>
        </tr>
        <?php
    	} // RepeatSelectionCounter_1 Begin Alternate Content
    	else{
    ?>
    <?php } // RepeatSelectionCounter_1 End Alternate Content
    		if(!$RepeatSelectionCounterBasedLooping_1 && $RepeatSelectionCounter_1_IterationsRemaining != 0){
    			if(!$row_rsContacts && $RepeatSelectionCounter_1_Iterations == -1){$RepeatSelectionCounter_1_IterationsRemaining = 0;}
    			$row_rsContacts = mysql_fetch_assoc($rsContacts);
    		}
    		$RepeatSelectionCounter_1++;
    	} // RepeatSelectionCounter_1 End Loop
    ?>
        <tr>
          <td colspan="3">&nbsp;</td>
          </tr>
        <tr>
          <td colspan="3"><input name="Update Contacts" type="submit" value="Compose Email..." />
            &nbsp;
            <input type="button" name="uncheck" value="Clear All Checkboxes" id="uncheck" onClick="uncheck_all(boxes, check_box);" /></td>
        </tr>
      </table>
    </form>
    Hope that makes sense.

    Many thanks.

  2. #2
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you need to look up 'concat'.
    Code mysql:
    select 
    employer
    concat_ws(
                  '/'
                , (ec.employee)                                             
                ) as employees asc
    from employers e
    inner
    join employer_contacts ec
    on ec.employer_id = e.employer_id

    That may not be exactly correct but should give you the idea.

    bazz

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    bazz, that won't work the way you think it will

    you're probably thinking of GROUP_CONCAT

    and i am not convinced that's the way to go

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    uh-oh.

    I had better find out the best way to go then coz that is what I have used.

    my scenario was where I had one of each customer value (details, address, contact etc., and there was >1 machine owned by customer). concat let me bring in each individual value and where there is a series of machines for the one customer, concat put them into an array, which I could then loop through to output them on the page.

    bazz

  5. #5
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the end I got it working using Adobe's Dreamweaver Developer Toolkit, which has a nested repeat region function.

    The relevant bit of code it generated looked like this if you're interested :

    Code:
     <?php
      if ($totalRows_rsContacts>0) {
        $nested_query_detail2EmployerContacts = str_replace("123456789", $row_rsContacts['EmployerID'], $query_detail2EmployerContacts);
        mysql_select_db($database_myDatabase);
        $detail2EmployerContacts = mysql_query($nested_query_detail2EmployerContacts, $connSearchTechUK) or die(mysql_error());
        $row_detail2EmployerContacts = mysql_fetch_assoc($detail2EmployerContacts);
        $totalRows_detail2EmployerContacts = mysql_num_rows($detail2EmployerContacts);
        $nested_sw = false;
        if (isset($row_detail2EmployerContacts) && is_array($row_detail2EmployerContacts)) {
          do { //Nested repeat
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •