www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29

Thread: 2 arrays with simplexml load string failing

  1. #16
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    ShrineDesigns, after swapping email with one of the developers over at WordPress.org support, I learned that the WP get_currentuserinfo() may not be able to query on custom fields in the WP DB, but that wp_get_current_user could.

    With that in mind and using some of your code examples, I've come up with a hybrid version that's still not exactly correct syntax and wanted to run it by you. This is what I have that's throwing an - unexpected T_ARRAY error on my line that begins with - $custom_field =. Using the WP codex support suggestion, this is what my code looks like now:

    PHP Code:
    // Call WordPress function get_currentuserinfo()
    // For 4 standard WP fields and wp_get_current_user()
    // To call 4 custom WP user fields
        /**
         * @example Safe usage: $current_user = wp_get_current_user();
         * if ( !($current_user instanceof WP_User) )
         *     return;
         */

    function get_currentuserinfo() {
    global 
    $user_firstname$user_lastname$user_email$user_login;
          
    get_currentuserinfo();

    function 
    wp_get_current_user() {
    $custom_field = ($data) array('mepr-field' => $user ));
    $data $custom_field->{'mepr-field'};
    if ( 
    $current_user->array_key_exists'mepr-field' ) ) 
         
    $current_user->get'mepr-field' ) ;
         
    global 
    $mepr-address-one$mepr-address-city$mepr-address-state$mepr-address-zip;
          
    wp_get_current_user();

        
    $data = array(
            
    'firstname' => $data['user_firstname'],
            
    'lastname' => $data['user_lastname'],
            
    'email' => $data['user_email'],
            
    'username' => $data['user_login'],
         );

        
    // address
        
    if (array_key_exists('mepr-address-one'$user))
            
    $data['address'] = $user['mepr-address-one'];
        elseif (
    array_key_exists('mepraddressone'$user))
            
    $data['address'] = $user['mepraddressone'];
        
    // city
        
    if (array_key_exists('mepr-address-city'$user))
            
    $data['city'] = $user['mepr-address-city'];
        elseif (
    array_key_exists('mepraddresscity'$user))
            
    $data['city'] = $user['mepraddresscity'];
        
    // state
        
    if (array_key_exists('mepr-address-state'$user))
            
    $data['state'] = $user['mepr-address-state'];
        elseif (
    array_key_exists('mepraddressstate'$user))
            
    $data['state'] = $user['mepraddressstate'];
        
    // zip
        
    if (array_key_exists('mepr-address-zip'$user))
            
    $data['zip'] = $user['mepr-address-zip'];
        elseif (
    array_key_exists('mepraddresszip'$user))
            
    $data['zip'] = $user['mepraddresszip'];

        return 
    $data;
        }

    I removed two fields from the WP user array and added them to the $query_vals array when I discovered I can't get the WP user password out of the DB in plain text because it's encrypted, so I'm going to pass a static value of the same password for all users to the receiving site and because the country will always be the US, I moved that field up also.

    How is my syntax incorrect?

  2. #17
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    ShrineDesigns, by making my declarations under the global for wp_get_current_user, now in the code for the custom mepr-fields, why couldn't I lose the if and elseif statements and just do the code like this:

    PHP Code:
    $data['address'] = $user['mepr-address-one'];
            
    $data['city'] = $user['mepr-address-city'];
            
    $data['state'] = $user['mepr-address-state'];
            
    $data['zip'] = $user['mepr-address-zip']; 
    Won't this work?

  3. #18
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    dashes were removed, so that ‘my-custom-field’ became ‘mycustomfield’
    http://goo.gl/x5deHi that's why the if/elseif are there (i.e. bad foresight leads to inconsistency, gg word press).
    are you trying to type-cast or multi-assign here?
    Code:
    $custom_field = ($data) array('mepr-field' => $user ));
    example of type-casting
    PHP Code:
    <?php
    $x 
    1;
    $arr = array('foo' => 'bar');
    var_dump(
        
    $x// (int) 1
        
    (bool) $x// (bool) true
        
    (string) $x// (string) "1"
        
    (object) $arr // object(stdClass){"foo" => "bar"}
    );
    ?>
    multi-assignment is the similar to declaring a variable, but you do it with multiple variables (typical use-case: establish common default values)
    PHP Code:
    <?php
    $x 
    $y 0// both $x and $y have the value of 0
    // a little more trickier but the same principle
    $x = ((bool) $y 0); // $x is cast to false and $y is 0
    ?>
    i suggest when naming global variables, functions, and/or classes that extend or are in tandem with scripts that are not your own to not use the naming conventions they use. this could cause naming conflicts down the road.
    PHP manual: user-defined functions
    you know that word press declares get_currentuserinfo() but you define a function with the same name.
    using global variables is good and all. but you still have to follow PHP's variable naming conventions. using dashes in a variable is not permitted:
    Code:
    global $mepr-address-one, $mepr-address-city, $mepr-address-state, $mepr-address-zip;
    valid variable names:
    PHP Code:
    global $mepr_address_one$mepr_address_city$mepr_address_state$mepr_address_zip
    Last edited by ShrineDesigns; 05-01-2014 at 03:15 PM.

  4. #19
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    Thanks for the direction. My code has undergone several changes in the last three hours. I have the correct array format now to get user data, but my post string is choking because of my inability to use the right syntax. Here's what it looks like now:

    PHP Code:
    // Call WordPress function get_currentuserinfo()
    // For 4 standard WP fields and wp_get_current_user()
    // To call 4 custom WP user fields
        /**
         * @example Safe usage: $current_user = wp_get_current_user();
         * if ( !($current_user instanceof WP_User) )
         *     return;
         */

    function get_currentuserinfo() {
    global 
    $current_user;

    function 
    wp_get_current_user() {
    $current_user wp_get_current_user ();

        
    $data = array(

            
    'firstname' $current_user->user_firstname '\n',
            
    'lasttname' $current_user->user_lastname '\n',
            
    'username' $current_user->user_login '\n',
            
    'email' $current_user->user_email '\n',
            
    'address' $current_user->mepr-address-one '\n',
            
    'city' $current_user->mepr-address-city '\n',
            
    'state' $current_user->mepr-address-state '\n',
            
    'zip' $current_user->mepr-address-zip '\n',
        );    

        return 
    $data;
        }
    }  
    // Generate the POST string
    $postdata '';
    foreach(
    $query_vals as $key => $value) {
         
    // since $key and $value are used in the outer loop
         // you need to use different variable names for the inner loop
        // otherwise you'll over-write $key and $value each time!
    foreach ($data as $keys ->  $values {'$\n'} ); 
        
    var_dump($keys$values);
        
    $postdata .= $s urlencode($key) . '=' urlencode($value)
                . 
    '&' urlencode($keys) . '$\n' urlencode($values);
        
    $s '&';

    I'm almost there I believe, but it's failing on the foreach ($data line. Thoughts?

  5. #20
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    ShrineDesigns - I understand the elseif statements now, but I believe the format I put the array into now may be correct (minus needing the statements) because it came from the WP codex and the code doesn't choke with parse errors until it get to the post string.

    So, is the answer to merge most of your code into the array format I'm using and change the post string?

  6. #21
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    ShrineDesigns - What I just read is this:

    If you have a variable key or a key with dashes, there are prettier aliases which you can use:

    if ( $current_user->has_prop( 'my-field' ) ) )
    echo '<p>
    ' . $current_user->get( 'my-field' ) . '
    </p>';

    Can part of this code be used without using else statements? Because every user registering will (always) have these custom-fields.

  7. #22
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    For your second foreach loop, change the -> to => . Also, you're missing the { } for your second foreach loop, so code starting with var_dump($keys, $values); is only in the first foreach loop, not in the second. If you're trying to write a nested foreach loop (which I think you are since 1 is inside the other), it would be:

    PHP Code:
    foreach($query_vals as $key => $value) {
        foreach(
    $data as $keys => $values) {
            
    var_dump($keys$values);
                    
    $s '&';
            
    $postdata .= $s urlencode($key) . '=' urlencode($value)
                . 
    '&' urlencode($keys) . '$\n' urlencode($values);
            
        }    

    I didn't include the '$\n' because I wasn't sure whether you wanted that in the nested foreach loop or where it was meant to be used. I also moved the $s, however, I haven't read all of your code so if you were re-assigning $s to '&' then keep it where you had it. If you never defined $s until in the nested foreach loop, then keep it where I moved it to.

  8. #23
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    Quote Originally Posted by NetMonkey View Post
    ShrineDesigns - What I just read is this:

    If you have a variable key or a key with dashes, there are prettier aliases which you can use:

    if ( $current_user->has_prop( 'my-field' ) ) )
    echo '<p>
    ' . $current_user->get( 'my-field' ) . '
    </p>';

    Can part of this code be used without using else statements? Because every user registering will (always) have these custom-fields.
    you can use the short-hand syntax for if/else
    PHP Code:
    $myvar = ($current_user->has_prop('my-field')) ? $current_user->get('my-field') : NULL// you can switch "NULL" for a default or fallback value 
    I typically use that when checking GET data, example:
    PHP Code:
    $id = (isset($_GET['id'])) ? (int) $_GET['id'] : NULL;

    if (!
    $id) {
        echo 
    'oops... invalid id received.';

    Last edited by ShrineDesigns; 05-01-2014 at 07:20 PM.

  9. #24
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    ShrineDesigns - thanks for the code tip. I declared my variable and the code ran through the array lines without any errors, hit line 55 which starts my foreach line in in post string section and choked. It's whining about array name $data being an undefined variable.

    Then it throws a warning - Invalid argument supplied for foreach(). Looks like assigning the $data variable and the correct post string is all that may be left to nail. Here's the code:

    PHP Code:
    function get_currentuserinfo() {
    global 
    $user_firstname $user_lastname$user_login$user_email;
          
    get_currentuserinfo();

    function 
    wp_get_current_user() {
    $current_user wp_get_current_user ();
    $myvar = ($current_user->has_prop('my-field')) . $current_user->get('my-field') . '\n';
        
        
    $data = array(

            
    'firstname' $current_user->user_firstname '\n',
            
    'lastname' $current_user->user_lastname '\n',
            
    'username' $current_user->user_login '\n',
            
    'email' $current_user->user_email '\n',
            
    'address' $current_user->mepr-address-one '\n',
            
    'city' $current_user->mepr-address-city '\n',
            
    'state' $current_user->mepr-address-state '\n',
            
    'zip' $current_user->mepr-address-zip '\n'
        
    );    

        return 
    $data;
        }
    }       
      
    // Generate the POST string
    $postdata '';
    foreach(
    $query_vals as $key => $value) {
        
    $postdata .= $key.'='.urlencode($value).'&';
    foreach(
    $data as $keys => $values) {
            
    var_dump($keys$values);
        
    $postdata .= $keys'$\n' urlencode($values) . $s '&';
            
        }    

    Thoughts? Error404, post string is failing...

  10. #25
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    I assume you are using the data from the functions we've been working on in the foreach() loops.
    PHP Code:
    $postdata '';

    foreach(
    $query_vals as $key => $value) {
        
    $postdata .= $key.'='.urlencode($value).'&';
    }
    $data wp_get_current_user();

    foreach(
    $data as $keys => $values) {
        
    var_dump($keys$values);
        
    $postdata .= $keys'$\n' urlencode($values) . $s '&';

    I'm not entirely sure why the need for a nested foreach(). this should give you a good starting point.

  11. #26
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    I'm using the nested foreach's, because I was told if I didn't, that the results of the first array would overwrite the second, or maybe because it would cause a loop, I don't remember. Anyway, after all of the code changes today, I've overcome all of my syntax errors and now just get the nondescript error - Status:fail.

    Man this is time consuming... Can I insert some $print statements somewhere for the first and second arrays to print to screen so I can tell where things are failing? I'm beginning to think a smarter approach may be to recode things into one array. Thoughts? About anything?

  12. #27
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    Would it help for me to re-post (all) of the code?

  13. #28
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    BTW, I meant to post this earlier. I've come to learn in the past few weeks through my experience with this one little 3kb file that's kickin' my butt, that I have some deep respect for you guys that code in php day in and out and I give you big props for all of the coding issues you must deal with daily to get it right.

    I'm normally a website designer/developer and graphic design guy. Not a hardcore coder or hacker. So I really appreciate the help here guys.

  14. #29
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    you can use print_r() or var_dump() to print data anywhere in your script(s).
    note: if you use setcookie() or header() it may interfere with those if you do not use output buffer extension (e.g. ob_start(), ob_flush(), etc.).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles