Page 1 of 2
Php search help
Posted: Wed Apr 13, 2016 4:22 am
by Mark
Hey guys
I am needing a bit of help if you can please.
For some reason my search code is displaying all the records even tho I have not typed anything in the text box how can I get it so it dosent show them until the button has been pressed and something has been typed.
<h1>Search Previous Orders</h1>
<form action="search.php" method="get">
<input type="text" name="k" size="50" value="<?php echo $_GET['k']; ?>"/>
<input type="submit" name="submit" value="Search Orders"/>
</form>
<hr />
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM orders WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "FullName LIKE '%$each%' ";
else
$query .= "OR FullName LIKE '%$each%' ";
}
require('connect/connect.php');
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$fullname = $row['FullName'];
$gotdate = $row['GotDate'];
$paydate = $row['PayDate'];
$money = $row['Money'];
$phone = $row['Phone'];
$info = $row['Info'];
$status = $row['Status'];
$orderby = $row['OrderBy'];
echo "$fullname<br/>";
}
}
else
echo "No Results Found For \"<b>$each</b>\"";
?>
Re: Php search help
Posted: Wed Apr 13, 2016 8:28 am
by Filip
Your $i variable is never set to 0. You also never check if get request has been made.
KR,
- Filip
Re: Php search help
Posted: Wed Apr 13, 2016 9:38 am
by Mark
How would I sort it.
Re: Php search help
Posted: Wed Apr 13, 2016 10:18 am
by Filip
Code: Select all <?php
if(isset($_GET['k'])) { // check if get request has been made and k has been set
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM orders WHERE ";
$i = 0; // setting counter to 0
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "FullName LIKE '%$each%' ";
else
$query .= "OR FullName LIKE '%$each%' ";
}
require('connect/connect.php');
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$fullname = $row['FullName'];
$gotdate = $row['GotDate'];
$paydate = $row['PayDate'];
$money = $row['Money'];
$phone = $row['Phone'];
$info = $row['Info'];
$status = $row['Status'];
$orderby = $row['OrderBy'];
echo "$fullname<br/>";
}
}
else
echo "No Results Found For \"<b>$each</b>\"";
} // Closing initial if
?>
Again, if you aren't sure why your queries don't work, you can always echo them and test them in phpMyAdmin.
KR,
- Filip
Re: Php search help
Posted: Wed Apr 13, 2016 11:17 am
by Mark
Hello thanks for all your help just one more question lol my table seems to be messing up what have i done wrong in it this is the code?
Code: Select all<?php
session_start();
if(!isset($_SESSION["sess_user"])){
header("location:index.php");
}
else {
?>
<html>
<head>
<title>MBDUES.co.uk</title>
<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
</head>
<body>
<div id="wrapper">
<div id="header">
<img src="img/Banner.png"/>
</div>
<div id="nav">
<a href='./members.php'>Home</a>
<a href='./add_order.php'>Add New Order</a>
<a href='./search.php'>Search Orders</a>
<a href='./logout.php'>Logout</a>
</div>
<div id="content">
<div id="full">
Welcome, <?=$_SESSION['sess_user'];?> <hr />
<h1>Search Previous Orders</h1>
<form action="search.php" method="get">
<input type="text" name="k" size="50" value="<?php echo $_GET['k']; ?>"/>
<input type="submit" name="submit" value="Search Orders"/>
</form>
<hr />
<?php
if(isset($_GET['k'])) { // check if get request has been made and k has been set
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM orders WHERE ";
$i = 0; // setting counter to 0
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "FullName LIKE '%$each%' ";
else
$query .= "OR FullName LIKE '%$each%' ";
}
require('connect/connect.php');
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$fullname = $row['FullName'];
$gotdate = $row['GotDate'];
$paydate = $row['PayDate'];
$money = $row['Money'];
$phone = $row['Phone'];
$info = $row['Info'];
$status = $row['Status'];
$orderby = $row['OrderBy'];
echo "
<table border='1' style='width:100%'>
<tr>
<th>Full Name</th>
<th>Date Got</th>
<th>Pay Date</th>
<th>Money Due</th>
<th>Status</th>
<th>Order By</th>
</tr>
<tr>
<td>$fullname</td>
<td>$gotdate</td>
<td>$paydate</td>
<td>$money</td>
<td>$status</td>
<td>$orderby</td>
</tr>
</table>
";
}
}
else
echo "No Results Found For \"<b>$each</b>\"";
} // Closing initial if
?>
</div>
</div>
<div id="footer">
Footer
</div>
</div>
</body>
</html>
<?php
}
?>
Re: Php search help
Posted: Wed Apr 13, 2016 11:26 am
by Filip
Hello,
here's the fixed code:
Code: Select all<?php
session_start();
if(!isset($_SESSION["sess_user"])){
header("location:index.php");
}
?>
<html>
<head>
<title>MBDUES.co.uk</title>
<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
</head>
<body>
<div id="wrapper">
<div id="header">
<img src="img/Banner.png"/>
</div>
<div id="nav">
<a href='./members.php'>Home</a>
<a href='./add_order.php'>Add New Order</a>
<a href='./search.php'>Search Orders</a>
<a href='./logout.php'>Logout</a>
</div>
<div id="content">
<div id="full">
Welcome, <? echo $_SESSION['sess_user']; ?> <hr />
<h1>Search Previous Orders</h1>
<form action="search.php" method="get">
<input type="text" name="k" size="50" value="<?php echo $_GET['k']; ?>"/>
<input type="submit" name="submit" value="Search Orders"/>
</form>
<hr />
<?php
if(isset($_GET['k'])) { // check if get request has been made and k has been set
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM orders WHERE ";
$i = 0; // setting counter to 0
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "FullName LIKE '%$each%' ";
else
$query .= "OR FullName LIKE '%$each%' ";
}
require('connect/connect.php');
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
echo "
<table border='1' style='width:100%'>
<tr>
<th>Full Name</th>
<th>Date Got</th>
<th>Pay Date</th>
<th>Money Due</th>
<th>Status</th>
<th>Order By</th>
</tr>";
// Echo table header
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$fullname = $row['FullName'];
$gotdate = $row['GotDate'];
$paydate = $row['PayDate'];
$money = $row['Money'];
$phone = $row['Phone'];
$info = $row['Info'];
$status = $row['Status'];
$orderby = $row['OrderBy'];
echo "
<tr>
<td>$fullname</td>
<td>$gotdate</td>
<td>$paydate</td>
<td>$money</td>
<td>$status</td>
<td>$orderby</td>
</tr>
";
}
echo "</table>";
}
else
echo "No Results Found For \"<b>$each</b>\"";
} // Closing initial if
?>
</div>
</div>
<div id="footer">
Footer
</div>
</div>
</body>
</html>
KR,
-Filip
Re: Php search help
Posted: Wed Apr 13, 2016 11:41 am
by Mark
Hello,
Sorry was ment to ask this is the last post if i wanted to just type John Doe in the text box how do i get it so it just searches for that name or any name put in like John Doe because at the moment if i type John Doe its searching for the name John and Doe and Displaying the records when i inly want it to show the John Doe records?
You can see what i mean by the screenshots its only ment to show the records for Mark Brodie but its searching for Brodie and its showing Paul Brodie Aswell.
Re: Php search help
Posted: Wed Apr 13, 2016 12:02 pm
by Mark
hello ive resolved it thank you for your help
Re: Php search help
Posted: Wed Apr 13, 2016 3:00 pm
by Filip
You would want to change
OR in query to
AND:
Code: Select all foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "FullName LIKE '%$each%' ";
else
$query .= "AND FullName LIKE '%$each%' "; //here
}
KR,
-Filip
Re: Php search help
Posted: Wed Apr 13, 2016 3:27 pm
by Mark
I just noticed if i press the button without anything in the text box it shows all the records lol?