tags:

views:

95

answers:

3

I need to only allow members to write a review on a accommodation if they have stayed at the accommodation and there member_id matches the Auth->user('id').

On the accommodation/view I pass the BookingRequests data.

which looks like this

BookingRequest 0 member_id => 4 accepted => 1 1 member_id => 5 accepted => 0 2 member_id => 4 accepted => 0

How would I search the array so I can allow only people who have stayed to add a review?

A: 

It's not as easy in PHP in other languages so you have to inject in a different way.

Something like this:

$vars = array();
foreach ($members as $request) {
  if ($request->accepted) {
    array_push($vars, $request);
  }
}
Chuck Vose
A: 

Try this,

function search($array, $key, $value)
    {
        $results = array();

        if (is_array($array))
        {
            if ($array[$key] == $value)
                $results[] = $array;

            foreach ($array as $subarray)
                $results = array_merge($results, search($subarray, $key, $value));
        }

        return $results;
    }

    $array = array(
                  "member_id" => 4, array("accepted" => 11),
                  "member_id" => 5, array("accepted" => 02), 
                  "member_id" => 4, array("accepted" => 0)
                   );


    $stayedmember = search($array,$memberID,"accepted");

    print_r($stayedmember );
streetparade
A: 

Assuming 'accepted' determines if they have stayed at the accommodation or not and the below test array is indeed the format of your data array, then you could use the following code:

/* Test data */
$BookingRequestData = array(
    array('member_id' => 4, 'accepted' => 1),
    array('member_id' => 5, 'accepted' => 0),
    array('member_id' => 4, 'accepted' => 0)
);

$canAddReview = (count(Set::extract("/data[member_id=" . $this->Auth->user('id') . "][accepted=1]", array('data' => $BookingRequestData))) > 0);
mscdex