john-formby Posted July 19, 2006 Share Posted July 19, 2006 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]<?phpinclude("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 ' - 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 https://forums.phpfreaks.com/topic/15065-search-result-links-need-to-open-in-new-window/ Share on other sites More sharing options...
akitchin Posted July 19, 2006 Share Posted July 19, 2006 problem is you're grabbing an object, not an array. try changing $row['new_window'] to $row->new_window. Link to comment https://forums.phpfreaks.com/topic/15065-search-result-links-need-to-open-in-new-window/#findComment-60591 Share on other sites More sharing options...
john-formby Posted July 19, 2006 Author Share Posted July 19, 2006 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 Link to comment https://forums.phpfreaks.com/topic/15065-search-result-links-need-to-open-in-new-window/#findComment-60597 Share on other sites More sharing options...
nogray Posted July 19, 2006 Share Posted July 19, 2006 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 https://forums.phpfreaks.com/topic/15065-search-result-links-need-to-open-in-new-window/#findComment-60613 Share on other sites More sharing options...
john-formby Posted July 19, 2006 Author Share Posted July 19, 2006 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 https://forums.phpfreaks.com/topic/15065-search-result-links-need-to-open-in-new-window/#findComment-60616 Share on other sites More sharing options...
akitchin Posted July 19, 2006 Share Posted July 19, 2006 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) Link to comment https://forums.phpfreaks.com/topic/15065-search-result-links-need-to-open-in-new-window/#findComment-60629 Share on other sites More sharing options...
john-formby Posted July 19, 2006 Author Share Posted July 19, 2006 Thanks, I knew it had to be something simple, I just couldn't find it. You are a legend ;DJohn Link to comment https://forums.phpfreaks.com/topic/15065-search-result-links-need-to-open-in-new-window/#findComment-60656 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.