Fix: best json_encode function for old PHP
Fix: remove unused function php2js
This commit is contained in:
parent
97573659c2
commit
97f0130861
@ -92,14 +92,10 @@ function ajax_autocompleter($selected='',$htmlname,$url,$option='',$minLength=2,
|
||||
*/
|
||||
function ajax_multiautocompleter($htmlname,$fields,$url,$option='',$minLength=2,$autoselect=0)
|
||||
{
|
||||
$script='';
|
||||
|
||||
$fields = php2js($fields);
|
||||
|
||||
$script.= '<!-- Autocomplete -->'."\n";
|
||||
$script = '<!-- Autocomplete -->'."\n";
|
||||
$script.= '<script type="text/javascript">';
|
||||
$script.= 'jQuery(document).ready(function() {
|
||||
var fields = '.$fields.';
|
||||
var fields = '.json_encode($fields).';
|
||||
var length = fields.length;
|
||||
var autoselect = '.$autoselect.';
|
||||
//alert(fields + " " + length);
|
||||
@ -284,33 +280,4 @@ function ajax_constantonoff($code,$input=array())
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a PHP array into a js array
|
||||
* @param $var
|
||||
* @return String with js array or false if error
|
||||
*/
|
||||
function php2js($var)
|
||||
{
|
||||
if (is_array($var)) {
|
||||
$res = "[";
|
||||
$array = array();
|
||||
foreach ($var as $a_var) {
|
||||
$array[] = php2js($a_var);
|
||||
}
|
||||
return "[" . join(",", $array) . "]";
|
||||
}
|
||||
elseif (is_bool($var)) {
|
||||
return $var ? "true" : "false";
|
||||
}
|
||||
elseif (is_int($var) || is_integer($var) || is_double($var) || is_float($var)) {
|
||||
return $var;
|
||||
}
|
||||
elseif (is_string($var)) {
|
||||
return "\"" . addslashes(stripslashes($var)) . "\"";
|
||||
}
|
||||
// autres cas: objets, on ne les gère pas
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
@ -41,8 +41,49 @@ if (! function_exists('json_encode'))
|
||||
*/
|
||||
function json_encode($elements)
|
||||
{
|
||||
if (is_array($elements)) return '["' . join('","', $elements) . '"]';
|
||||
else return '"'.$elements.'"';
|
||||
$num = count($elements);
|
||||
|
||||
// determine type
|
||||
if (is_numeric(key($elements)))
|
||||
{
|
||||
// indexed (list)
|
||||
$output = '[';
|
||||
for ($i = 0, $last = ($num - 1); isset($elements[$i]); ++$i)
|
||||
{
|
||||
if (is_array($elements[$i])) $output.= json_encode($elements[$i]);
|
||||
else $output .= _val($elements[$i]);
|
||||
if($i !== $last) $output.= ',';
|
||||
}
|
||||
$output.= ']';
|
||||
}
|
||||
else
|
||||
{
|
||||
// associative (object)
|
||||
$output = '{';
|
||||
$last = $num - 1;
|
||||
$i = 0;
|
||||
foreach($elements as $key => $value)
|
||||
{
|
||||
$output .= '"'.$key.'":';
|
||||
if (is_array($value)) $output.= json_encode($value);
|
||||
else $output .= _val($value);
|
||||
if ($i !== $last) $output.= ',';
|
||||
++$i;
|
||||
}
|
||||
$output.= '}';
|
||||
}
|
||||
|
||||
// return
|
||||
return $output;
|
||||
}
|
||||
|
||||
function _val($val)
|
||||
{
|
||||
if (is_string($val)) return '"'.rawurlencode($val).'"';
|
||||
elseif (is_int($val)) return sprintf('%d', $val);
|
||||
elseif (is_float($val)) return sprintf('%F', $val);
|
||||
elseif (is_bool($val)) return ($val ? 'true' : 'false');
|
||||
else return 'null';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user