View Full Version : Displaying results from a many to many db relationship

07-02-2007, 09:02 PM
I would really appreciate any help on this one.

The query I built here selects all of the business info and also what types of business it is (so this returns more than one result for the same business)
I have a <select multiple> box that has all of the types of business in it.
When I go from the list business page and pass the edit page the business ID I want it to select those types already associated with the business but right now since im passing it a business id it is only showing me the last one since it is not in a while loop, which doesn't work i've already tried nesting while loops
here is what i have so far if anyone wants to help me out.

$result = $db->query("SELECT b_id, b_title, b_address1, b_address2, b_phone, b_fax, b_email, b_weburl, b_desc, b_image, business_type.t_id, business_type.t_name, bus_types_link.type_id, bus_types_link.detail_id FROM business_detail LEFT OUTER JOIN bus_types_link ON business_detail.b_id = bus_types_link.detail_id LEFT OUTER JOIN business_type ON bus_types_link. type_id = business_type.t_id WHERE b_id = '{$id}'");
$result2 = $db->query("SELECT t_name, t_id FROM business_type");
//echo $db->getQuery();
$num = $db->getNumRows($result);
$row = $db->fetchArray($result);
$numrows = $db->getNumRows($result2);

<form name="business" action="processEdit.php?action=business&amp;id=<? echo $id; ?>" enctype="multipart/form-data" method="post">
Business Type(s):<br />
<select multiple size="<? echo $numrows; ?>" name="types[]">
while ($row2 = $db->fetchArray($result2)) {
<option value="<? echo $row2['t_id']; ?>"><? echo $row2['t_name']; ?></option>
<br />
<label for="b_title">Business Name:</label><br />
<input type="text" name="b_title" value="<? echo $row['b_title']; ?>" /><br />
<label for="b_address1">Address:</label><br />
<input type="text" name="b_address1" value="<? echo $row['b_address1']; ?>" /><br />
<label for="b_address2">Address2:</label><br />
<input type="text" name="b_address2" value="<? echo $row['b_address2']; ?>" /><br />
<label for="b_phone">Phone:</label><br />
<input type="text" name="b_phone" value="<? echo $row['b_phone']; ?>" /><br />
<label for="b_fax">Fax:</label><br />
<input type="text" name="b_fax" value="<? echo $row['b_fax']; ?>" /><br />
<label for="b_email">Email:</label><br />
<input type="text" name="b_email" value="<? echo $row['b_email']; ?>" /><br />
<label for="b_weburl">Website:</label><br />
<input type="text" name="b_website" value="<? echo $row['b_weburl']; ?>" /><br />
<label for="b_desc">Description:</label><br />
<textarea name="b_desc" cols="20" rows="10"><? echo $row['b_desc']; ?></textarea><br />
<? if($row['b_image'] == "") { ?>
<input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
<label for="b_image">Business Image:</label><br />
<input type="file" name="imagefile" /><br />
<? } else { ?>
Image: <br /><img src="../images/thumb_b/<? echo $row['b_image']; ?>" /><br />
<a href="#" onclick="deleteImage('business', '<? echo $id; ?>');">Delete</a><br />
<? } ?>
<input type="submit" value="Edit Business" />