v5:userguide:learn_abstraction:key_value_pairs
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
v5:userguide:learn_abstraction:key_value_pairs [2016/03/15 01:21] – created mnewnham | v5: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: | <- v5: | ||
+ | ~~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: | 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: | ||
Line 19: | Line 21: | ||
); | ); | ||
</ | </ | ||
+ | ===== 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:// | ||
+ | ==== Client Side Code ==== | ||
+ | On the client side, the javascript code is defined to use an Ajax server | ||
+ | <code javascript> | ||
+ | (" | ||
+ | ajax: { | ||
+ | url: " | ||
+ | dataType: ' | ||
+ | delay: 250, | ||
+ | data: function (params) { | ||
+ | return { | ||
+ | q: params.term, | ||
+ | definition continues......... | ||
+ | }); | ||
+ | </ | ||
+ | ==== Server Side Code ==== | ||
+ | On the server side, we query a table containing USA State codes and names. | ||
+ | <code php> | ||
+ | /** | ||
+ | * Program adodbExample.php | ||
+ | */ | ||
+ | include_once ' | ||
+ | |||
+ | /* | ||
+ | * 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 ' | ||
+ | */ | ||
+ | public $id; | ||
+ | public $text; | ||
+ | } | ||
+ | |||
+ | $rows = array(); | ||
+ | |||
+ | $db = newAdoConnection(' | ||
+ | # | ||
+ | $db-> | ||
+ | |||
+ | $sql = " | ||
+ | FROM us_states | ||
+ | ORDER BY state_code"; | ||
+ | $result = $db-> | ||
+ | while ($r = $result-> | ||
+ | { | ||
+ | $o = new jsonObject; | ||
+ | $o-> | ||
+ | $o->text = $r[1]; | ||
+ | $rows[] | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * Return the data to the calling progam | ||
+ | */ | ||
+ | if(!$db-> | ||
+ | header(' | ||
+ | } | ||
+ | print json_encode($rows); | ||
+ | </ | ||
+ | |||
+ | ===== Other Methods For Ajax ====== | ||
+ | The method [[v5: | ||
+ | |||
+ | {{htmlmetatags> | ||
+ | {{htmlmetatags> | ||
v5/userguide/learn_abstraction/key_value_pairs.1458001298.txt.gz · Last modified: 2017/04/21 11:40 (external edit)