title: Tick checkbox from a WHILE loop
body: <p>So I have three tables:</p>
  
  <pre><code>docs
    doc_id
  
  cats
    cat_id
  
  doc_cat_join
    id
    doc_id
    cat_id
  </code></pre>
  
  <p>I have created a way to create a doc (much like a post) and categories the docs go into the docs table and the categories go into the cats table. when editing a doc I show the available categories by way of a while loop. this works perfectly well whichever ones I tick it take the doc_id form the url and the cat_id of the checkboxes you tick. What I want to do is when I then go back and edit again it only shows ticks in the checkboxes from the ones I selected before by looking at the join table.</p>
  
  <p>Here is how I currently loop through and show the categories:</p>
  
  <pre><code>&lt;?php
  function build_cat_checkboxes(){
  
      global $dbc;
      global $id;
  
      $q = "SELECT * FROM cats";
      $r = mysqli_query ($dbc, $q); // Run the query.
  
      $currentDoc = $id;
  
      while ($row = mysqli_fetch_array($r)) {
  
      $checked = (($row['cat_id'] == $currentDoc) ? 'checked' : '');
  
      echo '&lt;ul&gt;
            &lt;li&gt;&lt;label&gt;
              &lt;input type="checkbox" name="cat_id[]" value="' . $row['cat_id'] . '"  checked="'.$checked.'"&gt; ' . $row["cat_name"] . '&lt;/label&gt;&lt;/li&gt;
            &lt;/ul&gt;';
      }
  
  }
  ?&gt;
  </code></pre>
  
  <p>As you can see I have attempted to create a way of checking them but all it is doing it ticking all the boxes because my query is obviously wrong as it isnt checking the join table to see if the doc_id is associated with the cat_id</p>
  
  <p>So...</p>
  
  <p>I put together this query:</p>
  
  <pre><code>$q1 = "SELECT * FROM doc_cat_join WHERE doc_id = '$id'";
  $r1 = mysqli_query($dbc, $q1);
  </code></pre>
  
  <p>and if I do a <code>print_r</code> on it, it gives me back the numbers of rows I expect by taking the doc_id form the url and then showing me if there is or isn't any rows in the join table associated with the doc I am editing. I need to find a way to tick the checkboxes but I am really struggling.</p>