2

私のscript.jsは、出力する3 つの変数 (echo)$all_categoriesと. ここで、これらをindex.htmlの 3 つの異なる領域で操作したいと考えています。$all_brands$all_filters

、、 、に配置$all_categoriesしたい。<div id="caregories">$all_brands<div id="vrands">$all_filtesr<div id="filters">

それぞれに別々に配置するにはどうすればよいdivですか? それらすべてを 1 つに配置する方法は知って<div>いますが、受け取った各変数を個別に配置する方法がわかりません。

index.php

<?php // Startup ?>

<?php 

    define('APP_PATH', '../');
    define('SYS_PATH', '../system/');
    define('STYLES', 'assets/styles/');
    define('SCRIPTS', 'assets/scripts/');

    require_once SYS_PATH . 'initializers/all.php'; 

?>

<?php // Controller ?>

<?php



?>

<?php // Viewer ?>

<?php template('header'); ?>

<body>

<div id="static_menu">
    <ul>
        <li><a href="#categories">Categories</a></li>
        <li><a href="#brands">Brands</a></li>
        <li><a href="#filters">Filters</a></li>
        <li><a href="#social">Social</a></li>
    </ul>
</div>
<hr>
<div id="categories">
    <ul></ul>
</div>
<hr>
<div id="brands">
    <ul></ul>
</div>
<hr>
<div id="filters">
    <ul></ul>
</div>
<hr>
<div id="social">
    <ul></ul>
</div>
<hr>

</body>

<?php template('footer'); ?>

script.js

// DOM ready
$(function(){

    // get categories, brands, filters and social
    $.ajax({
        type: "POST",
        url: "get_all.php"
    }).done(function(data){
        // manipulate recieved in three different divs
    })

});

get_all.php

<?php // Startup ?>

<?php 

    define('APP_PATH', '../');
    define('SYS_PATH', '../system/');
    define('STYLES', 'assets/styles/');
    define('SCRIPTS', 'assets/scripts/');

    require_once SYS_PATH . 'initializers/all.php'; 

?>

<?php // Controller ?>

<?php

$result_arr = $category->select_all();
$all_categories = $html->list_anchor_loop($result_arr, 'category');
echo $all_categories

$result_arr = $brand->select_all();
$all_brands = $html->list_anchor_loop($result_arr, 'brand');
echo $all_brands;

$result_arr = $filter->select_all();
$all_filters = $html->list_anchor_loop($result_arr, 'filter');
echo $all_filters;
4

3 に答える 3

7

PHP スクリプトが結果を JSON オブジェクトとして送信するようにします (「参考文献」を参照json_encode)。

JS では、オブジェクト、たとえば を受け取ります。これには、およびrespの 3 つのプロパティがあります。resp.categoriesresp.filtersresp.brands

詳細と具体性については、コードを投稿してください。

get_all.php

$result = array(
  'categories' => $all_categories,
  'brands' => $all_brands,
  'filters' => $all_filters
);

echo json_encode($result);

script.js

$.ajax({
    type: "POST",
    url: "get_all.php",
    dataType : 'json', //specify that data will be returned as JSON
}).done(function(data){
    // use data.categories, data.brands, data.filters here
    // manipulate recieved in three different divs
})
于 2012-11-11T07:34:05.300 に答える
1

ajax 呼び出しがJSONを介して返される場合、必要な div にデータを簡単に挿入できます。

PHP

<?php
$return = array(
    'categories'=>$all_categories,
    'vrands'=>$all_brands,
    'filters'=>$all_filters
    );
echo $_GET['jsoncallback'].'('.json_encode($return).')';?>

JQuery:

<script type="text/javascript">
$.getJSON("URL_TO_PHP?jsoncallback=?",{
    /*DATA TO SEND*/
    },function(response){
        $('#categories').html(response.categories);
        $('#vrands').html(response.vrands);
        $('#filters').html(response.filters);

});
</script>
于 2012-11-11T07:37:27.070 に答える
0

http://phery-php-ajax.netのように、それを自動的に行うライブラリを使用できます。DOM を順番に操作しながら、JSON データを PHP から直接渡すことができます。Phery は URL では機能しませんが、関数コールバックで機能し、あなたの場合のように、PHP 自体から DIV に割り当てることができます。

<?php

Phery::instance()->set(array(
    'get_all' => function($data){
       /* globals, initialization, etc*/
       $r = new PheryResponse;
       $result_arr = $category->select_all();
       return 
         $r
         ->jquery('#categories')->html($html->list_anchor_loop($result_arr, 'category'))
         ->jquery('#filter')->html($html->list_anchor_loop($result_arr, 'filter'))
         ->jquery('#brand')->html($html->list_anchor_loop($result_arr, 'brand'));

    }
))->process();

次にget_all、DOMReady から関数を呼び出します

$(function(){
  phery.remote('get_all');
});

完了すると、追加のクライアント側コードなしで、すべてが満たされます。

于 2012-11-12T02:50:31.973 に答える