php - 在回显表格时从数组中获取每 7 个元素
问题描述
我正在获取我需要的数据fgetcsv
并将它们存储到$data
. 它包含一个带有标题行和大量信息的表格。每 7 列是存储文件的路径。
我已经搜索过我的问题是什么,但我似乎找不到解决方案。
我的代码还:
echo '<table border="0" cellspacing="0" cellpadding="5" class="csvTable" width="auto">';
$handle = fopen("index.csv", "r");
$start = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
echo '<tr>' . "\n";
for ( $x = 0; $x < count($data); $x++) {
if ($start == 0 && $hasTitle == true)
echo '<th>'.$data[$x].'</th>' . "\n";
else
echo '<td>'.$data[$x].'</td>' . "\n";
}
$start++;
echo '</tr>' . "\n";
}
fclose($handle);
echo '</table>';
我想<a href=?>
在每 7 列添加一个超链接,但我不知道如何。我该怎么做,这是正确的方法吗?
解决方案
您检查每一列是第 7 列或可被 7 整除,您只需检查变量是否像这样被 7 整除。
echo '<table border="0" cellspacing="0" cellpadding="5" class="csvTable" width="auto">';
$handle = fopen("index.csv", "r");
$start = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
echo '<tr>' . "\n";
for ( $x = 0; $x < count($data); $x++) {
if ($start == 0 && $hasTitle == true)
echo '<th>'.$data[$x].'</th>' . "\n";
else
echo '<td>'.$data[$x].'</td>' . "\n";
if( $x && !($x % 7) ){
echo '<a href=?>'
}
}
$start++;
echo '</tr>' . "\n";
}
fclose($handle);
echo '</table>';
推荐阅读
- tensorflow - 尝试打开序列化 Keras 模型时出现条件变量错误
- python - 命令对象没有meta属性,Django管理命令
- r - 通过 `density.ppp` 将参数传递给内核函数
- python - Python:Unicode 对象必须在散列之前进行编码
- javascript - 带有需要环境变量/访问令牌/api 密钥的客户端 Browserify (Watchify) js 文件的 Jekyll
- ios - 如何添加唯一值以区分 Swift 数组中的重复数据?
- rest - 无效的处理程序方法返回值:ModelAndView:在spring MVC中使用名称引用视图
- c++ - cl::Event::waitForEvents 返回 -7 (CL_EXEC_STATUS_ERROR_ FOR_EVENTS_IN_WAIT_LIST)
- hive - Hive - 使用“选择查询”和“分区依据”命令创建表语句
- data-modeling - 在 Python 的混合模型中使用多个预测器