I want to add a search function to the list screen.

Asked 2 years ago, Updated 2 years ago, 110 views

We are creating a system that allows you to apply for overtime on the web using PHP.
I want to display everything you have applied for in the list so that you can search at the top of the screen.I want to narrow down the search using the drop-down menu, but it doesn't work.

What do you want to do

I'm trying to google, but I can't stand my teeth.
There are no errors at the moment, but when I press Search, nothing comes back.
Please let me know if anyone understands.

Here's the code:

// Connect to database
<?php include('dbc.php');?>

<?php
// View all applications
$list=$pdo->prepare("SELECT* FROM Table Name");
$list->execute();
$temp = $list->fetchall();

// I'm filling in the process of narrowing down the search from here...
$where_sql="";

if(isset($_GET['search'])){
    if($name!=""){
        $where_sql="WHERE table name.name='".$name."';
    }
    if($kenban!=""){
        $where_sql="WHERE table name.kenban='".$kenban."';
    } else {
        $where_sql="AND table name.kenban='".$kenban."';
    }
    if($jissday!=""){
        if($where_sql==""){
          $where_sql="WHERE table name.jissday='".$jissday.';
        } else {
          $where_sql.="AND table name.jissday='".$jissday."';
        } 
    }
}

?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <metacharset="UTF-8">
    <link rel="stylesheet" type="text/css" href=css/stylesheet2_test.css>
    <link href="https://use.fontawesome.com/releases/v5.6.1/css/all.css"rel="stylesheet">

    <title> Closed Application List</title>
</head>

<a style="margin-left:100px;text-decoration:none;font-size:1.2rem" href='/try_gyoumu/index.php'>Go to Business Support Menu </a>

<body>
    <h1class="title"> ~Applied overtime list~</h1>

    // Search field
        <div class="selectContainer">
            <div class="selectBox">
                <form action="jikangai_list_test.php" method="get">

                <select name="name">
                <option>Name</option>
                <?php foreach($namelist as$output){?>
                    <option><?phpecho$output['name'];?></option>
                <?php}?>
                </select>

                <select name="kenban">
                <option>item number</option>
                <?php foreach($gyoumulist as$moutput){?>
                    <option value='<?phpecho$moutput[kenban]?>'>?phpecho$moutput[kenban].'.$moutput['kenmei'];?></option>
                <?php}?>
                </select>
                <br>

                <input type="date"> to <input type="date">
                <input class="search-button" type="submit" name="search" value="search" value>
         // That's it

                <ul class="sort">
                <li><a class="ichiran" href="jikangai_list.php">View all </a></li>
                <li><a class="ichiran" href="misoumuin.php">List of non-commissioned seals</a></li>;
                <li><a class="ichiran" href="jissibi_mikinyu.php">List of Date Not Entered</a><li>;
                <li><a class="ichiran" href="list_newer.php">New order</a></li>
                <li><a class="ichiran" href="list_older.php">Older order</a></li>
                </ul>
                </form>
            </div>
        </div>


    <br>
    <table>
        <tr>
        <th>Name</th>
        <th> Estimated Date </th>
        <th> Estimated Start Time </th>
        <th>Expected end time</th>
        <th>Approval </th>
        <th> Date </th>
        <th>Start Time </th>
        <th>End of execution time</th>
        <th>After Hours</th>
        <th>Item number</th>
        <th>Contents </th>
        <th>General Affairs </th>
        <th>Edit </th>
        <th>Delete </th>
        </tr>

    <?php
    foreach($temp as$data){
    ?>
        <tr>
        <?php printf("<td>%s</td>",$data['name']);?>
        <?php printf("<td>%s</td>", $data['yoteiday']);?>
        <?php printf("<td>%s</td>", $data['yoteistarttime']);?>
        <?php printf("<td>%s</td>",$data['yoteiendtime']);?>
        <?php printf("<td>%s</td>",$data['syounin']);?>
        <?php printf("<td>%s</td>",$data['jissday']);?>
        <?php printf("<td>%s</td>", $data['jissistarttime']);?>
        <?php printf("<td>%s</td>",$data['jissiendtime']);?>
        <?php
        $from = $data ['jissiendtime'];
        $to = $data ['jissistarttime'];
        $result=date_diff(new DateTime($from), new DateTime($to)) ->format('%H:%I:%S');
        printf("<td>%s</td>", $result);
        ?>
        <?php printf("<td>%s</td>",$data['kenban']);?>
        <?php printf("<td>%s</td>",$data['naiyou']);?>
        <?php printf("<td>%s</td>",$data['soumuin']);?>
        <td><a class="fa fa-edit" href="update_form_test.php?id=<?phpecho$data['id']?>">a>></td>;
        <td><a class="fa fa-trash delete" href="delete.php?id=<?phpecho$data['id']?>">a></td>;
        </tr>
   <?php
   }
   ?>
    </table>

</body>
</html>

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script>
<script>
$('.delete').click(function(){
if(!confirm('Are you sure you want to delete it?')){
    return false;
}
});
</script>

php mysql pdo

2022-09-30 17:55

1 Answers

It's hard to present the source code, but it's not good for learning, so I dare
I will explain it without writing the source.

Press the search button to display the items that meet the selected criteria in the list

Looking at the source code, there are two problems.

(1) How to retrieve data from input
(2) Obtaining the variable and merging it into SQL

(1) How to retrieve data from input
If you look at the source code, it's written in $name or $kenban to automatically expand to variables, but
This is not the case and is stored as an associative array in $_GET.Let's take advantage of it.

(2) Obtaining the variable and merging it into SQL

$list=$pdo->prepare("SELECT* FROM table name");
$list->execute();
$temp = $list->fetchall();

If you look at the source code, I try my best to create where statements after creating prepare in SQL, but unfortunately SQL is already running, so it is difficult to play with SQL later.
Create a where statement before running SQL.
Instead of embedding data directly into SQL, use Bind.
(An example of a reference site destination is how to write a bind.)

$list=$pdo->prepare("SELECT* FROM table name WHERE1{$where_sql}", $param);
$list->execute();
$temp = $list->fetchall();

Show all when nothing is searched


when parameters do not cross Let's make the above $where_sql and $param empty

The period (jissday) is specified as a range

This depends on the column structure of the table, but
Input requires a form above 〇 以上 and below 以下 の.For example,

<input type="date" name="kara"> to <input type="date" name="made">

with the input
Search the range if there is an input value on the PHP side

For example, if you have kara,

AND jissday>=:kara 

I wish I could generate a Where statement.

I think the official website is the most stable and easy to understand.Please try making it while referring to that area.


2022-09-30 17:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.