Web开发中遇到的数据传递问题(一)

来源:互联网 发布:量化股票模型软件 编辑:程序博客网 时间:2024/04/29 08:58
如何在model层js中获取control层中的数组变量?
1.在后端php文件中对数组进行json转换,也就是转成字符串的形式。
如:$task_phase_conf_json = json_encode($task_phase_conf);

2.在前段js文件中,用eval()对字符串做解析。
var task_phase_conf = eval({$task_phase_conf_json});


如何将model数组变量传递到control层?
前端:<input type="checkbox" name="ids[]" value="'+json[i]['uid']+'">
后端:$ids and $str_ids = implode(',',$ids);


如何用ajax获取数组并在前端动态列出?
前端:
$.post("index.php?do=$do&view=$view&ac=ser",{u:txt_u,type:u_type},
          function(json){
                   if(json.status === undefined){
                        for(var i=0;i<json.length;i++){
                              $('#info_ul').append('<li style="width:200px;float:left;"><input type="checkbox" name="ids[]"        value="'+json[i]['uid']+'">'+json[i]['username']+'</li>');
                         }
                        
                     }else if(json.status==2){
                            art.dialog.alert(json.msg);return false;
                      }
           }    ,'json')
后端:
$user_info=db_factory::query(" select uid,username from ".TABLEPRE."witkey_space where email <> ‘’  and $where ");
if(!$user_info){
        kekezu::echojson('查无此人','2');die();
}else{
        echo JSON($user_info);die();
}

Json处理函数:
/* * ************************************************************
 *
* 将数组转换为JSON字符串(兼容中文)
* @param array $array  要转换的数组
* @return string  转换得到的json字符串
* @access public
*
* *********************************************************** */
function arrayRecursive(&$array, $function, $apply_to_keys_also = false) {
    static $recursive_counter = 0;
    if (++$recursive_counter > 1000) {
        die('possible deep recursion attack');
    }
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            arrayRecursive($array[$key], $function, $apply_to_keys_also);
        } else {
            $array[$key] = $function($value);
        }

        if ($apply_to_keys_also && is_string($key)) {
            $new_key = $function($key);
            if ($new_key != $key) {
                $array[$new_key] = $array[$key];
                unset($array[$key]);
            }
        }
    }
    $recursive_counter--;
}
function JSON($array) {
    arrayRecursive($array, 'urlencode', true);
    $json = json_encode($array);
    return urldecode($json);

}
0 0