2016-08-10 17 views
0

PHPを使用してデータベースからAdobe InDesignにインポートするXMLドキュメントを作成しています。私は最後のものではなく、各エントリの後にカンマを追加できるようにしたい。私はimplode()を使ってみましたが、私は運がありませんでした。どんな助けでも大歓迎です。コンマを追加しようとする試みはありません。私はちょうど終わりの後にコンマを追加することができますが、それでも私は最後のエントリに1つを与えるでしょう。これをどのように攻撃するかについてのアドバイスは、非常に高く評価されます。ありがとう!implode()複数のデータベース列を持つ配列(最後のエントリを除く)PHP

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

あなたはこのクエリの助けを借りてそれを行うことができます

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); 
+1

'join'はPHP関数の' implode'のエイリアスです。 –

0

私はすぐにここにあなたの意図を把握していないことを恐れています。私は(別名join()implode()しかし

の前提条件である、配列の任意の並べ替えを使用しているあなたのコードには何も表示されない、ここで私は何度も使用されてきたちょっとしたトリックだとき、Iループで何かを構築しています:

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

最初時間をループに通し、$commaカンマではありません!  空の文字列です。  ループを通した最初の反復の終わりに、それはコンマになります。の次のの時間です。任意の長さの "カンマ区切り文字列"をインプレースで構築するのは簡単で効果的な方法です。

HTH!

関連する問題