Jump to content

Recommended Posts

Hey

 

I have this function which checks if an number is in a array. What i am using this for is to check if a periode of days is booked in a calendar.

The function works fine if i just manually write the numbers into the array. But i want to use the variable i create with the numbers. The wierd

thing is that this works if i make a foreach on the array. The in_array() function just wont work when i declare the array with an variable.

 

Here is the code:

 


function checkBusyDate($day, $month, $year){
$date = $day."/".$month."/".$year;

$query = mysql_query("SELECT toa FROM calendar WHERE froma = '$date'");
$data = mysql_fetch_array($query);

$from 	= $day;
$to 	= substr($data['toa'], 0, 2);

while($from < $to){
	$numForArray .= $from.", ";
	$from++;
}

$numForArray .= $to;
$array = array($numForArray);

if(in_array(25, $array, true)){
	echo "Yep its there.";
}else{
	echo "Nopez, sry";
}
}

checkBusyDate(24, 12, 2007);

 

Does'nt in_array() support what im trying to do or whats wrong?

Link to comment
https://forums.phpfreaks.com/topic/82676-solved-in_array-problem/
Share on other sites

$array = array($numForArray) would give the array only one value. Try this:

 

<?php

function checkBusyDate($day, $month, $year){
$date = $day."/".$month."/".$year;

$query = mysql_query("SELECT toa FROM calendar WHERE froma = '$date'");
$data = mysql_fetch_array($query);

$from 	= $day;
$to 	= substr($data['toa'], 0, 2);

$array = array();

while($from < $to){
	$array[] = $from;
	$from++;
}

$array[] = $to;

if(in_array(25, $array)){
	echo "Yep its there.";
}else{
	echo "Nopez, sry";
}
}

checkBusyDate(24, 12, 2007);

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.