البرمجة

فلترة السجلات حسب التاريخ في PHP

To implement the functionality you described, you need to make a few adjustments to your code. Here’s an updated version:

HTML

html
<form action="" method="post"> <p>Date:p> <input id="dateorderasc" type="radio" name="dateorder" onclick="this.form.submit()" value="dateasc"php if (isset($_POST['dateorder']) && $_POST['dateorder'] == 'dateasc') echo ' checked="checked"';?> /> <label for="dateorderasc">Newest Oldestlabel> <br> <input id="dateorderdesc" type="radio" name="dateorder" onclick="this.form.submit()" value="datedesc" php if (isset($_POST['dateorder']) && $_POST['dateorder'] == 'datedesc') echo ' checked="checked"';?> /> <label for="dateorderdesc">Oldest Newestlabel><br> <input type="submit" name="clear" value="Clear Filters" /> form>

PHP

php
//connect include("db.connect.php"); //Retrieve the tasks from the database table $query = "SELECT * FROM tasks"; //If RadioButton Clicked Sort the Database by Date Asc / Desc if(isset($_POST['dateorder']) && !empty($_POST['dateorder'])){ if ($_POST['dateorder'] == 'dateasc') { $query .= " ORDER BY date ASC"; } if ($_POST['dateorder'] == 'datedesc') { $query .= " ORDER BY date DESC"; } } if (isset($_POST['clear'])) { // Clear Filters $query = "SELECT * FROM tasks"; } $result = mysqli_query($dbc, $query) or die('Error querying database.'); include("dbresults.php");

This code will allow users to filter the records by date ascending or descending by clicking on the radio buttons. Clicking the “Clear Filters” button will reset the filters and display the original results.

المزيد من المعلومات

لتحسين تجربة المستخدم، يمكنك إضافة بعض التحسينات إلى تصميم الواجهة الخاصة بك. على سبيل المثال، يمكنك إضافة زر لمسح الفلاتر بجانب أزرار الفلاتر لزيادة وضوحية العملية. كما يمكنك أيضًا تحسين رسائل الخطأ لتوضيح المشكلة في حالة حدوث خطأ في الاستعلام.

تحقق من أن متغيرات POST المستخدمة في الاستعلام محمية من هجمات إدخالات غير صالحة. يمكنك القيام بذلك باستخدام mysqli_real_escape_string أو استخدام prepared statements لتجنب استخدام بيانات المستخدم المدخلة مباشرة في الاستعلام.

علاوة على ذلك، يمكنك أيضًا تقديم ترتيبات أخرى للبيانات، مثل ترتيب حسب الاسم أو الحالة، وتحسين تصميم الجدول لتوفير تجربة مستخدم أفضل.

زر الذهاب إلى الأعلى