duanlu9816 2012-07-26 11:12
浏览 90
已采纳

PHP获取列中(不同)数据的数量

Hy!

How can I get number of distinct data from column 3 and 6 of my table in my PHP:

<?php

open text file (first row is of headers which are divided by ;)

$fp = fopen('C:\...\usedlicences.txt','r') or die("can't open file");

create table with border

echo "<table id='MyTable' border='1'>
";

create header row for table with headers

echo "<td><b>id</b><td><b>name</b></td><td><b>surname</b></td><td><b>address</b></td><td><b>state</b></td><td><b>phone</b></td><td><b>city</b></td><td><b>date</b></td><td><b>color</b></td>";                          
$length = 1000;
$delimiter = ";";      
$k=1;

from file create rows and populate them with data (skip first (header) row) and add first column where id of row is written

$csv_line = fgetcsv( $fp, $length, $delimiter); 
while($csv_line = fgetcsv( $fp, $length, $delimiter ) ) {
echo "<tr>";            
echo "<td>$k</td>";
$k++;   
for ($i = 0, $j = count($csv_line); $i < $j; $i++) {
echo '<td>'.$csv_line[$i].'</td>';  
}
}
echo "<tr>";   
echo '</table>';
fclose($fp) or die("can't close file");
?>

for showing alert how much licences are used (all rows minus header row)

<script language="JavaScript">
var oRows = document.getElementById('MyTable').getElementsByTagName('tr');
var iRowCount = oRows.length-1;
alert('Licences used: ' + ((iRowCount)-1)+'!');
</script>

test.txt looks like:

id;name;surname;address;state;phone;city;date;color
1;John;Simts;Yellow 12;Greenik;1234567;Mannds;12/3/1234;blue    

Thanks, greetings

  • 写回答

1条回答 默认 最新

  • dongtuo5262 2012-07-26 11:35
    关注

    Inside your for loop place the following code:

    if ($i==3) 
       $surname[$csv_line[$i]]++;
    else if ($i==6)
       $phone[$csv_line[$i]]++;
    

    Then number of distinct records for surnames (column 3) and phones (column 6) would be:

    count($surname);
    count($phone);
    

    respectively.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制