在PHP中响应之前搜索CSV文件 [英] Search through CSV file before responding in PHP

查看:74
本文介绍了在PHP中响应之前搜索CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过php读取CSV文件来制作促销代码验证器.这是我的验证器的代码,而"test.csv"是我的csv文件:

I'm trying to make a promotional code validator via php, by reading from a CSV file. Here's the code for my validator, while 'test.csv' is my csv file :

<?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代码:

Here's my HTML code :

<!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).如果找不到该代码,则会将无效消息发送回该页面.

The CSV file comes with 2 column in each row - first one is the coupon code while the second one is discount amount. If this processor succeeded in searching the inputted code, it will return the discounted amount and a line of word back to the original file (a html with javascript ajax sender/receiver). If the code couldn't be found, it will send invalid message back to the page.

问题是,由于我的csv文件中有100多行,因此100行无效消息将被发布到我的收件人页面. (或者在成功找到代码之前的行数)我应该如何让它在发送回响应之前搜索csv?

The problem is, since there are more than 100 lines in my csv file, 100 lines of invalid message will be posted to my receiver page. (or for an amount of lines before it successfully found the code) How am I suppose to let it search through the csv before sending back response?

推荐答案

如果行无效,您要执行的操作不会回显.如果有效,只需回显,以防万一无效($ promocodevalid = false;),则可以回显该错误.在这里,您去了:

What you want to do is not echo if the line is invalid. Just echo if it is valid, in case there is none valid ($promocodevalid = false;) then you can echo the error. Here you go:

<?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.";
       }
    }
    ?> 

这篇关于在PHP中响应之前搜索CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆