Re: [Pro. PHP Dev.] Array by reference - mental block

0 views
Skip to first unread message
Message has been deleted

Robert Gonzalez

unread,
Sep 7, 2010, 12:25:39 PM9/7/10
to professi...@googlegroups.com
Since $_SESSION is a superglobal why not just run it through your function without passing it to it? Or better yet, when setting the values into the session, set them as NULL. 

On Tue, Sep 7, 2010 at 5:34 AM, Jeff <jeff...@gmail.com> wrote:
Mission:

To create a function which will 'clean up' the contents of an array so
it is ready to be inserted in a MySQL table or to use as a source for
updating a row in a MySQL table.

Background:

I'm using the dbWrapper class at http://www.ricocheting.com/code/php/mysql-database-class-wrapper-v3

Since I pre-populate all my fields in a form with "" if I have no
other default value, before using his db->insert() or db->update(), I
need to convert all those fields which have a submitted value of "" to
NULL.  Easy enough, foreach() through the array and if(trim($key) ==
"" or trim($key) ==  "Please select"), set the $val as "NULL".

Problem:

All the form data is in a $_SESSION['Something'] array.  Rather than
passing all the array elements TO the function which will do this
cleanup, then pass all the values back and substitute the new values
in the original array, I'd much prefer to update the
$_SESSION['Something'] array from within the function, but I'm not
getting the syntax right for updating directly to the array.

Here's what I have (but doesn't work);

       function dbPrep($Arr)
       {
               foreach($Arr as $key=>$val)
               {
                       if (trim($val) == '' or trim($val) == 'Please select')
                       {
                               $Arr[$key] = 'NULL';
                       }
               }

               return TRUE;
       }

Then down in the code, I'm calling it like this:

if (dbPrep($_SESSION['Something']))
{
 $primary_id = $db->insert(TABLE, $_SESSION['Something']);
}


Any suggestions on how to get this to update the
$_SESSION['Something'] array would be much appreciated!

Jeff

--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.

For information or project assistance please visit :
http://www.360psg.com

You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP

Jeff

unread,
Sep 8, 2010, 8:54:02 AM9/8/10
to Professional PHP Developers
Thanks Robert -

I essentially did what you suggest - just needed more time to figure
out the syntax. My problem was that I was trying to pass the entire
array name $_SESSION['Something'] to the function. That was not easy
to reference to ... but if I just passed the actual array name
'Something' to the function, then referred to $_SESSION[$Arr] within
the function, I was able to change the blanks ("") to NULL from within
the function.

Jeff

On Sep 7, 11:25 am, Robert Gonzalez
<robert.anthony.gonza...@gmail.com> wrote:
> Since $_SESSION is a superglobal why not just run it through your function
> without passing it to it? Or better yet, when setting the values into the

Robert Gonzalez

unread,
Sep 8, 2010, 12:25:47 PM9/8/10
to professi...@googlegroups.com
If you plan to use that functionality with other members of the session array then I can see passing the index you are after to the function as an argument. If the sole purpose of the function is massage that one particular session array member, you don't need to pass anything to the function at all, just use it as is. Just a thought.
Reply all
Reply to author
Forward
0 new messages