2

I have the code which return json response but I don't know how pass the that value in php function.

Code:

 <div class="container"  ng-controller="askbyctrl" >
            <div class="row" ng-repeat="q in qa.data" >
            <div class="col-md-12 col-xs-12 col.sm.12" >
                <div class="artist-data pull-left" style = "background-color:#BEB7B7;">
                    <div class="artst-pic pull-left">
                        <?php
                            function cache_image($id){
                                //replace with cache directory
                                $image_path = 'C:\xampp\htdocs\QA_UI\images';

                                $image_url = 'http://localhost:8000/api/image/' + id;

                                //get the name of the file
                                $exploded_image_url = explode("/",$image_url);
                                $image_filename = 'id.jpg';
                                $exploded_image_filename = explode(".",$image_filename);
                                $extension = end($exploded_image_filename);
                                //make sure its an image
                                if($extension=="jpg") {
                                    //get the remote image
                                    $image_to_fetch = file_get_contents($image_url);
                                    if($image_to_fetch == '{"error":true,"details":"No image."}') {
                                        echo 'file not found';
                                    } else {
                                        //save it
                                        $local_image_file  = fopen($image_path.'/'.$image_filename, 'w+');
                                        chmod($image_path.'/'.$image_filename,0755);
                                        fwrite($local_image_file, $image_to_fetch);
                                        fclose($local_image_file);
                                        echo 'copied';  
                                    }
                                }
                            //}
                            ?>
                        <img ng-src="images/{{q.userID}}.jpg" alt="" class="img-responsive" />
                    </div>

Please see the code how can I pass the {{q.userID}} in php function..

4
  • had you thought about ajax Commented Sep 2, 2014 at 6:35
  • I don't have any idea how can I do this, if you have pleas tell me, I am new to angularJs and I don't know how can I use php code in angularJs.... I don't know I have to write seprate controller to take php code or I can use directly... Commented Sep 2, 2014 at 6:39
  • 1
    You cannot use server-side code inside client-side code. But your client, can call a resource with ajax calls, and that resource can contain documents rendered with php. Commented Sep 2, 2014 at 6:50
  • Can you please tell me how can I use ajax call in angularJs with php code... Commented Sep 2, 2014 at 7:18

1 Answer 1

2

You can't do it that way. PHP code is running on server side and angular-code in client-side, in browser. That is, when server returns compiled html to browser it has already done all php-code in that file.

If you want to check if image exists and then cache it, you can do it from angular for example with $http. Call with $http your backend-script, which does what you want, and then returns if it was success or not.

Sign up to request clarification or add additional context in comments.

2 Comments

So how can I pass the object as variable in that php code because I have to pass the userID and take that image and copy in my folder.....and this task is doing by php code which I have post.....please see and tell how can I do this....
You can't do it in ng-repeat. If you get that qa.data collection from server-side you can loop through that list before returning it to angular controller and there call that function.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.