Log in

View Full Version : Resolved PHP in_array() issue.



mitchamus
03-15-2012, 03:42 AM
So to explain the problem.

I have a product table and an product application table,

The application table contains an ID, the name of the application. (and other stuff not relevant)

e.g.
table_application
ID Name
1 Disk Duplication
2 Disk Erasure

The product table, has an application field, which contains a serialised list of the application ID's

e.g.

Id Name APPL
1 Disk Doubler 1|2|5|9


now then to the problem....
I have an admin screen that shows a 'select multiple' combo box that lists the names of the applications, and highlights the ones that have been registered against the product.

to build the combo box, I retrieve all of the applications ID's and the APPL field for the product being edited.

Select ID, Name from product_application

Select APPL from products where ID =1

I convert the 'APPL' field into and array around the pipe:


$APPL = explode("|", $row[APPL]);
I then have an array with the applications that the product is assigned to:
[0]1, [1]2, [2]5, [3]9


I then build an options list by looping through the full list of application ID's and see if any of the values in the array I just made are found in the list.

When one is found (is_array=true), I can add the 'selected' option to the optionList so it appears as selected in the form..


//start the option list
$optionList="<select multiple name='applicationSelect'>";

//get all the applications
select * from table_application
while $row(blahblah)
{
if (in_array($row[ID], $APPL)) {

//product is marked as belonging to the application group
//add SELECTED to the option, and append to variable

$optionlist .="<option value='$row[ID]' SELECTED>$row[Name]</option>";
}else{

//just insert a regular option appended to variable

$optionlist .="<option value='$row[ID]' >$row[Name]</option>";

}

}
//close the option
$optionList .="</select>";


I hope that's clear what I'm trying to do... simply see if the application ID in the array is in the returned list of possible application ID's and add the SELECTED flag. (easy right? :rolleyes:)

The problem is - no matter what I do, the if statement:
if (in_array($row[ID], $APPL))

always returns false.

I think this has something to do with the two variable types being compared by 'in_array' not being the same.

I have tried setting $row[ID] to be a straight variable inside the while loop, and then using settype to make it an integer.. e.g.:




$optionList="<select multiple name='applicationSelect'>";

select * from table_application
while $row(blahblah)
{
$appIDROW=$row[ID];
$appIDROW=settype($appIDROW, 'Int');

if (in_array($appIDROW, $APPL)) {

..............


but this doesn't seem to work either...

If I do this...



select * from table_application
while $row(blahblah)
{
$appIDROW=2;

if (in_array($appIDROW, $APPL)) {


then this works fine....

So this tells me it MUST have to with the variables not being the same type... right???


can anyone help?


cheers,
Mitch.

fastsol1
03-16-2012, 12:00 AM
Have you verified in the view page source when the page is rendered that it's not echoing the SELECTED in the option tag or are you assuming it's not working cause it's not highlighting any of the options. I ask cause you are using the wrong selected tag in your code so it's not going to highlight the option no matter what at this moment. It should be this

$optionlist .="<option value='$row[ID]' selected=\"selected\">$row[Name]</option>";
If that doesn't fix it try echoing the $row[ID] on it's own (outside the <select> or it won't show anything) and make sure it's the value you expect it to be.

mitchamus
03-21-2012, 04:44 AM
No it's definitely not that, because if I set the value manually - it works fine.

'selected' on it's own still works... it's deprecated... but it still works.

mitchamus
03-21-2012, 10:29 PM
As expected - this was a problem with the variable types not being the same.

This was a simple fix by just looping through the array and setting the variable types to be integers.




foreach ($APPL as $i => $value) {
settype($APPL[$i], "integer");

}


the lesson is when using explode() - use var_dump($array)
to make sure that the variable types created by explode are the types you expect.