Kindof a php question.. but..re: adding to arrays |
[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(); 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?
[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( $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?
[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");
[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: <? if ($row['requires_code']) { ?><p><label>Shortcode:</label><input name="Shortcode" id="Shortcode" value="<? echo $row['Shortcode'];?>" type="text"/></p><? } ?> 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. They claim this should work fine: Code: Ariz Jacinto Blarg!
[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. They claim this should work fine: Code: Ariz Jacinto 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
[eluser]Phil Sturgeon[/eluser]
Don't see a need to push them in here. Why not just use basic array syntax. Code: $editData = array(); Does the exact same thing.
[eluser]xwero[/eluser]
the DRY version Code: $editData = array(); |
Welcome Guest, Not a member yet? Register Sign In |