• Top Members
    Reps
    Posts
  • 834 Replies
    2585 Replies
  • 716 Replies
    2025 Replies
  • 331 Replies
    1928 Replies

Php search help

User avatar
Mark
VIP - Donator
Posts: 370

Php search help

Wed Apr 13, 2016 5:22 am

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>\"";

?>

User avatar
Filip
Coding Guru
Posts: 833

Re: Php search help

Wed Apr 13, 2016 9:28 am

Your $i variable is never set to 0. You also never check if get request has been made.

KR,
- Filip
CodenStuff wrote:Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D

User avatar
Mark
VIP - Donator
Posts: 370

Re: Php search help

Wed Apr 13, 2016 10:38 am

How would I sort it.

User avatar
Filip
Coding Guru
Posts: 833

Re: Php search help

Wed Apr 13, 2016 11:18 am

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
CodenStuff wrote:Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D

User avatar
Mark
VIP - Donator
Posts: 370

Re: Php search help

Wed Apr 13, 2016 12:17 pm

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
}
?>
You do not have the required permissions to view the files attached to this post.

User avatar
Filip
Coding Guru
Posts: 833

Re: Php search help

Wed Apr 13, 2016 12:26 pm

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
CodenStuff wrote:Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D

User avatar
Mark
VIP - Donator
Posts: 370

Re: Php search help

Wed Apr 13, 2016 12:41 pm

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.
You do not have the required permissions to view the files attached to this post.

User avatar
Mark
VIP - Donator
Posts: 370

Re: Php search help

Wed Apr 13, 2016 1:02 pm

hello ive resolved it thank you for your help

User avatar
Filip
Coding Guru
Posts: 833

Re: Php search help

Wed Apr 13, 2016 4:00 pm

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
CodenStuff wrote:Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D

User avatar
Mark
VIP - Donator
Posts: 370

Re: Php search help

Wed Apr 13, 2016 4:27 pm

I just noticed if i press the button without anything in the text box it shows all the records lol?

Post Reply

Return to “Help & Support”