SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: php code within an array

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Location
    England
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php code within an array

    i want to store multiple functions and $POST form details within an array, thus making it easier to store into a database.

    but when i echo the results back it puts the php code in a form of a string,

    PHP Code:
    $formArray = array(date('d-m-Y'),
        
    'validationCheckShortStr',               '($_POST ["rf_name"]);',
        
    'validationCheckShortStr',               '($_POST ["rf_surname"]);',
        
    'validationCheckShortStr',               '($_POST ["rf_badge"]);',
        
    'validationCheckEmail',                   '($_POST ["rf_email"]);',
        
    'ValidationEmailCompare',              '($_POST ["rf_email"], $_POST ["rf_conemail"]);',
        
    'checkAge',                             '($_POST ["rf_months"], $_POST ["rf_days"], $_POST ["rf_years"]);',
        
    'validationCheckLongStr',               '($_POST ["rf_address"]);',
        
    'validationCheckShortStr',              '($_POST ["rf_phone"]);',
        
    'validationCheckShortStr',             '($_POST ["rf_secret"]);',
        
    'validationCheckShortStr',               '($_POST ["rf_emergencyName"]);',
        
    'validationCheckNum',                   '($_POST ["rf_emergencyNumber"]);',
        
    'validationWordMatch',                   '($_POST ["rf_additionalNotes"]);',
        
    'termsSet',                               '($_POST ["rf_terms"]);'); 
    is there anyway i can display code without it printing out as plain text

  2. #2
    SitePoint Addict Phidev's Avatar
    Join Date
    Oct 2008
    Location
    Texas
    Posts
    204
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you want to execute code from within the array? If that is the case then you need to use the function

    eval(String code);

    check the manual for more info on eval()

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you want to store php code into a database? A better solution to your problem almost certainly exists.

    You shouldn't need to write php code dynamically. If you think you do, you're probably going about it wrong.

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Location
    England
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Why do you want to store php code into a database? A better solution to your problem almost certainly exists.

    You shouldn't need to write php code dynamically. If you think you do, you're probably going about it wrong.
    i dont want to store php code in the database but i want to execute the validation check first when running through the array and once it has checked i can then perform a loop which skips two indexes each time so it will only insert the values into the database.

    i want to create a script which validates and inserts into a database without needing to update the variables when a change has been made. all that is required it adding the validation proceedure and the form $_POST into the array.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you want a function.
    http://www.php.net/manual/en/language.functions.php

    call_user_func() and call_user_func_array() may be useful.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,849
    Mentioned
    16 Post(s)
    Tagged
    1 Thread(s)
    'validationCheckShortStr', '($_POST ["rf_name"]);'
    'validationCheckShortStr', '($_POST ["rf_surname"]);'
    'validationCheckShortStr', '($_POST ["rf_badge"]);'
    ... and anther 3
    Start with them. All these keys in the $_POST array need to run through the validationCheckShortStr ( ) function.

    So change your function to accept an array as well as a single value, or, just loop through them:

    $singles = array('rf_name','rf_surname','rf_badge'); // you have 3 more of these

    $b = array_map("validationCheckShortStr" , $singles );

    It'll be important that you catch the return value of you validation functions in a consistent manner, or you will get confused.

    If it fails on one, then stop the script, if its all clear, then move on to all the others.

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    Ontario, Canada
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Am I misunderstanding or is this a simple problem of using single quotes? You should remove all single quotes from around your $_POST variables so that PHP parses them as PHP and not as text.
    Brock Ferguson
    Lead Developer, Caribou CMS
    A Subscription/Membership CMS and Ecommerce Platform - FREE Trial

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •