• Resolved loopforever

    (@loopforever)


    Hi,
    In the same file, there are sequential action definitions:

    //First Action:
    do_action('vse_da_update',$id,$data[$rkey],$status);

    //Second Action:
    do_action('second_vse_da',$id);

    I want to use the variable I created in the first one in the function.php file in the second one. However, the variable returns null. What can I do for this problem? Firstly, I have defined the variable as global. However, I did not get any results.
    (Same order again)

    function.php

    add_action('vse_da_update', 'new_vse_da_update', 10, 2);
    function new_vse_da_update ($id, $data){
    $new_value[$id] = $data;
    }
    add_action('second_vse_da', 'new_vse_da_update', 10, 2);
    function new_vse_da_update ($id){
    add_user_meta($id, $new_value);
    }

    Why am I not doing this action in the same action?
    Because the action named vse_da_update is defined in a foerach loop. I want to store all the data in a single array in the database.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Dion

    (@diondesigns)

    You must globalize the variable in each function where you want to access it. To do this, add the following as the first line in all functions where you want to use $new_value, and it will be available to the function:

    global $new_value;

    Thread Starter loopforever

    (@loopforever)

    As I said before, yes, I tried that but I can only get the last value.
    $new_value works in a foreach loop. So the first hook is defined in a loop. Because of this, I can’t get other values. I can only get the last inserted value
    .

    foreach($x as y){
    //...codes
      foreach($a as $b){
    //...codes
    //First Action in here
    do_action('vse_da_update',$id,$data[$rkey],$status);
      }
    }

    Just to be sure, I’ve redid the definition you said:

    add_action('vse_da_update', 'new_vse_da_update', 10, 2);
    function new_vse_da_update ($id, $data){
    global $new_value;
    $new_value[$id] = $data;
    }
    
    add_action('second_vse_da', 'new_vse_da_update', 10, 2);
    function new_vse_da_update ($id){
    global $new_value;
    add_user_meta($id, $new_value);
    }
    Dion

    (@diondesigns)

    You must declare the variable as global just before it is created. Did you do that, and also declare it as global in every function where it is used?

    I’m not sure how your code can even work…you have defined two functions with the same name, which will generate a PHP fatal error.

    You have also added an action with two parameters, but the function it calls is defined with only one parameter. I suspect that will generate a PHP fatal error as well.

    For what it’s worth, it looks like you should be calling your functions directly and not messing with actions.

    Thread Starter loopforever

    (@loopforever)

    Yes, you’re right, I didn’t see that. Sorry. Of course, action is not like this normally.
    I’ve been struggling for a few days but couldn’t find a solution. I would really appreciate if you can help. I have prepared a detailed file for you:

    The file content I’m processing on: Link
    All file content: Link

    Moderator bcworkz

    (@bcworkz)

    In new_meta_for_member() you have $get_meta_value[$req_key] = $data;, but $req_key is not assigned any value. Remember that all variables are local in scope to the function unless explicitly declared global.

    Elsewhere, you save the entire $get_meta_value array to user meta. Is that really what you want? Seems to me you’d only want to save the individual keyed value for each user. But then I don’t fully follow your intent.

    There may be other issues, I only took a quick look.

    Thread Starter loopforever

    (@loopforever)

    Thank you very much for your response.
    “In new_meta_for_member() you have $get_meta_value[$req_key] = $data;, but $req_key is not assigned any value. “

    If you have noticed, this value is defined in the first foreach. So it’s not undefined.
    The only problem is to use this variable ($get_meta_value) in the other hook.
    How do I know it’s okay?
    If I save as meta key (add_user_meta) in the first hook, all values ​​are successfully saved in the database? Code Image Link

    What is the problem in such a case? : New meta key is generated repeatedly for values ​​in each loop.
    For example, in the database,

    ————meta_key———————–meta_value
    Loop 1 :member_new_meta—————a
    Loop 2: member_new_meta—————a,b
    Loop 3: member_new_meta—————a,b,c

    “Seems to me you’d only want to save the individual keyed value for each user. “
    Yes, I want to do exactly that. I’ll use them elsewhere later. But for now this is not important. The important thing is to save the data “under a single meta key” and “as an array”.

    What happens if I define global as you mentioned at the beginning?
    Only the final value is assigned. Consider the example above, in such a case not the value a,b,c; I can only get the value of c. Also, only one meta key occurs.
    meta_key————————meta_value
    member_new_meta ———–c

    • This reply was modified 4 years, 8 months ago by loopforever.
    • This reply was modified 4 years, 8 months ago by loopforever.
    • This reply was modified 4 years, 8 months ago by loopforever.
    • This reply was modified 4 years, 8 months ago by loopforever.
    • This reply was modified 4 years, 8 months ago by loopforever.
    • This reply was modified 4 years, 8 months ago by loopforever.
    • This reply was modified 4 years, 8 months ago by loopforever.
    • This reply was modified 4 years, 8 months ago by loopforever.
    • This reply was modified 4 years, 8 months ago by loopforever.
    Moderator bcworkz

    (@bcworkz)

    First foreach?? I don’t see any foreach in the function new_meta_for_member(). I’m looking at:

    function new_meta_for_member($m_key,$id,$data,$status){
    global $get_meta_value;
        //The meta key assigned in the admin panel. 
       if($m_key == 'info_member'){
            $get_meta_value[$req_key] = $data;
    
        }
    }

    A foreach in some other calling process is of no help. Remember, all PHP variables are local in scope to the function unless declared global. A variable assigned a value in a different function has no bearing in new_meta_for_member().

    The values in global $get_meta_value will be saved in user meta alright, but the information passed as $data which you assigned to $get_meta_value[$req_key] will not be saved because the $req_key index is invalid as mentioned above.

    You can verify data is saved in the DB by using the phpMyAdmin app and examining the table directly. Newly added data is typically located at the end of the table. Arrays are saved as a serialized string. They’re a little difficult to parse on sight, but if you’re familiar with the array structure and the data it contains you should be able to make sense of it.

    While it’s possible to directly edit data via phpMyAdmin, I advise that you do not try to do so unless you’re very sure you know what you’re doing. And even then, make a backup before proceeding. It’s better to look but don’t touch.

    I don’t understand the nature of data in $get_meta_value. Maybe it’s OK to save the entire thing in user meta. But instead of incrementally adding meta data in each step of the loop and saving each, collect all data into the array first. Only when all data has been assigned should you save it all to user meta. This entails restructuring your code. TBH, it could stand some restructuring anyway. It seems more convoluted than is necessary.

    I think I’ve answered your last question above, but I’m not sure I truly understand your last question. $get_meta_value is global. Its keys are presumably unique, so data saved under different keys will all persist until overwritten or unset. You can assign individual values to each key at different points and all other data in the array will persist. In fact, as a global, before you attempt to start the process on another user (maybe there is no other user, but let’s say there is for the purpose of discussion), you’d need to unset all earlier values, or reinitialize $get_meta_value as an empty array. Otherwise you risk one user getting another’s data.

    OTOH, if it were local to a function, once the function returns, all assigned values are gone. Re-entering the function would be as if the variable never existed, it’d have to be reassigned all over again. This behavior can at times be an advantage, other times it’s a curse 🙂

    Thread Starter loopforever

    (@loopforever)

    Thanks again for your answer. Especially for taking the time to comment on this.
    I guess I’ll try to find another way. To answer your question, I would like to complete the section below.
    Isn’t it enough to be in the same loop? Am I wrong?
    “First foreach?? I don’t see any foreach in the function new_meta_for_member(). I’m looking at:”

    It’s here: Image Link

    Update: I read it again. I think this is your answer: “A foreach in some other calling process is of no help. Remember, all PHP variables are local in scope to the function unless declared global.”

    I want to ask one last thing to find another solution. Thank you again for your time.

    Below are two scenarios. The first one is the scenario that I have been dealing with for a few days, and which is the main question of this subject. The second is the “other way”, as I said at the beginning.

    1-) Collecting all the data in the registration form under a single meta key. There are about 30 text fields. Hence creating a serialized array.

    2-) The alternative is to create a separate meta key for each text field. The plugin allows this, as I said earlier. For example, a separate metadata for the date of birth, a separate metadata for the hobby … Again, of course, there are about 30 text fields.

    If I go the 2nd way, will I load too much into the database? Would this turn out to be a major performance issue for me? Actually, that’s why I’ve been trying to do the first scenario for days. I think there will be more queries as there are multiple rows and this will bloat the system. Am I wrong ?

    Where will I transfer this data?
    For each data–>, wp-admin>users>(any user)edit

    • This reply was modified 4 years, 8 months ago by loopforever.
    • This reply was modified 4 years, 8 months ago by loopforever.
    Moderator bcworkz

    (@bcworkz)

    I read it again. I think this is your answer…

    Right! Now you see 🙂

    Your subsequent questions indicate you’ve encountered the classic dilemma about WP meta data. You’re right, everything in an array under a single key is more efficient in storage. But the array keys in the first scenario still take up space, which you wouldn’t have under the second scenario. The gains are less than it may seem. How many users are you anticipating? Unless you’re anticipating thousands, I doubt it’ll materially affect DB operations. And 30-some rows per users isn’t any worse than we often see in post meta, per post.

    The most important consideration in deciding array or multiple keys is how you anticipate using this data to query for users. Using data in serialized arrays as query criteria is very sub-optimal. OTOH, if you will only be fetching this data once the user is known, an array is a great way to save the data.

    Furthermore, if you anticipate a huge number of users (thousands) AND you think you’ll be frequently querying for users based on this meta data, the default usermeta table is inefficient to start with. With a lot of users that need to be queried by certain data, that data is best kept in a custom table for the purpose. One that’s organized and indexed for optimal queries.

    It may be a hybrid solution would be optimal. Frequently queried data would have their own meta keys (if not their own table). Other user data that’s only needed once the user is known can all be kept in an array under a single meta key.

    It looks like the plugin already saves data under separate keys. You’re trying to optimize this by saving it all as an array? An admirable effort, but IMO it’s likely not worth the effort in terms of real world gains.

    Thread Starter loopforever

    (@loopforever)

    I understood.
    Thank you very much for your reply and suggestion.
    Best regards.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Passing Variables Between Hooks’ is closed to new replies.