44659

how to make search query results show up as links to their pages

Question:

I am new to php as you can see :)

how can i make the search query results show up as links to their pages. do i need to add to my database a 'links' column in my pages table? Then search_output them out?

<?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 id, page_title AS title FROM pages WHERE MATCH (page_title,page_body) AGAINST ('$searchquery'))"; } include_once("db_connects.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 .= ""; } // close while } else { $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand"; } } ?> <html> <head> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Search For: <input name="searchquery" type="text" size="44" maxlength="88"> </select> <input name="myBtn" type="submit"> <br /> </form> <div> <?php echo $search_output; ?> </div> </body> </html>

Answer1:

You would need to append all the output from the query fetch, to your $search_output string.

while($row = mysql_fetch_array($query)){ $id = $row["id"]; $title = $row["title"]; $search_output .= "<a href='".$_SERVER['SERVER_NAME']."/".$title."'>".$title."</a><br>"; }

But it also depends on how your links look as well, they might not be as simple as above, or might link externally. Then in that case, yes an extra field where you hardinput the link might be useful.

<strong>NEW EDIT TO ANSWER NEXT QUESTION...</strong>

while($row = mysql_fetch_array($query)){ $id = $row["id"]; $title = $row["title"]; $link = $row["links"]; $search_output .= "<a href='".$link."'>".$title."</a><br>"; }

Recommend

  • Rails loads all stylesheets except application.css.scss
  • Mysql query check the blob column type in where clause
  • Get page title & URL and echo in HTML
  • scrapy csv output having all results in single row
  • scrapy : another method to avoid a lot of try except
  • Angular not working correctly with Meteor 1.3.1
  • Making the horizontal axis labels clickable
  • Using multiple CSS selectors for the same ArticleItem in Scrapy
  • How do you move the cursor to the next element in a contenteditable div in Chrome/Safari?
  • why warning with my performSelector
  • How to determine which eclipse dependencies to use
  • Subtract fetched datetime from current date time
  • make header, sidebar & footer constant across website
  • How would WP plugins need to be recoded in order to work on Modx?
  • performSelector in NSOperation subclass
  • JavaScript get page coordinates of closest clicked character
  • My PHP session won't save on hostgator
  • select only 1st level of nested elements
  • How do I interpolate a line number from __LINE__ into the name of a test in Perl?
  • Output from SQL Server and C# different
  • An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.
  • RegEx for Phone Number Numbers with Letters
  • php regex remove digits
  • ASP.NET Gridview Paging Problem
  • Get highlight text in current window and send it in a popup
  • Replace and retrieve placeholder value
  • Get specific string
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Extracting HTML between tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Display issues when we change from one jquery mobile page to another in firefox
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • To display the title for the current loaction in map in iphone
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • trying to dynamically update Highchart column chart but series undefined
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to stop GridView from loading again when I press back button?