-2

I am very new to CodeIgniter and I recently heard about RESTful API because I built an Android app in PhoneGap (monaca), consequently I had to research on how to have a PHP backend to support it.

I learnt that with CodeIgniter restful API and AJAX/JSON I can call and display data like channel name, logo, channel URL and description from the database to show inside my app. But many of the tutorials I watched were either in Spanish (I don't speak so I just sit through the video) or it just doesn't explain enough.

Hence I find it hard to believe that I could call about details of about 5 channels at once via a URL string with a get verb. It's hard to explain, that's how confused I am.

If there's a book or video somewhere i can watch to get in-depth knowledge on this, I would be happy. I am more of a visual person so watching a step by step tutorial would be good for me. If not, a brief good explanation could help me archive my goal.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Do you know how to use PHP at a basic level? What data format will your app expect to receive upon connecting to this theoretical REST service? – MonkeyZeus Jun 29 '15 at 14:30
  • yeah i know php quite well . i have already developed the backend with codeignitor . i understand the mvc very well now . json will be the data format . thanks for your response. i also just read about this after posting my question above. i noticed that its possible to send all that data . but if you have anything to explain it further, would be appreciated. – Richard Otabil Jun 29 '15 at 14:58
  • That is good to know, how many users do you expect to use your app at the same time. How many requests per second do you anticipate? – MonkeyZeus Jun 29 '15 at 15:07

1 Answers1

0

For the sake of simplicity I will be using PDO:

URL - example given has 2 "channels"

http://www.example.com/api/get_channel_info?id[]=2&id[]=67

CodeIgniter

class Api extends CI_Controller
{
    function get_channel_info()
    {
        if(is_array($this->input->get('id')) && $this->input->get('id'))
        {
            $dbh = new PDO($db_conn_string, $user, $pass);

            $sql = 'select * from channels where id in(?'.str_repeat(',?', (count($this->input->get('id')) - 1)).')';

            $sth = $dbh->prepare($sql);
            $sth->execute($this->input->get('id'));

            echo json_encode($sth->fetchAll(PDO::FETCH_ASSOC));
        }
    }
}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • my problem now is ... how do i connect the app to get the info from the url. – Richard Otabil Jun 29 '15 at 16:24
  • @RichardOtabil It's easy. Read Android, maybe PhoneGap specific, tutorials on how to consume JSON APIs – MonkeyZeus Jun 29 '15 at 16:25
  • One more thing though . i thought database business should be done in a model ? So can i simply pass values to the model to do the database business? i think the main thing is the echo json_encode($sth->fetchAll(PDO::FETCH_ASSOC)); right? which that will stay in a controller ? Thank you boss. – Richard Otabil Jun 29 '15 at 16:58
  • @RichardOtabil That is 100% at your discretion. The MVC model is just a guideline with suggested practices. The only purpose for my example was to show you the concept. If PHP execution speed becomes a top priority then you may opt to make a standalone file named `get_channel_info.php` so that you can avoid the overhead of loading a framework. – MonkeyZeus Jun 29 '15 at 17:18
  • Thank you sir! . if you were on twitter i will follow you . – Richard Otabil Jun 29 '15 at 17:24
  • @RichardOtabil ha, I am flattered! I just might consider making one, good luck with your app! – MonkeyZeus Jun 29 '15 at 18:12
  • Hi my boss. i figured a way to fix everything but i am having problems displaying the data in my app. please check this post if you can . thank you. [link]( http://stackoverflow.com/questions/31139840/how-do-i-consuming-rest-service-with-jquery-in-phonegap?noredirect=1#comment50294148_31139840) – Richard Otabil Jun 30 '15 at 16:09
  • @RichardOtabil did you find this post useful? – MonkeyZeus Jun 30 '15 at 17:29