0

The below code is returning rows of checkboxes (the right number per the mySQL table) and with no error. The problem is that it is not grabbing the column values ((as per: ".$row['Zone'].": ".$row['RowNumber']. ))to place beside said checkboxes.

<?php
include'connect.php'
?>
<form method="post" action="chooseDate.php">

<?php   
$sql = "
    SELECT s.RowNumber, s.Zone, 
    FROM Seat AS s 
        LEFT JOIN Booking AS b, ON s.RowNumber = b.RowNumber, 
                                AND b.PerfDate = ?, 
                                AND b.PerfTime = ?, 
    WHERE b.RowNumber IS NULL, 
    GROUP BY s.RowNumber
";
$date = "$_POST[Date]";
$time = "$_POST[Time]"; 
$handle = $conn->prepare($sql); 
$handle->execute(array($date, $time));  
$res = $handle->fetchAll(); 
    foreach($res as $row) {
        echo "<input name='Seat' type='checkbox' value='".$row['Zone'].":   
             ".$row['RowNumber']."'><br>";  
    }   
?>

<input class="mybutton" type="submit" value="Choose Seat"/>
</form>

I have run queries using identical methods and they display the results as expected. The only difference here is that the query is LEFT JOIN. The results display in shell. This is a sample of the results and the expected output of one checkbox.

|**RowNumber**|**Zone**|
|-------------|--------|
|Z20          |box 4   |

Where am I going wrong? Thanks in advance.

4
  • What are the Names of the Columns ** RowNumber ** or RowNumber Commented Dec 3, 2016 at 12:18
  • 1
    What does print_r($res) look like when executed after $res = $handle->fetchAll();? Commented Dec 3, 2016 at 12:18
  • @MartinCup very ugly. About 500 of these in a consecutive string: "Array ( [0] => Array ( [RowNumber] => U03 [0] => U03 )" Commented Dec 3, 2016 at 12:21
  • Yes so it seems everything is there, it is not printed correctly in the HTML. You should have a look at GolezTrol's answer. Commented Dec 3, 2016 at 12:22

1 Answer 1

2

Have you looked at the HTML output? You put the text inside the input element. The closing > is after the text.

You could fix that simply by just moving the > to the front, but I think it's bettter to generate a <label> element after the checkbox or surrounding the checkbox. If you give the checkbox an id, and the label a for attribute pointing to that id, the text is clickable too, which make an awesome UX. :)

So I suggest changing the for loop like this:

$idcounter = 0;
foreach($res as $row) {
    $id = 'Seat' . ($idcounter++); 
    echo "<input name='Seat' type='checkbox' id='$id'><label for='$id'>".$row['Zone'].":".$row['RowNumber']."</label><br>";  
}  

Note that I removed the value attribute. I'm not sure if you would need it, but maybe you need both: the value and the label. If so you can put back that attribute like you had before, as long as you make sure to close the input element before opening the label.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you demonstrate?
You're a gem. Thanks a lot.

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.