รบกวนช่วยเรื่อง json หน่อยครับ

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา รบกวนช่วยเรื่อง json หน่อยครับ

รบกวนช่วยเรื่อง json หน่อยครับ
คือเมื่อคลิกรับค่า id มา จากตาราง project_tbl แล้วแสดงชื่อความต้องการจาก requirement_tbl ทำการนับจำนวน testcase จาก testcase_tbl
ตารางผมมี3ตารางดังนี้ 

ตาราง project_tbl
p_id p_name
1 aaaaa

ตาราง requirement_tbl
req_id p_id req_name
1 1 bbbbb
2 1 ccccc

 ตาราง testcase_tbl
tc_id p_id req_id tc_name
1 1 1 ddddd
2 1 1 eeeee


อยากให้เป็นประมาณนี้ครับ ส่งออกข้อมูลตารางแรก
ชื่อโปรเจค จำนวนความต้องการ จำนวนtestcase
aaaaa 2 2

ผมใช้ json 
<?php
    header("Content-type: text/html; charset=UTF-8");
    header('Cache-Control: no-cache');
    header('Pragma: no-cache');
    header('Expires: 0');
    include("connect_project.php");
    $where = "";
    $sql = "SELECT project_tbl.p_id AS p_id,p_name,COUNT(tc_id) AS tc_id FROM testcase_tbl 
    INNER JOIN project_tbl ON project_tbl.p_id = testcase_tbl.p_id GROUP BY project_tbl.p_id $where";
    $result = runSQL($sql);
    $numrow = countRec('p_id','project_tbl',$where);
    if($numrow>0){
        $json = "";
        $json .= "{";
        $json .= ""aaData":[";
        $rc = false;
        while ($row = mysql_fetch_array($result)) {
            if ($rc) $json .= ",";
            $json .= "[";
            //$json .= """.$row['p_name'].""";
            $json .= """."<a href=javascript:openWindow('?id=".$row['p_id']."')>".$row['p_name']."</a>".""";
            $json .= ","".number_format($row['tc_id']).""]";
            $rc = true;
        }
            $json .= "]";
            $json .= "}";
            echo $json;
    }
?>
แล้วเมื่อคลิกที่ชื่อ aaaaa อยากให้เป็นประมาณนี้ ตารางที่สอง
ชื่อความต้องการ จำนวนtestcase
bbbbb 2
ccccc 0

ผมใส่code ไปประมานนี้
<?php
    header("Content-type: text/html; charset=UTF-8");
    header('Cache-Control: no-cache');
    header('Pragma: no-cache');
    header('Expires: 0');
    include("connect_project.php");
        //รับ pk ที่มาจากหน้า display
    $id = $_GET['id'];
    $where = "INNER JOIN requirement_tbl ON requirement_tbl.req_id = testcase_tbl.req_id AND requirement_tbl.p_id = testcase_tbl.p_id WHERE testcase_tbl.p_id = $id";
    $sql = "SELECT requirement_tbl.req_id AS req_id,req_name,COUNT(tc_id) AS tc_id FROM testcase_tbl $where";
    $result = runSQL($sql);
    $numrow = countRec('requirement_tbl.req_id','testcase_tbl',$where);
    if($numrow>0){
        $json = "";
        $json .= "{";
        $json .= ""aaData":[";
        $rc = false;
        while ($row = mysql_fetch_array($result)) {
            if ($rc) $json .= ",";
            $json .= "[";
            $json .= """.$row['req_name']."</a>".""";
            $json .= ","".number_format($row['tc_id']).""]";
            $rc = true;
        }
            $json .= "]";
            $json .= "}";
            echo $json;
    }
?>
ปัญหาคือ ตารางแรกแสดงข้อมูล แต่ไม่มีการนับจำนวนความต้องการ
ตารางที่สองไม่แสดงข้อมูล

รบกวนพี่ด้วยนะครับผมไม่ค่อยเข้าใจเรื่องนี้อ่าครับว่าจะทำยังไงให้ออกมาตามที่ผมต้องการ


Eodza13579 171.6.143.xxx 25-06-2016 14:16:49

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1

SQL ตารางแรกครับ

SELECT project_tbl.p_id,project_tbl.p_name,COUNT(requirement_tbl.req_id) AS num_requirement, COUNT(testcase_tbl.tc_id) AS num_testcase FROM project_tbl INNER JOIN requirement_tbl on(requirement_tbl.p_id = project_tbl.p_id) INNER JOIN testcase_tbl on(testcase_tbl.req_id = requirement_tbl.req_id) 

SQL ตารางที่สองครับ

SELECT requirement_tbl.req_id,requirement_tbl.req_name, COUNT(testcase_tbl.tc_id) AS num_testcase FROM requirement_tbl INNER JOIN testcase_tbl on(testcase_tbl.req_id = requirement_tbl.req_id) where requirement_tbl.req_id = '$id' 

ผมไม่รู้ว่าได้ตามที่ต้องการหรือป่าว ลองดูครับ


 



Suthee Khongnapha 183.89.132.xxx 25-06-2016 15:35
 ความคิดเห็นที่ 2
แก้ครับ SQL ตารางที่สอง
SELECT requirement_tbl.req_id,requirement_tbl.req_name, COUNT(testcase_tbl.tc_id) AS num_testcase FROM requirement_tbl INNER JOIN testcase_tbl on(testcase_tbl.req_id = requirement_tbl.req_id) where requirement_tbl.p_id = '$id' 



Suthee Khongnapha 183.89.132.xxx 25-06-2016 15:40
 ความคิดเห็นที่ 3
รบกวนพี่ทำเป็นรูปแบบที่ผมขึ้นได้ไหมครับ ผมเอาไปใช้กับ datatable แบบ ajax 

ปล.ผมลองเอาไปใส่ดูแล้วขึ้นแบบนี้ Error: could not query dataSELECT count(requirement_tbl.req_id) FROM testcase_tbl อ่าครับ


eodza13579 171.6.143.xxx 25-06-2016 15:46
 ความคิดเห็นที่ 4
เวลารันไฟล์อยากให้เป็นรูปแบบ แบบนี้อ่าครับ 

{"aaData":[["aaaaa","2","2"]]}


eodza13579 171.6.143.xxx 25-06-2016 15:49
 ความคิดเห็นที่ 5
<?php
    header("Content-type: text/html; charset=UTF-8");
    header('Cache-Control: no-cache');
    header('Pragma: no-cache');
    header('Expires: 0');
    include("connect_project.php");
    $where = "";
    $sql = "SELECT project_tbl.p_id,project_tbl.p_name,COUNT(requirement_tbl.req_id) AS num_requirement, COUNT(testcase_tbl.tc_id) AS num_testcase FROM project_tbl INNER JOIN requirement_tbl on(requirement_tbl.p_id = project_tbl.p_id) INNER JOIN testcase_tbl on(testcase_tbl.req_id = requirement_tbl.req_id) ";
    $result = runSQL($sql);
    $numrow = countRec('p_id','project_tbl',$where);
    $res = array();
    if($numrow>0){
        while ($row = mysql_fetch_array($result)) {
            $res[] = $row; 
        }
        echo json_encode(array('aaData' => $res));
    }
?>
ไม่แน่ใจว่าใช้ได้ไหม ไม่ได้ลองครับ


Suthee Khongnapha 183.89.132.xxx 25-06-2016 16:06
 ความคิดเห็นที่ 6
{"aaData":[{"0":"1","p_id":"1","1":"u0e01u0e32u0e23u0e1eu0e31u0e12u0e19u0e32u0e23u0e30u0e1au0e1au0e08u0e31u0e14u0e01u0e32u0e23u0e2bu0e49u0e2du0e07u0e1eu0e31u0e01","p_name":"u0e01u0e32u0e23u0e1eu0e31u0e12u0e19u0e32u0e23u0e30u0e1au0e1au0e08u0e31u0e14u0e01u0e32u0e23u0e2bu0e49u0e2du0e07u0e1eu0e31u0e01","2":"2","num_requirement":"2","3":"2","num_testcase":"2"}]}

มันออกได้งงมากเลยครับ 


eodza13579 171.6.143.xxx 25-06-2016 16:18
 ความคิดเห็นที่ 7
ผมใช้อันนี้เป็นตัวอย่างอ่ะครับ http://www.tutorialdev.com/php/datatableประยุกต์สร้างลิงค์ภาย/


eodza13579 171.6.143.xxx 25-06-2016 16:19
 ความคิดเห็นที่ 8
<?php
    header("Content-type: text/html; charset=UTF-8");
    header('Cache-Control: no-cache');
    header('Pragma: no-cache');
    header('Expires: 0');
    include("connect_project.php");
    $where = "";
    $sql = "SELECT project_tbl.p_id,project_tbl.p_name,COUNT(requirement_tbl.req_id) AS num_requirement, COUNT(testcase_tbl.tc_id) AS num_testcase FROM project_tbl INNER JOIN requirement_tbl on(requirement_tbl.p_id = project_tbl.p_id) INNER JOIN testcase_tbl on(testcase_tbl.req_id = requirement_tbl.req_id) ";
    $result = runSQL($sql);
    $numrow = countRec('p_id','project_tbl',$where);
    if($numrow>0){
        $json = "";
        $json .= "{";
        $json .= ""aaData":[";
        $rc = false;
        while ($row = mysql_fetch_array($result)) {
            if ($rc) $json .= ",";
            $json .= "[";
            $json .= """.$row['p_name'].""";
            $json .= ","".$row['num_requirement'].""";
            $json .= ","".$row['requirement_tbl'].""]";
            $rc = true;
        }
            $json .= "]";
            $json .= "}";
            echo $json;
    }
?>

แบบตัวอย่างที่ให้มาครับ



Suthee Khongnapha 183.89.132.xxx 25-06-2016 16:46
 ความคิดเห็นที่ 9
ขอบคุณมากครับ แล้วอีกตารางที่จะต้องส่งค่า primary ไป นี่อะครับ
แล้วเมื่อคลิกที่ชื่อ aaaaa อยากให้เป็นประมาณนี้ ตารางที่สอง
ชื่อความต้องการ จำนวนtestcase
bbbbb 2
ccccc 0
 

{"aaData":[["bbbbb","2"],["ccccc","0"]]}


eodza13579 171.6.143.xxx 25-06-2016 17:40
 ความคิดเห็นที่ 10
    $id = $_GET['id'];  
    $where = "WHERE requirement_tbl.p_id = $id" ;  
    $sql = "SELECT requirement_tbl.req_id,requirement_tbl.req_name, COUNT(testcase_tbl.tc_id) AS num_testcase FROM requirement_tbl INNER JOIN testcase_tbl on(testcase_tbl.req_id = requirement_tbl.req_id) $where";  
    $result = runSQL($sql);  


Suthee Khongnapha 183.89.132.xxx 25-06-2016 17:54
1 2 Next






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ