FILTER DROPDOWN and ONLY DISPLAY DATA FIELD

basically, what I need to do is have when a user clicks on index.php?beat=NORTH, it displays the north data where they can then filter it by week I have split the week up with 1234567 so if its mon to fri it would be 12345, I need to turn the Search into a dropdown with 3 fields and label them 12345= MON - FRI OR 13456 = MON - SAT or 1234567 = MON - SUN

MYSQL DATA BELOW

--
-- Table structure for table `DATA`
--

CREATE TABLE `DATA` (
  `id` smallint(10) NOT NULL,
  `street` varchar(250) NOT NULL,
  `restriction` varchar(250) NOT NULL,
  `day` varchar(250) NOT NULL,
  `time` varchar(250) NOT NULL,
  `beat` varchar(25) NOT NULL,
  `week` varchar(15) NOT NULL,
  `map` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `DATA`
--

INSERT INTO `DATA` (`id`, `street`, `restriction`, `day`, `time`, `beat`, `week`, `map`) VALUES
(1, 'PETER STREET', 'NO BALL GAMES', 'MON - SUN', '9AM - 5PM', 'NORTH', '1234567', 'PETER STREET NEWCASTLE'),
(2, 'BILL STREET', 'NO CARS', 'MON - FRI', '9AM - 3PM', 'SOUTH', '12345', 'BILL STREET NEWCASTLE'),
(3, 'WOOL ST', 'NO CARD GAMES', 'MON - SAT', '9AM - 8PM', 'SOUTH', '123456', 'WOOL STREET NEWCASTLE');

--
-- Indexes for dumped tables

PHP SCRIPT BELOW

<!DOCTYPE html>

<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <meta charset="UTF-8">
  <title>SCHOOL STREET RULES</title>
  <style>
#customers {
  font-family: Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

#customers td, #customers th {
  border: 1px solid #ddd;
  padding: 8px;
}

#customers tr:nth-child(even){background-color: #f2f2f2;}

#customers tr:hover {background-color: #ddd;}

#customers th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #04AA6D;
  color: white;
}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav a.active {
  background-color: #04AA6D;
  color: white;
}

</style>
</head>
<body>

<div class="container">
    <div class="topnav">
  <a href="index.php">HOME</a>

</div>

<div style="padding-top:25px">
  <center><h2>street rules</h2></center>
</div>
    
   <div class="row">
   <div class="col-md-20 col-md-offset-0" style="margin-top: 2%;">
   <div class="row">

<?php 

     $conn = new mysqli('localhost', '0000000', '00000000', '00000000000');
     if(isset($_GET['search'])){
        $searchKey = $_GET['search'];
        $sql = "SELECT * FROM DATA WHERE Week LIKE '%$searchKey%'" ;
     }else
     $sql = "SELECT * FROM DATA WHERE week LIKE ''" ;
     $result = $conn->query($sql);
     
   ?>

   <form action="" method="GET"> 
     <div class="col-md-6">
        <input type="text" name="search" class='form-control' placeholder="Search By day" value=<?php echo @$_GET['search']; ?> > 
     </div>
     <div class="col-md-6 text-left">
      <button class="btn">Search</button>
     </div>
   </form>

   <br> 
   <br>
</div>

<table class="table table-bordered" id="customers">
  <tr>
     <th>Street / Location</th>
     <th>Restriction</th>
     <th>Day / Time</th>
     <th>Beat</th>
     <th>Map</th>
  </tr>
  <?php while( $row = $result->fetch_object() ): ?>
  <tr>
     <td><?php echo $row->street ?></td>
     <td><?php echo $row->restriction ?></td>
     <td><?php echo $row->day ?> | <?php echo $row->time ?></td>
     <td><?php echo $row->beat ?></td>
     <td>
     <a href="https://www.google.com/maps/place/<?php echo $row->map ?>">MAP</a>
     </td>
  </tr>
  <?php endwhile; ?>
</table>
</div>
</div>
</div>
<P>
<CENTER>
<footer>&copy; Copyright 2023 SCHOOL TEAM</footer></CENTER></P>
</body>
</html>

I recommend that you lay out the code on the page in this general order -

  1. initialization
  2. post method form processing
  3. get method business logic - get/produce data needed to display the page
  4. html document

This will make it easier to design, write, test, debug, and maintain your code. It will also let you post just the relevant code that you are having a problem with. If you are not having a problem with any of the css, database connection, or producing the output, we don’t need to see any of that code.

The code to query and retrieve the search data would go in item #3 on this list. You would fetch the data into a uniquely named php variable, then test (in case the query matched nothing) and display the results at the appropriate location in the html document.

Do not put external, unknown, dynamic values directly into sql query statements. Use a prepared query instead. If it seems like using a prepared query with the mysqli extension is overly complicated, it is. This would be a good time to switch to the much simpler and more modern PDO extension.

You need to validate your resulting web pages at validator.w3.org There are some mistakes and obsolete elements in the markup.

Where’s your attempt at doing this or is this the actual question you are asking? Do you want to list just the beat values that exist in the stored data or list all possible values?

You should not store both the day and week values. The day values are derived data, based on the week values. Store only the week values.

For the day/week search, do you always want the three specific choices you have listed, only the choices that exist in the matching beat data, or do you want to be able to arbitrarily select days (a separate checkbox for each day of the week), and be able to match any rule containing that day? For example, if someone checks the MON checkbox, the code would match all three example rows since they all include Monday? Selecting the SAT checkbox would match the first and third example data? Or do you want to allow both methods, a select option menu with the range choices and day checkboxes?

Sponsor our Newsletter | Privacy Policy | Terms of Service