Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Repeat Regions inside Repeat Regions

Contributor ,
Jul 23, 2012 Jul 23, 2012

I'm not sure how to do this.  Dreamweaver says you can't put server behaviors inside server behaviors.   But it seems like a pretty standard thing someone might want to do.  For instance:

Let's say you wanted to create an online menu.  And say...you have two tables:

Table Items

  • item_id
  • item_name
  • item_description
  • item_price

Table Ingredients

  • ing_id
  • ing_name
  • item_id

So on your page, you want to show a repeat region of every menu item in the database.  Plus, inside each menu item, you want to have a repeat region of every ingredient that has the same item id.

Does that make sense?  So it may look like:

Fishy Lips
Wonderful Lips for all you Fishy Lip Lovers!  Get em today!

Ingredients

Fishy Lips

Fishy Seasonings

Fishy Herbs

Fishy Spices

Luckity Hoppity
Amazing Rabbit legs that bring good luck!

Ingredients

Bunny Legs

Bunny Spices

Cow Tongue

TOPICS
Server side applications
7.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 23, 2012 Jul 23, 2012

Sorry, I'm not a php programmer, but I can explain the concept.

First of all, your idea of retrieving both recordsets and then displaying the matching data in the inner loop should work. But it seems to be you're fetching the wrong records.  If I understand this, you are getting a list of caregivers and then displaying the education for each. Correct? If so, then the caregiver is the outer loop, and education is the inner loop, yet your inner loop is advancing the caregiver RS: mysql_fetch_assoc(

...
Translate
Contributor ,
Jul 23, 2012 Jul 23, 2012

Here is what I've tried:

<form id="form1" name="form1" method="post" action="">

  <?php do { ?>

      <tr>

        <td><?php echo $row_getCaregiver['firstname']; ?></td>

        <td><?php echo $row_getCaregiver['lastname']; ?></td>

        <td><input name="cg_id" type="hidden" id="cg_id" value="<?php echo $row_getCaregiver['cg_id']; ?>" /></td>

        <td><?php do {

                    if($row_getEducation['cgid']= $row_getCaregiver['cg_id']){

                    echo $row_getEducation['ename'];

                    } ?></td>

      </tr>

      <?php } while ($row_getCaregiver = mysql_fetch_assoc($getCaregiver));

  } while ($row_getEducation = mysql_fetch_assoc($getEducation));

            ?>

</form>

This doesn't work though.  It only pulls one record from the Caregiver table and 3 duplicates of the first record in getEducation.   (There are 3 records in the Education table).

The result is showing this:

PersonA  Record1 Record1 Record1

I want it to show what is in the database though:

PersonA  Record1 Record2 Record3

PersonB

PersonC Record4

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 23, 2012 Jul 23, 2012

Your inner loop should execute a new SQL statement based on criteria from the outer statement.

So you loop through your menu items recordset. In each iteration, you execute the SQL for your ingredient recordset, using the item_id as a filter. Make sense?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 23, 2012 Jul 23, 2012

No.  Set me on fire or something.  : )

Can you show me an example of what you mean?  I'm not use to working with loops so I don't understand that well.  Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 23, 2012 Jul 23, 2012

Sorry, I'm not a php programmer, but I can explain the concept.

First of all, your idea of retrieving both recordsets and then displaying the matching data in the inner loop should work. But it seems to be you're fetching the wrong records.  If I understand this, you are getting a list of caregivers and then displaying the education for each. Correct? If so, then the caregiver is the outer loop, and education is the inner loop, yet your inner loop is advancing the caregiver RS: mysql_fetch_assoc($getCaregiver)

You might try just switching those.

However, it is more common to first retrieve the outer loop recordset. Then when you loop through that, you execute a new inner recordset for each iteration of the outer. Your SQL for the inner RS has a filter so that it only contains matching records.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 23, 2012 Jul 23, 2012

Is there any way to highlight code and make the colors show up?

Anyways, I tried what you said:

<form id="form1" name="form1" method="post" action="">

  <?php do { ?>

      <tr>

        <td><?php echo $row_getCaregiver['firstname']; ?></td>

        <td><?php echo $row_getCaregiver['lastname']; ?></td>

        <td><input name="cg_id" type="hidden" id="cg_id" value="<?php echo $row_getCaregiver['cg_id']; ?>" /></td>

        <td><?php do {

                    if($row_getEducation['cgid']= $row_getCaregiver['cg_id']){

                   echo $row_getEducation['ename'];

                    } ?></td>

      </tr>

      <?php } while ($row_getEducation = mysql_fetch_assoc($getEducation));

  } while ($row_getCaregiver = mysql_fetch_assoc($getCaregiver));

            ?>

</form>

That actually works, but here is what the page shows:

Jason Couch Record 1 Record 2 Record 3 Jason Armchair

Notice: Undefined index: ename in C:\xampp\htdocs\testground\lkjlkj.php on line 93

Jason Bedding

Notice: Undefined index: ename in C:\xampp\htdocs\testground\lkjlkj.php on line 93

Line 93 is the bold part of the code above.  It looks like the code is working though.  There are only education records under the first person and not the second or third.  So what does this error mean?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 23, 2012 Jul 23, 2012

That error means you are trying to reference an index that is out of bounds. Looks like you are not resetting the inner recorset to the first record after looping through it. I'm not sure what the function is for that, but it should be easy to find.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 23, 2012 Jul 23, 2012
LATEST

Ok, after much trial and error - I found out how to write it.  It is thus:

<div>

<?php

mysql_select_db($database_localhost, $localhost);

$query_getCaregivers = "SELECT * FROM caregivers ORDER BY lastname ASC";

$getCaregivers = mysql_query($query_getCaregivers, $localhost) or die(mysql_error());

          while ($row_getCaregivers = mysql_fetch_assoc($getCaregivers)) {

                    echo $row_getCaregivers['firstname'];

                    echo $row_getCaregivers['lastname'];

                    echo $row_getCaregivers['email'];

$Caregiverid = $row_getCaregivers['cg_id'];

mysql_select_db($database_localhost, $localhost);

$query_geteducation = "SELECT * FROM  education WHERE cg_id='$Caregiverid'";

$geteducation = mysql_query($query_geteducation, $localhost) or die(mysql_error());

          while ($row_geteducation = mysql_fetch_assoc($geteducation)) {

                    echo $row_geteducation['ename'];

          }

}

?>

</div>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 23, 2012 Jul 23, 2012

Don't use the following code.  It makes an infinite loop of Record1.  It crashed my computer.    lol

<form id="form1" name="form1" method="post" action="">

  <?php do { ?>

      <tr>

        <td><?php echo $row_getCaregiver['firstname']; ?></td>

        <td><?php echo $row_getCaregiver['lastname']; ?></td>

        <td><input name="cg_id" type="hidden" id="cg_id" value="<?php echo $row_getCaregiver['cg_id']; ?>" /></td>

        <td><?php while ($row_getEducation['cgid'] = $row_getCaregiver['cg_id']) {

                    echo $row_getEducation['ename'];

                     ?></td>

      </tr>

      <?php } while ($row_getCaregiver = mysql_fetch_assoc($getCaregiver));

  } while ($row_getEducation = mysql_fetch_assoc($getEducation));

            ?>

</form>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines