What's new
  • Welcome to PHCorner Forums. Take a moment to Sign up and gain unlimited access and extra privileges that guests are not entitled to, such as: All that and more! Registration is quick, simple and absolutely free. Join our community today!

Help how to add search in PHP guys

PHC _ Mark

Forum Guru
Established
Joined
Sep 11, 2016
Messages
4,058
Reaction score
1,017
help me po sana sa mga masters dyan :(
thanks in advance guys.
 

Imkaiser101

Enthusiast
<?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
 

nogats07

Addict
<?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.
 

pixkit

Addict
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.
 

Trending On This Forum

Unanswered Topics

Top