implode() an array with multiple database columns, except on last entry PHP

放肆的年华 提交于 2020-01-07 05:43:09

问题


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']));

回答1:


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

$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);



回答2:


I'm afraid that I don't readily grasp your intent here. I don't see anything in your code that is using any sort of array, which is a pre-requisite for implode() (a.k.a. join())

However, here's a little trick that I've used numerous times when I am building something in a loop:

 $result = "";
 $comma  = "";
 foreach (...) {
   .. calculate some $value ..
   $result = $result . $comma . $value;
   $comma = ",";
 }

The first time through the loop, $comma isn't a comma!   It's an empty string.   At the end of the first iteration through the loop, it becomes a comma, for use next time. It's a simple but effective way to build an arbitrarily-long "comma-separated string" in-place.

HTH!



来源:https://stackoverflow.com/questions/38878016/implode-an-array-with-multiple-database-columns-except-on-last-entry-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!