I'm trying to make a search function on this website that loops through my $result_array. I've tried a bunch of different approaches on my own but to no avail. Thought it was about time I asked here. This is my code:
<?php
include 'database_info.php';
$search_string = $_POST['search1'];
$query = "SELECT * FROM customers_info WHERE first_name='$search_string'";
//Try to query the database
if($result = $data_connect->query($query)){
echo '<br /><br />Successfully sent query!<br /><br />';
}
else {
echo 'Error getting customers from the database: '.mysqli_error($data_connect).'<br />';
}
//Create Table
echo "<table id='Grid'><tr>";
echo "<th>customer_id</th>";
echo "<th>First Name</th>";
echo "<th>Last Name</th>";
echo "<th>Email</th>";
echo "<th>Country</th>";
echo "<tr>\n";
$class = "odd"; //Keep track of whether a row is equal or odd
//Loop through all the rows returned by the query, creating a table row for each
while($result_array = mysqli_fetch_assoc($result)){
echo "<tr class=\"$class\">";
echo "<td>".$result_array['customer_id']."</td>";
echo "<td><a href='view_customer.php?email=".$result_array['email']."'>" .$result_array['first_name']. "</a></td>";
echo "<td>" .$result_array['last_name']. "</td>";
echo "<td>" .$result_array['email']. "</td>";
echo "<td>" .$result_array['country']. "</td>";
echo "</td></tr>\n";
//If the last row was even make the next one odd
if($class =="odd"){
$class ="even";
}
else{
$class = "odd";
}
}
echo "</table>";
$data_connect->close();
?>
Can anybody tell me a way I could accomplish this? A function or approach I could use?
P.S.
My current approach is to alter the query, this does work but then I can only search for the customer first_name for example. I want to be able to search for email address or last_name or country. These are all columns in my database.