在PHP中响应之前搜索CSV文件

时间:2012-12-28 02:54:35

标签: php ajax csv

我正在尝试通过php从CSV文件中读取促销代码验证器。这是我的验证器的代码,而'test.csv'是我的csv文件:

<?php
// if data are received via POST, with index of 'test'
if (isset($_POST['test'])) {
    $file  = fopen('test.csv', 'r');
    $coupon = array($_POST['test']); 
    $coupondef = $_POST['test'];             // get data
    $coupon = array_map('preg_quote', $coupon);
    $regex = '/'.implode('|', $coupon).'/i';
    while (($line = fgetcsv($file)) !== FALSE) {  
    list($promocode, $amount) = $line;

    if(preg_match($regex, $promocode)) {
        echo "Coupon: '<i>".$coupondef."</i> is valid, with ".$amount."% of discount";
        $promocodevalid = 1;
        break;
    } else {
        echo "Coupon: '<i>".$coupondef."</i> is invalid.";
        $promocodevalid = 0;}
}
}
?> 

这是我的HTML代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ro">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
<title>Example Ajax POST</title>

<script type="text/javascript"><!--
function get_XmlHttp() {
  var xmlHttp = null;

  if(window.XMLHttpRequest) {
    xmlHttp = new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {   
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

  return xmlHttp;
}

function ajaxrequest(php_file, tagID) {
  var request =  get_XmlHttp();

  var  the_data = 'test='+document.getElementById('txt2').value;

  request.open("POST", php_file, true);

  request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  request.send(the_data);
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      document.getElementById(tagID).innerHTML = request.responseText;
    }
  }
}
--></script>
</head>
<body>

<h3 style="cursor:pointer;" onclick="ajaxrequest('couponcheck.php', 'context')"><u>Click</u></h3>
<input type="text" id="txt2" value=""></div>
<div id="context">Here will be displayed the response from the php script.</div>

</body>
</html>

CSV文件每行有2列 - 第一列是优惠券代码,第二列是折扣金额。如果此处理器成功搜索输入的代码,它将将折扣金额和一行字返回到原始文件(带有javascript ajax发送方/接收方的html)。如果找不到代码,它会将无效消息发送回页面。

问题是,由于我的csv文件中有超过100行,因此将在我的接收者页面上发布100行无效消息。 (或者在成功找到代码之前的行数)我怎么想让它在发回响应之前搜索csv?

2 个答案:

答案 0 :(得分:0)

如果该行无效,您想要做的就是回显。如果它是有效的,只要回显,如果没有有效($ promocodevalid = false;),那么你可以回应错误。你走了:

<?php
    // if data are received via POST, with index of 'test'
    if (isset($_POST['test'])) {
        $promocodevalid = false;
        $file  = fopen('test.csv', 'r');
        $coupon = array($_POST['test']); 
        $coupondef = $_POST['test'];             // get data
        $coupon = array_map('preg_quote', $coupon);
        $regex = '/'.implode('|', $coupon).'/i';
        while (($line = fgetcsv($file)) !== FALSE) {  
           list($promocode, $amount) = $line;

           if(preg_match($regex, $promocode)) {
               echo "Coupon: '<i>".$coupondef."</i> is valid, with ".$amount."% of discount";
               $promocodevalid = true;
               break;
           }
        }
       if(!$promocodevalid) {
           echo "Coupon: '<i>".$coupondef."</i> is invalid.";
       }
    }
    ?> 

答案 1 :(得分:0)

由于您使用Ajax调用此php脚本,因此您应该在匹配时立即返回:

<?php
// if data are received via POST, with index of 'test'
if (isset($_POST['test'])) {
    $file  = fopen('test.csv', 'r');
    $coupon = array($_POST['test']); 
    $coupondef = $_POST['test'];             // get data
    $coupon = array_map('preg_quote', $coupon);
    $regex = '/'.implode('|', $coupon).'/i';
    while (($line = fgetcsv($file)) !== FALSE) {  
    list($promocode, $amount) = $line;

    if(preg_match($regex, $promocode)) {
        echo "Coupon: '<i>".$coupondef."</i> is valid, with ".$amount."% of discount";
        exit; // match made, stop the php file now
    } 
}
// if you get here you know there was no match
 echo "Coupon: '<i>".$coupondef."</i> is invalid.";
?> 

否则,如果你在你的循环上达到终止条件,你知道没有匹配(因为脚本没有被杀死)所以只要等到那时输出“无效”消息。