Checkbox Fields From Database

Asked

Viewed 913 times

1

I have 3 tables, respectively:

  • Vehicles
    • veculo_id, veculo_name.
  • Accessories
    • acessorio_id, accessorio_name
  • Vehicles
    • veculo_id
    • accessorio_id

On the vehicle registration page the fields accessories come from table database accessories as a field of the kind checkbox. When I register the vehicle, the registration is inserted in the tables vehicles and vehicles.

Even this part works normally, but on the page of edit I can only catch the ones that have already been marked (by checkbox) during the registration of the vehicle, and what I wanted was to show all the accessories of the table, including those not marked, leaving only marked those that were marked during the registration.

The SQL query code of the edit page looks like this: This query only returns the checkboxes that were marked in the register!

<?php 
$getVehicleID = filter_input(INPUT_GET, 'vehicleID', FILTER_VALIDATE_INT);       

$ReadItens = new Read;
$ReadItens->FullRead("SELECT cars.car_id,car_vehicles_and_items.car_id,car_additional.additional_id,car_additional.additional_title
FROM car_vehicles_and_items INNER JOIN cars ON cars.car_id = car_vehicles_and_items.car_id 
INNER JOIN car_additional ON car_additional.additional_id = car_vehicles_and_items.additional_id where car_vehicles_and_items.car_id  = : id", " id=$getVehicleID");

foreach ($ReadItens->getResult() as $lisItens):

echo "<input ";

if ($lisItens['car_id'] == $getVehicleID):  

echo "checked ";
endif;

echo "type=\"checkbox\" id=\"{$lisItens['additional_id']}\"  name=\"car_additional[]\" value=\"{$lisItens['additional_id']}\">";
echo $lisItens['additional_title'];
endforeach;

Na página de cadastro os acessórios estão assim

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • What have you already done? Are you creating HTML directly from a PHP file? Because there are different ways to do it. Edit the question with the code that has already been done in that part.

  • 1

    I don’t understand very well what Voce is trying to do, Do you want to search the database for the values and put them in a checkbox with active marking? that’s it?

  • Yes that’s right I want to list all accessories and stay active only those that were marked in the register!

  • Despite the editing, it’s still hard to understand what you really want.

  • from what I understand, just rip the filter from the "checked": " id=$getVehicleID" of consultation.

  • Bring everything without filter, in the loop, you mark what is checked.

Show 1 more comment

3 answers

1

Okay... I believe the simplest way for you to resolve this issue is:

1 - create a query to list all registered accessories 2 - create a query to recover all the accessories that the XPTO vehicle has. Do the while of the query result and store in an array. 3 - when you loop item "1" (which lists the registered accessories), do an if:

if (in_array($linhaDoLoopDoItem1, $arrayDoItem2)) { 
    echo "checked";
}

Remembering that this IF needs to be inside the checkbox tag.

1

You can use this single select that will return both the accessories in a particular car and the ones that are not.

SELECT A.ACESSORIO_ID, A.ACESSORIO_NOME, "TRUE" CHECKED FROM ACESSORIOS A JOIN VEICULOS_ACESSORIOS VA ON VA.ACESSORIO_ID = A.ACESSORIO_ID WHERE VA.VEICULO_ID = X UNION SELECT A.ACESSORIO_ID, A.ACESSORIO_NOME, 'FALSE' FROM ACESSORIOS A WHERE NOT EXISTS (SELECT 1 FROM VEICULOS V JOIN VEICULOS_ACESSORIOS VA ON VA.ACESSORIO_ID = A.ACESSORIO_ID WHERE V.VEICULO_ID = VA.VEICULO_ID AND V.VEICULO_ID = X)

  • Only returned the 4 checkbox result!

  • Please see the edition.

  • Error code [Err] 1054 - Unknown column 'A.ACESSORIO_ID' in 'on clause'

  • Please check the fields to see if they have the correct nomenclature.

  • I checked you’re all right.

1


From what I see, your problem is of condition, you are bringing only the marked inputs, in this case, just put all and ignore the filter, look just how you could do it:

$getVehicleID = filter_input(INPUT_GET, 'vehicleID', FILTER_VALIDATE_INT);  

$ReadItens = new Read;
$ReadItens->FullRead("
      SELECT cars.car_id,
             car_vehicles_and_items.car_id,
             car_additional.additional_id,
             car_additional.additional_title
      FROM   car_vehicles_and_items 
      LEFT JOIN cars
             ON cars.car_id = car_vehicles_and_items.car_id
      LEFT JOIN car_additional 
             ON car_additional.additional_id = car_vehicles_and_items.additional_id
      WHERE :n", " n=1");

foreach ($ReadItens->getResult() as $lisItens) {

    echo "<input type=\"checkbox\" " . 
         "id=\"{$lisItens['additional_id']}\" ";
         "name=\"car_additional[]\ " .
         "value=\"{$lisItens['additional_id']}\"" . 
         (($lisItens['car_id'] == $getVehicleID) ? 'checked = \"checked\"' : '') .
         ">" . $lisItens['additional_title'];
}
  • Only returned the 4 checkbox result!

  • 1

    because your query should run just that. try switching to LEFT JOIN

  • 1

    Strange because I removed the comparison to WHERE 1; Thinking that this query is based on user records and not on the table containing all categories.

  • I used LEFT JOIN and it worked out!

  • A query ficou assim deu certo : $ReadItens = new Read;&#xA;$ReadItens->ExeRead('car_additional', "LEFT JOIN car_vehicles_and_items on car_vehicles_and_items.additional_id = car_additional.additional_id");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.