我的頁面
範例頁面
我要如何得知類別的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='存'> ";
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($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:
基本上能實作出這樣就差不多了.
非常謝謝您!
我目前做到這樣,想請問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>
想請問rowspan那邊該怎麼寫好?
魚大提醒的可能要思考一下
寫一個SQL使用GROUP BY抓出每個類別的筆數
也就是第一個SQL是依照GROUP BY類別後,Select 類別 及 類別的筆數
rowspan就是 類別的筆數
然後再依照查詢到的類別當作條件一一抓取對應的所有資料
再來看起來您如果只想要得知所有資料的筆數方便分頁,可以試試mysqli_num_rows,應該就不必額外再查詢一次
最後個人建議,能不省略的盡量就別省略了
多打幾個也許會更方便後續的維護
我剛剛看了一下你原本就有這個
SELECT COUNT(*) FROM todo
那你不如改成
SELECT COUNT(1) FROM todo GROUP BY TODO_CLASS
分開來就是類別的數量,
另外一個變數全部加起來就是總數量.
謝謝你們的幫助