Jump to content


Photo

Search result links need to open in new window


  • Please log in to reply
6 replies to this topic

#1 john-formby

john-formby
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationMahe, Seychelles

Posted 19 July 2006 - 06:54 PM

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.

<?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");
?>

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

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>';
}


Thanks,

John

"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." ~ Stephen Hawking

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 19 July 2006 - 06:56 PM

problem is you're grabbing an object, not an array.  try changing $row['new_window'] to $row->new_window.

#3 john-formby

john-formby
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationMahe, Seychelles

Posted 19 July 2006 - 07:15 PM

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
"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." ~ Stephen Hawking

#4 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 19 July 2006 - 07:48 PM

try this, may work

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>';
}

Also, check the data in your database and check if it's the correct value.

NoGray.com


#5 john-formby

john-formby
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationMahe, Seychelles

Posted 19 July 2006 - 07:55 PM

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
"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." ~ Stephen Hawking

#6 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 19 July 2006 - 08:26 PM

missing a closing quote:

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

(after href)

#7 john-formby

john-formby
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationMahe, Seychelles

Posted 19 July 2006 - 09:10 PM

Thanks, I knew it had to be something simple, I just couldn't find it.  You are a legend ;D

John
"I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image." ~ Stephen Hawking




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users