对于每个这些数字,在PHP上进行循环

时间:2016-01-21 20:06:38

标签: php mysql

从mySQL行开始,我从列coop中得到以下内容(使用逗号)

1,2,6,27

我的问题是我怎么能有像

这样的东西
for

作为列

的数字
do the loop {
{

2 个答案:

答案 0 :(得分:4)

假设您将值存储在字符串中,让我们将其称为<html> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/ libs/jquery/1.4.3/jquery.min.js"></script> <script type="text/javascript"></script> <head> <link rel="stylesheet" type="text/css" href="bootstrap.css" /> <link rel="stylesheet" href="stylesGrilcheeserie.css" type="text/css"/> <link href='../Fonts/Reef/reef-webfont.woff' rel='stylesheet' type='text/css'> <title> Grilcheeserie </title> </head> <body> <h1>La Grilcheeserie</h1> </body> </html> ,您可以将该字符串拆分为数组:

$dbValue

然后循环遍历该数组:

$values = explode(",", $dbValue);

顺便说一句......在单个数据库字段中存储分隔值经常一个坏主意。每个数据库字段应包含一个离散值。如果需要多个值,您可以创建一个具有多对一关系的单独表。

答案 1 :(得分:3)

似乎是预谋

$tmp = explode(',', $yourvalue)   // $yourvalue  = '1,2,6,27'
foreach ( $tmp as $key => $value ) {

   echo $value;
}