0

I am trying to create a multi criteria post rating function with simple form and post_meta and user meta.

The Goal

when users submits their ratings 2 things should happen:

1.the ratings is stored in the post_meta called 'wpd_rating' like this:

array(user id => array(
          field1 => 5
          field2 => 3
          field3 => 4
          ),
      another user id => array(
          field1 => 2
          field2 => 5
          field3 => 4
          )

) 

2.the ratings is stored in the user_meta called 'plgn_rating' like this:

array(post id => array(
          field1 => 5
          field2 => 3
          field3 => 4
          ),
      another rated post id => array(
          field1 => 2
          field2 => 5
          field3 => 4
          )

) 

The Code (Not working Obviously)

In single.php:

the header part:

<?php // top of page
if ( isset( $_POST['drw_inventory'] ) && wp_verify_nonce($_POST['drw_inventory'],'update_drw_postmeta') )
    { //if nonce check succeeds.
        global $post;
        $postid = $post->ID;
        $data = $_POST['wpd_function_rating'];
        $data2 = $_POST['wpd_feature_rating'];
        $data3 = $_POST['wpd_support_rating'];
        $currentusr = get_current_user_id();

        //*******************The post meta field Part****************************
        //Get the Existing user ratings of this post
        $ls_up_votes = get_post_meta($postid, 'wpd_rating');

        //if the current user already rated, unset the rating
        foreach ( $ls_up_votes as $key => $value )
          {
            if ( $key == $currentusr )
              {
                unset( $ls_up_votes[$key] );
              }
          }

    //Add post meta 'wpd_rating' with this structure: 
    //array(userid => array('wpd_function_rating' => '3','wpd_feature_rating' => '5','wpd_support_rating' => '4',))
        $up_vote = array($currentusr => array('wpd_function_rating' => $data,'wpd_feature_rating' => $data2,'wpd_support_rating' => $data3));

        //appending new user ratings after one another
        $ls_up_voted = array_merge($ls_up_votes, $up_vote);
        update_post_meta($postid,'wpd_rating',$ls_up_voted);



        //*******************The user field Part****************************
        //Get the Current user ratings of this post
        $user_rated_posts = get_user_meta($currentusr, 'plgn_rating',true);
        foreach ( $user_rated_posts as $key => $value )
          {
            if ( $key == $postid )
              {
                unset( $user_rated_posts[$key] );
              }
          }
        //add a user field 'plgn_rating' with this structure
        //(postid => array('wpd_function_rating' => '3','wpd_feature_rating' => '5','wpd_support_rating' => '4',))
        $usr_vote = array($postid => array('wpd_function_rating' => $data,'wpd_feature_rating' => $data2,'wpd_support_rating' => $data3));
        //appending new user ratings after one another
        $ls_up_voted = array_merge($user_rated_posts, $usr_vote);
        update_user_meta($currentusr,'plgn_rating',$ls_up_voted);

    }

?>

The Form:

<form method="post" action="">
   <?php wp_nonce_field('update_drw_postmeta','drw_inventory'); ?>
   <label>This is label</label>
   <input type='text' name='wpd_function_rating' value='' />
   <input type='text' name='wpd_feature_rating' value='' />
   <input type='text' name='wpd_support_rating' value='' />
   <input type='submit' value='save' />
</form>

Issues with the current code

  1. The array is not saving as I structured. I am pretty sure I am using the array_merge wrong. or the calling the get_post_meta wrong.

  2. The code is not checking for the existing user rating and replacing the old ratings with the new one.

Update

as s_ha_dum suggested tried this with no avail:

foreach ( $ls_up_votes as $key => $value ){ 
var_dump($key,$value);

   if ( $key == $currentusr ){ 
      unset( $ls_up_votes[$key] );
   }
} 

When I add the user input 2 times, the unset commands should remove the previous array that was added by the same user and add the new one, instead its adding all 2 times.Here is what I am getting:

Array ( [0] => Array ( [0] => [1] => Array ( [wpd_function_rating] => 33 [wpd_feature_rating] => 23 [wpd_support_rating] => 66 ) ) [1] => Array ( [wpd_function_rating] => 44 [wpd_feature_rating] => 11 [wpd_support_rating] => 32 ) ) 
5
  • What have you done to debug this?
    – s_ha_dum
    Commented Feb 4, 2014 at 14:40
  • Thanks for the reply. I am using the debogger plugin with no error/warning anywhere. and after submitting a rating I am getting this when I run get_post_meta: Array ( [0] => Array ( [wpd_function_rating] => 33 [wpd_feature_rating] => 12 [wpd_support_rating] => 66 ) )
    – Towfiq
    Commented Feb 4, 2014 at 15:01
  • A debugger is good, but you won't get an error for correct syntax with incorrect logic.
    – s_ha_dum
    Commented Feb 4, 2014 at 15:03
  • yes. I am afraid this is where I am wrong. the logic.. did you find any flaw in the logic I setup? thanks
    – Towfiq
    Commented Feb 4, 2014 at 15:07
  • Just posted an answer. Take a look.
    – s_ha_dum
    Commented Feb 4, 2014 at 15:11

1 Answer 1

0

Based on this:

Array ( 
  [0] => Array ( 
    [wpd_function_rating] => 33 
    [wpd_feature_rating] => 12 
    [wpd_support_rating] => 66 
  ) 
)

I can spot at least one problem. You have a nested array but are only looping over the top level. Here:

foreach ( $ls_up_votes as $key => $value )
      {
        if ( $key == $currentusr )
          {
            unset( $ls_up_votes[$key] );
      }
 }

Try var_dump($key,$value) inside that foreach.

I think that your solution is going to be to pass the third parameter to get_post_meta so that it returns "single" and not an array. It is a bit weird but works somewhat counter-intuitively with serialized arrays like I think you have.

$ls_up_votes = get_post_meta($postid, 'wpd_rating');
3
  • thanks tried as you suggested: foreach ( $ls_up_votes as $key => $value ){ var_dump($key,$value);if ( $key == $currentusr ){ unset( $ls_up_votes[$key] );}} with no avail. When I add the user input 2 times, the unset commands should remove the previous array that was added by the same user and add the new one, instead its adding all 2 times.Here is what I am getting: Array ( [0] => Array ( [0] => [1] => Array ( [wpd_function_rating] => 33 [wpd_feature_rating] => 23 [wpd_support_rating] => 66 ) ) [1] => Array ( [wpd_function_rating] => 44 [wpd_feature_rating] => 11 [wpd_support_rating] => 32 ) )
    – Towfiq
    Commented Feb 4, 2014 at 15:41
  • Please edit that comment into the question. Code formatting in comments in minimal making that very hard to read.
    – s_ha_dum
    Commented Feb 4, 2014 at 17:42
  • sorry. Updated the question with my previous comment. Thanks
    – Towfiq
    Commented Feb 4, 2014 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.