I am trying to build a PHP search option then return a page of results from the database.
For example: One of the options is your userid that they have to fill in. Well, I want to return a page of results using a search option to find all records in the userid field of the table.
Any ideas on how to do this? I have tried several different ways and it won't display. I have one record and it shows nothing.
Note: I want the search options to include either the userid and/or the date. They manually put in the current date in this format mm-dd-yyyy
Any help would be greatly appreciated.
PHP Search with results
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: PHP Search with results
I am not quite sure I know what you are having a problem with, coding-wise. Since a basic search is just a simple SQL statement, I am pretty sure that is not the issue. Is it an issue with displaying multiple results? That took me a bit to figure out, but I do have a script I can post where I had been working on a pet training/fighting game where the player had to be given a page that showed every monster they owned for them to select. So I had to search the database for every occurrence of that playerID and then display all of the monsters from the one query.
If it is an issue with regular expression searching, there are some good resources online. It all depends on how "intelligent" you want your search to be, with some of the most intelligent searching engines being extraordinarily complex and intricate.
If it is an issue with regular expression searching, there are some good resources online. It all depends on how "intelligent" you want your search to be, with some of the most intelligent searching engines being extraordinarily complex and intricate.
The indelible lord of tl;dr
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: PHP Search with results
I am not exactly sure what you mean but make your variable surround by ` ` the squiggly lines above the tab button
example
say i want to select all
say i want to select userid
not sure if this is what you are looking for
example
say i want to select all
Code: Select all
$selection = "*";
$query = "Select `$selection` from tableCode: Select all
$selection = "userid";
$query = "Select `$selection` from table- SpiritWebb
- Posts: 3107
- Joined: Sun Jul 12, 2009 11:25 pm
Re: PHP Search with results
Here is the code.
Now when I try to search, it just says Please enter a search query. However there is one record in the database and it won't display it. Am I doing something wrong here?
EDIT: I got it working...found out the web template I am using will not allow this too work. It has to be its own file...but either way, its working.
Code: Select all
<center><h2><strong>Search Metrics</strong></h2></center>
<p>You may search either by the date or the userid</p>
<form method="post" action="search.php?go" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
<?php
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
if(preg_match("/^[ a-zA-Z0-9]+/", $_POST['name'])){
$name=$_POST['name'];
//connect to the database
$db=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("metrics");
//-query the database table
$sql="SELECT id, date, userid FROM shiftmetrics WHERE date LIKE '%" . $name . "%' OR userid LIKE '%" . $name ."%'";
//-run the query against the mysql query function
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$date =$row['date'];
$userid=$row['userid'];
$id=$row['id'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "<a href=\"search.php?id=$id\">" .$date . " " . $userid . "</a></li>\n";
echo "</ul>";
}
}
else{
echo "<p>Please enter a search query</p>";
}
}
}
?>
EDIT: I got it working...found out the web template I am using will not allow this too work. It has to be its own file...but either way, its working.
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: PHP Search with results
This isn't going to be public, right? If it is public you should add floodgate code to it. But it looks more like an intranet application from the looks of it.
The indelible lord of tl;dr
- SpiritWebb
- Posts: 3107
- Joined: Sun Jul 12, 2009 11:25 pm

