【问题标题】:PHP foreeach loop capture the first 9 charsPHP foreach 循环捕获前 9 个字符
【发布时间】:2016-04-14 11:03:53
【问题描述】:

我有一个返回数据的 MySQL 查询,我运行一个 foreach 循环来回显结果。

每行数据都是一个字符串,并且在开头有一个唯一标识符,长度为九 (9) 个字符。

我要做的是从每行中提取前九个字符并在$_GET 请求中使用它。以下是我的工作数据。

每行的开头是唯一标识符(九个字符)例如:SeqID 0101。我可以提取该代码并在“查看”按钮上的$_GET 中使用它吗:

foreach ($row as $k => $v)
echo '<tr>
<td>'.$k.'</td>
<td>'.$v.'</td>
<td><a href="imaint_failed_items_group_iframe.php?Seq=<?php echo $identifier;?>" target="_self"><img src="../../../nav/images/view_button.png"/></a></td>
</tr>';

【问题讨论】:

    标签: php


    【解决方案1】:

    使用 PHP substr()

    $identifier = substr($v, 0, 9);
    

    这将返回前 9 个字符。

    0 开始

    AND 9 是字符长度。

    【讨论】:

      【解决方案2】:

      正如@Pupil 已经说过你可以使用substr()

      <?php foreach($row as $k => $v): ?>
          <tr>
              <td><?php echo $k; ?></td>
              <td><?php echo $v; ?></td>
              <td><a href="imaint_failed_items_group_iframe.php?Seq=<?php echo substr($k, 0, 9); ?>" target="_self"><img src="../../../nav/images/view_button.png"/></a></td>
          </tr>
      <?php endforeach; ?>
      

      【讨论】:

      • 子字符串应该是$v而不是$k
      猜你喜欢
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多