Can you have 2 queries on different tables to create check boxes and have them "checked"?

49 views Asked by At

I am currently using a table that uses a show columns query to echo all the columns into checkboxes. The below code works for that. I tried to create another query right under the query to search a different table that has the same columns and if the row in this table has a 0 or 1 it will show a "checked" box. The reason I use the "show columns" query is because of always adding more columns for items.

/////EDIT after posting I realized that it will be better just to use the same table since the "Columns" represent all the "checkboxes" when output anyway. But the same problem still exists. I am trying to double up on the queries. Query 1 = Extract all columns and create checkboxes Query 2 = use row that matches $newloc and if checkboxes are in specific columns mark out put columns with "Checked" boxes. I have another query on the same page that populated user information which works fine. This page works as a confirmation page which has all user information as well as certain items they have (the Checkboxes) and then they can edit on this page and submit if needed. I want to be clear on all that is in this page just in case that effects this part. One other thing I need to add is since I am using the same table I need to skip the first 3 columns since that is other data that I dont want as columns.

 <?php 
  $conc = new mysqli("127.0.0.1:3306", "root", "", "mydb");
  $res= $conc ->query("SHOW COLUMNS from matmemmatrix");
  
  // this part here is what I was imagining would work
  $sql = "SELECT * FROM matmemmatrix WHERE memid1 = '$uid' AND memlocid = '$newloc'";
  $datam = mysqli_query($conc, $sql);
  $a = 1;
  $row1 = $datam[$a]; // first column ?? 
  //      


  echo "<html>";
  echo "<body>";               
  echo "<table>";
  $i = 0;
  while ($row = $res->fetch_assoc()) {
     $id = $row['Field']; 


       // EDIT : check to see if $id matches $row1
     if($id = $row1){
        $checked = 1;
        }else{
        $checked = 0;
        }
        //



     if($i >'5'){
        $i=='0';
    
     if($i=='5'){
        echo "</tr>";
        echo "<tr>";
        $i ='0'; 
     ;   
     if ($i =='0' ){
        echo "<tr>";
  };
  echo "<td>";
  echo '<input type ="checkbox"  
                       value="'.htmlspecialchars($id).'"  
                           id="'.htmlspecialchars($id).'"          
                          name="'.htmlspecialchars('mat[]').'"  
                        checked= '.$checked.'>
                        </option>';
 echo '<label for ="'.htmlspecialchars($id).'">'.htmlspecialchars($id).'</label>';
 echo "</td>";
 $i++;
 // EDIT Increment next column to see if 0 or 1
 $a++;
//
 }
  echo "</table>";
  echo "</body>";
  echo "</html>";   
?>

I tried to use the query below with the one above to get the check box "checked" columns. But failed.

$sql = "SELECT * FROM matmemmatrix WHERE memid1 = '$uid' && memlocid = '$newloc'";

I couldn't get any results from that query to work with the above query.

I tried using this query above by itself to extract all the columns to use as checkboxes and use the row data with a 1 or 0 to mark as checked but failed.

Is it possible to get the columns from this query to use as checkboxes and use the row data to show it checked if 1 or 0? I want to make sure all columns in the table are showing whether or not they are checked.

1

There are 1 answers

1
Don On

I got this code to work from 2 queries. Needs a little cleaning though.

<?php
$conc = new mysqli("127.0.0.1:3306", "root", "", "database");           
$sqla = "SHOW COLUMNS from matmemmatrix";
$res= mysqli_query($conc, $sqla);
$uid = $_SESSION[ 'uid' ];
$newloc = $_SESSION[ 'newloc'];

$sql = "SELECT * FROM matmemmatrix WHERE memid1 = '$uid' AND memlocid = '$newloc'";
$resu = $conc->query($sql);
$datam = $resu->fetch_assoc();
$a = "0";
echo "<html>";
echo "<body>";               
echo "<table>";             
$i = 0;
while ($row = $res->fetch_assoc()) {
$id = $row['Field']; //prints column names
$id1 = $rowa['MatName']; 
if ($datam[array_keys($datam)[$a]] == "1")
{ $checked = "checked";
}else{
$checked = "";
}
$a++;
if($i >'5'){
$i=='0';
};

if($i=='5'){
echo "</tr>";
echo "<tr>";      
$i ='0';           
};
if ($i =='0' ){
echo "<tr>";
};
echo "<td>";
echo '<input type ="checkbox"  value="'.htmlspecialchars($id).'"  id="'.htmlspecialchars($id).'"  name="'.htmlspecialchars('mat[]').'"  '.$checked.'>
</option>';
echo '<label for ="'.htmlspecialchars($id).'">'.htmlspecialchars($id).'</label>';
echo "</td>";
$i++;
                  
}   
echo "</table>";
echo "</body>";
echo "</html>";   
$_POST['access_listb'];
?>