<?php echo '<?xml version="1.0" encoding="utf-8"?>' ."\n" ?>
<?php mb_http_output('utf-8'); ?>

<!--
## (c)2007 Takuya NISHIMOTO (nishi@hil.t.u-tokyo.ac.jp)
## PostgreSQL + PHP + VoiceXML
## $Id: goods-php-utf.txt,v 1.1 2007/08/09 03:07:47 nishi Exp $
-->

<vxml version="2.0" xml:lang="ja">

<?php
$db_user = "galatea";
$db_pass = "galatea";
$db_host = "localhost";
$db_name = "galatea_db";
$db = pg_connect("host=$db_host port=5432 dbname=$db_name user=$db_user password=$db_pass");

if (isset($_REQUEST['item']) && $_REQUEST['item'] != '') {
   $item = $_REQUEST['item'];
   mb_convert_variables("UTF-8", mb_detect_encoding($item), $item);
   $rs = pg_query($db, "SELECT name,price FROM goods WHERE code = $item");
   if ($row = pg_fetch_assoc($rs)) {
      $name  = $row['name'];
      $price = $row['price'];
   }
?>

<form id="answer">
  <block>
    <log>name: <?php echo $name ?>，price: <?php echo $price ?></log>
    <prompt>
      <?php echo $name ?>は<?php echo $price ?>円です。
      <break/>
    </prompt>
    <goto next="#ask"/>
  </block>
</form>

<?php } ?>

<form id="ask">
  <field name="item">
   <prompt timeout="20s"> 値段を聞きたい果物は何ですか？ </prompt>
   <grammar version="1.0" root="#item_rule">
    <rule id="item_rule">
     <one-of>

      <item> <token sym="まいくてすと">マイクテスト</token> </item>
<?php
$rs = pg_query($db, "SELECT code,name,yomi FROM goods");
while ($row = pg_fetch_assoc($rs)) {
  $code = $row['code'];
  $name = $row['name'];
  $yomi = $row['yomi'];
  echo "      <item> <token sym=\"$yomi\" slot=\"item\" value=\"$code\">$name</token> </item>\n";
}
?>      
     </one-of>
    </rule>
   </grammar>
  </field>
  <block>
    <submit method="post" next="<?php echo basename(__FILE__) ?>"/>
  </block>
</form>

</vxml> 
