0

I have a table that is read in from a postgresql database. I am having trouble adding checkboxes to a final row in the table so that I can then, using an add to basket function pass items to a basket.php file.

Firstly I need to add a checkbox to a final column on each row so that I can check against the items I want to add when I click add to cart. I am struggling massively with this. Any help would be greatly appreciated as my code is below. If you could explain what needs to be done that'd be awesome as I can learn from it.

    <table border="1">
    <tr>
    <th>ref</th>
    <th>title</th>
    <th>platform</th>
    <th>description</th>
    <th>price</th>
    <th>select</th>
    </tr>

<?php $resource = pg_query ($connect, "select refnumber,title,platform,description,price from csgames");
        while ($a = pg_fetch_array ($resource)) {
            echo "<tr>";
            for ($j = 0; $j < pg_num_fields ($resource); $j++) {
                echo "<td>".$a[$j] ."</td>";
            }
            echo "</tr>";
        }


    ?>
    </table>

1 Answer 1

1

Try this

while ($a = pg_fetch_array ($resource)) {
   echo "<tr>";
   for ($j = 0; $j < pg_num_fields ($resource); $j++) {
      echo "<td>".$a[$j] ."</td>";
   }
   echo '<input type="checkbox" name="items[]" value="'.$id.'" />'; //replace with item id
   echo "</tr>";
}

Submitting the form will populate the items[] variable

$items = $_POST['items']; //array of itemid selected

output
---------
var_dump(items) = Array ( [items] => Array ( [0] => item123 [1] => item125 ) )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.