Looking for help with my search code

Hey guys,

The search code below works for pulling document info and descriptions out of my database. The issue is, that sometimes, if the search term is vague, it will pull multiple results. That can get somewhat messy when there are a ton of results, I would like to see if there is some modification I could do to have the document name turn into a link that can be clicked on to only show that result. Any thoughts?

[php]
table {
border-collapse: collapse;
border: solid 1px black;
}
td {
padding: 2px 6px;
border: solid 1px black;
}

' method='post'>

Search for:

<?php if(isset($_POST['search'])) { $connx = mysql_connect('localhost', '*****', '*****') or die("connx"); $db = mysql_select_db('dvetter_sugarce') or die(mysql_error()); # convert to upper case, trim it, and replace spaces with "|": $search = (ini_get('magic_quotes_gpc')) ? stripslashes($_POST['search']) : $_POST['search']; $search = mysql_real_escape_string($search); $search = strtoupper(preg_replace('/\s+/', '|', trim($_POST['search']))); # create a MySQL REGEXP for the search: $regexp = "REGEXP '[[:<:]]($search)[[:>:]]'"; $query = "SELECT document_name, description FROM `documents` WHERE UPPER(`description`) $regexp OR ". "`document_name` $regexp"; $result = mysql_query($query) or die($query . " - " . mysql_error()); echo "\n"; while($row = mysql_fetch_assoc($result)) { echo ""; foreach($row as $key => $value) { echo ""; } echo "\n"; } } ?> [/php]
$value

Do you mean that rather than showing the details of every result, it shows a list of items that you can then click to view the details of that particular item?

You could try replacing:
[php] echo “

\n”;
while($row = mysql_fetch_assoc($result))
{
echo “”;
foreach($row as $key => $value)
{
echo “”;
}
echo “\n”;
} [/php]

With:

[php] echo “

$value
\n”;
while($row = mysql_fetch_assoc($result))
{
echo “”;
echo ‘’;
echo “\n”;
} [/php]

view-document.php:
[php]if(isset($_GET[‘docname’])) {
// Connect to MySQL
// Check docname
$docname = mysql_real_escape_string($_GET[‘docname’]);
$query = mysql_query(“SELECT * FROM documents WHERE documents.document_name=’” . $docname . “’”);

if(mysql_num_rows($query) == 1) {
    $doc = mysql_fetch_array($query);
    
    echo $doc['description']; // Replace with code to nicely display the document
} else {
    echo 'Document not found!';
}

}[/php]

’, $value, ‘

It appears to be on the right track, but all it is giving me is a very small table with no links in it, it does not seem to be presenting the $value. Here is the revised code. Thanks so much for your help!

[php]
table {
border-collapse: collapse;
border: solid 1px black;
}
td {
padding: 2px 6px;
border: solid 1px black;
}

' method='post'>

Search for:

<?php if(isset($_POST['search'])) { $connx = mysql_connect('localhost', '*****', '*****') or die("connx"); $db = mysql_select_db('dvetter_sugarce') or die(mysql_error()); # convert to upper case, trim it, and replace spaces with "|": $search = (ini_get('magic_quotes_gpc')) ? stripslashes($_POST['search']) : $_POST['search']; $search = mysql_real_escape_string($search); $search = strtoupper(preg_replace('/\s+/', '|', trim($_POST['search']))); # create a MySQL REGEXP for the search: $regexp = "REGEXP '[[:<:]]($search)[[:>:]]'"; $query = "SELECT document_name, description FROM `documents` WHERE UPPER(`description`) $regexp OR ". "`document_name` $regexp"; $result = mysql_query($query) or die($query . " - " . mysql_error());
 echo "<table>\n"; 

while($row = mysql_fetch_assoc($result))
{
echo “

”;
echo ‘’, $value, ‘’;
echo “\n”;
}
}
?> [/php]

Silly mistake on my side! $value should have been $row[‘document_name’]:

[php] echo ‘

’, $value, ‘’;[/php]

Should be:

[php] echo ‘

’, $row[‘document_name’], ‘’;[/php]

You are amazing! If I could figure out how to give you more Karma points, I would!
Now I am going to tackle the formatting, so that it is formatted the same as it shows up in our other databasing system, if you have any ideas on that feel free to chime in!

Sponsor our Newsletter | Privacy Policy | Terms of Service