Jump to content

[SOLVED] in_array() problem :S


Jahren

Recommended Posts

I have this little glimpse of code that wont work

 

function valider_journees($nb_dispos)
{
	$valide = false;
	$date = array();

	//I have to start at 1. its been tested already
	for($i = 1; $i <= $nb_dispos; ++$i)
	{
		$valide = $valide && !in_array($_POST['date' . $i], $date);
		$date .= $_POST['date' . $i];
	}

	return $valide;
}

 

when I echo the values :

echo $_POST['date' . $i] . ' ' . !in_array($_POST['date' . $i], $date);

 

I get this error :

27/01/2009 1

Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\Program Files\utilities\EasyPHP 2.0b1\www\gesti\ajouter_benevole.php on line 413

28/01/2009

 

I've tested types with gettype() and they all give me strings

A little help ? Thanks!

 

Link to comment
Share on other sites

Try this:

 

<?php
function valider_journees($nb_dispos)
{
$valide = false;
$date = array();

//I have to start at 1. its been tested already
for($i = 1; $i <= $nb_dispos; ++$i)
{
	$valide = $valide && !in_array($_POST['date' . $i], $date);
	$date[] = $_POST['date' . $i];
}

return $valide;
}
?>

Link to comment
Share on other sites

      for($i = 1; $i <= $nb_dispos; ++$i)
      {
         $valide = $valide && !in_array($_POST['date' . $i], $date);
         $date .= $_POST['date' . $i];
      }

 

You are setting $date to a string when it should be an array. Thus the error.

 

      $strDate = "";
      for($i = 1; $i <= $nb_dispos; ++$i)
      {
         $valide = $valide && !in_array($_POST['date' . $i], $date);
         $strDate .= $_POST['date' . $i];
      }

 

Try that.

 

This assumes you want it in a string, if you want it in an array, use flyhoney's version.

Link to comment
Share on other sites

ahaha, yep that's right. :P

edit : its been 14 hours straigtht every day of coding including weekends for a full month.

I have a week to go only as a deadline to have everything working top notch.

 

i'm tired as hell. thanks everyone for your support.

Link to comment
Share on other sites

Well for starters your for is weird, it starts at 1 and adds 1 to it before anything happens, thus you start at 2.

 

   //I have to start at 1. its been tested already
   for($i = 1; $i <= $nb_dispos; $i++)
   {
      $valide = $valide && !in_array($_POST['date' . $i], $date);
      $date[] = $_POST['date' . $i];
   }

 

That would be better, if you do not have a date0 and only have a date1...etc.

 

Also this part:

$valide = $valide && !in_array($_POST['date' . $i], $date);

 

Is weird as $valide will only amount to the last date. So if the last date is in the array, than it will always return false. And if $valide, is false than it will always return false, cause $valide can never be set to true. That statement in itself is flawed.

 

Hope that helps to get you on the right track.

 

I would be more than happy to help more, I just need to know what your goal is for this function. What does it ultimately need to do?

Link to comment
Share on other sites

indeed. It was flawed.

I changed the initial value from false to

$valide = $nb_dispos > 0;

 

so if I have no element to check, it returns false.

 

and there is no date0, this is why I start at 1. :)

now it works like a charm

 

Edit : This function is used to verify that there are no double inputs in all the form. I do not know how many entries I have to check before it is sent which is why I have to use that weird setup. (PHP generating Javascript Generating HTML)

Link to comment
Share on other sites

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.