如何在php中的表中实现爆炸功能?

时间:2017-09-04 06:56:26

标签: javascript php html mysql

我有基于逗号的字符串,用逗号分隔,我需要在每一行中盯着转换并添加到id,在爆炸功能后如何旋转代码的内部循环

<?php $conn = new mysqli("localhost","root","","test");    if($conn->connect_error){
die("Connection Failed<br>".$conn->connect_error); }


     $sql = 'SELECT id,mobile,code,createdon FROM tset';
     $result = $conn->query($sql);
                          while($row = mysqli_fetch_array($result)){
                              $id = $row['id'];
                              $mobile = $row['mobile'];
                              $code = $row['code'];                                   
                              $date = $row['date'];
                              echo "<tr><td>".$id."</td><td>".$mobile."</td>
                              <td>".$code."</td><td>".$date."</td></tr>";

                          }
     mysqli_close($conn);  ?>

示例数据 ID:10542 手机:1234567890 代码:H5413910102,H5413910201,H5413910301,H5413910701,H5413910802,
dateL:2017-08-30 16:57:00

我需要使用示例数据

以此格式获取数据

&#13;
&#13;
<table>
<tr>
<th>ID</th>
<th>MOBILE</th>
<th>CODE</th>
<th>DATE</th>
</tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910102</td><td>9/4/2017</td>
</tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910201</td><td>9/4/2017</td>
</tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910301</td><td>9/4/2017</td>
</tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910701</td><td>9/4/2017</td>
<tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910802</td><td>9/4/2017</td>
</tr>
</table>
&#13;
&#13;
&#13;

$myString = "H5413910102,H5413910201,H5413910301,H5413910701,H5413910802,";
$myArray = explode(',', $myString); 

foreach($myArray as $my_Array){
    echo $my_Array.'<br>';   
}

2 个答案:

答案 0 :(得分:3)

从数据库的角度来看,不建议将数据作为逗号分隔列表,这当然不是一个好习惯。您应该考虑normalizing您的数据库。话虽如此,您应该遵循以下程序,以达到目前(或暂时)的预期结果。

<?php 
    $conn = new mysqli("localhost","root","","test");    
    if($conn->connect_error){
        die("Connection Failed<br>".$conn->connect_error); 
    }
    $sql = 'SELECT id,mobile,code,createdon FROM tset';
    $result = $conn->query($sql);
    ?>
    <table>
        <tr>
            <th>ID</th>
            <th>MOBILE</th>
            <th>CODE</th>
            <th>DATE</th>
        </tr>
    <?php
    while($row = mysqli_fetch_array($result)){
        $codes = explode(",", $row['code']);
        foreach($codes as $code){
            ?>
            <tr>
                <td><?php echo $row['id']; ?></td>
                <td><?php echo $row['mobile']; ?></td>
                <td><?php echo $code; ?></td>
                <td><?php echo $row['date']; ?></td>
            </tr>
            <?php
        }
    }
    ?>
    </table>
    <?php
    mysqli_close($conn);  
?>

答案 1 :(得分:1)

希望此代码可以帮助您:

 $sql = 'SELECT id,mobile,code,createdon FROM tset';
 $result = $conn->query($sql);
 while($row = mysqli_fetch_array($result)){
      $id = $row['id'];
      $mobile = $row['mobile'];
      $code = $row['code'];                                   
      $date = $row['date'];
      $code = explode(",",$code);
      foreach($code as $key=>$value){
         echo "<tr><td>".$id."</td><td>".$mobile."</td><td>".$value."</td><td>".$date."</td></tr>";
      }                              
}