views:

46

answers:

4
 <?php  
 function check($user_id, $topic_id){

        $query = mysql_query("SELECT user_id, topic_id FROM sa where topic_id='$topic_id' and user_id='$user_id'");
        if (mysql_num_rows($query)==1){



           return 'you have already voted';
        }
        else {

            $against = ' <li>
    <button type="submit" value="Actions" class="against" title="against">
      <i></i><span>Against</span></button>
          </li>';

            $support ='<li>
            <button type="submit" value="Actions" class="support" title="support">
                <i></i>
                <span>Support</span>
            </button>
        </li>';
            return compact('support', 'against');
        }

extract(check($_SESSION['user_id'], $topicId));

  echo $against;
  echo $support;
?>

i keep getting this error:

Warning: extract() [function.extract]: First argument should be an array 
A: 

First, function definition is not closed in your code (no closing "}")

Second, your function returns string if user has already voted - in this case PHP would obviously say "First argument should be an array" on extract() function call.

Generally, I'd recommend not to use compact-extract construction, but use structures or associative arrays as return value instead. In this case behavior would be much clearer.

Kel
oh yeh thats true, but how could i resolve this?
getaway
I've just edited the answer. I'd recommend to use structure or associative array as return value. Structure may have field "status", which may denote, whether vote was successful or not.
Kel
+2  A: 

The extract function expects an array. You're passing it the results of your check function, which is sometimes an array

return compact('support', 'against');

but sometimes a string.

return 'you have already voted';

So, I imagine you're seeing the error when the first return statements is executed.

Alan Storm
+1  A: 

Maybe something like this is better:

function check($user_id, $topic_id){
    $query = mysql_query("SELECT user_id, topic_id FROM sa where topic_id='$topic_id' and user_id='$user_id'");
    if (mysql_num_rows($query)==1){
       return array('error'=>'you have already voted');
    }
    else {
        $against = ' <li>
<button type="submit" value="Actions" class="against" title="against">
  <i></i><span>Against</span></button>
      </li>';

        $support ='<li>
        <button type="submit" value="Actions" class="support" title="support">
            <i></i>
            <span>Support</span>
        </button>
    </li>';
        return compact('support', 'against');
    }
}

$result = check($_SESSION['user_id'], $topicId);

if(isset($result['error'])) {
    echo $result['error'];
}
else {
    echo $result['against'];
    echo $result['support'];
}

You could also consider to use exceptions.

Felix Kling
works brillantly thank you
getaway
A: 

So as it suggests: Warning: extract() [function.extract]: First argument should be an array

Try following:

$arr = check($_SESSION['user_id'], $topicId);
if(is_array($arr)) {
  extract($arr);
  echo $against;
  echo $support;
} else {
  echo $arr;
}
tanjir