摆脱分隔符

发布于 2024-10-21 09:22:17 字数 1253 浏览 2 评论 0原文

您好,我遇到了以下问题,我正在使用以下代码。

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

趴在窗边数星星i 2024-10-28 09:22:17

尝试使用 implode

while ($row = mysql_fetch_array($new)) {
    $myrows = array();
    foreach ($sam[0] as $v) {
        myrows[] = $row[$v];
    }
    echo implode($DELIMITER, $myrows).'<br />';
}

Try using implode :

while ($row = mysql_fetch_array($new)) {
    $myrows = array();
    foreach ($sam[0] as $v) {
        myrows[] = $row[$v];
    }
    echo implode($DELIMITER, $myrows).'<br />';
}
煮茶煮酒煮时光 2024-10-28 09:22:17

方法一:
将每一行的数据写入数组,然后使用 implode

方法2:
设置一个标志以省略分隔符一次;


<?php
while (...)
{
    $tmp = 1;
    foreach (...)
    {
        if ($tmp)
        {
            $tmp = 0;
        }
        else
        {
            echo $DELIMETER;
        }
        echo $row[...];
    }
}

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;


<?php
while (...)
{
    $tmp = 1;
    foreach (...)
    {
        if ($tmp)
        {
            $tmp = 0;
        }
        else
        {
            echo $DELIMETER;
        }
        echo $row[...];
    }
}

ま昔日黯然 2024-10-28 09:22:17

你可以尝试这样的事情

// querys the database
$new = mysql_query("SELECT * FROM {$DB_TABLE}");

// while loop to loop through selected fields
while ($row = mysql_fetch_array($new)) {

    echo $row[0];
    for($i = 1; $i < count($row); $i++) {
        echo $DELIMITER.$row[$i];
    }
    echo "<br />";
}

You can try something like this

// querys the database
$new = mysql_query("SELECT * FROM {$DB_TABLE}");

// while loop to loop through selected fields
while ($row = mysql_fetch_array($new)) {

    echo $row[0];
    for($i = 1; $i < count($row); $i++) {
        echo $DELIMITER.$row[$i];
    }
    echo "<br />";
}
躲猫猫 2024-10-28 09:22:17

感谢人们设法让它与以下内容一起工作的所有回复;)

// querys the database
$new = mysql_query("SELECT {$ROWS} FROM {$DB_TABLE}");

// while loop to loop through selected fields
while ($row = mysql_fetch_assoc($new)) {

    $tmp = array();

    $tmp[] = implode($DELIMITER, $row);

    echo $tmp[0] . "<br />";

}

Thanks for all the responses people manage to get it to work with the following ;)

// querys the database
$new = mysql_query("SELECT {$ROWS} FROM {$DB_TABLE}");

// while loop to loop through selected fields
while ($row = mysql_fetch_assoc($new)) {

    $tmp = array();

    $tmp[] = implode($DELIMITER, $row);

    echo $tmp[0] . "<br />";

}
去了角落 2024-10-28 09:22:17

您可以将所需的值插入到数组中(表示为替代方式),并避免对从数据库获取的每一行进行 foreach,但如果您确实想使用现在获得的代码,您可以执行以下操作:

while ($row = mysql_fetch_array($new)) {
    $values = array();
    foreach ($sam[0] as $v) { 
        $values[] = $row[$v];
    }
    echo implode($DELIMITER , $values) . '<br/>';
}

或选择

while ($row = mysql_fetch_array($new)) {
    $values = array($row['id'], $row['firstname'], $row['lastname']);
    echo implode('(|)',$values) . '<br/>';
}

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:

while ($row = mysql_fetch_array($new)) {
    $values = array();
    foreach ($sam[0] as $v) { 
        $values[] = $row[$v];
    }
    echo implode($DELIMITER , $values) . '<br/>';
}

or the alternative

while ($row = mysql_fetch_array($new)) {
    $values = array($row['id'], $row['firstname'], $row['lastname']);
    echo implode('(|)',$values) . '<br/>';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文