Jump to content


Photo

Searchscript help please


  • Please log in to reply
4 replies to this topic

#1 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 07 July 2006 - 12:11 PM

Hi all,

Have a searchscript which seaches for stories in one table cms_stories

Now say my search returned 3 results:

- One Business Story
- One Sports Story
- One News Story

I want the <href> of the story to output to go to:

…/sport/story.php?story_id='.$row['story_id'] – For Sport results returned
…/business/story.php?story_id='.$row['story_id'] – For Business results returned
…/news/story.php?story_id='.$row['story_id'] – For News results returned

IE going to different folders depending on the section ie the nature of the story – be it a news/sport or business (which are defined as section in the db):

Now I’ve tried to do this (please see below), but it does not seem to work.  Can anyone please advise:

<?php 
/* ======== START FIRST FORM SEARCH ======== */ 

include("../*************************");

// Define Variable form 'First Query' post; 
$section = $_GET['section']; 
$searchstring = ($_GET['searchstring'] != "") ? $_GET['searchstring'] : false; 

if($searchstring === false){ 
die("No searchstring was entered!"); 
} 

$query=mysql_query("select *, DATE_FORMAT(appeared, '%W %e %M %Y') as formatted_date from cms_stories where section like '%$section%' AND ( headline LIKE '%$searchstring%' OR body_text LIKE '%$searchstring%' )") or die(mysql_error());  
echo "<table width=\"100%\" border=0 bgcolor=\"#cccccc\" cellpadding=1 cellspacing=1>\n"; 
echo "<tr> <th><DIV ALIGN=\"LEFT\">Type of Story</div></th>
<th><DIV ALIGN=\"LEFT\">Appeared in Newspaper</div></th> 
<th><DIV ALIGN=\"LEFT\">Headline</div></th> 
<th><DIV ALIGN=\"LEFT\">Read Story</div></th> </tr>";

    $link = ($row['section'] == 's')? 'news/': 'sport'/: 'business'/:''; // Define Folders
    $link .= 'story.php?story_id='.$row['story_id']; 

while($rows = mysql_fetch_assoc($query))
  {
  echo "<tr BGCOLOR=\"#FFFFFF\"><td>";
  echo $rows['section'];
  echo "</td><td>";
  echo $rows['formatted_date'];
  echo "</td><td>";
  echo $rows['headline'];
  echo "</td><td>";
  echo '<a href="' . $link . '">[ more ]</a>';  // Output the Story ID
  echo "</td></tr>";
  }
echo "</table>";

/* ======== END FIRST FORM SEARCH ======== */ 
?>




#2 semtex

semtex
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 07 July 2006 - 01:14 PM

Is there any field in your table cms_stories where you store the type of story that record identifies with?
If no, you should probably do that, then read the field and according to what's in there, change the link.

eg field="storytype"
if ($storytype == 'sports') {
    $link = "../sports/story.php?etc";
} else if ($storytype == 'news') {
    $link = "../news/story.php?etc";
}

the code is not really fitting your code, but i guess it explains what you need.

#3 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 07 July 2006 - 01:53 PM

This line

$link = ($row['section'] == 's')? 'news/': 'sport'/: 'business'/:''; // Define Folders

looks like your problem, for a start you've not defined the $row array anywhere, and you are trying to supply four options for the ternery operator, whereas the statement ($row["section"] == "s") will only return two values, true or false.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#4 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 07 July 2006 - 02:10 PM

Cheers Guys,

I've decided to go with semtex's idea, which seems easier to implement rather than they way I was going:

<?php 
/* ======== START FIRST FORM SEARCH ======== */ 

include("***********************************");

// Define Variable form 'First Query' post; 
$section = $_GET['section']; 
$searchstring = ($_GET['searchstring'] != "") ? $_GET['searchstring'] : false; 

if($searchstring === false){ 
die("No searchstring was entered!"); 
} 

if ($section == 'news') {
    $link = "../news/story.php?story_id='.$row['story_id']";
} else if ($storytype == 'sport') {
    $link = "../sport/story.php?story_id='.$row['story_id']";
} else if ($storytype == 'business') {
    $link = "../business/story.php?story_id='.$row['story_id']";
}

$query=mysql_query("select *, DATE_FORMAT(appeared, '%W %e %M %Y') as formatted_date from cms_stories where section like '%$section%' AND ( headline LIKE '%$searchstring%' OR body_text LIKE '%$searchstring%' )") or die(mysql_error());  
echo "<table width=\"100%\" border=0 bgcolor=\"#cccccc\" cellpadding=1 cellspacing=1>\n"; 
echo "<tr> <th><DIV ALIGN=\"LEFT\">Type of Story</div></th>
<th><DIV ALIGN=\"LEFT\">Appeared in Newspaper</div></th> 
<th><DIV ALIGN=\"LEFT\">Headline</div></th> 
<th><DIV ALIGN=\"LEFT\">Read Story</div></th> </tr>";

while($rows = mysql_fetch_assoc($query))
  {
  echo "<tr BGCOLOR=\"#FFFFFF\"><td>";
  echo $rows['section'];
  echo "</td><td>";
  echo $rows['formatted_date'];
  echo "</td><td>";
  echo $rows['headline'];
  echo "</td><td>";
  echo '<a href="' . $link . '">[ more ]</a>'; 
  echo "</td></tr>";
  }
echo "</table>";

/* ======== END FIRST FORM SEARCH ======== */ 
?> 

But I get an error on this line:

   $link = "../news/story.php?story_id='.$row['story_id']";

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /***********/search/results.php on line 15

Any ideas please?

Thanks

Chris

#5 Crimpage

Crimpage
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts
  • LocationBrisbane, Australia

Posted 07 July 2006 - 02:20 PM

Just make it:

$link = "../news/story.php?story_id=" . $row['story_id'];

And do for the other $link rows as well...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users