Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Yeah, there’s something odd on how arrays are stored as metadata. This results in the odd structure you see when using array_push(). I’m not sure of the solution because I don’t fully understand what’s happening, but I think it lies in the direction of either $voter_ips[] = $ip; or array_push($voter_ips[0], $ip);

    Or maybe the first pass is the problem? Is there a $voter_ips = array(); before all this starts? Somehow, an extra dimension is being introduced and you need to counteract that. It may take writing a var_dump() from various points to a file. Then analysis of how and where the array dimensions are growing will tell you what you need to do.

    Sorry for the rambling, that’s what I do when I don’t have a real answer 🙂

    Thread Starter r0bbiem

    (@r0bbiem)

    Thanks bcworkz

    I tried some of your suggestions but in the end I’m getting around it like this

    $ip = $_SERVER['REMOTE_ADDR'];
    $id = $_POST['post_id'];
    $voter_ips = get_post_meta($id, 'voter_ips', true);
    $voter_ips_array = explode(",", $voter_ips);
    
    //use $voter_ips_array for comparison
    
    if(strlen($voter_ips) == 0)
    {
    	$voter_ips .= $ip;
    }
    else
    {
    	$voter_ips .= ',' . $ip;
    }
    update_post_meta($id, 'voter_ips', $voter_ips);

    Not ideal because I’m also storing a date along with my vote which I’d like to cross reference so saving and retrieving a multi-dimensional array would seem to be the most elegant solution but for now this will have to do.
    I’m planning on storing the ips and dates as alternate values and then using some kind of +1 or -1 system to access and compare the two.

    If anyone has any more insight on this I’d love to be enlightened.

    If you’re going to do this properly don’t use the posts meta data to save the values. That will work up until a certain point, but when you get a lot of votes it’s going to cause bottlenecks at best and crashes at worst.

    The ideal solution to this is to create a new table in your database that stores votes that are referenced to each post and hods the data of IP address, date/time, etc. You can add to this with whatever otehr columns you need, like ranking or something for a +/-1 situation. This lets you easily look up IP addresses, total votes, total scores and more all through the database queries which is a whole lot faster and more efficient then trying to process multi-dimensional arrays that could easily be massive.

    Thread Starter r0bbiem

    (@r0bbiem)

    Thanks catacaustic, I’m looking into that approach now.

    Couple of questions…for something like this is it ok to do all my mysql interactions using the $wpdb class, or am I better off using vanilla php and mysql?

    and

    I have some $wpdb code for creating a table, but it strikes me that this might be something that only has to be done once ever. That leaves me wondering where to put the code…putting it in my plugin doesn’t seem to make sense. Am I wrong?

    Moderator bcworkz

    (@bcworkz)

    Whether to use $wpdb for non-WP tables or not might be somewhat debatable, but I think $wpdb is a good approach as long as your tables are in the same database as WP.

    The code for creating a table by plugin would called from the plugin’s activation hook. You still need to wrap it in a conditional that checks if the table exists since people deactivate & reactivate plugins for various reasons.

    You should use $wpdb. That’s the tried-and-tested way of doing things. As for creating the table as a once-off, there’s some graet information at http://codex.wordpress.org/Creating_Tables_with_Plugins that will answer that and a whole lot more. Having the code for creating the database table/s in your plugin depends on what you’re going ot do with it. If you’re going to only use that plugin for yourself then it’s not a big deal, but if you’re going to distribute it any time in the future it’s best to write it in so that it’s all contained in the one place.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘update_post_meta array problem’ is closed to new replies.