Page 1 of 1

Linking results returned from a database search

Posted: Mon Nov 14, 2011 6:15 am
by Rob_Scott
Hi,

I am a newbie to php,

I have managed to run a search and get results from my database,

However I do not know how to link the results to the pages returned???

Help would be much appreciated :D

Here is my code:

Code: Select all

        include_once("connect_to_mysql.php");
        $query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
while($row = mysql_fetch_array($query)){
            $id = $row["id"];
   $title = $row["title"];
   $search_output .= "Item ID: $id - $title<br />";
                } // close while
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
}
}
?>
<html>
<head>
</head>
<body>
<h2>Search the Exercise Tables</h2>
<form action="http://www.bertgroup.co.uk/?q=node/19" method="post">
Search For: 
  <input name="searchquery" type="text" size="44" maxlength="88"> 
Within: 
<select name="filter1">
<option value="Whole Site">Whole Site</option>
<option value="Pages">Pages</option>
<option value="Blog">Blog</option>
</select>
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
</div>
</body>
</html>

Re: Linking results returned from a database search

Posted: Mon Nov 14, 2011 6:22 am
by Celauran
Rob_Scott wrote:However I do not know how to link the results to the pages returned???
What does this mean?

Re: Linking results returned from a database search

Posted: Tue Nov 15, 2011 3:37 am
by Rob_Scott
I get page names returned as search results from my query, and I want to them to be linked, so that when you click them they go to the actual page.

Re: Linking results returned from a database search

Posted: Tue Nov 15, 2011 4:15 am
by mikeashfield

Code: Select all

<a href='www.example.com/example><?php echo $valueFromDB; ?>.html'><?php echo $linkTitleFromDB; ?></a>

Re: Linking results returned from a database search

Posted: Tue Nov 15, 2011 6:14 am
by Rob_Scott
Thanks this helps, but if I have multiple results coming back from my query it highlights them all as one link :?

Re: Linking results returned from a database search

Posted: Tue Nov 15, 2011 6:17 am
by Celauran
Create the link inside your while loop.

Code: Select all

while ($row = mysql_fetch_assoc($result))
{
    echo "<a href=\"{$row['url']}\">{$row['url']}</a><br />";
}

Re: Linking results returned from a database search

Posted: Tue Nov 15, 2011 7:23 am
by Rob_Scott
I have modified my code.

Once the query has returned the results which are pages in my table, i want individual links to each page.

With the current code it highlights the amount of results returned and the results themselves as one link.

Sorry for the confusion!!!!!!!

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
$searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);

$sqlCommand = "SELECT nid, title AS title FROM node WHERE title LIKE '%$searchquery%' OR type LIKE '%$searchquery%'";

        include_once("connect_to_mysql.php");
        $query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 0){
$search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />";
while($row = mysql_fetch_array($query)){
            $id = $row["nid"];
   $title = $row["title"];
   $search_output .= "$id - $title<br />";
                } // close while
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
}
}
?>
<html>
<head>
</head>
<body>
<h2>Search the Exercise Tables</h2>
<form action="http://www.bertgroup.co.uk/?q=node/12" method="post">
Search cars: 
  <input name="searchquery" type="text" size="44" maxlength="88"> 
<input name="myBtn" type="submit">
<br />

</form>
<div>
<a href='?q=node/><?php echo $id; ?>.html'><?php echo $search_output; ?></a>
</div>
</body>
</html>

Re: Linking results returned from a database search

Posted: Tue Nov 15, 2011 8:14 am
by Celauran

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if (isset($_POST['searchquery']) && $_POST['searchquery'] != "")
{
    $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);

    $sqlCommand = "SELECT nid, title AS title FROM node WHERE title LIKE '%$searchquery%' OR type LIKE '%$searchquery%'";

    include_once("connect_to_mysql.php");
    $query = mysql_query($sqlCommand) or die(mysql_error());
    $count = mysql_num_rows($query);
    if ($count > 0)
    {
        $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />";
        while ($row = mysql_fetch_array($query))
        {
            // $id = $row["nid"];
            // $title = $row["title"];
            // $search_output .= "$id - $title";
            $search_output .= "<a href=\"?q=node/{$row['nid']}\">{$row['title']}</a><br />";
        } // close while
    }
    else
    {
        $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />";
    }
}

?>
<html>
    <head>
    </head>
    <body>
        <h2>Search the Exercise Tables</h2>
        <form action="http://www.bertgroup.co.uk/?q=node/12" method="post">
            Search cars: 
            <input name="searchquery" type="text" size="44" maxlength="88"> 
            <input name="myBtn" type="submit">



        </form>
        <div>
            <?php echo $search_output; ?>
        </div>
    </body>
</html>

Re: Linking results returned from a database search

Posted: Wed Nov 16, 2011 4:01 am
by Rob_Scott
Thanks soooooooo much!!!!!! this works perfectly :D