Twilio Cookbook
上QQ阅读APP看书,第一时间看更新

Building a survey tree

The survey builder performs three functions: it shows you the stats on sent surveys, lets you send unsent surveys, and lets you build your new surveys.

Surveys in this system are simple: one question and six possible answers.

Each answer will be assigned a number of 1 to 6.

Getting ready

The complete source code for this recipe can be found in the Chapter3/ folder.

How to do it...

We've got subscribers but we need to send them what they've subscribed to. This recipe will set up our survey builder. We'll also build a home page as part of our builder, where we can choose to send surveys or view results.

  1. Download the Twilio Helper Library from https://github.com/twilio/twilio-php/zipball/master and unzip the file.
  2. Upload the Services/ folder to your website.
  3. Create a file on your website and name it survey-builder.php. The file will have the following content:
    <?php
    include("config.php");
    include("pdo.class.php");
    include 'Services/Twilio.php';
    switch($_GET['action'] ){
      case 'save':
        $fields = array('question','answer1','answer2','answer3','answer4','answer5','answer6','status');
        $pfields = array();
        foreach( $fields as $k){
          $v = $_POST[$k];
          $pfields[] = "{$k} = '{$v}'";
        }
        $sql = "INSERT INTO survey SET ".implode(",",$pfields);
        $pdo = Db::singleton();
        $pdo->exec($sql);
        $qid = $pdo->lastInsertId();
        if( isset($qid) && !empty($qid) ){
    ?>
          <a href="send-survey.php?qid=<?=$qid?>">Send survey</a> or <a href="survey-builder.php">Return to home</a>
    <?php
        }
      case 'build':
        include("buildform.php");
        break;
      default:
        include("home.php");
        break;
    }
    ?>
    Note

    The survey-builder.php file is the root of our system as it handles saving surveys and displaying results.

  4. Now, upload buldform.php (bearing the following content) to your website.
    <h2>Prepare your survey</h2>
    <form method="POST" action="survey-builder.php?action=save">
    <table>
    <tr>
      <td>Question</td>
      <td><input type="text" name="question" /></td>
    </tr>
    <?php
    for($i = 1;$i<= 6;$i++){
    ?>
      <tr>
        <td>Answer <?=$i?></td>
        <td><input type="text" name="answer<?=$i?>" /></td>
      </tr>
    <?php
    }
    ?>
    </table>
    <button type="submit">Save</button>
    </form>
    Note

    buildform.php is the form for building surveys.

  5. Upload home.php (bearing the following content) to your website:
    <a href="survey-builder.php?action=build">Add new survey</a><hr />
    <h2>Pending Surveys</h2>
    <table width=100%>
    <?php
    $res = $pdo->query("SELECT * FROM survey WHERE status=0");
    while( $row = $res->fetch() ){
    ?>
    <tr>
       <td><?=$row['question']?></td>
       <td><a href="send-survey.php?qid=<?=$row['ID']?>">Send</a></td>
    </tr>
    <?php
    }
    ?>
    </table>
    <br />

    The first part of this file displays surveys that have not been sent yet. The second part displays surveys that have been sent, and a link to view responses.

    <h2>Sent Surveys</h2>
    <table width=100%>
    <?php
    $res = $pdo->query("SELECT * FROM survey WHERE status=1");
    while( $row = $res->fetch() ){
    ?>
    <tr>
      <td><?=$row['question']?></td>
      <td><a href="view-survey.php?qid=<?=$row['ID']?>">View Responses</a></td>
    </tr>
    <?php
    }
    ?>
    </table>

How it works...

In steps 1 and 2, we downloaded and installed the Twilio Helper Library for PHP, which is at the heart of your Twilio-powered apps.

Finally, in steps 3, 4, and 5, we created survey-builder.php, buildform.php, and home.php respectively.

When you first load the survey builder, you will get a list of surveys; you can view the stats or send pending surveys. Once you choose to build a new survey, you will get a form that lets you build the survey with a list of answers.

Once you save the survey, you can choose to send it right away or return to the index page.

On the index of surveys, we display unsent surveys and sent surveys.

Unsent surveys will have a link that you can use to send them, whereas sent surveys will have a link to view the results. We'll cover both of these capabilities in upcoming recipes in this chapter.