dounan9070 2014-09-18 03:16
浏览 27
已采纳

PHP在逗号后解析MYSQL并输出到html表

Thank you all for you help! I received some phenomenal help about 17 days ago which saved my *!. I am still learning PHP and have run across a new problem. I have a MYSQL database field called "Tracking" this is a long text field in which we want to input tracking numbers (for shipping) separated by commas . We ship some times 20-50 individual boxes in the same order.

The following script outputs to an HTML table the number of boxes being shipped (EX Box 1 of 12, Box 2 of 12...etc) and then the value of the tracking number field. I need to modify it so it parses at the "comma" in the Tracking field and includes the tracking number for each box. So Box 1: Tracking Number "take first tracking number and end at comma", So Box 2: Tracking Number "take second tracking number and end at comma"....etc...does this make sense?

Please provide whatever help possible, would greatly be appreciated!

Script:

    <?php
$q = intval($_GET['q']);

$con = mysqli_connect('***','***','***','***');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"db116223_wmi");
$sql="SELECT * FROM tbl_purchase_order WHERE purchase_order_id = '".$q."'";
$result = mysqli_query($con,$sql);



while($row = mysqli_fetch_array($result)) {
for ($i=0; $i<$row['product_quantity']; $i++) {
$my_val=1;
$x += 1;

echo "<table>";

echo "<tr>";

echo  "<td width=400><br>Box $x of ". $row['product_quantity'] ." Tracking Number:". $row['Tracking'] ."</td>";

echo "</tr>";
echo "</table>";
}
}
mysqli_close($con);
?>

JB

  • 写回答

1条回答 默认 最新

  • dongzhun1857 2014-09-18 03:33
    关注

    As jjonesdesign mentioned, you need to use explode to break the tracking number field up at each comma. explode creates an array of values, each split out of the Tracking field. Look below for the solution:

    <?php
    $q = intval($_GET['q']);
    
    $con = mysqli_connect('***','***','***','***');
    if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
    }
    
    mysqli_select_db($con,"db116223_wmi");
    $sql="SELECT * FROM tbl_purchase_order WHERE purchase_order_id = '".$q."'";
    $result = mysqli_query($con,$sql);
    
    
    
    while($row = mysqli_fetch_array($result)) {
    $myTracking = explode(',',$row['Tracking']); // get an array of tracking numbers. Array element starts with 0
    for ($i=0; $i<$row['product_quantity']; $i++) {
    $my_val=1;
    $x += 1;
    
    
    echo "<table>";
    
    echo "<tr>";
    
    echo  "<td width=400><br>Box $x of ". $row['product_quantity'] ." Tracking Number:".  $myTracking[$i] ."</td>";
    
    echo "</tr>";
    echo "</table>";
    }
    }
    mysqli_close($con);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改