PDA

View Full Version : Conditionals and in_array...


LWillmann
03-09-2006, 11:57 AM
I have created a table, and in that table I have fields. Some of those fields store multiple values (a group of checkbox values). The selected items are stored in the fields as comma seperated strings (using PHP's implode).

I have created a page, and on that page, I have managed to get it to pull the data from that table. I run the resultset through a foreach loop to do some testing.

foreach ($read as $key => $data)
{
if ($key == 'bodyart')
$bodyart = explode(", ", $data);
}

Then in my template I have a series of checkboxes, here is one for example:

<input type="checkbox" name="bodyart[]" value="Pierced tongue"
<if condition="in_array('Pierced tongue', $bodyart)">checked</if>
/>

This is attempting to see if the value for this checkbox is in the bodyart array, and if it is, then print checked so the box is checked.

When I attempt to save the template in the ACP I get this message:

Warning: in_array(): Wrong datatype for second argument in /includes/adminfunctions_template.php(3537) : eval()'d code on line 364

In my source file for the page, I have declared $bodyart as global and as an array, but it still gives me the error.

global $bodyart;
$bodyart = array();

If I click continue and go on and save the template, and load the page, it shows the proper box checked. So technically it works. But how do I keep from getting this warning on my template for each instance of that array?

Zachery
03-09-2006, 12:03 PM
I have created a table, and in that table I have fields. Some of those fields store multiple values (a group of checkbox values). The selected items are stored in the fields as comma seperated strings (using PHP's implode).

I have created a page, and on that page, I have managed to get it to pull the data from that table. I run the resultset through a foreach loop to do some testing.

foreach ($read as $key => $data)
{
if ($key == 'bodyart')
$bodyart = explode(", ", $data);
}

Then in my template I have a series of checkboxes, here is one for example:

<input type="checkbox" name="bodyary[]" value="Pierced tongue"
<if condition="in_array('Pierced tongue', $bodyart)">checked</if>
/>

This is attempting to see if the value for this checkbox is in the bodyart array, and if it is, then print checked so the box is checked.

When I attempt to save the template in the ACP I get this message:

Warning: in_array(): Wrong datatype for second argument in /includes/adminfunctions_template.php(3537) : eval()'d code on line 364

In my source file for the page, I have declared $bodyart as global and as an array, but it still gives me the error.

global $bodyart;
$bodyart = array();

If I click continue and go on and save the template, and load the page, it shows the proper box checked. So technically it works. But how do I keep from getting this warning on my template for each instance of that array?

Not sure whats wrong with your php

but your html should be checked="checked"

Princeton
03-09-2006, 12:33 PM
have you tried adding:
<input type="checkbox" name="bodyary[]" value="Pierced tongue"<if condition="in_array('Pierced tongue', $bodyart)"> checked="checked"</if> />on another template? (just to see if it allows you to save it)

if it goes thru .. it's something else on your template

LWillmann
03-09-2006, 12:38 PM
The problem started when I added the <if condition> to the template. Otherwise, the template saves just fine.

I fixed the checked="checked" thing, thanks for that!

Oh, and fixed a typo above, $bodyary should be $bodyart

Marco van Herwaarden
03-09-2006, 01:24 PM
If you do an implode, the result i a string, not an array.

LWillmann
03-09-2006, 03:23 PM
I do an explode when I read the data to turn it back into an array. You should see it in the code above.

LWillmann
03-13-2006, 07:11 PM
anyone else have an idea?

merk
03-13-2006, 08:32 PM
Because saving the template processes it to provide that warning, it isnt possible to do anything about it short of supressing the error.

Try @in_array(), other than that you'll have to live with it.

Paul M
03-13-2006, 09:05 PM
Have you tried using a different name for that array since you've used bodyart for the name of the input field as well.

LWillmann
03-15-2006, 01:38 PM
It has nothing to do with the field name, or the array name.

I did another test.

I edited index.php in the forum root folder and added the $bob lines below:

eval('$navbar = "' . fetch_template('navbar') . '";');
$bob = array();
$bob[]="testing";
eval('print_output("' . fetch_template('FORUMHOME') . '");');

then I edited the FORUMHOME template and added the conditional line below:
<if condition="in_array('testing',$bob)"><!-- It is there --></if>

When I attempted to save the template, I get this:
The following error occurred when attempting to evaluate this template:

Warning: in_array(): Wrong datatype for second argument in /includes/adminfunctions_template.php(3537) : eval()'d code on line 14

This is likely caused by a malformed conditional statement. It is highly recommended that you fix this error before continuing, but you may continue as-is if you wish.

If I click continue and save the template anyway, it saves just fine, if I reload the forum home page, I see my comment in the source. So I know that it's reading the array properly.

There MUST be a way to declare custom arrays and have the template system not error out.

merk
03-15-2006, 10:39 PM
Did you add in_array to the allowed functions list? You have to realise that the evaluation of a template at that point of saving a template does not touch (or read) the forumhome code in any way.

Use the warning supressor.

LWillmann
03-16-2006, 03:24 PM
in_array is one of the 'safe functions' in vBulletin based on what the documentation says.