Jump to content

Searchscript help please


Mr Chris

Recommended Posts

Hi all,

Have a searchscript which seaches for stories in one table [b]cms_stories[/b]

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'] – [B]For Sport results returned[/b]
…/business/story.php?story_id='.$row['story_id'] – [B]For Business results returned[/b]
…/news/story.php?story_id='.$row['story_id'] – [B]For News results returned[/b]

IE going to different folders depending on the [b]section[/b] 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:

[code]
<?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 ======== */
?>
[/code]


Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This line

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

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.
Link to comment
Share on other sites

Cheers Guys,

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

[code]
<?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 ======== */
?>
[/code]

But I get an error on this line:

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

[b] 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[/b]

Any ideas please?

Thanks

Chris
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.