Jump to content

Show checkbox is checked based on database value by joining two tables


Go to solution Solved by mythri,

Recommended Posts

Hello,

 

I want to show checkbox is checked when there is entry of that id in a table in my database. 

 

I have 2 tables page and access_level. I am getting data from page table and displays it in <ul><li> tag with checkbox to select all or only few. After selecting the checkbox, i will store only selected checkbox value in access_level table along with table id. Page link and page name details will be stored in page table. 

 

Now if i want to edit , i should display all the pages which is there in page table and i should also mark checked to those which are already stored in access_level table.

 

I am using LEFT OUT JOIN, It displays all the pages. But it is not displaying the check mark to those which are already selected.

 

Here is my code

<?php $s1 = mysql_query("SELECT pages.page_id, pages.code, pages.page, pages.href, access_level.aid, access_level.page_id as pgid, access_level.department, access_level.position, access_level.active FROM pages LEFT OUTER JOIN access_level ON pages.page_id=access_level.page_id WHERE access_level.department=".$department." AND access_level.position=".$position." AND pages.code='sn'") or die(mysql_error());
				while($s2 = mysql_fetch_array($s1)) { ?>
				<tr><td><li><?php echo $s2['page']; ?>  </td><td><input type="checkbox" name="sn[]" value="<?php echo $s2['page_id']; ?>"  
                <?php if($row['pgid'] === $s2['page_id']) echo 'checked="checked"';?> />

here is my pages table 

 

post-168283-0-63747800-1418583916_thumb.jpg

 

access_level

post-168283-0-41072500-1418583922_thumb.jpg

 

In access_level table i do not have page ids 8 and 9. But i want to display that also from pages table and for 1 to 7 and 10 i should display check mark. 

 

How i can achieve this? Please Help

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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