Jump to content

Archived

This topic is now archived and is closed to further replies.

john-formby

Search result links need to open in new window

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

Share this post


Link to post
Share on other sites
problem is you're grabbing an object, not an array.  try changing $row['new_window'] to $row->new_window.

Share this post


Link to post
Share on other sites
Hi,

Thanks for your reply.  That was one of the first things I tried but it still didn't work?  I have no idea how to make this work.

John

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
missing a closing quote:

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

(after href)

Share this post


Link to post
Share on other sites
Thanks, I knew it had to be something simple, I just couldn't find it.  You are a legend ;D

John

Share this post


Link to post
Share on other sites

×

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.