QA@IT
«質問へ戻る

質問を投稿

クーポンコードを使って割引率を決める

クーポンコードを使って割引率を決めたいのですが、discount2.phpの46行目とdescountForm2.phpの40行目のエラー該当箇所を見ても解決方法がわかりません。
htdocsの中にsaledata.phpとdiscountForm2.phpとdiscount2.phpとutil.phpが並列して入っています。
どなたかご教示頂けると幸いです。宜しくお願いします。

エラーコード

[27-Jul-2018 08:51:30 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function getCouponRate() in /Applications/MAMP/htdocs/discountForm2.php:40
Stack trace:
#0 {main}
  thrown in /Applications/MAMP/htdocs/discountForm2.php on line 40
[27-Jul-2018 08:52:03 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function getCouponRate() in /Applications/MAMP/htdocs/discount2.php:46
Stack trace:
#0 {main}
  thrown in /Applications/MAMP/htdocs/discount2.php on line 46

saledata.php

<php
    //販売データ
    $couponList = [ "nf23qw"=>0.75, "ha45as"=>0.8, "hf56zx"=>8.5];
    $priceList = [ "ax101"=>2300, "ax102"=>2900];


     function getCouponRate($code) {
        global $couponList;

        $isCouponCode = array_key_exists($code, $couponList);
        if ($isCouponCode) {
            return $couponList[$code];
        } else {

            return NULL;
            }
    }


    function getPrice($id) {
         global $priceList;

    $isGoodsID = array_key_exists ($id, $priceList);
    if ($isGoodsID) {
         return $priceList[$id];
    } else {

    return NULL;
    }    
 }   

  ?>  

discount2.php

<!DOCTYPE html>
<html lang="ja">
<head>
     <meta charset="utf-8">
     <title>金額の計算</title>
     <link href=" ../../css/style.css" rel="stylesheet">
</head>
<body>
<div>
<?php
       require_once("util.php");

       if (!cken($_POST)) {
           $encoding = mb_internal_encoding();
           $err = "Encoding Error! The expected encoding is " . $encoding ;

           exit($err);
       }

       $_POST = es($_POST);
?>

<?php

       $errors = [];

     if (isset($_POST[ 'couponCode' ])) {
     $couponCode = $_POST[ 'couponCode' ];
      } else {

      $couponCode = "";
      }

     if (isset($_POST[ 'goodsID' ])) {
         $goodsID = $_POST[ 'goodsID' ];
    } else {   

        $goodsID = "";
       }
?>

<?php

     require_once("saledata.php");

      $discount = getCouponRate($couponCode);
      $tanka = getPrice($goodsID);

     if (is_null($discount) || is_null($tanka)) {

       $err = '<div class="error">不正な操作がありました。</div>';
      exit($err);
      }
?>

<?php

if(isset($_POST[ 'kosu' ])) {
$kosu = $_POST[ 'kosu' ];

if (!ctype_digit($kosu)) {

$errors[] = "個数は整数で入力して下さい。";
    }
   } else {

   $errors[] = "個数が未設定";
    }
?>

<?php
if (count($errors)>0) {

echo '<ol class="error">';
foreach ($errors as $value) {
   echo "<li>" , $value , "</li>";
   }
   echo "</ol>";
} else {

$price = $tanka * $kosu;
$discount_price = floor($price * $discount);
$off_price = $price - $discount_price;
$off_per = (1- $discount)*100;

$tanka_fmt = number_format($tanka);
$discount_price_fmt = number_format($discount_price);
$off_price_fmt = number_format($off_price);

echo "単価:{$tanka_fmt}円、", "個数:{$kosu}個", "<br>";
echo "金額:{$discount_price_fmt}円", "<br>";
echo "(割引:-{$off_price_fmt}円、{$off_per}% OFF)", "<br>";
}
?>


<form method="POST" action="discountForm2.php">

          <input type="hidden" name="kosu" value="<?php echo $kosu; ?>">
    <ul>
          <li><input type="submit" value="戻る" ></li>
   </ul>
</form>

</div>
</body>
</html>

discountForm2.php

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>割引購入ページ</title>
<link href=" ../../css/style.css" rel="stylesheet">
</head>
<body>
<div>
       <?php
          require_once("util.php");

         if (!cken($_POST)){
              $encoding = mb_internal_encoding();
              $err = "Encoding Error! The expected encoding is " . $encoding;

              exit($err);
          }

           $_POST = es($_POST);
    ?>

<?php


      if (isset($_POST[ 'kosu' ])) {
          $kosu = $_POST[ 'kosu' ];
      } else {
          $kosu = "";
      }
?>

<?php

       require_once("saledata.php");

         $couponCode = "ha45as";
         $goodsID = "ax102";

      $discount = getCouponRate($couponCode);
      $tanka = getPrice($goodsID);

      if (is_null($discount) || is_null($tanka)) {

     $err = '<div class="error">不正な操作がありました。</div>';
      exit($err);
       }
?>

<?php
      $off = (1 - $discount) * 100;
      if ($discount>0) {
           echo "<h2>このページでのご購入は{$off}% OFFになります!</h2>";
       }

  $tanka_fmt = number_format($tanka);
  ?>


  <form method="POST" action="discount2.php">

  <input type="hidden" name="couponCode" value="<?php echo $couponCode; ?>">
  <input type="hidden" name="goodsID" value="<?php echo $goodsID; ?>">
  <ul>
     <li><label>単価:<?php echo $tanka_fmt; ?></label></li>
     <li><label>個数:
           <input type="number" name="kosu">
    </label></li>
    <li><input type="submit" value="計算する" ></li>
 </ul>
</form>
</div>
</body>
</html>

画像の説明をここに入力

画像の説明をここに入力

クーポンコードを使って割引率を決めたいのですが、discount2.phpの46行目とdescountForm2.phpの40行目のエラー該当箇所を見ても解決方法がわかりません。
htdocsの中にsaledata.phpとdiscountForm2.phpとdiscount2.phpとutil.phpが並列して入っています。
どなたかご教示頂けると幸いです。宜しくお願いします。

エラーコード

    [27-Jul-2018 08:51:30 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function getCouponRate() in /Applications/MAMP/htdocs/discountForm2.php:40
    Stack trace:
    #0 {main}
      thrown in /Applications/MAMP/htdocs/discountForm2.php on line 40
    [27-Jul-2018 08:52:03 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function getCouponRate() in /Applications/MAMP/htdocs/discount2.php:46
    Stack trace:
    #0 {main}
      thrown in /Applications/MAMP/htdocs/discount2.php on line 46

saledata.php

    <php
        //販売データ
        $couponList = [ "nf23qw"=>0.75, "ha45as"=>0.8, "hf56zx"=>8.5];
        $priceList = [ "ax101"=>2300, "ax102"=>2900];
        
        
         function getCouponRate($code) {
            global $couponList;
            
            $isCouponCode = array_key_exists($code, $couponList);
            if ($isCouponCode) {
                return $couponList[$code];
            } else {
                
                return NULL;
                }
        }
        
        
        function getPrice($id) {
             global $priceList;
        
        $isGoodsID = array_key_exists ($id, $priceList);
        if ($isGoodsID) {
             return $priceList[$id];
        } else {
        
        return NULL;
        }    
     }   
        
      ?>  

discount2.php

    <!DOCTYPE html>
    <html lang="ja">
    <head>
         <meta charset="utf-8">
         <title>金額の計算</title>
         <link href=" ../../css/style.css" rel="stylesheet">
    </head>
    <body>
    <div>
    <?php
           require_once("util.php");
    
           if (!cken($_POST)) {
               $encoding = mb_internal_encoding();
               $err = "Encoding Error! The expected encoding is " . $encoding ;
    
               exit($err);
           }
    
           $_POST = es($_POST);
    ?>
    
    <?php
    
           $errors = [];
    
         if (isset($_POST[ 'couponCode' ])) {
         $couponCode = $_POST[ 'couponCode' ];
          } else {
    
          $couponCode = "";
          }
    
         if (isset($_POST[ 'goodsID' ])) {
             $goodsID = $_POST[ 'goodsID' ];
        } else {   
         
            $goodsID = "";
           }
    ?>
         
    <?php
    
         require_once("saledata.php");
    
          $discount = getCouponRate($couponCode);
          $tanka = getPrice($goodsID);
     
         if (is_null($discount) || is_null($tanka)) {
      
           $err = '<div class="error">不正な操作がありました。</div>';
          exit($err);
          }
    ?>
    
    <?php
    
    if(isset($_POST[ 'kosu' ])) {
    $kosu = $_POST[ 'kosu' ];
    
    if (!ctype_digit($kosu)) {
    
    $errors[] = "個数は整数で入力して下さい。";
        }
       } else {
    
       $errors[] = "個数が未設定";
        }
    ?>
    
    <?php
    if (count($errors)>0) {
    
    echo '<ol class="error">';
    foreach ($errors as $value) {
       echo "<li>" , $value , "</li>";
       }
       echo "</ol>";
    } else {
    
    $price = $tanka * $kosu;
    $discount_price = floor($price * $discount);
    $off_price = $price - $discount_price;
    $off_per = (1- $discount)*100;
    
    $tanka_fmt = number_format($tanka);
    $discount_price_fmt = number_format($discount_price);
    $off_price_fmt = number_format($off_price);
    
    echo "単価:{$tanka_fmt}円、", "個数:{$kosu}個", "<br>";
    echo "金額:{$discount_price_fmt}円", "<br>";
    echo "(割引:-{$off_price_fmt}円、{$off_per}% OFF)", "<br>";
    }
    ?>
    
    
    <form method="POST" action="discountForm2.php">
    
              <input type="hidden" name="kosu" value="<?php echo $kosu; ?>">
        <ul>
              <li><input type="submit" value="戻る" ></li>
       </ul>
    </form>
    
    </div>
    </body>
    </html>

discountForm2.php

    <!DOCTYPE html>
    <html lang="ja">
    <head>
    <meta charset="utf-8">
    <title>割引購入ページ</title>
    <link href=" ../../css/style.css" rel="stylesheet">
    </head>
    <body>
    <div>
           <?php
              require_once("util.php");
    
             if (!cken($_POST)){
                  $encoding = mb_internal_encoding();
                  $err = "Encoding Error! The expected encoding is " . $encoding;
    
                  exit($err);
              }
    
               $_POST = es($_POST);
        ?>
    
    <?php
    
    
          if (isset($_POST[ 'kosu' ])) {
              $kosu = $_POST[ 'kosu' ];
          } else {
              $kosu = "";
          }
    ?>
    
    <?php
    
           require_once("saledata.php");
    
             $couponCode = "ha45as";
             $goodsID = "ax102";
    
          $discount = getCouponRate($couponCode);
          $tanka = getPrice($goodsID);
    
          if (is_null($discount) || is_null($tanka)) {
          
         $err = '<div class="error">不正な操作がありました。</div>';
          exit($err);
           }
    ?>
    
    <?php
          $off = (1 - $discount) * 100;
          if ($discount>0) {
               echo "<h2>このページでのご購入は{$off}% OFFになります!</h2>";
           }
      
      $tanka_fmt = number_format($tanka);
      ?>
        
        
      <form method="POST" action="discount2.php">
      
      <input type="hidden" name="couponCode" value="<?php echo $couponCode; ?>">
      <input type="hidden" name="goodsID" value="<?php echo $goodsID; ?>">
      <ul>
         <li><label>単価:<?php echo $tanka_fmt; ?>円</label></li>
         <li><label>個数:
               <input type="number" name="kosu">
        </label></li>
        <li><input type="submit" value="計算する" ></li>
     </ul>
    </form>
    </div>
    </body>
    </html>

[![画像の説明をここに入力][1]][1]

[![画像の説明をここに入力][2]][2]


  [1]: https://i.stack.imgur.com/U1XYL.png
  [2]: https://i.stack.imgur.com/pCqXt.png