Jump to content

Search result links need to open in new window


john-formby

Recommended Posts

Hi,

I have followed the tutorial in the tutorials section - MySQL Full-Text Search With PHP and have modified it to fit in with my page layout.  The problem is that the search results are links to other sites and some of them need to open in a new window.  In my table I have the field new_window which has two values, 0 = open in existing window, 1 = open in new window.  I have tried adding an IF statement to the code to get the value of new_window and either open it in a new window or the existing one.  Unfortunately I am unable to get it to work.

Could someone please have a look at my code and tell me where I am going wrong.

[code]<?php
include("common/header1.php");
?>

</head>

<body>

<?
include("common/header2.php");
?>

<?php
// Conect to the database.
include "dblinks.inc";


// Create the search function:

function searchForm()
{
  // Re-usable form
 
  // variable setup for the form.
    echo '<div id="maincol"><h1>Search</h1>';

  $searchwords = (isset($_GET['words']) ? htmlspecialchars(stripslashes($_REQUEST['words'])) : '');
  $normal = (($_GET['mode'] == 'normal') ? ' selected="selected"' : '' );
  $boolean = (($_GET['mode'] == 'boolean') ? ' selected="selected"' : '' );
 
  echo '<form method="get" action="'.$_SERVER['PHP_SELF'].'">';
  echo '<input type="hidden" name="cmd" value="search" />';
  echo 'Search for: <input type="text" name="words" value="'.$searchwords.'" /> ';
  echo 'Mode: ';
  echo '<select name="mode">';
  echo '<option value="normal"'.$normal.'>Normal</option>';
  echo '<option value="boolean"'.$boolean.'>Boolean</option>';
  echo '</select> ';
  echo '<input type="submit" value="Search" />';
  echo '</form>';
}

// Create the navigation switch
$cmd = (isset($_GET['cmd']) ? $_GET['cmd'] : '');

switch($cmd)
{
  default:
    searchForm();
 
  break;
 
 
  case "search":
    searchForm();
    echo '<h3>Search Results:</h3>';
   
    $searchstring = mysql_escape_string($_GET['words']);
    switch($_GET['mode'])
    {
      case "normal":
        $sql = "SELECT link_id, link_text, url, description, keywords, time_added, new_window, 
              MATCH(link_text, description, keywords) 
              AGAINST ('$searchstring') AS score FROM links 
              WHERE MATCH(link_text, description, keywords) 
              AGAINST ('$searchstring') ORDER BY score DESC";
      break;
     
      case "boolean":
        $sql = "SELECT link_id, link_text, url, description, keywords, time_added, new_window,
              MATCH(link_text, description, keywords) 
              AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM links 
              WHERE MATCH(link_text, description, keywords) 
              AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC";
      break;
    } 
   
    // echo $sql;
   
    $result = mysql_query($sql) or die (mysql_error());
   
    while($row = mysql_fetch_object($result))
    {

      echo '<hr /><br />';


if ($row['new_window'] == 1) {
echo '<b class="linkhead"><a href="'.$row->url.' target=\"_blank\">'.stripslashes(htmlspecialchars($row->link_text)).'</a></b>';
} else {
echo '<b class="linkhead"><a href="'.$row->url.'">'.stripslashes(htmlspecialchars($row->link_text)).'</a></b>';
}

      echo ' &nbsp; - &nbsp; Relevance: '.number_format($row->score, 1).'<br />';
      echo stripslashes(htmlspecialchars($row->description)).'<br />';
      echo '<a href="'.$row->url.'">'.$row->url.'</a>';
    }
  break;
}

echo '<hr /><br /></div>';
?>

<?
include("common/rightcol.php");
include("common/navigation.php");
include("common/footer.php");
?>[/code]

The bit of code that is supposed to load the link in an existing / new window is below:

[code]if ($row['new_window'] == 1) {
echo '<b class="linkhead"><a href="'.$row->url.' target=\"_blank\">'.stripslashes(htmlspecialchars($row->link_text)).'</a></b>';
} else {
echo '<b class="linkhead"><a href="'.$row->url.'">'.stripslashes(htmlspecialchars($row->link_text)).'</a></b>';
}
[/code]


Thanks,

John
Link to comment
Share on other sites

try this, may work

[code]
if ($row->new_window == "1") {
echo '<b class="linkhead"><a href="'.$row->url.' target=\"_blank\">'.stripslashes(htmlspecialchars($row->link_text)).'</a></b>';
} else {
echo '<b class="linkhead"><a href="'.$row->url.'">'.stripslashes(htmlspecialchars($row->link_text)).'</a></b>';
}
[/code]

Also, check the data in your database and check if it's the correct value.
Link to comment
Share on other sites

Thanks, but it still isn't working.  When I hover over a link the following shows up in the status bar:

http://www.google.co.uk/ target=/

It is definately the right value because I use load links on standard pages using it and it works fine.

Any ideas?

Thanks
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.