🔒 Closed how to add search in PHP guys

Status
Not open for further replies.
D

Deleted member 622421

help me po sana sa mga masters dyan :(
thanks in advance guys.
 
<?php
include 'connection.php';
$search = "";
if(isset($_GET['submit'])){
$search = $_GET['search'];
}

?>
<!DOCTYPE html>
<html>
<head>
<title>View Contacts</title>
</head>
<body>
<div align="center">
<div style="display: inline-block;">Click Here to<a href="index.php"> add </a>Contacts</div>
<div style="display: inline-block;">
<form method="GET" action="">
<input type="text" name="search" placeholder="SEARCH HERE">
<input type="submit" name="submit" value="submit" >
</form>

</div>
<div>
<table border="1">
<tr>
<th>ID</th>
<th>NAME</th>
<th>PHONE NUM</th>
<th>EMAIL</th>
<th colspan="2">ACTION</th>
</tr>
<?php

$getresultsql = mysqli_query($connect,"SELECT * FROM contacts WHERE (name LIKE '%$search%' OR id = '$search')");
while($getresult = mysqli_fetch_array($getresultsql)){
$id = $getresult[0];
$name = $getresult[1];
$phone = $getresult[2];
$email = $getresult[3];

?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $phone; ?></td>
<td><?php echo $email; ?></td>
<td><a href="<?php echo "update.php?getid=".$id; ?>"><button>View</button></a></td>
<td><a href="<?php echo "delete.php?getid=".$id; ?>"><button>Delete</button></a></td>
</tr>

<?php
}

?>
</table>
</div>
</div>
</body>
</html>


Yan buong php para alam mo
 
<?php
include 'connection.php';
$search = "";
if(isset($_GET['submit'])){
$search = $_GET['search'];
}

?>
<!DOCTYPE html>
<html>
<head>
<title>View Contacts</title>
</head>
<body>
<div align="center">
<div style="display: inline-block;">Click Here to<a href="index.php"> add </a>Contacts</div>
<div style="display: inline-block;">
<form method="GET" action="">
<input type="text" name="search" placeholder="SEARCH HERE">
<input type="submit" name="submit" value="submit" >
</form>

</div>
<div>
<table border="1">
<tr>
<th>ID</th>
<th>NAME</th>
<th>PHONE NUM</th>
<th>EMAIL</th>
<th colspan="2">ACTION</th>
</tr>
<?php

$getresultsql = mysqli_query($connect,"SELECT * FROM contacts WHERE (name LIKE '%$search%' OR id = '$search')");
while($getresult = mysqli_fetch_array($getresultsql)){
$id = $getresult[0];
$name = $getresult[1];
$phone = $getresult[2];
$email = $getresult[3];

?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $phone; ?></td>
<td><?php echo $email; ?></td>
<td><a href="<?php echo "update.php?getid=".$id; ?>"><button>View</button></a></td>
<td><a href="<?php echo "delete.php?getid=".$id; ?>"><button>Delete</button></a></td>
</tr>

<?php
}

?>
</table>
</div>
</div>
</body>
</html>


Yan buong php para alam mo
Hindi mo na kailangan i-declare yung variable $search bago gamitin. Delikado yung queries mo, risk sa sql injection.
 
Datatable'based search is only applicable if you are using datatable (of course) and only wanna do client side search

Ideally if your goal is like Google Search's search feature, you should have a backend api powered by something like ElasticSearch and a frontend component that utilizes async calls aka AJAX.

Now if you are just making a db search aka db query, then your standard db queries should work.

If you are only searching like 10 static items, then frontend, client side search is probably the most practical. 10 items are nothing. That's easily searchable via JavaScript.
 
Status
Not open for further replies.

Similar threads

About this Thread

  • 13
    Replies
  • 1K
    Views
  • 10
    Participants
Last reply from:
pixkit

Trending Topics

Online now

Members online
1,029
Guests online
1,101
Total visitors
2,130

Forum statistics

Threads
2,289,558
Posts
29,059,225
Members
1,212,890
Latest member
takodachi
Back
Top