Php search help

11 posts Page 1 of 2
Contributors
User avatar
Mark
VIP - Donator
VIP - Donator
Posts: 372
Joined: Mon Aug 17, 2009 10:35 pm

Php search help
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>\"";

?>
http://www.mbappz.com
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

Re: Php search help
Filip
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
VIP - Donator
Posts: 372
Joined: Mon Aug 17, 2009 10:35 pm

Re: Php search help
Mark
How would I sort it.
http://www.mbappz.com
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

Re: Php search help
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
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
User avatar
Mark
VIP - Donator
VIP - Donator
Posts: 372
Joined: Mon Aug 17, 2009 10:35 pm

Re: Php search help
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
}
?>
You do not have the required permissions to view the files attached to this post.
http://www.mbappz.com
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

Re: Php search help
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
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
User avatar
Mark
VIP - Donator
VIP - Donator
Posts: 372
Joined: Mon Aug 17, 2009 10:35 pm

Re: Php search help
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.
You do not have the required permissions to view the files attached to this post.
http://www.mbappz.com
User avatar
Mark
VIP - Donator
VIP - Donator
Posts: 372
Joined: Mon Aug 17, 2009 10:35 pm

Re: Php search help
Mark
hello ive resolved it thank you for your help
http://www.mbappz.com
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

Re: Php search help
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
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
User avatar
Mark
VIP - Donator
VIP - Donator
Posts: 372
Joined: Mon Aug 17, 2009 10:35 pm

Re: Php search help
Mark
I just noticed if i press the button without anything in the text box it shows all the records lol?
http://www.mbappz.com
11 posts Page 1 of 2
Return to “Help & Support”