Hello, i have the following column in mysql table.
number | name
123 | ABC XYZ
456 | DEF QRS

hre is my mysql query:

$query = "SELECT name, extension FROM users ORDER BY name ASC";
$resultID = mysql_query($query, $linkID) or die("Data not found.");

$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<AddressBook>\n";
for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
$row = mysql_fetch_assoc($resultID);
$xml_output .= "\t<Contact>\n";
$xml_output .= "\t\t<LastName>" . $row['name'] . "</LastName>\n";
$xml_output .= "\t\t<FirstName></FirstName>\n";

for the moment my First name is blank..i need to retreive the name column and display it into different output. can someone advise how to split the data.

thanks in advance.

Hi,
you could explode by the spaces between the names:

$names = explode(' ', $row['name']);
echo $names[0];
echo $names[1];

BUT you cannot distinguish between name, middlename and lastname. The correct solution would be to normalize this information during the collection step. Which means:

firstname: <input type="text" name="firstname" /><br />
middlename: <input type="text" name="middlename" /><br />
lastname: <input type="text" name="lastname" /><br />

And then insert it in the appropriated columns:

insert into users (firstname, middlename, lastname) values($fname, $mname, $lname);

More information here:

thanks i will try the explode and revert back as i cannot modify the input form.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.