Jump to content


Photo

Open Search Results In A New Window


  • Please log in to reply
4 replies to this topic

#1 john-formby

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

Posted 29 June 2006 - 08:07 PM

Hi,

I have a search on my website which returns results based on the keywords row in my database. The link_text and url are displayed as hyperlinks to other websites. There is also a new_window row in the database table which is used to determine whether a link will be opened in the current or a new window; 0 = current, 1 = new. The problem is that I am not sure how to get the links to open in a new window.


Here is the database table:

CREATE TABLE `links` (
  `link_id` int(11) NOT NULL auto_increment,
  `category_code` varchar(10) NOT NULL default '0',
  `link_text` varchar(254) NOT NULL default '',
  `url` varchar(254) NOT NULL default '',
  `description` text NOT NULL,
  `keywords` text NOT NULL,
  `time_added` timestamp(14) NOT NULL,
  `new_window` tinyint(4) NOT NULL default '0',
  PRIMARY KEY  (`link_id`)
) TYPE=MyISAM AUTO_INCREMENT=234;



Here is the search results processing and display:

<? 
//This is only displayed if they have submitted the form 
if ($searching =="yes") 
{ 

//If they did not enter a search term we give them an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

// Otherwise we connect to our Database 
mysql_connect("localhost", "USER", "PASS") or die(mysql_error()); 
mysql_select_db("pagelinks") or die(mysql_error()); 

// We preform a bit of filtering 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

//Now we search for our search term, in the field the user specified 
$data = mysql_query("SELECT * FROM links WHERE keywords LIKE '%$find%'"); 

//And we display the results 
while($result = mysql_fetch_array( $data )) 
{ 

echo "<hr /><br />";
echo "<b><a href=\"".$result['url']."\">".$result['link_text']."</a></b>"; 
echo "<br />"; 
echo $result['description']; 
echo "<br />"; 
echo "<a href=\"".$result['url']."\">".$result['url']."</a>"; 
echo "<br /><br />";  
} 

echo "<hr /><br />";

//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match your query<br><br>"; 
} 

//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find; 
} 
?>


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 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 29 June 2006 - 08:17 PM

Edit: I just read over your last post. Try the following for opening in self or new.

echo "<hr /><br />";
if ($data['new_window'] == 1) {
  echo "<b><a href=\"".$result['url']."\" target=\"_new\">".$result['link_text']."</a></b>"; 
} else {
  echo "<b><a href=\"".$result['url']."\">".$result['link_text']."</a></b>"; #assumption that 'new_window' == 0 and open in self..
}


#3 john-formby

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

Posted 29 June 2006 - 08:42 PM

I tried the code but it isn't working.
"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 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 29 June 2006 - 08:50 PM

what's not working john? have you viewed the outputted HTML to see if the HTML target information is being added to the hyperlinks? if not, then there's a problem with the my IF logic.

nm, found my problem:

echo "<hr /><br />";
if ($result['new_window'] == 1) {
  echo "<b><a href=\"".$result['url']."\" target=\"_new\">".$result['link_text']."</a></b>"; 
} else {
  echo "<b><a href=\"".$result['url']."\">".$result['link_text']."</a></b>"; #assumption that 'new_window' == 0 and open in self..
}

i had $data in place of $result and i'm assuming that the $data var is actually your SQL syntax.

#5 john-formby

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

Posted 29 June 2006 - 09:10 PM

Yes, its working great now.  Thanks again 
"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