2011-10-28 1 views
1

これはおそらく本当に簡単ですが、私はこれを行うための最善の方法を知らない。私は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); 
} 
?> 
+0

ucwords(strtolower($ str))? – galchen

+0

newbのように聞こえて申し訳ありませんが、私はこれをbind文の後に置いていますか?それは重要ですか?私はucwordsを追加しました(strtolower($ Title)); xmlの出力で私のタイトルは表示されません。これが私を踏みにじっている。 –

+0

ucwordsとstrtolowerは変更された文字列を返します。彼らは元の値を変更しません。あなたが$ Title = ucwords(strtolower($ Title))をするならば。この時点から、$ Titleは変更されたバージョンになります。あなたが望むのは$ xw-> writeElement( 'Title'、ucwords(strtolower($ Title)))です。 – galchen

答えて

1

は、クエリのhttp://php.net/manual/en/function.ucwords.php

<?php 
$foo = 'hello world!'; 
$foo = ucwords($foo);    // Hello World! 

$bar = 'HELLO WORLD!'; 
$bar = ucwords($bar);    // HELLO WORLD! 
$bar = ucwords(strtolower($bar)); // Hello World! 
?> 

の関連する部分は、私が代わると思います:

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

で:

$results = mysqli_query("SELECT * FROM table"); 

は、その後にあなたのwhileループを変更します:

foreach($results as $row) { 
    $xw->startElement('item'); 
    $xw->writeElement('Name', ucwords(strtolower($row['name'])); 
    $xw->writeElement('Title', ucwords(strtolower($row['title'])); 
    $xw->endElement(); 
} 

私はあなたのデータベーススキーマを知らないので、明らかにこれを修正する必要があります。

mysqliを変更する主な理由は、将来データベースのスキーマを変更すると、データベース列の順序が同じであるとは限りません。

幸運を祈る!

+0

ありがとうHafichuk、私が従ったマニュアルしかし初心者私は後で働くXML部分を得るように見えることができません。 –

+0

は、ヒントHafichuk、ありがとう、ありがとう! –

+0

np、答えがあなたに役立つことを願って – hafichuk

関連する問題