Jump to content

join two mysql tables with php


lional

Recommended Posts

Hi

I have one table called bucket_list that lists all the available bucket lists. Another table called client_list that lists the bucket lists that the client has previously selected.

I want to create a edit facility that allows users to edit there bucket list. What I am trying to do is create a list of checkboxes that will be checked if the entry exists in the client list and show the bucket list unchecked if the user has not selected it.

 

Here is the code that I have been trying.

$query_subcat = "SELECT bucket_list.cat_id, bucket_list.category, client_list.bucket_id FROM bucket_list, client_list WHERE client_list.client_id = '$cid_out' AND bucket_list.active = '1'";
		$result_subcat = mysql_query($query_subcat, $conn);
		while ($row_subcat = mysql_fetch_assoc($result_subcat)){
		$sub_cat_id_out = $row_subcat["cat_id"];
		$subcat_out = $row_subcat["category"];
		$bucket_id_out = $row_subcat['bucket_id'];
		
print '<td width="190" align="left" valign="top"style="color:black;float:left">';
		
	
	



print '<input type="checkbox" value="' . $sub_cat_id_out . '" name="cat[' . $sub_cat_id_out . ']"'; 
if ($bucket_id_out == $sub_cat_id_out) {
print 'checked="checked">';
} 
print '  ' . $subcat_out . '</td>';

 

Link to comment
https://forums.phpfreaks.com/topic/276339-join-two-mysql-tables-with-php/
Share on other sites

If I understand your table structure

 

SELECT bucket_list.cat_id, bucket_list.category, client_list.bucket_id 
FROM bucket_list
LEFT JOIN client_list 
    ON client_list.client_id = '$cid_out' 
    AND client_list.bucket_id = bucket_list.cat_id
WHERE  bucket_list.active = '1'

 

client_list.bucket_id will be NULL if not selected

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.