Welcome Guest, Not a member yet? Register   Sign In
Kindof a php question.. but..re: adding to arrays
#1

[eluser]octavianmh[/eluser]
Guys-

So I'm working on a form which updates a database..thing is I'm trying to make this relatively generic, and the form — depending on what's being edited — can have a variable number of fields. Getting the form to work has been easy-peasy...getting it into the model to execute the update has been harder.

Here's an excerpt:

Code:
$editData = array();

    if ($this->input->post('Shortcode')) { array_push($editData['Shortcode'], $this->input->post('Shortcode')); }
    if ($this->input->post('ListName')) { array_push($editData['ListName'], $this->input->post('ListName')); }
    if ($this->input->post('InjectPassword')) { array_push($editData['InjectPassword'], $this->input->post('InjectPassword')); }
    if ($this->input->post('APIusername')) { array_push($editData['APIusername'], $this->input->post('APIusername')); }    

    $this->service->editService($this->input->post('service_id'), $editData);

But I get this error:

Message: array_push() [function.array-push]: First argument should be an array

While I THINK i'm following the instructionf from: http://us2.php.net/array_push

Down the page a bit they show the method for inserting key=>value pairs into an array_push.

Anyway, probably something dumb, any ideas?
#2

[eluser]TheFuzzy0ne[/eluser]
I don't think you even need to bother with array_push. It just seems to complicate things in your case:

Code:
$editData = array(
        'Shortcode' => $this->input->post('Shortcode'),
        'ListName' => $this->input->post('ListName'),
        'InjectPassword' => $this->input->post('InjectPassword'),
        'APIusername' => $this->input->post('APIusername')
    );

$this->service->editService($this->input->post('service_id'), $editData);

$this->input->post() returns FALSE if the specified key doesn't exist in the post array. I trust you're validating the input before sending it to the model?
#3

[eluser]Dam1an[/eluser]
for future reference, array_push adds the item to the end of the array, so you the array itself is the paramiter, not an item in the array
Code:
array_push($my_array, "New item");
#4

[eluser]octavianmh[/eluser]
Thanks for the help guys, though I'm not quite happy with inserting "0"s into all of my un-set form fields.. I'd rather they remain empty/null?

Trouble with validation is that my form configures itself depending on the "service" in question, for example:
Code:
&lt;? if ($row['requires_code']) { ?&gt;<p><label>Shortcode:</label>&lt;input name="Shortcode" id="Shortcode" value="&lt;? echo $row['Shortcode'];?&gt;" type="text"/&gt;&lt;/p>&lt;? } ?&gt;

And thus only the fields required for the particular service are returned for updating. I.e. I hide the irrelevant fields from the end-user.

So I guess I need to insert a big-ass if statement before validation runs to determine which rules to enforce.

Anyway, I don't really love the standard array() method recommended above, I wish array_push were working like the php documentation says it would. Smile They claim this should work fine:

Code:
Ariz Jacinto
27-Jun-2008 11:41
if you're going to use array_push() to insert a "$key" => "$value" pair into an array, it can be done using the following example:

    array_push($data[$key], $value);
    ...
    array_push($data[$key], $value);

Blarg! Smile
#5

[eluser]Dam1an[/eluser]
[quote author="octavianmh" date="1240506848"]
Anyway, I don't really love the standard array() method recommended above, I wish array_push were working like the php documentation says it would. Smile They claim this should work fine:

Code:
Ariz Jacinto
27-Jun-2008 11:41
if you're going to use array_push() to insert a "$key" => "$value" pair into an array, it can be done using the following example:

    array_push($data[$key], $value);
    ...
    array_push($data[$key], $value);
[/quote]

First of all, 'they' in this case is refering to a user, not an author of the PHP project
Secondly, the description states
Quote:array_push() treats array as a stack, and pushes the passed variables onto the end of array . The length of array increases by the number of variables pushed.
which states the item gets added to the end of the array

It then goes on to state if you just want to add a single value, use th $array['key'] = ... notation

Hope this clears things up a bit
#6

[eluser]Phil Sturgeon[/eluser]
Don't see a need to push them in here. Why not just use basic array syntax.

Code:
$editData = array();

    if ($this->input->post('Shortcode')) { $editData['Shortcode'] = $this->input->post('Shortcode'); }
    if ($this->input->post('ListName')) { $editData['ListName'] = $this->input->post('ListName'); }
    if ($this->input->post('InjectPassword')) { $editData['InjectPassword'] = $this->input->post('InjectPassword'); }
    if ($this->input->post('APIusername')) { $editData['APIusername'] = $this->input->post('APIusername'); }    

    $this->service->editService($this->input->post('service_id'), $editData);

Does the exact same thing.
#7

[eluser]xwero[/eluser]
the DRY version
Code:
$editData = array();

foreach(array('Shortcode','ListName','InjectPassword','APIusername') as $expected)
{
   if ($this->input->post($expected)) { $editData[$expected] = $this->input->post($expected); }
}

$this->service->editService($this->input->post('service_id'), $editData);




Theme © iAndrew 2016 - Forum software by © MyBB