Jump to content

[SOLVED] Search Function Error...


xmanofsteel69

Recommended Posts

I can't seem to figure this out.  I've never used preg_match before and it shows.  Can anybody help me out here!  Here's my code:

 

<?php


$Movie = file_get_contents('Movies.txt');
parse_str($Movie);

function title_cmp($a, $b)
{
$a_cmp = preg_replace('/^(A|An|The) /', '', $a['Title']);
$b_cmp = preg_replace('/^(A|An|The) /', '', $b['Title']);

return strcmp($a_cmp, $b_cmp);
}



usort($Movie, 'title_cmp');

if($_POST['search'] =! '')
{
	$search = $_POST['search'];

	foreach($Movie as $key =>$val)
	{
		if(preg_match($search, $Movie[$key][Title]));
		{
			echo $Movie[$key][Title].'<br>';
		}
	}

}

?>

 

There's a form above it that just catches 'search' with 'post', so I didn't think it was important.

 

I keep getting this error for everything I have in there:

 

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in E:\Tyson\wamp\www\jedit\Test1\Search.php on line 49

A

 

Where you see 'A', that's where the value is.  This is what my array looks like.

 

Movie[1][Title]=Troy&Movie[1][Genre]=Action / Drama / History /

 

Romance&Movie[2][Title]=Mr. and Mrs. Smith&Movie[2][Genre]=Action /

 

Comedy / Romance / Thriller

 

And so on.  Any clues on how to fix this?

Link to comment
https://forums.phpfreaks.com/topic/40064-solved-search-function-error/
Share on other sites

Changing the relevant line to the following should correct the error.

 

if (preg_match('/'.preg_quote($search, '/').'/i', $Movie[$key][Title]));
{
echo $Movie[$key][Title].'<br>';
}

 

If you're only testing whether the movie string contains the search string, you should use strpos instead.

Unfortunately that's not working =(...It's not displaying ALL values, not just the one.

 

The post was to correct the error you posted nothing else.

 

The only other error that I notice is this line

if($_POST['search'] =! '')

 

it should be "!=" not "=!".

 

If you're still having problems put the following at the top of the script.

 

error_reporting(E_ALL);
ini_set('display_errors', '1');

 

Explain the problem again and post the code you're currently using. What isn't it showing that it should?

What it is suppose to do is search through the array and display the lines that have the $search within it.

 

Say $search = 'Troy'

 

It will display only the values that contain 'Troy'

 

What it is current;y doing is displaying all values.  Here's my code.

 

<div style='display:none;'>
<html>
<head>
<title>The Movie List</title>
</head>
<body>
</div>
<center>
<img src="movielister.jpg"><br>
<img name="image" src="home.jpg" width="650" height="60" border="0" id="image" usemap="#m_image" alt="" /><map name="m_image" id="m_image">
<area shape="rect" coords="480,18,630,50" href="http://tjjam4.farvista.net/Suggest.php" alt="" />
<area shape="rect" coords="360,18,447,42" href="http://tjjam4.farvista.net/Search.php" alt="" />
<area shape="rect" coords="129,18,327,42" href="http://tjjam4.farvista.net/The_Movie_List.php" alt="" />
<area shape="rect" coords="17,18,97,42" href="http://tjjam4.farvista.net/Home.php" alt="" />
</map>
</center>
<form action="Search.php" method="post">
<table border="0" bgcolor=white cellspacing="5">
<tr><td><font color=red>Search</font></td><td><input type="text" size="60" name="search"></td></tr>
<tr><td> </td><td><input type="submit" value="Send"><font face="arial" size="1">  Click Send To Continue</font></td></tr>
</table>
</form>
<font color = red>

<h1>Under Construction</h1>

<?php


$Movie = file_get_contents('Movies.txt');
parse_str($Movie);

function title_cmp($a, $b)
{
$a_cmp = preg_replace('/^(A|An|The) /', '', $a['Title']);
$b_cmp = preg_replace('/^(A|An|The) /', '', $b['Title']);

return strcmp($a_cmp, $b_cmp);
}



usort($Movie, 'title_cmp');

if($_POST['search'] != '')
{
	$search = $_POST['search'];
	foreach($Movie as $key =>$val)
	{
		if (preg_match('/'.preg_quote($search, '/').'/i', $Movie[$key][Title]));
		{
			echo $Movie[$key][Title].'<br>';
		}
	}
}

?>

Archived

This topic is now archived and is closed to further replies.

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