Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am setting up an input autocomplete using jquery tokeninput plugin and XML data.

I have an XML file structured as shown:

<?xml version="1.0" encoding="UTF-8"?>
    <majors>
        <major program="GCIS">Computing &amp; Info Sci (PHD)</major>
        <major program="UINT">Business Administration (AAS)</major>
        etc..
    </majors>

I load it into PHP:

$majors = simplexml_load_file('../ajax/majors.xml');

I then want to do the following things:

  • reformat each <major>element into string, including program attribute

    • (ex: <major program="GCIS">Computing &amp; Info Sci (PHD)</major> turns into string GCIS - Computing &amp; Info Sci (PHD)
  • run the converted string through a filter function. The filter function checks for strpos($convertedString, $userQuery) and returns true/false if the user's query is present
  • elements which DO contain the $userQuery are all then encoded with json_encode($arr)
  • return JSON data.

This is the code I currently have... I can't seem to get the formatting / filtering to work correctly.

if(isset($_POST['query']) ) {
    $majors = simplexml_load_file('../ajax/majors.xml');

    # iterate through.
    foreach ($majors as $key => $value) {
        $arr = array('major' => $value['program'] . " - " . $value);
    }

    # filter the response using our query
    $arr = array_filter($arr, 'filterArrayWithQuery');

    # JSON-encode the response
    $json_response = json_encode($arr);

    # Return the response
    return $json_response;
}

# ensures that the search query is present
function filterArrayWithQuery( $string ) {
    return !strpos( $string, $query ) === false;
}

The end-result JSON output should look like this:

{"major":"GCIS - Computing &amp; Info Sci (PHD)","major":"UINT - Business Administration (AAS)"}
share|improve this question
    
Is the JSON end-result JSON output correct? Your associative array has 2 keys named "major". –  Kamehameha Feb 4 '14 at 19:58
    
@Yeah: The end-result JSON output is my desired output. I want to know what changes I need to make to get that output :) –  Prefix Feb 4 '14 at 20:00
    
@Prefix That's not valid JSON. So you can't use json_encode() to generate such output. –  hindmost Feb 4 '14 at 20:08
    
Look at this output format: {"majors":["GCIS - Computing & Info Sci (PHD)","UINT - Business Administration (AAS)"]}. Is it suits to you? –  hindmost Feb 4 '14 at 20:16

3 Answers 3

In your iterate through line, you are not appending new entries -

# iterate through.
foreach ($majors as $key => $value) {
   $arr[] = array('major' => $value['program'] . " - " . $value);
//     ^^
}

With this the output would be-

[{"major":"GCIS - Computing & Info Sci (PHD)"},{"major":"UINT - Business Administration (AAS)"}]
share|improve this answer
    
Thank you -- this was part of the issue in my initial code which I oversaw. I upvoted you for the contribution. I also figured out another issue (I was "returning" the JSON and not echoing it -- apparently you must echo JSON data? ) –  Prefix Feb 4 '14 at 20:55

Try this:

function convert($xml) {
    # ensures that the search query is present
    function filterArrayWithQuery($string) {
        static $query = null;
        if (is_null($query) && isset($_POST['query'])) $query = $_POST['query'];
        return !$query || strpos($string, $_POST['query']) !== false;
    }

    $majors = simplexml_load_string($xml);
    $arr = array();

    # iterate through
    foreach ($majors as $key => $value) {
        $arr[] = array('major' => $value['program'] . " - " . htmlentities($value));
    }

    # filter the response using our query
    $arr = array_filter($arr, 'filterArrayWithQuery');

    # Return the response (JSON-encoded)
    return json_encode(array('majors' => $arr));
}

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
    <majors>
        <major program="GCIS">Computing &amp; Info Sci (PHD)</major>
        <major program="UINT">Business Administration (AAS)</major>
    </majors>
XML;

$_POST['query'] = 'Info';
echo convert($xml);
share|improve this answer
up vote 0 down vote accepted

Thank you to the input I received, however I was able to figure out the solution.

Here is the final PHP script for those interested.

<?php


# vars
$reqMethod    = $_SERVER['REQUEST_METHOD'];
$ref          = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : "http://www.google.com";                            
$allowedHosts = array('redacted', 'localhost'); 
$majors       = null;
$arr          = array();
$query        = isset($_POST['q']) ?  $_POST['q'] : "";

# confirm request is being sent from where we allow.
if ( $reqMethod != 'POST' || !in_array(parse_url($ref, PHP_URL_HOST), $allowedHosts) ) {

    header('Location: ' . $ref);

} else {

    # load in XML
    $majors = simplexml_load_file('../ajax/majors.xml');

    # iterate through XML. if element contains query, add it to an array.
    foreach ($majors as $key => $value) {
        if( !stristr( $value, $query ) === false ){
            $arr[] = array('major' => $value['program'] . " - " . $value);
        }
    }

    # Return the response - JSON encoded
    header('Content-type: application/json');
    echo json_encode($arr);

}

?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.