0

Hey, I'm trying to make a form of a search engine for my website. I have permission from my teacher to do it for class even though its for personal use. I have part of it built but I'm stuck at the "proper translation of the file" part. Heres the primary file's code:

<html>
<head>
<title>Search Engine Test</title>
</head>

<body>
<form name="search" method="POST" action="search.php">
<input type="text" name="search_string" value="">
<input type="submit" name="submit" value="search"
</form>
</body>
</html>

This is the part that I don't have any problems with. Thent heres the function of my script that I'm having the problem with:

<html>
<body>
<?php
$search_text = file("search_text.txt");
$search_string = $_POST['search_string'];

foreach($search_text as $key => $val) 
{ 
   $data[$key] = explode("||", $val);
}

echo 'Search Results for "'.$search_string.'"';
echo "<br><br>";

for($k = 0; $k < sizeof($search_text); $k++) 
{
if ($search_string == $data[$k][0] or $data[$k][1] or $data[$k][2] or $data[$k][3])
{
echo '<a href="'.$data[$k][5].'">'.$data[$k][4].'</a>';
echo '<br><br>';
}
else
{
echo "<br><br><br>";
echo "Your search returned 0 results. Please go back and try again.";
}
}
?>
</body>
</html>

What I'm trying to do is make it read this file for the search info:

toy||red||truck||large||Big Red Truck||bigredtruck.html
pie||apple||truck||small||Apple Pie Truck||applepietruck.html
dog||brown||small||annoying||Annoying Brown Dog||browndog.html
cat||tan||large||annoying||Annoying Tan Cat||tancat.html

(The links don't really matter they are just references for the script, I'm going to change them later.)

When it reads the file its suppose to explode the || and label each part as a tag (parts 0-3 are tags, part 4 is the title, and part 5 is the link to go to. My problem is that when it reads the file if it finds even one matching tag it shows everything. This is where I'm stuck. I want it to only show the title/link combo that has the matching tag. Can someone help me or at least tell me if this is possible?

3
Contributors
16
Replies
17
Views
7 Years
Discussion Span
Last Post by Dragennd
0
if(in_array($search_string,array($data[$k][0],$data[$k][1],$data[$k][2],$data[$k][3]))){
...
}

instead of the 'or's?

0

Ok, sweet that worked now I got one other problem that I got no clue how to fix. When I type in something that only has tags for say 2 of them it returnes saying "Your search returned 0 results. Please go back and try again." for each of the ones that didn't match the tags. Any ideas on that one, I can post a screenshot if necessary.

0

Your problem is in the IF/ELSE conditional.

Because it is doing a search for each line in the file, it is generating output for each line in the file. Instead of outputting the result or failure in the for loop, assign the result to an array like: $result[] = 'link and title'; Then eliminate the ELSE part of the conditional all together.

after the for loop add a conditional check like

if(isset($result) && is_array($result)){
  echo implode('<br />', $result);
} else {
  echo 'Search returned 0 results';
}
0

I don't understand how this works... What part of my code would work as the result and be able to make the code check against the tags to see if any of them matched?

0

** With ardav's suggestion **

if (in_array($search_string,array($data[$k][0],$data[$k][1],$data[$k][2],$data[$k][3])))
{
echo '<a href="'.$data[$k][5].'">'.$data[$k][4].'</a>';
echo '<br><br>';
}
else
{
echo "<br><br><br>";
echo "Your search returned 0 results. Please go back and try again.";
}

Change it to something like this:

if (in_array($search_string,array($data[$k][0],$data[$k][1],$data[$k][2],$data[$k][3])))
{
$results[] = '<a href="'.$data[$k][5].'">'.$data[$k][4].'</a><br><br>';
}

After the for loop:

if( is_isset($result) && is_array($result) ){
  foreach($result as $page){
    echo $page;
  }
}

resulting in:

<html>
<body>
<?php
$search_text = file("search_text.txt");
$search_string = $_POST['search_string'];
 
foreach($search_text as $key => $val) 
{ 
   $data[$key] = explode("||", $val);
}
 
echo 'Search Results for "'.$search_string.'"';
echo "<br><br>";
 
for($k = 0; $k < sizeof($search_text); $k++){
    if (in_array($search_string,array($data[$k][0],$data[$k][1],$data[$k][2],$data[$k][3]))){
        $results[] = '<a href="'.$data[$k][5].'">'.$data[$k][4].'</a><br><br>';
    }
}

if( is_isset($result) && is_array($result) ){
    foreach($result as $page){
      echo $page;
    }
}
?>
</body>
</html>
0

Ok the code sorta worked. When I put the code into my script it gave me an error that I have not seen before (that or it was the same error but worded differently).

The page looked like this:

Search Results for "dog"


Fatal error: Call to undefined function is_isset() in C:\xampp\htdocs\xampp\PHP\PHP Search Engine\search.php on line 21

If you have any ideas as to why this happened please let me know mschroeder. In the mean time I am going to try to figure this out...

Edited by Dragennd: n/a

0

Now the error is gone but it still won't say the results, dog is one of the tags and it didnt post any results for the matched tag.

0

I just tried multiple different tags, I wonder if its simply not reading the file at all...

I'm an idiot XD... I forgot to change the is_array() tag.
I changed it and got a new message :).

Warning: Invalid argument supplied for foreach()

Edited by Dragennd: n/a

0

Again a simple typo. There is a reason my signature says the code may not be tested. Both of these errors were well within your ability to find and correct.

Change $results[] to $result[] on line 17.

0

Should the $results tag be $result?

$results[] = '<a href="'.$data[$k][5].'">'.$data[$k][4].'</a><br><br>';

0

lol just changed it and still dont work, I changed it earlier and figure that much out but didn't know if php could compensate...

0
<html>
<body>
<?php
$search_text = file("search_text.txt");
$search_string = $_POST['search_string'];
 
foreach($search_text as $key => $val) 
{ 
   $data[$key] = explode("||", $val);
}
 
echo 'Search Results for "'.$search_string.'"';
echo "<br><br>";
 
for($k = 0; $k < sizeof($search_text); $k++){
    if (in_array($search_string,array($data[$k][0],$data[$k][1],$data[$k][2],$data[$k][3]))){
        $result[] = '<a href="'.$data[$k][5].'">'.$data[$k][4].'</a><br><br>';
    }
}

if( isset($result) && is_array($result) ){
    foreach($result as $page){
      echo $page;
    }
}
?>
</body>
</html>
0

Ok last thing that it doesn't do is it won't post "Your search found no results, please go back and try again." when there is no results.

0
if( isset($result) && is_array($result) ){
    foreach($result as $page){
      echo $page;
    }
}

that is doing several things in that IF statement. It is making sure the variable $result exsists. It is also making sure that we actually have an array of results.

Currently there is no statement for it execute in the "else" condition. Add something like:

else {
  echo 'No Results';
}

immediately after the closing } of the if statement.

0

Ok cool, it finally works lol. Thanks for all the help man (php is kinda confusing but I think I'm getting it.)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.