It seems I miss a few crucial issues when it comes to arrays

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?

Which usually translates as “I screwed up my SQL query.”

What database engine are you using, and have you looked up the proper syntax for WEEKDAY?

1 Like

The error is because your logic is imploding/joining the array into a string, so there’s nothing to foreach(){} over. The print_r() only LOOKS correct, because it contains the value(s) you expect to see. You are also putting that string directly into the sql query statement, rather than the necessary place-holders (using an IN() statement requires a place-holder for each value.)

An alternative to using IN(), is to use FIND_IN_SET(), which will take a single place-holder and an imploded comma separated value.

Edit: which I see you were already told in a reply in another thread for this same problem - How to select results when variable is an array - #3 by mabismad

I rechecked my WEEKDAY SYNTAX and corrected it
AND WEEKDAY(c_time) IN ($days)

My engine is mysql.

I looked at similar questions and answers here in SitePoint and StackOverflow. I read about arrays, foreach and bindParam
I didn’t find a proper explanation.
I didn’t find an explanation for & in &$val

I can’t miss the fact that WEEKDAY() SCREWED MY SQL

I read about FIND_IN_SET()
I tried to use it. I don’t remember what wa my code. I do remember getting error messages

P.S. I imploded the array with “,”, .not with a string

You would need to post the attempted code and any error message to get help with what was wrong.

Using an IN() comparison will require you to dynamically build a term with from 1 to 6 (*) place-holders in the sql query statement and supply the corresponding n values when the query gets executed.

(*) For the case where all 7 week days are going to be matched, just leave the weekday term out of the sql query statement.

“,” is a string and the result of the implode is also a string, so I don’t know what the Post Script is about.

BTW - implode/join works correctly with an array with a single element (you get just that single element’s value), so the initial logic testing the sizeof() the submitted array is unnecessary and doesn’t actually get the correct value if you only select one weekday.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.