Here is my form:
<div id="days" class="selector">
<p>Select days (One day at least)</p>
<div id="days_left" class="">
<label for="Sunday">
<input type="checkbox" name="weekday[]" value="6" class="days"> Sunday
</label>
<label for="Monday">
<input type="checkbox" name="weekday[]" value="0" class="days"> Monday
</label>
<label for="Tuesday">
<input type="checkbox" name="weekday[]" value="1" class="days"> Tuesday
</label>
</div>
<div id="days_right" class="">
<label for="Wednesday">
<input type="checkbox" name="weekday[]" value="2" class="days"> Wednesday
</label>
<label for="Thursday">
<input type="checkbox" name="weekday[]" value="3" class="days"> Thursday
</label>
<label for="Friday">
<input type="checkbox" name="weekday[]" value="4" class="days"> Friday
</label>
</div>
Here is the code which handles the form’s output:
//code for days
if(isset($_POST['weekday'])) {
if((sizeof($_POST['weekday']) == 1)) {
$days = ($_POST['weekday']);
}else{
if(sizeof($_POST['weekday']) > 1){
$days = join(",", $_POST['weekday']);
}
}
}else{
$days = [0,1,2,3,4,5,6];
$days = join(",", $days);
}
And here is the code which works with the results:
$sql = "INSERT INTO `filtered`
(id, username, ticket, o_time, type, size, item, o_price, s_l, t_p, c_time, c_price, profit)
SELECT *
FROM `data`
WHERE c_time BETWEEN :start_date AND :end_date
AND TIME(`c_time`) BETWEEN :hour_start AND :hour_end AND WEEKDAY IN ($days)";
$stmt = $db->prepare($sql);
$stmt->bindParam(':start_date', $start_date, PDO::PARAM_STR);
$stmt->bindParam(':end_date', $end_date, PDO::PARAM_STR);
$stmt->bindParam(':hour_start', $hour_start, PDO::PARAM_STR);
$stmt->bindParam(':hour_end', $hour_end, PDO::PARAM_STR);
foreach ($days as $key => &$val) {
$stmt->bindParam($key, $val);
}
$stmt->execute();
The idea here is that a user can select a weekday or a few weekdays or not select anything, in which case - all weekdays are selected.
The query gets time periods and huors ranges and all worked well untill I added weekdays.
Trying to solve the problem I checked the data and added print_r() and echo statements to see the forn inputs
$days = [0,1,2,3,4,5,6];
echo 'Here: '.sizeof($days).'<br>';
$days = join(",", $days);
print_r($days);
Results in
Here: 7
0,1,2,3,4,5,6 and an errormessage :
Warning: Invalid argument supplied for foreach() in C:\wamp\www\trade_analyzer_filter_ACTIVE\action_page.php on line 169
It refers to the sql query code.
This code
: $days = [0,1,2,3,4,5,6];
$days = join(",", $days);
echo 'Here: '.sizeof($days).'<br>';
print_r($days);
Adds an error message
Warning: sizeof(): Parameter must be an array or an object that implements Countable in …
I searched the internet for bindParam with array variable.
I changed the foreach loop to
foreach ($days as &$val) {
$stmt->bindParam($days, &$val);
}
I still get the same error message
I also tried this:
$stmt->execute($days);
Checking data using print_r()
and echo gives
Here: 1
0,1,2,3,4,5,6
Selecting all chechboxes outputs:
Here: 6
Array ( [0] => 6 [1] => 0 [2] => 1 [3] => 2 [4] => 3 [5] => 4 )
size of array is 1 yet 7 vaues in the array. why?
What is the correct syntax foe bindParam to an array?
What’s the difference between
Array ( [0] => 6 [1] => 0 [2] => 1 [3] => 2 [4] => 3 [5] => 4 )
and
0,1,2,3,4,5,6
What do I need fo change / fix to mKE IT WORK?