PHP读取文件匹配if语句
每个人我都有下面的文件,我想显示符合if条件并通过另一个条件的行 我有这个TXT文件:PHP读取文件匹配if语句,php,file,if-statement,conditional-statements,Php,File,If Statement,Conditional Statements,每个人我都有下面的文件,我想显示符合if条件并通过另一个条件的行 我有这个TXT文件: Doc. number|Date|Price|Description|Name 100|11/11/2015|99|Test 1|Alex 101|11/11/2015|120|Test 2 102|11/11/2015|100|Test 3|John 102|11/11/2015|140|| 103|11/11/2015|110|Test 4| 这是我的PHP代码: $file_handle = fope
Doc. number|Date|Price|Description|Name
100|11/11/2015|99|Test 1|Alex
101|11/11/2015|120|Test 2
102|11/11/2015|100|Test 3|John
102|11/11/2015|140||
103|11/11/2015|110|Test 4|
这是我的PHP代码:
$file_handle = fopen("file.txt", "rb");
$i = 0;
echo "<table border='1'>";
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>";
while (!feof($file_handle)) {
$line_of_text = fgets($file_handle);
$parts = explode('|', $line_of_text);
if($i > 1) { // Pass the first line
echo "<tr>";
echo "<td>" . $parts[0] . "</td>"; // Doc. number
echo "<td>" . $parts[1] . "</td>"; // Date
echo "<td>" . $parts[2] . "</td>"; // Price
echo "<td>" . $parts[3] . "</td>"; // Description
echo "<td>" . $parts[4] . "</td>"; // Name
echo "</tr>";
}
$i++;
}
fclose($file_handle);
echo "</table>"
$file\u handle=fopen(“file.txt”、“rb”);
$i=0;
回声“;
回显“Doc.numberDatePriceDescriptionName”;
而(!feof($file_handle)){
$line\u of_text=fgets($file\u handle);
$parts=分解(“|”,文本的$line_);
如果($i>1){//通过第一行
回声“;
回显“$parts[0]”;//文档编号
回显“$parts[1]”;//日期
echo“$parts[2]”;//价格
echo“$parts[3]”;//说明
echo“$parts[4]”;//名称
回声“;
}
$i++;
}
fclose($file\u handle);
回声“”
如何检查表中是否没有“说明”和/或“名称”,并传递此行。我只想在if条件下显示(获取)匹配的行
如果有人有主意,我将非常感激。提前谢谢。尽可能简单
$file_handle = fopen("file.txt", "rb");
$i = 0;
echo "<table border='1'>";
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>";
while (!feof($file_handle)) {
$line_of_text = fgets($file_handle);
$parts = explode('|', $line_of_text);
if($i > 1 && !empty($parts[3]) && !empty($parts[4])) { // Pass the first line and lines without description / name
echo "<tr>";
echo "<td>" . $parts[0] . "</td>"; // Doc. number
echo "<td>" . $parts[1] . "</td>"; // Date
echo "<td>" . $parts[2] . "</td>"; // Price
echo "<td>" . $parts[3] . "</td>"; // Description
echo "<td>" . $parts[4] . "</td>"; // Name
echo "</tr>";
}
$i++;
}
fclose($file_handle);
echo "</table>"
$file\u handle=fopen(“file.txt”、“rb”);
$i=0;
回声“;
回显“Doc.numberDatePriceDescriptionName”;
而(!feof($file_handle)){
$line\u of_text=fgets($file\u handle);
$parts=分解(“|”,文本的$line_);
如果($i>1&&!empty($parts[3])&&!empty($parts[4]){//传递第一行和没有说明/名称的行
回声“;
回显“$parts[0]”;//文档编号
回显“$parts[1]”;//日期
echo“$parts[2]”;//价格
echo“$parts[3]”;//说明
echo“$parts[4]”;//名称
回声“;
}
$i++;
}
fclose($file\u handle);
回声“”
尽可能简单
$file_handle = fopen("file.txt", "rb");
$i = 0;
echo "<table border='1'>";
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>";
while (!feof($file_handle)) {
$line_of_text = fgets($file_handle);
$parts = explode('|', $line_of_text);
if($i > 1 && !empty($parts[3]) && !empty($parts[4])) { // Pass the first line and lines without description / name
echo "<tr>";
echo "<td>" . $parts[0] . "</td>"; // Doc. number
echo "<td>" . $parts[1] . "</td>"; // Date
echo "<td>" . $parts[2] . "</td>"; // Price
echo "<td>" . $parts[3] . "</td>"; // Description
echo "<td>" . $parts[4] . "</td>"; // Name
echo "</tr>";
}
$i++;
}
fclose($file_handle);
echo "</table>"
$file\u handle=fopen(“file.txt”、“rb”);
$i=0;
回声“;
回显“Doc.numberDatePriceDescriptionName”;
而(!feof($file_handle)){
$line\u of_text=fgets($file\u handle);
$parts=分解(“|”,文本的$line_);
如果($i>1&&!empty($parts[3])&&!empty($parts[4]){//传递第一行和没有说明/名称的行
回声“;
回显“$parts[0]”;//文档编号
回显“$parts[1]”;//日期
echo“$parts[2]”;//价格
echo“$parts[3]”;//说明
echo“$parts[4]”;//名称
回声“;
}
$i++;
}
fclose($file\u handle);
回声“”
如果我们有名称和说明,则仅打印表格行:
if($i > 1 && $parts[3] && $parts[4]) {
如果我们有名称和说明,则仅打印表行:
if($i > 1 && $parts[3] && $parts[4]) {
您可以将条件放在echo语句之前,如果条件为false,只需跳过“echo”
if(计数($parts)==5){
$error=0;
foreach($parts作为$part){
if(空($part))错误++;
}
如果($i>1&&$error==0){
回声“;
回显“$parts[0]”;//文档编号
回显“$parts[1]”;//日期
echo“$parts[2]”;//价格
echo“$parts[3]”;//说明
echo“$parts[4]”;//名称
回声“;
}
}
您可以将条件放在echo语句之前,如果条件为false,只需跳过“echo”
if(计数($parts)==5){
$error=0;
foreach($parts作为$part){
if(空($part))错误++;
}
如果($i>1&&$error==0){
回声“;
回显“$parts[0]”;//文档编号
回显“$parts[1]”;//日期
echo“$parts[2]”;//价格
echo“$parts[3]”;//说明
echo“$parts[4]”;//名称
回声“;
}
}
我有一个解决方案可以帮助您。但为什么我认为你需要换个方向。所以我把if($I>1)改为if($I>0)
$file\u handle=fopen(“file.txt”、“rb”);
$i=0;
回声“;
回显“Doc.numberDatePriceDescriptionName”;
而(!feof($file_handle)){
$line\u of_text=fgets($file\u handle);
$parts=分解(“|”,文本的$line_);
如果($i>0){//通过第一行
如果((!empty($parts[3])和(!empty($parts[4])){
回声“;
回显“$parts[0]”;//文档编号
回显“$parts[1]”;//日期
echo“$parts[2]”;//价格
echo“$parts[3]”;//说明
echo“$parts[4]”;//名称
回声“;
}
}
$i++;
}
fclose($file\u handle);
回声“”
我有一个解决方案可以帮助您。但为什么我认为你需要换个方向。所以我把if($I>1)改为if($I>0)
$file\u handle=fopen(“file.txt”、“rb”);
$i=0;
回声“;
回显“Doc.numberDatePriceDescriptionName”;
而(!feof($file_handle)){
$line\u of_text=fgets($file\u handle);
$parts=分解(“|”,文本的$line_);
如果($i>0){//通过第一行
如果((!empty($parts[3])和(!empty($parts[4])){
回声“;
回显“$parts[0]”;//文档编号
回显“$parts[1]”;//日期
echo“$parts[2]”;//价格
echo“$parts[3]”;//说明
echo“$parts[4]”;//名称
回声“;
}
}
$i++;
}
fclose($file\u handle);
回声“”
您的文件具有CSV
结构,以管道分隔。因此,将其解析为
CSV
请注意,使用获取CSV的标题并将分隔符参数传递给
也应考虑使用<代码> MyPodie<代码>,而不是明确地将数组中的每个成员在<代码> TD/<代码>标签之间传递。
下面是一个使用两个函数的示例,一个用于解析CSV
并返回数据,另一个用于显示数据和进行验证
function getData($file){
$rows = array();
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, null, "|")) !== FALSE) {
$rows[] = $data;
}
fclose($handle);
}
return $rows;
}
function displayData($rows){
$header = array_shift($rows);
$output = '<table border="1">' . PHP_EOL;
$output .= '<tr><th>' . implode('</th><th>',$header) . '</th></tr>' . PHP_EOL;
foreach($rows as $row){
if (isset($row[3]) and isset($row[4]) and $row[3]!='' and $row[4]!=''){
$output .= '<tr><td>' . implode('</td><td>',$row) . '</td></tr>' . PHP_EOL;
}
}
$output .= '</table>';
return $output;
}
$rows = getData("pipe.txt");
print displayData($rows);
函数getData($file){
$rows=array();
if(($handle=fopen($file,“r”))!==FALSE){
而(($data=fgetcsv($handle,
<table border="1">
<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>
<tr><td>100</td><td>11/11/2015</td><td>99</td><td>Test 1</td><td>Alex</td></tr>
<tr><td>102</td><td>11/11/2015</td><td>100</td><td>Test 3</td><td>John</td></tr>
</table>