iT邦幫忙

0

PHP 如何將兩個SQL語法產生的值同時列在網頁上

各位大大好:

想請問如何ajax將兩個SQL語法產生的值同時列在網頁上,謝謝!
如果writeback.php的兩個值傳到ajax使用,兩個值同時回傳console.log會有錯誤..
$numm這樣寫應該是可以把它轉成物件!

$.ajax({
    
    type: "POST",
    url: "writeback.php",
    async:false,
    data: {
        "namen":namen,
        "lab":lab,
        "phone":phone,
        "bdate":bdate,
        "clr":clr,
        "thing":thing,
        "timest":timest,
        "timeov":timeov,
        "mdata":mdata
            },
    error: function(jqXHR, textStatus, errorThrown) {
    alert(jqXHR.responseText);
    },
    success: function(data) {
      $("#cca").fadeOut(4000);
        var obj=JSON.parse(data);
        console.log(obj);
        document.getElementById(bdate).innerHTML +='<a id="'+obj[0].num+'" onclick="showblock(this)">•'+obj[0].timest+' '+obj[0].class+'</a><br>';     
    },
    beforeSend:function(){
          $("#cca").show();
         $('#exampleModal').modal('hide');
    }

writeback.php,請問怎麼將$show與$numm同時回傳到ajax當值

$sql3="select * from ".$mdata." where wdate= :wdate";
$sql4 =$conn->prepare($sql3);
$ar_val2=array('wdate'=>$wdate);
if($sql4->execute($ar_val2)){
$show=array();
while($row=$sql4->fetch())

{
$show[] = array('class'=>$row['clr'],'bdate'=>$row['bdate'],'timest'=>$row['timest'],'timeov'=>$row['timeov'],'name'=>$row['namen'],'thing'=>$row['thing'],'phone'=>$row['phone'],'lab'=>$row['lab'],'num'=>$row['識別碼']);	
}
echo json_encode($show,JSON_UNESCAPED_UNICODE);
}

$sql5="select * from ".$mdata." where bdate= :bdate";
$sql6 =$conn->prepare($sql5);
$ar_val3=array('bdate'=>$bdate);
if($sql6->execute($ar_val3))
{

$show1=array();
while($row=$sql6->fetchAll())
{
$numm=count($row);
}
echo json_encode($numm,JSON_UNESCAPED_UNICODE);
}
$conn=null; //結束對象資源
froce iT邦大師 1 級 ‧ 2018-03-07 11:50:25 檢舉
老話一句,JSON。
weiclin iT邦高手 4 級 ‧ 2018-03-07 11:56:30 檢舉
不就是這樣:
echo json_encode([$show, $numm],JSON_UNESCAPED_UNICODE);
mayyola iT邦研究生 2 級 ‧ 2018-03-07 12:40:20 檢舉
PO完才發現解決了>"< 我是打這樣..謝謝兩位
echo json_encode(array('show'=>$show,'length'=>$numm),JSON_UNESCAPED_UNICODE);
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
philoreiser
iT邦新手 5 級 ‧ 2018-03-09 02:05:10

我看了也是心想: 你都用JSON了... 不就一起傳嗎...? XD

我倒是好奇一件事,你都已經用 jQuery了,為什麼還要穿插vanilla JavaScript混用呢?純粹為了加入"onclick" event-listener 方便嗎?

mayyola iT邦研究生 2 級 ‧ 2018-03-09 10:30:52 檢舉

是的,為了onclick 方便,我比較惜慣用js(感覺比較直觀),還是有更好的方法嗎,謝謝

document.getElementById(bdate).innerHTML +='<a id="'+obj[0].num+'" onclick="showblock(this)">'+obj[0].timest+' '+obj[0].class+'</a><br>';     

這裡用 jQuery 的寫法可以寫成類似這樣:

$("<a></a>").text( obj[0].timest + obj[0].class ).attr({
    "id": obj[0].num,
    "onclick": "showblock(this)",
    "href": "javascript:void(0)"
}).appendTo($("#bdate"));

$("<br>").appendTo($("#bdate"));

我不曉得你的網頁功能實際運作是怎麼樣的情形,
如果你有要對 "a" tag 做事件處理,
建議加上

href="javascript:void(0)" 

否則IE瀏覽器可能就會忽略你的"onclick"而執行"href"裡的行為。 (Chrome不會)

你再試試看吧。有錯也歡迎指正喔。

我要發表回答

立即登入回答