ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:userguide:learn_abstraction:key_value_pairs

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
v5:userguide:learn_abstraction:key_value_pairs [2016/03/15 01:21] – created mnewnhamv5:userguide:learn_abstraction:key_value_pairs [2021/02/21 13:45] (current) – typo, httpheader for json, param names for mysqli example peterdd
Line 1: Line 1:
 <- v5:userguide:learn_abstraction:basic_query|A Basic Query ^ v5:userguide:learn_abstraction:start_lesson|Start Of Lesson ^ v5:userguide:learn_abstraction:using_execute|Using The Execute Method -> <- v5:userguide:learn_abstraction:basic_query|A Basic Query ^ v5:userguide:learn_abstraction:start_lesson|Start Of Lesson ^ v5:userguide:learn_abstraction:using_execute|Using The Execute Method ->
 +~~NOTOC~~
 ====== Key/Value Pairs ====== ====== Key/Value Pairs ======
 +===== An Associative Array =====
 An often-used SQL construction is to read a table for a key/value pair. This can be used in creating Select Boxes, Radio groups etc. ADOdb contains a shortcut command [[v5:reference:connection:getassoc|getAssoc()]] to assist in the task. An often-used SQL construction is to read a table for a key/value pair. This can be used in creating Select Boxes, Radio groups etc. ADOdb contains a shortcut command [[v5:reference:connection:getassoc|getAssoc()]] to assist in the task.
  
Line 19: Line 21:
 ); );
 </code>  </code> 
 +===== Supplying Data To An Ajax Client =====
 +PHP routines are often used to provide data to Javascript frameworks, such as Jquery. The data is often returned in JSON format, which can easily be created using ADOdb. The sample program below provides data to the Jquery plugin [[https://select2.github.io/|Select2]] which creates replacement Select Boxes.
  
 +==== Client Side Code ====
 +On the client side, the javascript code is defined to use an Ajax server
 +<code javascript>
 +(".js-data-example-ajax").select2({
 +  ajax: {
 +    url: "http://127.0.0.1/adodbExample.php",
 +    dataType: 'json',
 +    delay: 250,
 +    data: function (params) {
 +      return {
 +        q: params.term, 
 +    definition continues.........
 +});
 +</code>
 +==== Server Side Code ====
 +On the server side, we query a table containing USA State codes and names. 
 +<code php>
 +/**
 +* Program adodbExample.php
 +*/
 +include_once 'adodb_dir/adodb.inc.php';
 +
 +/*
 +* We define a simple object to represent the data,
 +* plus an array to hold them in
 +*/
 +class jsonObject
 +{
 +   /*
 +    * The jquery plugin requires each pair of data be a json object,
 +    * with the key called 'id' and the description called 'text'
 +    */
 +    public $id;
 +    public $text;
 +}
 +
 +$rows = array();
 +
 +$db = newAdoConnection('mysqli');
 +#$db->debug=true;
 +$db->connect('dbhost','dbuser','dbpassword','dbname');
 +
 +$sql = "SELECT state_code,name 
 +         FROM us_states
 +     ORDER BY state_code";
 +$result = $db->execute($sql);
 +while ($r = $result->fetchRow())
 +{
 +    $o       = new jsonObject;
 +    $o->id   = $r[0];
 +    $o->text = $r[1];
 +    $rows[]  = $o;
 +}
 +
 +/*
 +* Return the data to the calling progam
 +*/
 +if(!$db->debug){
 +    header('Content-Type: application/json');
 +}
 +print json_encode($rows);
 +</code>
 +
 +===== Other Methods For Ajax ======
 +The method [[v5:reference:recordset:fetchnextobj|fetchNextObject()]] returns data as a PHP object which can be easily JSON encoded.
 +
 +{{htmlmetatags>metatag-keywords=(php, programming, json, javascript, jquery, adodb, select2, plugin, ajax)}}
 +{{htmlmetatags>metatag-description=(An example of how to use ADOdb to serve a jquery plugin using ajax )}}
  
  
v5/userguide/learn_abstraction/key_value_pairs.1458001298.txt.gz · Last modified: 2017/04/21 11:40 (external edit)