iT邦幫忙

0

PHP 如何將相同類別合併

2021-06-24 01:26:081214 瀏覽
  • 分享至 

  • xImage

我的頁面
https://ithelp.ithome.com.tw/upload/images/20210624/20138814PZ6ooYmfof.png
範例頁面
https://ithelp.ithome.com.tw/upload/images/20210624/20138814HUAs0kfyIK.png
我要如何得知類別的value又同時知道他在的列數?
附上我的程式碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<?php
        $link = mysqli_connect("localhost","root","","todolist");
        if(!$link)
            die("資料庫連接失敗");
        $sql = "set names utf8";
        mysqli_query($link,$sql);
        if(isset($_POST["post"]))
        {
            $sql="INSERT INTO `todo` 
            (`TODO_CLASS`, `TODO_LIST`) 
            VALUES ('".$_POST["todo_class"]."', '".$_POST["post_content"]."');";
            mysqli_query($link,$sql);
        }
        if(isset($_POST["delete"]))
        {
            $post_no = key($_POST["delete"]);
            $sql = "DELETE FROM todo
                          WHERE TODO_NO = ".$post_no;
            mysqli_query($link,$sql);
        }
        if(isset($_POST["save"]))
        {
            $post_no = key($_POST["save"]);
            $sql = "UPDATE `todo` 
                       SET `TODO_CLASS` = '".$_POST["edit_post_name"]."', 
                           `TODO_LIST` = '".$_POST["edit_post_content"]."' 
                     WHERE `todo`.`TODO_NO` = ".$post_no;
            mysqli_query($link,$sql);
        }
        if(isset($_POST["delete_all"]))
        {
            $post_no = $_POST["checkbox"];
            foreach($post_no as $value)
            {
                $sql="DELETE FROM `todo`
                            WHERE `TODO_NO` = '$value'";
                mysqli_query($link,$sql);
            }
        }
        if(isset($_POST["save_edit_all"]))
        {
            for($i=0;$i<count($_POST["post_no"]);$i++)
            {
                $sql = "UPDATE `todo` 
                           SET `TODO_CLASS` = '".$_POST["edit_post_name"][$i]."', 
                               `TODO_LIST` = '".$_POST["edit_post_content"][$i]."' 
                         WHERE `TODO_NO` = ".$_POST["post_no"][$i];
                mysqli_query($link,$sql);
            }
        }
        $edit="";
        if(isset($_POST["edit"]))
            $edit = key($_POST['edit']);
        $merge="";
        if(isset($_GET["todo_class"]))
            $merge = key($_GET['todo_class']);
    ?>
    <form method="post">
        <table border="1" align="center" width="800">
            <tr>
                <td colspan="5" align="center">代辦事項</td>
            </tr>
            <tr>
                <td colspan="5">
                    類別 <select name="todo_class">
                        <option value="普通">普通</option>
                        <option value="緊急">緊急</option>
                        <option value="重要">重要</option>
                    </select>
                    事項 <input type="text" name="post_content" size="30">
                    <input type="submit" name="post" value="增">
                    <input type="submit" name="delete_all" value="勾選刪除">
                </td>
            </tr>
            <tr>
                <td align="center" width="20">選</td>
                <td align="center" width="100">功能</td>
                <td align="center" width="100">類別</td>
                <td align="center">事項</td>
                <td align="center" width="150">時間</td>
            </tr>
            <?php
                $sql = "SELECT COUNT(*)
                          FROM todo";
                $result = mysqli_query($link,$sql);
                while($row = mysqli_fetch_row($result))
                    $sum = $row[0];
                $result = mysqli_query($link,$sql);
                $per_num = 10;
                $sum_page = ceil($sum / $per_num);
                $sql = "SELECT TODO_NO,
                               TODO_CLASS,
                               TODO_LIST,
                               TODO_TIME
                          FROM todo";
                $result = mysqli_query($link,$sql);
                while($row = mysqli_fetch_assoc($result))
                {
                    echo "<tr>";
                    echo "<td><input type='checkbox' name='checkbox[]' value='".$row["TODO_NO"]."'></td>";
                    echo "<td align='center'>";
                    if($edit==$row["TODO_NO"])
                    {
                        echo "<input type='submit' name='save[".$row["TODO_NO"]."]' value='存'>&nbsp;";
                        echo "<input type='submit' value='消'>";
                    }
                    else
                    {
                        echo "<input type='hidden' name='post_no[]' value='".$row["TODO_NO"]."'>";
                        echo "<input type='submit' name='edit[".$row["TODO_NO"]."]' value='編'>&nbsp;";
                        echo "<input type='submit' name='delete[".$row["TODO_NO"]."]' value='刪'>";
                    }
                    echo "</td>";
                    echo "<td align='center'>";
                    if($row["TODO_NO"])
                    if($edit==$row["TODO_NO"])
                        echo "<input type='text' style='width:90%' name='edit_post_name' value='".$row["TODO_CLASS"]."'>";
                    else
                        echo $row["TODO_CLASS"];
                    echo "</td>";
                    echo "<td>";
                    if($edit==$row["TODO_NO"])
                        echo "<input type='text' style='width:90%' name='edit_post_content' value='".$row["TODO_LIST"]."'>";
                    else
                        echo $row["TODO_LIST"];
                    echo "</td>";
                    echo "<td align='cneter'>".$row["TODO_TIME"]."</td>";
                    echo "</tr>";
                }
            ?>
        </table>
    </form>
    
</body>
</html>

myAdmin:
https://ithelp.ithome.com.tw/upload/images/20210624/20138814y2SnvB3tVX.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
小魚
iT邦大師 1 級 ‧ 2021-06-24 01:34:04
最佳解答
  1. 一般像類別這個比較不會存中文, 通常會另外建一個資料表, 然後這個表就存它的id (如果使用者不能新增編輯,id寫死也沒關係)
  2. 你可以先寫一個SQL使用GROUP BY抓出每個類別的筆數, 然後抓資料的時候要ORDEY BY把相同類別抓在一起. 前端輸出的時候每個類別的第一列的類別那欄rowspan=那個類別的筆數, 第二列開始不用輸出類別(因為rowspan, 所以會跳過那一欄).

基本上能實作出這樣就差不多了.

看更多先前的回應...收起先前的回應...
iT邦新手 5 級 ‧ 2021-06-24 02:26:34 檢舉

非常謝謝您!
我目前做到這樣,想請問rowspan那邊該怎麼寫好?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<?php
        $link = mysqli_connect("localhost","root","","todolist");
        if(!$link)
            die("資料庫連接失敗");
        $sql = "set names utf8";
        mysqli_query($link,$sql);
        if(isset($_POST["post"]))
        {
            $sql="INSERT INTO `todo`
            (`TODO_CLASS`, `TODO_LIST`) 
            VALUES ('".$_POST["todo_class"]."', '".$_POST["post_content"]."');";
            mysqli_query($link,$sql);
        }
        if(isset($_POST["delete"]))
        {
            $post_no = key($_POST["delete"]);
            $sql = "DELETE FROM todo
                          WHERE TODO_NO = ".$post_no;
            mysqli_query($link,$sql);
        }
        if(isset($_POST["save"]))
        {
            $post_no = key($_POST["save"]);
            $sql = "UPDATE `todo` 
                       SET `TODO_CLASS` = '".$_POST["edit_post_name"]."', 
                           `TODO_LIST` = '".$_POST["edit_post_content"]."' 
                     WHERE `todo`.`TODO_NO` = ".$post_no;
            mysqli_query($link,$sql);
        }
        if(isset($_POST["delete_all"]))
        {
            $post_no = $_POST["checkbox"];
            foreach($post_no as $value)
            {
                $sql="DELETE FROM `todo`
                            WHERE `TODO_NO` = '$value'";
                mysqli_query($link,$sql);
            }
        }
        if(isset($_POST["save_edit_all"]))
        {
            for($i=0;$i<count($_POST["post_no"]);$i++)
            {
                $sql = "UPDATE `todo` 
                           SET `TODO_CLASS` = '".$_POST["edit_post_name"][$i]."', 
                               `TODO_LIST` = '".$_POST["edit_post_content"][$i]."' 
                         WHERE `TODO_NO` = ".$_POST["post_no"][$i];
                mysqli_query($link,$sql);
            }
        }
        $edit="";
        if(isset($_POST["edit"]))
            $edit = key($_POST['edit']);
    ?>
    <form method="post">
        <table border="1" align="center" width="800">
            <tr>
                <td colspan="5" align="center">待辦事項</td>
            </tr>
            <tr>
                <td colspan="5">
                    類別 <select name="todo_class">
                        <option value="普通">普通</option>
                        <option value="緊急">緊急</option>
                        <option value="重要">重要</option>
                    </select>
                    事項 <input type="text" name="post_content" size="30">
                    <input type="submit" name="post" value="增">
                    <input type="submit" name="delete_all" value="勾選刪除">
                </td>
            </tr>
            <tr>
                <td align="center" width="20">選</td>
                <td align="center" width="100">功能</td>
                <td align="center" width="100">類別</td>
                <td align="center">事項</td>
                <td align="center" width="150">時間</td>
            </tr>
            <?php
                $sql = "SELECT COUNT(*)
                          FROM todo";
                $result = mysqli_query($link,$sql);
                while($row = mysqli_fetch_row($result))
                    $sum = $row[0];
                $result = mysqli_query($link,$sql);
                $per_num = 10;
                $sum_page = ceil($sum / $per_num);
                $sql = "SELECT TODO_NO,
                               TODO_CLASS,
                               TODO_LIST,
                               TODO_TIME
                          FROM todo";
                $result = mysqli_query($link,$sql);
                $sql = "SELECT *
                          FROM todo
                      ORDER BY TODO_CLASS";
                $result = mysqli_query($link,$sql);
                while($row = mysqli_fetch_assoc($result))
                {
                    echo "<tr>";
                    echo "<td><input type='checkbox' name='checkbox[]' value='".$row["TODO_NO"]."'></td>";
                    echo "<td align='center'>";
                    if($edit==$row["TODO_NO"])
                    {
                        echo "<input type='submit' name='save[".$row["TODO_NO"]."]' value='存'> ";
                        echo "<input type='submit' value='消'>";
                    }
                    else
                    {
                        echo "<input type='hidden' name='post_no[]' value='".$row["TODO_NO"]."'>";
                        echo "<input type='submit' name='edit[".$row["TODO_NO"]."]' value='編'> ";
                        echo "<input type='submit' name='delete[".$row["TODO_NO"]."]' value='刪'>";
                    }
                    echo "</td>";
                    echo "<td align='center'>";
                    if($edit==$row["TODO_NO"])
                        echo "<input type='text' style='width:90%' name='edit_post_name' value='".$row["TODO_CLASS"]."'>";
                    else
                        echo $row["TODO_CLASS"];
                    echo "</td>";
                    echo "<td>";
                    if($edit==$row["TODO_NO"])
                        echo "<input type='text' style='width:90%' name='edit_post_content' value='".$row["TODO_LIST"]."'>";
                    else
                        echo $row["TODO_LIST"];
                    echo "</td>";
                    echo "<td align='cneter'>".$row["TODO_TIME"]."</td>";
                    echo "</tr>";
                }
            ?>
        </table>
    </form>
    
</body>
</html>
archer9080 iT邦研究生 3 級 ‧ 2021-06-24 09:05:03 檢舉

想請問rowspan那邊該怎麼寫好?

魚大提醒的可能要思考一下

寫一個SQL使用GROUP BY抓出每個類別的筆數

也就是第一個SQL是依照GROUP BY類別後,Select 類別 及 類別的筆數
rowspan就是 類別的筆數
然後再依照查詢到的類別當作條件一一抓取對應的所有資料

再來看起來您如果只想要得知所有資料的筆數方便分頁,可以試試mysqli_num_rows,應該就不必額外再查詢一次

最後個人建議,能不省略的盡量就別省略了
多打幾個也許會更方便後續的維護

小魚 iT邦大師 1 級 ‧ 2021-06-24 09:23:30 檢舉

我剛剛看了一下你原本就有這個

SELECT COUNT(*) FROM todo

那你不如改成

SELECT COUNT(1) FROM todo GROUP BY TODO_CLASS

分開來就是類別的數量,
另外一個變數全部加起來就是總數量.

iT邦新手 5 級 ‧ 2021-06-26 01:54:31 檢舉

謝謝你們的幫助/images/emoticon/emoticon02.gif

我要發表回答

立即登入回答