<?php include("db_connect.php"); header('Content-type: text/html; charset=utf-8'); if (isset($_POST["add"])) { $sql = mysql_query("INSERT INTO `message` (`name`, `surname`, `patronymic`, `date`, `text`) VALUES ('".$_POST['name']."','".$_POST['surname']."','".$_POST['patronymic']."','".$_POST['date']."','".$_POST['text']."')"); } ?> <!DOCTYPE html> <html lang="ru"> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap Template</title> <link href="css/bootstrap.css" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="css/style.css"> <link href="https://fonts.googleapis.com/css?family=Martel|Rubik" rel="stylesheet"> </head> <body> <header> <div class="container"> <div class="row"> <form action="index.php" method="POST" class="col-lg-6" role="form"> <div class="form-group"> <label for="surname">Фамилия</label> <input type="name" class="form-control qwerty" name="surname" id="surname"> </div> <div class="form-group"> <label for="name">Имя</label> <input type="name" class="form-control qwerty" name="name" id="name"> </div> <div class="form-group"> <label for="patronymic">Отчетсво</label> <input type="name" class="form-control qwerty" name="patronymic" id="patronymic"> </div> <div class="form-group"> <label for="date">Дата рождения</label> <input type="date" class="form-control qwerty" name="date" id="date" placeholder="Дата"> </div> <div class="form-group"> <label for="text">Биография</label> <textarea name="text" class="form-control" rows="7" id="text"></textarea> </div> <button type="submit" name="add" class="btn btn-primary">Добавить</button> </form> </div> <hr /> <table class="table"> <thead> <tr> <th class="col-md-1">ID</th> <th class="col-md-3" >Дата рождения</th> <th class="col-md-2">Фамилия</th> <th class="col-md-2">Имя</th> <th class="col-md-2">Отчество</th> <th class="col-md-2">Биография</th> </tr> </thead> <tbody> <?php $result=mysql_query("SELECT id,date,surname,name,patronymic,text FROM message"); while ($row=mysql_fetch_array($result)) { // выводим данные echo "<tr>\n\t<td>".$row["id"]."</td>"."\n\t"."<td >"."".$row["date"]." </td>"."\n\t"."<td>"."".$row["surname"]."</td>"."\n\t"."<td>"."".$row ["name"]."</td>"."\n\t"."<td>"."".$row["patronymic"]."</td>"."\n\t"."<td> "."".$row["text"]."</td>"."\n\t"."</tr>"."\n"; } mysql_close(); ?> </tbody> </table> </div> </header> <script type="text/javascript" charset="utf8" src="/DataTables/datatables.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="js/bootstrap.js"></script> </body> </html> 

    1 answer 1

    I see this option:

    First, when you form a table, as a title to register some links / buttons that will update the page with the transfer of data through GET.

    Further, in the head of PHP-shki you get these same GETs. And if any one has a certain parameter, you add an IF block, in which MySQL instructions for sorting go (or several).

    Accordingly, if a certain GET contains, say, a value for sorting in ascending order, then in the header of the table a link is already formed for descending.

    It makes sense to add a reset sorting somewhere, or make the third sorting type (ie, ascending / descending / none) no sorting by field.

    Or another option: make in the header 2 links, one to sort in ascending order, the second in descending order.

    If the filter is applied, the link to the corresponding sorting is changed to cancel the given sorting.


    And now an example:

     $f = $_GET['f']; //GET данные. Первое - числовое от 0 до 2 - сортировка $search = $_GET['search']; //Этот для поиска по слову $query = 'select `fdid`, `fdsize`, `fdcolor`, `fddata` from `f3ds`'; // начало запроса, т.е. "база" if ($search <> '') //Если некая GET переменная - в данном случае поиск слова в описании - имеется, то добавляем поиск слова { $query = $query . ' where fddata like "%' . $search . '%"'; //fddata - то самое текстовое поле, в котором осуществляется поиск по слову } switch ($f) //А вот и некая вторая переменная, она для сортировки { case 1: $query = $query . ' order by fdsize desc'; //По убыванию break; case 2: $query = $query . ' order by fdsize asc'; //И по возрастанию break; } 

    Next, an example of constructing a table:

      echo '<table width = 100% border = 3>'; //Начинаем табличку echo '<tr><th width = 50>Image</th><th width = 50>ID</th><th width = 70>Size<a href = "?PID=0&f='; //Доходим до поля сортировки if($f == 0) //Если переменная == 0, т.е. сортировка отсутствует { echo '1&search=' . $search . '"><img src = "img/n.bmp">'; //Пихаем картинку "Нет сортировки". В данном случае выводится не опция, а текущий статус сортировки } else { //Иначе, т.е. сортировка есть if($f == 1) //По возрастанию { echo '2&search=' . $search . '"><img src = "img/u.bmp">'; //Пихаем картинку со стрелкой вверх } else { //Иначе (остаётся только 1 вариант) echo '0&search=' . $search . '"><img src = "img/d.bmp">'; //Картинка со стрелкой вниз } } echo '</a></th><th>Data</th><th width = 20>#</th><th width = 20>#</th></tr>'; //И добиваем первую заголовочную строчку таблички. 

    An example from my old project on the registration of floppy disks / disks / hard drives, but for review will come down.

    Next, you unwind the answer from MySQL as usual - you continue the label, insert the data ...

    • To sort by several columns (separately), you need to create several sort status variables, for example, F0, F1, F2 ... And do, respectively, several conditions for each sorting value separately. In my example, by the way, pay attention to the beginning of the lines in the conditions when constructing the title of the table - they start with the numbers 0, 1 and 2 - these are the values ​​of the variable that I assign earlier. - Vladimir Volk
    • Thank you, it’s difficult of course, I’m new and easy to figure out)) - Artem Fomkin