摆脱分隔符
您好,我遇到了以下问题,我正在使用以下代码。
<?php
//DB CONNECTION
$ROWS = "id,firstname,lastname";
// explode at the comma and insert into an array
$test = explode("," , $ROWS);
//adds array test to the var sam
$sam = array($test);
// querys the database
$new = mysql_query("SELECT * FROM {$DB_TABLE}");
// while loop to loop through selected fields
while ($row = mysql_fetch_array($new)) {
foreach ($sam[0] as $v) {
echo $row[$v] . $DELIMITER . "<br />";
}
echo "<br />";
}
?>
这将输出以下内容。
834(|)Step(|)Thompson(|)
835(|)Lucy(|)kim(|)
836(|)Iwan(|)Will(|)
837(|)Sarah (|)Good(|)
我正在努力解决的是我想摆脱最后一个分隔符
,所以我会
834(|)Step(|)Thompson
835(|)Lucy(|)kim
836(|)Iwan(|)Will
837(|)Sarah (|)Good
尝试使用
// while loop to loop through selected fields
while ($row = mysql_fetch_array($new)) {
foreach ($sam[0] as $v) {
$test = $row[$v] . $DELIMITER;
echo substr($test, 0, -1);
}
echo "<br />";
}
,但这会摆脱所有的分隔符???
这将输出以下内容。
834Stephompson
835Lucykim
836IwanWill
837SarahGood
理想情况下,我希望每一行都单独存储在一起。
请帮忙,我今天过得很糟糕,我的头完全炸了???
hi i have got the following issues i am using the following code.
<?php
//DB CONNECTION
$ROWS = "id,firstname,lastname";
// explode at the comma and insert into an array
$test = explode("," , $ROWS);
//adds array test to the var sam
$sam = array($test);
// querys the database
$new = mysql_query("SELECT * FROM {$DB_TABLE}");
// while loop to loop through selected fields
while ($row = mysql_fetch_array($new)) {
foreach ($sam[0] as $v) {
echo $row[$v] . $DELIMITER . "<br />";
}
echo "<br />";
}
?>
This will output the following .
834(|)Step(|)Thompson(|)
835(|)Lucy(|)kim(|)
836(|)Iwan(|)Will(|)
837(|)Sarah (|)Good(|)
what i am struggling with is i want to get rid off the last delimiter
so it would be
834(|)Step(|)Thompson
835(|)Lucy(|)kim
836(|)Iwan(|)Will
837(|)Sarah (|)Good
i have tried using
// while loop to loop through selected fields
while ($row = mysql_fetch_array($new)) {
foreach ($sam[0] as $v) {
$test = $row[$v] . $DELIMITER;
echo substr($test, 0, -1);
}
echo "<br />";
}
but this gets rid off the delimiter for all off them???
This will output the following .
834Stephompson
835Lucykim
836IwanWill
837SarahGood
ideally i would like each row to be its own stored together.
Any Help Please i am having a bad day today my head is fried completely???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试使用 implode :
Try using implode :
方法一:
将每一行的数据写入数组,然后使用 implode
方法2:
设置一个标志以省略分隔符一次;
Method 1:
Write the data of each row into an array, and then use implode
Method 2:
set a flag to omit the delimeter once;
你可以尝试这样的事情
You can try something like this
感谢人们设法让它与以下内容一起工作的所有回复;)
Thanks for all the responses people manage to get it to work with the following ;)
您可以将所需的值插入到数组中(表示为替代方式),并避免对从数据库获取的每一行进行 foreach,但如果您确实想使用现在获得的代码,您可以执行以下操作:
或选择
You could insert into an array (denoted as the alternative way) the values you need and avoid the foreach for each row you get from database but if you really want to use the code as you got it now you can do the following:
or the alternative