php数组合并执行每个循环时不会显示JSON数组数据

时间:2015-07-23 12:30:00

标签: jquery json loops

我有两个php数组$ pricelist& $产品。在将这些数组与json编码合并后,我得到了json数组,如下所示。

我的php数组如下:

Array
(
    [0] => Array
        (
            [PriceList] => Array
                (
                    [price_id] => 2
                    [price_name] => abc
                    [date_time] => 2015-07-06 16:22:56
                    [dealer_type] => Dealer
                    [purpose] => dealer
                    [status] => ACTIVE
                )

        )

    [1] => Array
        (
            [Product] => Array
                (
                    [cat_id] => 1
                    [subcat_id] => 3
                    [brand_id] => 1
                    [p_code] => PP12567
                    [name] => akai
                    [model_no] => 
                    [specification] => color tv
                    [color] => 
                    [quality] => 
                    [size] => 
                    [p_unavail] => 1
                    [demo_avail] => 0
                    [brochure] => 
                    [status] => active
                )

            [ProductPrice] => Array
                (
                    [id] => 154
                    [p_code] => PP12567
                    [price_id] => 1
                    [quantity] => 233
                    [purchase_price] => 344.00
                    [selling_price] => 44.00
                    [discount_price] => 33.00
                    [tax] => 5.00
                    [datetime] => 2015-07-23 15:47:11
                )

            [ProductSubCategory] => Array
                (
                    [subcat_id] => 3
                    [cat_id] => 1
                    [subcat_name] => samsung
                    [status] => active
                )

            [ProductCategory] => Array
                (
                    [cat_id] => 1
                    [cat_name] => Electronics
                    [cat_type] => Product 
                    [status] => active
                )

        )

这是我的JSON数组:

[
  {
     "PriceList":
     {
        "price_id":"2",
        "price_name":"abc",
     }
  },

  {
    "Product":
      {
         "cat_id":"1",
         "subcat_id":"3",
         "p_code":"PP12567",
         "name":"akai"
      },
    "ProductSubCategory":
      {
         "subcat_id":"3",
         "cat_id":"1",
         "subcat_name":"samsung"
         },
    "ProductCategory":
      {
         "cat_id":"1",
         "cat_name":"Electronics"
      }
   }
]

我的每个循环如下:

function render_price_list_details(resp){

    table = '';

    $.each(resp,function(indx,obj){

            table += '<tr>';
            table += '<td>'+ parseInt(indx+1) +'</td>';
            table += '<td>'+ obj.ProductCategory.cat_name +'</td>';
            table += '<td>'+ obj.ProductSubCategory.subcat_name +'</td>';
            table += '<td>'+ obj.Product.name +'</td>';
            table += '<td>'+ obj.PriceList.price_name +'</td>';
            table += '</tr>';

    });

            $("tbody#product_price").append(table);

}
执行循环时,

表格未显示。

如果我提醒resp那么它显示如下: [对象,对象],[对象,对象]

1 个答案:

答案 0 :(得分:0)

追加部分在你的循环/每个函数之外。因此,当整个循环完成时,它会被附加。

try {
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );
    echo $cid . "<br>";
    echo $tid;
//Prepare
if ($stmt = $con->prepare("SELECT * FROM forum_topics WHERE `category_id`=? AND `id`=? LIMIT 1")) {

    $stmt->bind_param("ii", $cid, $tid);
    $stmt->execute();
    $stmt->bind_result($topic_id, $category_id, $topic_title, $topic_creator, $topic_last_user, $topic_date, $topic_reply_date, $topic_views); 

    //var_dump($stmt);

    if (!$stmt) {
        throw new Exception($con->error);
    }
}
$stmt->store_result();
$numrows = $stmt->num_rows;
echo $numrows;


if($numrows == 1){
    echo "<table width='100%'>";
    if ( $_SESSION['user'] ) { 
        echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onclick=\"window.location = 
    'forum_post_reply.php?cid=".$cid."$tid=".$tid."'\"> <hr />";
    } else {
        echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td></tr>";
    }
    }

    while ($row = $stmt->fetch()) {

        //Prepared SELECT stmt to get forum posts
        if($stmt2 = $con->prepare("SELECT `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, `post_date` FROM forum_posts WHERE `category_id`=? AND `topic_id`=?")) {

        //var_dump($stmt2);

            $stmt2->bind_param("ii", $cid, $tid);
            $stmt2->execute();
            $stmt2->store_result();
            $stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
            if (!$stmt2) {
            throw new Exception($con->error);
            }
        }
    }
    if ($result2 = $con->query($stmt2)) {
        while ($row2 = $result2->fetch_assoc() ) {
            echo "<tr><td valign='top' style='border: 1px solid #000000;'>
            <div style='min-height: 125px;'>".$row['topic_title']."<br />
            by ".$row2['post_creator']." - " .$row2['post_date']. "<hr />" . $row2['post_content'] ."</div></td>
            <td width='200' valign='top' align='center' style='border: 1px solid #000000;'>User Info Here!</td></tr>
            <tr><td colspan='2'><hr /></td></tr>";
        }   
    }   else {
        echo "<p>This topic does not exist.</p>";
        }
}
catch (Exception $e)
{
    echo "Error: " . $e->getMessage();
}