0

I am currently building an application with the use of MobiOne studio in order to use their publication features. I however have run across a unique problem. The app, at it's most basic, queries the user for data to be entered into a hosted MYSQL database. Then via search box, thy can recall certain records from the same MYSQL database. The insertion part works perfectly. However, I can not get the results to display. I CAN make this work in any old browser with a simple PHP snippet to display the records in a loop for as many results as there are.

However, PHP does not run smoothly in a client run situation as this. MobiOne preferes the use of Javascript/JSON/JQuery to display data. Is there anyway to display the contents of the loop created by the PHP script serverside via Javascript to a HTML block or DOM?

The below is the PHP code I use to find the content from a GET form with the id "query" on a seperate index.php file. Like I said, this works great. But I need to be able to take the data this spits out and display it with some JS.

Please check this link out as an example of what the code actually does, if you are not sure. Just enter "cracker" as a test search query.

http://tjrcomputersolutions.com/apps/edibles/v1.0/index.php

<?php

//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['query']);

//check whether the name parsed is empty
if($searchTerm == "")
{
    echo "Enter name you are searching for.";
    exit();
}

//database connection info
$host = "localhost"; //server
$db = "*****"; //database name
$user = "*****"; //database user name
$pwd = "*****"; //password

//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);

//MYSQL search statement
$query = "SELECT * FROM items WHERE item LIKE '%$searchTerm%'";

$results = mysqli_query($link, $query);

/* check whether there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
    $output = "";
    while($row = mysqli_fetch_array($results))
    {
        $output .= "Item: " . $row['item'] . "<br />";
        $output .= "Store: " . $row['store'] . "<br />";
        $output .= "Size: " . $row['size'] . "<br />";
        $output .= "Price: " . $row['price'] . "<br /><br />";
    }
    echo $output;
}
else
    echo "There was no matching record for the name " . $searchTerm;
?>
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Tyler Roy
  • 3
  • 1
  • 2
  • 3

1 Answers1

0

You can use json_encode to transform PHP arrays into JSON format. For example:

$items = array(
    array(
        'Item' => 'a',
        'Store' => 'b',
        'Size' => 'c',
        'Price' => 'd',
    ),
    array(
        'Item' => 'e',
        'Store' => 'f',
        'Size' => 'g',
        'Price' => 'h',
    ),
);    

echo json_encode($items); // [{"Item":"a","Store":"b","Size":"c","Price":"d"},{"Item":"e","Store":"f","Size":"g","Price":"h"}]

In your case, you'd have to mount your array structure using data from your DB and then call this function to transform it into JSON.

Guilherme Sehn
  • 6,727
  • 18
  • 35
  • I guess I should clarify. Basically what I am trying to do is use a form to run an externally hosted PHP file, then output the results of that PHP file into an HTML document using Javascript. The HTML document will not be on the same server as the PHP file. The use for this is building a mobile app that accesses a MYSQL DB, edits it and displays search results. Maybe this can help in finding a solution for this. – Tyler Roy Nov 06 '13 at 04:08
  • Well, I do not know MobiOne, but if you are able to use HTML and JavaScript in your client application, you could use AJAX to query this data from your server and then mount the HTML in your client application based on the returned JSON response. – Guilherme Sehn Nov 06 '13 at 10:50