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?

Member Avatar for diafol
if(in_array($search_string,array($data[$k][0],$data[$k][1],$data[$k][2],$data[$k][3]))){
...
}

instead of the 'or's?

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.

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';
}

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?

** 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>

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...

typo on my part. change is_isset($result) to isset($result)

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.

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()

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.

Should the $results tag be $result?

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

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...

<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>

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.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.