无法创建所需的json数据并正确读取它们的值(php,ajax)

时间:2017-01-21 14:53:45

标签: javascript php json ajax

我正在制作服装店,我希望在点击产品链接时以模态显示数据。每种产品都可以有更多尺寸,每种尺寸都有其数量。

此行打开模态(此工作正常)

<a href="#" class="quick-view-btn" id="<?php echo $a_id; ?>"><span>View</span> </a>

以下代码发送&#34; id&#34;到fetch_id.php文件并以模态

显示数据

&#13;
&#13;
<script type="text/javascript">

 $(document).ready(function() {
    $('.quick-view-btn').bind('click', function() {

        var id=$(this).attr("id");

        $.ajax({
              url:"fetch_id.php",
              cache:false,
              data:'id='+id,
dataType: "json",


           	              success:function(data){
        var aslika = data['artikli']['artslika'];              //get picture
        var aime = data['artikli']['artime'];          //get name
        var acijena = data['artikli']['artcijena'];           //get price
        var artcode = data['artikli']['artcode'];           //get art code
        var aopis = data['artikli']['artopis']; 


        
        $('.a_slika').attr("src", 'slike/'+aslika);
        $('.a_ime').html(aime);
        $('.a_cijena').html(acijena+' Kn');
        $('.a_artcode').html('šifra: '+artcode);
        $('.a_opis').html(aopis);
        $('.a_id').attr("href", 'single-product.php?id='+artcode);
        

               }
        }); 
    });
 });
  </script>
&#13;
&#13;
&#13;

这是我的fetch_id.php文件

&#13;
&#13;
      $prid = $_GET['id'];
$active = 1;


	if ($stmtart = $mysqli->prepare("SELECT aid, art_code, glkat, kat, ime_a, opis, kolicina, cijena, stara_cijena, ocjena, slika, status, datum, xs, s, m, l, xl, xxl, xxxl FROM artikli WHERE status = ? and aid = ? LIMIT 1")) {
		  
        $stmtart->bind_param('ii', $active, $prid );  
        $stmtart->execute();    
        $stmtart->store_result();
        $stmtart->bind_result($a_id, $aart_code, $a_glkat, $a_kat, $a_ime, $a_opis, $a_kolicina, $a_cijena, $a_scijena, $a_ocjena, $a_slika, $a_status, $a_datum, $a_xs, $a_s, $a_m, $a_l, $a_xl, $a_xxl, $a_xxxl);
        $stmtart->fetch();
	 }	
	 
	 
	 	if ($stmtvelicine = $mysqli->prepare("SELECT vid, oznaka, kolicina FROM velicine WHERE artid = ? ORDER BY vid ASC")) {
		  
        $stmtvelicine->bind_param('i', $prid );  
        $stmtvelicine->execute();    
        $stmtvelicine->store_result();
        $stmtvelicine->bind_result($v_id, $v_oznaka, $v_kolicina);

	 }
  
  	$artikli = array("artikli" => array("artslika"=>$a_slika, "artime"=>$a_ime, "artcijena"=>$a_cijena, "artcode"=>$aart_code, "artopis"=>$a_opis));
	 
	$counter = 0;
  
   while($stmtvelicine->fetch()) {
	 	$counter = $counter + 1;
	 
	 $velicine = array('vel'.$counter => $v_oznaka);
	 $komada = array('kom'.$counter => $v_kolicina);


	 array_push($artikli, $velicine);
	 array_push($artikli, $komada);
	 }
  
  $json = json_encode($artikli);
   echo $json;
&#13;
&#13;
&#13;

我收到的结果是这种格式...

   {"artikli":{"artslika":"sn.jpg","artime":"Dress","artcijena":"129.56","artcode":"PD1001","artopis":"Default Description"},"0":{"vel1":"XS"},"1":{"kom1":5},"2":{"vel2":"L"},"3":{"kom2":2},"4":{"vel3":"XL"},"5":{"kom3":4}}

问题在于我的&#34; while循环&#34;将数字创建为键,因此如果某些产品具有许多大小,则会有许多未知键。 你能帮我用ajax读他们的价值观吗?有没有更好的方法来解决这个问题? 非常感谢你, 伊凡。

0 个答案:

没有答案