Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I'm kinda struggling with yii framework. I would like to connect to mobile app from my php server. How should I do the Json encoding and sent to mobile app in yii framework?

share|improve this question
3  
What have you tried so far? Please read How do I ask a good question?. – DavidPostill Mar 27 at 15:26
    
@imcrazy Let me know if my answer was help you – 4EACH Mar 28 at 21:25

3 Answers 3

up vote 0 down vote accepted

Q: How should I do the Json encoding and sent to mobile app in yii framework?

A:

echo CJSON::encode($myArray);

Q: If I want to pass in just user_name to mobile app, is it possible? How should I do that?

A:

$criteria=new CDbCriteria;
$criteria->select='user_name';  // only select the 'user_name' column
$criteria->condition='user_id=:user_id';
$user_id = filter_input(INPUT_GET,'user_id');
if(FALSE===$user_id){
   header($_SERVER['SERVER_PROTOCOL'].' 404 not found');
   return false;
}

$criteria->params=array(':user_id'=>$user_id); // check 
$user=Users::model()->find($criteria); // $params is not needed

//echo json as written at answer #1

Good luck!

share|improve this answer

The Yii way to do it is

echo CJSON::encode($myArray);

The underlying code use json_encode, but there are advantages discussed here : Why use CJSON encode when we have json_encode

share|improve this answer

Im using this:

 public function actionGetUser($user_id)
        {   
            header('Content-type: application/json');
            $user = User::model()->findAll();
            echo CJSON::encode($user);
            Yii::app()->end();
     }   

this does work when i test: localhost/myproject/index.php/user/getuser/user_id

Is it possible for json to just encode just one attribute and send to mobile app? Example: there are the attributes like: user_id, user_name, user_email, user_photo

If I want to pass in just user_name to mobile app, is it possible? How should I do that?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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