Please, please help. I am new to this, but trying hard. I have been trying to use the search function discussed in the article written by Dan LaFlamme on the PHPBuilder site, "Storing Checkbox Data in Your Database." http://www.phpbuilder.com/columns/laflamme20001016.php3
Can't seem to post a question at the article. I am interested in just the searching function, the last part of the article, not the inserting function.
I would like be able to access the query that results from using the search function skill_($skills). I am able to generate the checkboxes (the form page). When I select the skills I can see that a valid query is returned. I placed the following command at the end of the function to test it: print skill_search($skill);
I saw this somewhere. It showed that I was getting a valid query. Exactly the query that I want.
The problem: How do I actually call the query that results from processing this function? What does the code look like and where do I put it? Do I need to call the function at the beginning of the PHP code? How do I do that? I tried:
$query = skill_search($skills);
at the beginning of the PHP code. Nothing happened. Here's the code I'm using at the moment. The first part is the function code copied directly from the article on PHPbuilder Web site, the search function, followed with my guess as how to call it at the end. Any advice or suggestions greatly appreciated. Thank you.
<?php
/ I added this my connect info /
include("mylogininfo.inc");
$connection = mysql_connect($host,$user,$password)
or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection)
or die ("Couldn't select database");
echo "<div align='center'><b>Searching skills using laflamme example<br></div>\n";
/ builds a query to search for the skills
checked off in the $skills array /
function skill_search($skills) {
if (!empty($skills)) {
$query = "SELECT DISTINCT user.username
FROM user, const_skills, lookup_skills
WHERE lookup_skills.uid = user.id
AND lookup_skills.skill_id = const_skills.id ";
$query .= " AND (";
foreach ($skills as $check) {
$query .= " const_skills.id = $check OR";
}
/* remove the final OR */
$query = substr($query, 0, -2);
$query .= ")";
$count = count($skills);
$query .= " GROUP BY user.username HAVING count(user.username) >= $count";
$query .= ";";
return $query;
}
}
/ problem starts here I'm guessing at what to do. I need to be able to use the results of the function: the query that results /
$result = mysql_query($query)
or die ("Couldn't execute query.");
/* display results in a table just because that is how I will eventually be using this code */
if (mysql_num_rows($result) > 0)
{
echo "<table width='450' style='font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 13px;' cellspacing='1' cellpadding='1' border='1'>";
echo "<tr align='left' valign='top'><td valign='top' width='25'><b>User Name</b></td>
</tr>\n";
echo "<tr align='left' valign='top'>\n";
while ($row = mysql_fetch_array($result,MYSQL_NUM))
{
/* display table with row for each text */
echo "<td valign='top' width='25'>{$row['0']}</td>\n";
}
echo "</table>\n";
echo "<br><br clear='all'>\n";
}
mysql_free_result ($result);
?>