implode()具有多个数据库列的数组,最后一次输入PHP除外 [英] implode() an array with multiple database columns, except on last entry PHP

查看:91
本文介绍了implode()具有多个数据库列的数组,最后一次输入PHP除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP从数据库创建XML文档,以导入到Adobe InDesign。我希望能够在每个条目之后添加逗号,但不能在最后一个条目上添加逗号。我曾尝试使用 implode(),但是我没有运气。任何帮助将不胜感激。这是没有尝试添加逗号的代码。我只能在结束语后添加一个逗号,但这仍然会在最后一个条目上给我一个逗号。任何有关如何对此进行攻击的建议将不胜感激。谢谢!

I am using PHP to create an XML document from a database to import into Adobe InDesign. I want to be able to add a comma after each entry but not on the last one. I have tried using implode() but I have had no luck. Any help would be greatly appreciated. Here is the code with out any attempt at adding the comma. I can just add a comma after the closing but that will still give me one on the last entry. Any advice on how to attack this would be much appreciated. Thanks!

function getAssocXML ($company) {
    $retVal = "";

    // Connect to the database by creating a new mysqli object
    require_once "DBconnect.php";

    $staffResult = $mysql->query("
        SELECT company,
               fName,
               lName,
               title,
               contact1,
               contact2
          FROM staff
         WHERE company = '$company'
           AND title
          LIKE '%Associate%'
           AND archive = 0
    ");

    if ($staffResult->num_rows >= 1 && $staffResult->num_rows < 4) {
        $retVal = $retVal;

        for ($i = 0; $i < $staffResult->num_rows; $i++) {
            // Move to row number $i in the result set.
            $staffResult->data_seek($i);
            // Get all the columns for the current row as an associative array -- we named it $aRow
            $staffRow = $staffResult->fetch_assoc();
            // Write a table row to the output containing information from the current database row.
            $retVal = $retVal . "<staff>";
            $retVal = $retVal . "<name>" . $staffRow['fName'] . " " . $staffRow['lName'] . "</name>";
            $retVal = $retVal . "<contact>" . staffContact($staffRow['contact1'], $staffRow['contact2']) . "</contact>";
            $retVal = $retVal . "</staff>";
        }

        $retVal = $retVal . " &#x2014; Associate&#xD;";
        $staffResult->free();
    }

    if ($staffResult->num_rows > 4) {
        $retVal = $retVal;
        $retVal = $retVal . "<staffHeader>Associates: </staffHeader>";

        for ($i = 0; $i < $staffResult->num_rows; $i++) {
            // Move to row number $i in the result set.
            $staffResult->data_seek($i);
            // Get all the columns for the current row as an associative array -- we named it $aRow
            $staffRow = $staffResult->fetch_assoc();
            // Write a table row to the output containing information from the current database row.
            $retVal = $retVal . "<staff>";
            $retVal = $retVal . "<name>" . $staffRow['fName'] . " " . $staffRow['lName'] . "</name>";
            $retVal = $retVal . "<contact>" . staffContact($staffRow['contact1'], $staffRow['contact2']) . "</contact>";
            $retVal = $retVal . "</staff>";
        }

        $retVal = $retVal . "&#xD;";
        $staffResult->free();
    }

    return $retVal;
}

print getAssocXML(addslashes($aRow['company']));


推荐答案

您可以在此查询的帮助下完成操作

You can do it with the help of this query

echo join(' and ', array_filter(array_merge(array(join(', ', array_slice($array, 0, -1))), array_slice($array, -1)), 'strlen'));




OR

OR



$last  = array_slice($array, -1);
$first = implode(', ', array_slice($array, 0, -1));
$both  = array_filter(array_merge(array($first), $last), 'strlen');
echo implode(' and ', $both);

这篇关于implode()具有多个数据库列的数组,最后一次输入PHP除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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