Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a textbox where I can type multiple text strings and a gridview that displays the result from an SQL SELECT based on what's in the textbox.
For example, if the textbox has "abc def -xyz" then the gridview would display all records that have the substrings 'abc' and 'def' and NOT the substring 'xyz'.
I also want the gridview to refresh whenever the space character is entered. That means right after 'abc' and then 'def'.
How do I do that? Many thanks for your help.

You really haven't providied enough information, such as what language you are using. But, I'll give it a shot using PHP. This example will require the user to submit the page to see the results. You will need to incorporate some AJAX to have the grid update dynamically. There's just way to much in your request to do all at once:
<?phpif (isset($_POST['search_string']))
{
//Convert search string into an array
$search_ary = explode(' ', trim($_POST['search_string']));
//Process each search string part
foreach ($search_ary as $str)
{
if($str!='')
{
if(substr($str, 0, 1)=='-')
{
$where_parts[] = "db_field NOT LIKE '%".substr($str, 1)."%'";
}
else
{
$where_parts[] = "db_field LIKE '%".$str."%'";
}
}
}$query = "SELECT * FROM table WHERE " . implode(' AND ', $where_parts);
$result = mysql_query($query) or die(mysql_error());
$output = "<table>\n";
echo "<tr><th>Col 1</th><th>Col 2</th><th>Col 3</th></tr>\n";
while ($record = mysql_fetch_assoc($result))
{
$output .= "<tr>";
$output .= "<td>{$record['field1']}</td>";
$output .= "<td>{$record['field2']}</td>";
$output .= "<td>{$record['field3']}</td>";
$output .= "</tr>";
}
$output .= "<table>\n";
}
?><html>
<body><form name="test" method="POST">
Enter Search Criteria: <input type="text" name="search_string" />
<button type="submit">Submit</button>
</form>
<br /><br />
<?php echo $output; ?></body>
</html>Michael J

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |