单独的逗号用php分隔mySql数据库字段值 [英] Seperate comma separated mySql database field value with php

查看:75
本文介绍了单独的逗号用php分隔mySql数据库字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您认为这是重复版,请现在就让我,因为我不完全确定要搜索的内容,(如果重复,我会删除)

If you believe this is a duplicate please let me now as I'm not totally sure what to search for to check, (I will remove if duplicate)

我在名为test_sess的表中名为sess_times的mySql字段中有一个数字列表,第一行的数字是:

I have a list of of numbers in a mySql field called sess_times in a table called test_sess, the numbers for the first row are:

25,38,40,50

25, 38, 40, 50

是否可以将这些分成php变量,例如:

is it possible to split these into php variable e.g:

$no1 = 25
$no2 = 38
$no3 = 40
$no4 = 50

我正在尝试将各个数字放在一个表格中,如下所示:

I'm trying to put the individual numbers in a table, something like below:

<table cellspacing="0" cellpadding="0" border="0">
  <tr>
    <td> 1 </td>
    <td><?php echo $no1; ?></td>
  </tr>
  <tr>
    <td> 2 </td>
    <td><?php echo $no2; ?></td>
  </tr>
  <tr>
    <td> 3 </td>
    <td><?php echo $no3; ?></td>
  </tr>
  <tr>
    <td> 4 </td>
    <td><?php echo $no4; ?></td>
  </tr>
</table>

任何有关这方面的帮助都会很棒,所以提前感谢任何答案。

Any help with this would be great so thanks in advance for any answers.

这里是我使用的解决方案,下面是两个答案的组合:

HERE'S THE SOLUTION I USED, A COMBINATION OF 2 ANSWERS BELOW:

$data = '25,38,40,50';
$string = explode(",", $data);
$number = 0;
echo '<table cellspacing="0" cellpadding="0" border="0">';
for($i=0; $i<count($string); $i++)
{
    $number = $number +1;
    echo '
        <tr>
            <td>' . $number . '</td>
            <td>' . $string[$i] . '</td>
        </tr>
    ';
}


推荐答案

你可以使用PHP的 explode function。

You can use PHP's explode function.

输出所有内容后,将其保存到变量中。

When you output everything, save it into a variable.

然后, $ string = explode(,,$ string); 将产生这个:

数组([0] => 28 [1] => 38 [2] => 40 [3] => 50)

要输出此值,您可以为每个值使用 $ string [number]

To output this, you can then use $string[number] for each value.

这篇关于单独的逗号用php分隔mySql数据库字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆