0

I currently have a MySQL database that I am accessing using PHP. You search a name and it returns the person's contact information. However, sometimes you search a name that already exists and it returns 2 peoples contact information.

I am inserting the information into an HTML table using JavaScript. PHP is returning the contact information as a long string separated by spaces. And I am using the split function while using the space character as a delimiter.

This is all fine and dandy when the search result only returns 1 name. When it returns 2 names I am having trouble figuring out how separate the two contact's information. Since PHP is returning them as 1 continuous string.

Here is an snippet of my php code:

$data = mysql_query($sqlString) or die("Issue here:" . mysql_error());

    while($row = mysql_fetch_array($data)){
        print $row['firstName'] . " " . $row['lastName'];
    };

Here is a snippet of my JavaScript Code:

$.post("searchDB.php", {search:$("#searchValue").val(), 
       searchType:$("#searchType").val()}, 
       function(results) {
          var parsedResults = results.split(" ");
          //code for inserting into HTML.
       });
2
  • And what mark-up are you inserting into? Do the first name and last name share a cell, or is one in the first cell and the other in the second? Commented Jan 1, 2012 at 0:52
  • The SQL in $sqlString might be illuminating - without that information, it is hard to see how you are running into problems. Commented Jan 1, 2012 at 2:02

2 Answers 2

2

I'd suggest using a delimeter to separate one person's full-name from the next:

$data = mysql_query($sqlString) or die("Issue here:" . mysql_error());

    while($row = mysql_fetch_array($data)){
        print $row['firstName'] . " " . $row['lastName'] . "__";
    };

Leading to the following jQuery (this on the assumption you don't need to separate out the first-name and last-name to different elements):

$.post("searchDB.php", {search:$("#searchValue").val(), 
       searchType:$("#searchType").val()}, 
       function(results) {
          var fullNames = results.split('__');
          var table = $('tableSelector');
          for (var i=0,len=fullNames.length; i<len; i++){
              var newTr = $('<tr />').append('<td>' + fullNames[i] + '</td>');
              $(newTr).appendTo(table);
          }
       });

If, on the other hand, you need to have first-name and last-name in separate cells:

$.post("searchDB.php", {search:$("#searchValue").val(), 
       searchType:$("#searchType").val()}, 
       function(results) {
          var fullNames = results.split('__');
          var table = $('tableSelector');
          for (var i=0,len=fullNames.length; i<len; i++){
              var firstName = fullNames[i].split(' ')[0];
              var lastName = fullNames[i].split(' ')[1];

              var newTr = $('<tr />').append('<td>' + firstName + '</td><td>' + lastName + '</td>');
              $(newTr).appendTo(table);
          }
       });
0
<?php
  $result = mysql_query("SELECT * FROM table WHERE name='$name'"); //sample query
  $output = "";
  while ($data = mysql_fetch_array($result){
    foreach($data as $key => &$value){
      $output .= $value." ";
    }
  $output .= "\n" // new MySQL row delimiter
  }
?>

You can choose whatever you want for new row delimiter (here i chose new line) Then you just output $output however you want :) (print / echo) // and then you parse it somehow trough javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.