Search code examples
phpmysqlixmlwriter

Capitalize first character of string using php. Then output with XMLWriter.


This is probably real simple, but I don't know best way to do this. I am pulling data from a mysqli database with php to create an XML document. My code below works but data in the Title field is all caps. I need just the first character capitalized, the rest lowercase. I know I need the ucwords function, but no dice yet. The Title field has more than one word in all caps.

I need the data formatted by ucwords before it goes into the XML area. I prefer to do this in the php instead of updating the data in the db. Thanks for the help!

<?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);
 }
?>

Solution

  • The relevant section in 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!
    ?>
    

    For your query, I'd replace:

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

    With:

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

    Then change your while loop to:

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

    Obviously you need to tinker with this since I don't know your database schema.

    The main reason for changing the mysqli stuff is that you aren't guaranteed to have the same ordering of the database columns if you make schema changes to the database in the future.

    Good luck!