-1

I'm using CodeIgniter and I have a PHP MySQL Statement in a model:

function getAllDevices() {
    $query = $this->db->query("SELECT * FROM Device_tbl ORDER BY Manufacturer");
    return $query->result();
}

I then pass this through my controller to my view using:

    $this->load->model("get_device");
    $data['results'] = $this->get_device->getAllDevices();

And output this into a div using the following PHP:

        <div class="hold-cont">
            <div class="holder">
                <div class="image-hold"><img class="image-icon" src="<?php echo base_url(); ?>assets/images/devices/<?php echo $row->Image; ?>"></div>
            </div>

            <div class="device-name devicename-txt">
                <?php $mod = $row->Model;
                    $model = str_replace(" ","-",$mod);
                ?><a href="<?php echo base_url(); ?>roms?device=<?php echo($row->Manufacturer . '-' . $model);  ?>"><?php echo($row->Manufacturer. ' ' .$row->Model);  ?></a><br>
            </div>
        </div><?php } ?>

This currently works fine, however I have now incorporated a searchbox into my page which uses the jQuery function autocomplete and uses the JSON Array of $results

What I need to do is "convert" the PHP into Javascript which will enable the page to be populated using the JSON Array. I then need to be able to use this to pass the selection from the search box into a query which will change the content on the page without reloading it.

How can I go about this? I previously mashed together a way of doing it using the ajax function in jQuery, however this requires a URL which contained a PHP MySql statement which I cannot do now. The idea behind doing it like this is so it only runs 1 SQL Query when the page loads and I cannot change this method.

3 Answers 3

1

There is nothing to stop PHP "writing" Javascript.

You would need to build up a suitably formatted Javascript object either manually or by using the json_encode method of PHP (which essentially turns a PHP object into it's JSON notation). Assuming your PHP object is in $row then something like:

<code>
<script language="text/javascript">
 var jsObject = <?php json_encode($row); ?>
</script>
</code>

Would give you a Javascript object in jsObject containing keys and values corresponding to the properties of the PHP $row object that you can then work with from Javascript.

Sign up to request clarification or add additional context in comments.

6 Comments

This is what I am using for my autocomplete statement. However PHP only loads when the page loads. So if someone selected Samsung in the search box, I need that php foreach loop to only show the results containing Samsung. Something you can't do using PHP
If you are doing searching and filtering using Javascript then either: a) your Javascript object needs to contain ALL results from your entire database which you then filter out using Javascript or b) you need to re-query the database with the more specific data set using AJAX. a) is only really a good solution if you have a small amount of data. What is your objection to using AJAX to re-fetch on search/filter? If you are having problems actually searching from JS have a look at jQuery.grep etc
When the page loads it runs a query and pull everything into a JSON Array which is used by my .autocomplete search box and a PHP array as shown above. The reason is I plan on setting the search box to search and reload the data on the page on keyup. If I keep querying the database on keyup then the database will get put under a lot of stress, hence the reason to search the JSON Array instead
Usual method is to fire off a timer on key up (say a few seconds) and only actually do the search when that timer fires and no keys have been pressed since. So if you type the sentence samsung into your box the search will only actually fire a couple of seconds after you type the last g. Or of course, have a search button ;o)
It'll be on keyup ;) Regardless, I cant search the database, I need to search the array
|
0
 json_encode();
 json_decode();

Turns PHP arrays into a JSON array and back.

http://php.net/manual/en/function.json-encode.php

They work great.

2 Comments

Thanks, but this doesn't really help my current situation. I don't know how to load the array like I have done in PHP and then be able to alter the contents with ajax depending on whats selection in the search box
Simple. return the json to javascript. set it equal to a value in Javascript. So... ajaxData = data; Then ajaxData."ArrayKey" EX: ajaxData.id and it will print that. It as can be nested. So ajaxData.id.name
0

Your ajax page:

  $query = mysql_query("SELECT * FROM my_table WHERE my_field LIKE '%$input%'");
    while ($row = mysql_fetch_assoc($query)) {
    $json = array();
    $json['value'] = $row['id'];
    $json['name'] = $row['username'];
    $json['image'] = $row['user_photo'];
    $data[] = $json;
    }
    header("Content-type: application/json");
    echo json_encode($data);

and fetch this echoed json encoded data in your javascript

this is just a rough code, avoid using mysql function.

1 Comment

The problem I have here is that it is searching the database, not the json array. I cannot use this method as it means searching the database for every search and not the json array

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.