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
Table Ingredients
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 |
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
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.
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>
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?
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>North America
Europe, Middle East and Africa
Asia Pacific