Jump to content

[SOLVED] Looping around to recordsets


mr_jim

Recommended Posts

Hi everyone,

 

Got a problem involving looping around 2 sets of records at the same time and comparing them. Basically I have 2 tables:

size_ref

size_id

size

 

garment_sizes

product_id

size_id

 

At the moment I loop around 'size_ref' to create a number of checkboxes. I want to loop compare the two recordsets to "check" the checkbox if the value in 'garment_sizes' matches the value in 'size_ref' (they are linked on 'size_id'). I have some code below but it is does not currently work, as I have been playing around with it to try and get this to work: -

 

<?php
$querysize = "SELECT * FROM size_ref ORDER BY size_id";
$resultsize = mysql_query($querysize);
f ($resultsize){
} else {
echo "Error connecting to database. Please contact the administrator of the site.";
exit();
}

$querygs = "SELECT DISTINCT size_id FROM garment_sizes WHERE product_id = $pid ORDER BY size_id";
echo $querygs;
$resultgs = mysql_query($querygs);
if ($resultgs){
} else {
echo "Error connecting to database. Please contact the administrator of the site.";
exit();
}

$i =1;
echo "<i>(More than one can be selected)</i><table border=\"0\"><tr>";

while ($size = mysql_fetch_array ($resultsize, MYSQL_ASSOC)) {
$sizeid = $size['size_id'];
$sizename = $size['size'];
$check = "";

//I had another loop that went around garment_sizes and checked here, but deleted it as it didnt work. It simply repeated every record. 

if ($i == 2) {
echo "<td><input type=\"checkbox\" value=\"$sizeid\" name=\"size[]\" $check> $sizename</td></tr>\n";
$i =1;
} else {
echo "<td><input type=\"checkbox\" value=\"$sizeid\" name=\"size[]\" $check> $sizename</td>\n";
$i = $i + 1;
}

//The loop ended here

}
echo "<td> </td></tr></table>";
mysql_free_result($resultsize);
mysql_free_result($resultgs);		  
?>

 

Please disregard the fact I have no connection etc as this is all done further up the page. Help would be much appreciated.

 

Jim

Link to comment
https://forums.phpfreaks.com/topic/41437-solved-looping-around-to-recordsets/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.