これはおそらく本当に簡単ですが、私はこれを行うための最善の方法を知らない。私はXML文書を作成するためにphpでmysqliデータベースからデータを取得しています。下のコードは動作しますが、タイトルフィールドのデータはすべて大文字です。最初の文字は大文字で、残りは小文字にする必要があります。私はucwords関数が必要だが、ダイスはまだないことを知っている。タイトルフィールドには、複数の単語がすべて含まれています。phpを使って文字列の最初の文字を大文字にします。 XMLWriterで出力します。
XMLエリアに入る前に、ucwordsでフォーマットされたデータが必要です。私は、データベース内のデータを更新する代わりにPHPでこれを行うことを好む。助けてくれてありがとう!
<?php
// Connect to the database
global $link;
$link = mysqli_connect("localhost", "root", "pass", "database");
// Verify the connection worked
if (!$link) {
printf("Connection to database failed: %s\n", mysqli_connect_error());
exit();
}
// Prepare the database query
$stmt = mysqli_prepare($link, "SELECT * FROM table");
// Run the database query
mysqli_stmt_execute($stmt);
// Bind the result columns to PHP variables
mysqli_stmt_bind_result($stmt, $Name, $Title);
// Create a new XML document in memory
$xw = new xmlWriter();
$xw->openURI('php://output');
$xw->openMemory();
$xw->startDocument('1.0');
// Start the outer data container
$xw->StartElement('rss');
$xw->WriteAttribute('version', '2.0');
// Fetch values
while (mysqli_stmt_fetch($stmt)) {
{
$xw->startElement('item');
// Write out the elements
$xw->writeElement('Name', $Name);
$xw->writeElement('Title', $Title);
$xw->endElement();
}
// End container
$xw->endElement();
// End the document
$xw->endDocument();
//header('Content-Type: text/xml');
print $xw->outputMemory(true);
// Close the database statement
mysqli_stmt_close($stmt);
// Close the database connection
mysqli_close($link);
}
?>
ucwords(strtolower($ str))? – galchen
newbのように聞こえて申し訳ありませんが、私はこれをbind文の後に置いていますか?それは重要ですか?私はucwordsを追加しました(strtolower($ Title)); xmlの出力で私のタイトルは表示されません。これが私を踏みにじっている。 –
ucwordsとstrtolowerは変更された文字列を返します。彼らは元の値を変更しません。あなたが$ Title = ucwords(strtolower($ Title))をするならば。この時点から、$ Titleは変更されたバージョンになります。あなたが望むのは$ xw-> writeElement( 'Title'、ucwords(strtolower($ Title)))です。 – galchen