So I said in another thread that I would be asking another question
about functions... So here it goes, I am attempting to write a
function to search the database, which used to work just fine when I
wrote it without using a function (Would that be considered static?)
Now that I am attempting to rewrite my stuff so I can reuse the code,
now it's not working... Here is what I used to do and it worked just
fine:
$qstring = "SELECT * FROM ".$table." WHERE FName like '%$search%' or
LName like '%$search%' or Add1 like '%$search%' or Add2 like '%$search
%' or City like '%$search%' or State like '%$search%' or Zip like '%
$search%' or XCode like '%$search%'";
if ($_SESSION['search'] != NULL){
echo "The search string is: <strong>$search</strong>.<BR>";
$qrow[]= mysql_query($qstring) or die(mysql_error());
$qresult = $qrow[0];
$num_rows = mysql_num_rows($qresult);
//display search form
echo "
<form action='search.php' method='GET'>
<label>Search:
<input type='text' name='search' id='search' />
</label>
<input type='submit' value='Go!' />
</form>";
echo <<<HTML
<a href='index.php'>Return to database</A>
<P>Total Records found: {$num_rows}</P>
<A href='excelexport.php'>Export selection to excel</A>
<form method='GET' action='edit.php'>
<table border='1'>
<tr>
<th><a href='?order=a'>First Name</A></th>
<th><A href='?order=b'>Last Name</A></th>
<th><A href='?order=c'>Address Line 1</A></th>
<TH><A href='?order=d'>Address Line 2</A></th>
<TH><A href='?order=e'>City</A></th>
<th><A href='?order=f'>State</A></th>
<th><A href='?order=g'>Zip</A></th>
<TH><A href='?order=h'>Code</A></th>
<th><A href='?order=i'>ID #</A></th>
<TH>Edit</th>
<th>Delete</th>
</tr>
HTML;
echo "Just testing: ".$_SESSION['search'];
while($qrow = mysql_fetch_assoc($qresult)) {
//Display the search results using heredoc syntax
echo <<<HTML
<tr>
<td>{$qrow['FName']}</td>
<td>{$qrow['LName']}</td>
<td>{$qrow['Add1']}</td>
<td>{$qrow['Add2']}</td>
<td>{$qrow['City']}</td>
<td>{$qrow['State']}</td>
<td>{$qrow['Zip']}</td>
<td>{$qrow['XCode']}</td>
<td>{$qrow['Record']}</td>
<td><a href='edit.php?Record={$qrow['Record']}'>Edit</a></td>
<td><a href='delete.php?Record={$qrow['Record']}'>Delete</a></td>
</tr>
</form>
HTML;
Now, here is what I have as a function and is not working:
<?PHP
$FName ="";
$LName ="";
$Add1 = "";
$Add2 = "";
// $_SESSION['search'] = $_GET['search'];
function search($searchvar, $table, $num_rows, $FName, $LName, $Add1,
$Add2) {
$qstring = "SELECT * FROM ".$table." WHERE FName like '%$searchvar%'
or LName like '%$searchvar%' or Add1 like '%$searchvar%' or Add2 like
'%$searchvar%' or City like '%$searchvar%' or State like '%$searchvar
%' or Zip like '%$searchvar%' or XCode like '%$searchvar%'";
$qrow[]= mysql_query($qstring) or die(mysql_error());
$qresult = $qrow[0];
$num_rows = mysql_num_rows($qresult);
//while($qrow = mysql_fetch_assoc($qresult)) {
$FName = $qrow['FName'];
$LName = $qrow['LName'];
$Add1 = $qrow['Add1'];
$Add2 = $qrow['Add2'];
return;
}
?>
And what happens, is first of all it displays the entire database on
the search page, which I'm kind of okay with... But when you search,
it updates the variables, and echo's out the right search term, but it
doesn't update the database to only show the search results... I think
it might be tied to it displaying the entire database at page load...
But I'm not sure.. Anyone have an idea of what I did wrong other then
everything? :)
Oh, and as far as calling the function I do this: search($searchvar,
$table, $num_rows, $FName, $LName, $Add1, $Add2);
--
Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424
www.raoset.com
japruim@xxxxxxxxxx