Hi,

I looked through the archives but could not find anything quite like this. I'm new to PHP so any help appreciated.

I have an MySQL database populated with data passed via excel from a client. It includes an ArtistName field that contains both First name and Surname in the following format:

Surname, Firstname

I need to remove the comma and reverse the word order so that the name reads naturally, eg:

Firstname Surname

Is this possible using a standard str_replace as the data is pulled out of MySQL (how?) or would I be better spending the time to split the First Name and Surname data across two fields in the database so that I can print the order as I please?

Many thanks,

Gary Crighton

    Hi Geoff,

    Tahnks for the tip but have still been unable to get this working. I suspect the problem is that the data is being puled out of MySQL and I'm associating the action I'm requesting with the fieldname correctly.

    Here's my original code:

    while ($row = mysql_fetch_array($query)) {
    print ("<tr><td width='65%' valign='top'><font class='name'>".$row["ArtistName"]."</font></td>\n");
    print ("<td width='35%' align='right'><img src=".$row["ArtistPhoto"]." width='80' height='80' border='0'></td></tr>\n");
    }

    I gather that $row["ArtistName"] needs to be associated with a variable, but not being a coder am not really sure on this one.

    Cheers anyway,

    Gary

      I'd suggest you read the section of the manual dealing with the MySQL functions before trying to continue any further:

      http://www.php.net/manual/en/ref.mysql.php

      mysql_fetch_array() doesn't accept an SQL statement, it takes a result set identifier.

      &nbsp;&nbsp;$conn = mysql_connect("your_host", "username", "password");
      &nbsp;&nbsp;mysql_select_db("your_db", $conn);

      &nbsp;&nbsp;$rs = mysql_query("SELECT ArtistName, ArtistPhoto FROM ArtistTable", $conn);

      &nbsp;&nbsp;while ($row = mysql_fetch_array($rs)) {
      &nbsp;&nbsp;&nbsp;&nbsp;print ("<tr><td width='65%' valign='top'>".$row["ArtistName"]."</td>\n");
      &nbsp;&nbsp;&nbsp;&nbsp;print ("<td width='35%' align='right'><img src=".$row["ArtistPhoto"]." width='80' height='80'
      &nbsp;&nbsp;&nbsp;&nbsp;border='0'></td></tr>\n");
      &nbsp;&nbsp;}

      &nbsp;&nbsp;mysql_free_result($rs);

      -geoff

        Hi Geoff,

        Thanks a lot for your input - the initial mail did in fact put me on the right track and I now have it working with the following code:

        $ArtistName = $row["ArtistName"];
        //Remove comma and reverse order of name.
        //Only works for Surname, Firstname format
        $ArtistName = explode(", ", $ArtistName);
        $ArtistName = array_reverse($ArtistName);
        $loop = count($ArtistName);
        $this_loop = 0;
        while($this_loop < $loop) {
        echo ($ArtistName[$this_loop])."<br>";
        $this_loop++;
        }

        A little verbose perhaps, but clearly legible to a beginner such as myself!

        Thanks again,

        Gary

          Write a Reply...