Jump to content

Search by Genre form tutorial?


Graxeon

Recommended Posts

Can someone point me to where I can make something like this search form: http://www.animenewsnetwork.com/encyclopedia/search/genre

 

However, the only part I want to know from that is how to use the stuff under "Genres:" where it says "action, adventure, comedy, drama, etc."

 

I can't use a database, though (besides...I won't have many things to search for). I can use PHP, though.

 

So from that page's source I just need this left over:

 

<form action="genreresults" method="get">

<h5>Genres:</h5>
<input id="tristate1in"  name="g" type="checkbox" value="action/A"        /><input id="tristate1ex"  name="g" style="display:none" type="checkbox" value="-action/A"        />action  
<input id="tristate2in"  name="g" type="checkbox" value="adventure/A"     /><input id="tristate2ex"  name="g" style="display:none" type="checkbox" value="-adventure/A"     />adventure<br />
<input id="tristate3in"  name="g" type="checkbox" value="comedy"          /><input id="tristate3ex"  name="g" style="display:none" type="checkbox" value="-comedy"          />comedy<br />
<input id="tristate4in"  name="g" type="checkbox" value="drama/D"         /><input id="tristate4ex"  name="g" style="display:none" type="checkbox" value="-drama/D"         />drama  

<input id="tristate7in"  name="g" type="checkbox" value="fantasy/F"       /><input id="tristate7ex"  name="g" style="display:none" type="checkbox" value="-fantasy/F"       />fantasy  
<input id="tristate8in"  name="g" type="checkbox" value="magic/F"         /><input id="tristate8ex"  name="g" style="display:none" type="checkbox" value="-magic/F"         />magic  
<input id="tristate9in"  name="g" type="checkbox" value="supernatural/F"  /><input id="tristate9ex"  name="g" style="display:none" type="checkbox" value="-supernatural/F"  />supernatural<br />
<input id="tristate10in" name="g" type="checkbox" value="horror"          /><input id="tristate10ex" name="g" style="display:none" type="checkbox" value="-horror"          />horror<br />
<input id="tristate11in" name="g" type="checkbox" value="mystery"         /><input id="tristate11ex" name="g" style="display:none" type="checkbox" value="-mystery"         />mystery<br />
<input id="tristate12in" name="g" type="checkbox" value="psychological"   /><input id="tristate12ex" name="g" style="display:none" type="checkbox" value="-psychological"   />psychological<br />
<input id="tristate13in" name="g" type="checkbox" value="romance"         /><input id="tristate13ex" name="g" style="display:none" type="checkbox" value="-romance"         />romance<br />
<input id="tristate14in" name="g" type="checkbox" value="science fiction" /><input id="tristate14ex" name="g" style="display:none" type="checkbox" value="-science fiction" />science fiction<br />

<input id="tristate15in" name="g" type="checkbox" value="thriller"        /><input id="tristate15ex" name="g" style="display:none" type="checkbox" value="-thriller"        />thriller<br />


<p>
<input type="submit" value="Search">
<input type="reset" value="Reset">
</p>
</form>

Link to comment
Share on other sites

Ok well I played with it a little. I made 2 files: popup.php and results.php

 

It works only for 1 genre, though. If I choose more than 1 of the genres then it only displays the last "g" input.

 

popup.php

<form action="results.php" method="get">

<h5>Genres:</h5>
<input id="1in"  name="g" type="checkbox" value="action"        /><input id="1ex"  name="g" style="display:none" type="checkbox" value="-action"        />action <br />
<input id="2in"  name="g" type="checkbox" value="adventure"     /><input id="2ex"  name="g" style="display:none" type="checkbox" value="-adventure"     />adventure<br />
<input id="3in"  name="g" type="checkbox" value="comedy"          /><input id="3ex"  name="g" style="display:none" type="checkbox" value="-comedy"          />comedy<br />
<input id="4in"  name="g" type="checkbox" value="drama"         /><input id="4ex"  name="g" style="display:none" type="checkbox" value="-drama"         />drama<br />
<input id="7in"  name="g" type="checkbox" value="fantasy"       /><input id="7ex"  name="g" style="display:none" type="checkbox" value="-fantasy"       />fantasy<br />
<input id="8in"  name="g" type="checkbox" value="magic"         /><input id="8ex"  name="g" style="display:none" type="checkbox" value="-magic"         />magic<br />
<input id="9in"  name="g" type="checkbox" value="supernatural"  /><input id="9ex"  name="g" style="display:none" type="checkbox" value="-supernatural"  />supernatural<br />
<input id="10in" name="g" type="checkbox" value="horror"          /><input id="10ex" name="g" style="display:none" type="checkbox" value="-horror"          />horror<br />
<input id="11in" name="g" type="checkbox" value="mystery"         /><input id="11ex" name="g" style="display:none" type="checkbox" value="-mystery"         />mystery<br />
<input id="12in" name="g" type="checkbox" value="psychological"   /><input id="12ex" name="g" style="display:none" type="checkbox" value="-psychological"   />psychological<br />
<input id="13in" name="g" type="checkbox" value="romance"         /><input id="13ex" name="g" style="display:none" type="checkbox" value="-romance"         />romance<br />
<input id="14in" name="g" type="checkbox" value="science fiction" /><input id="14ex" name="g" style="display:none" type="checkbox" value="-science fiction" />science fiction<br />
<input id="15in" name="g" type="checkbox" value="thriller"        /><input id="15ex" name="g" style="display:none" type="checkbox" value="-thriller"        />thriller<br />


<p>
<input type="submit" value="Search">
<input type="reset" value="Reset">
</p>
<form>

 

results.php

<?php

$action1 = array("Action Title1",
                "Action Title2");
$adventure1 = array("Adventure Title",
                "Adventure Title2");
$comedy1 = array("Comedy Title",
                "Comedy Title2");



$genre = $_GET['g'] ;
$f = '1';
$var = $genre . $f;
$locations = $$var;

  $xml .= 'Search results for: ' . $genre . PHP_EOL;
  $xml .= '<br><br>' . PHP_EOL;
foreach($locations as $location)
{

  $xml .= $location . PHP_EOL;
  $xml .= '<br>' . PHP_EOL;

}

  $xml .= '<br>' . PHP_EOL;
  $xml .= 'Now go back to the site and select one of these titles!' . PHP_EOL;
  $xml .= "<br><br>" . PHP_EOL;
  $xml .= '...or search again' . PHP_EOL;

print $xml;

?>

 

I don't know how to tell the script that there are different values of "g". Can you guys help me fix up this code so if there's more than 1 genre chosen then it displays all of the genres' "Title"s requested?

Link to comment
Share on other sites

hope this helps can you tell how you want your expected results are that should give a clearer idea

 

<?php

$action1 = array("Action Title1",
                "Action Title2");
$adventure1 = array("Adventure Title",
                "Adventure Title2");
$comedy1 = array("Comedy Title",
                "Comedy Title2");


$xml = '';
foreach ($_GET['g'] as $genre) {


  $xml .= '<br />Search results for: ' . $genre . '</annotation>' . PHP_EOL;
  $xml .= '<br><br>' . PHP_EOL;

  $xml .=  implode('<br />', ${$genre.'1'});
}


  $xml .= '<br /><br>' . PHP_EOL;
  $xml .= 'Now go back to the site and select one of these titles!' . PHP_EOL;
  $xml .= "<br><br>" . PHP_EOL;
  $xml .= '...or search again' . PHP_EOL;

print $xml;

?>

Link to comment
Share on other sites

That gave this error:

 

Invalid argument supplied for foreach() in line 12 which is "foreach ($_GET['g'] as $genre) {"

 

And the final result for more than 1 genre would be say we chose "action" and "comedy":

 

Search results for: action, comedy

Action Title1
Action Title2
Comedy Title1
Comedy Title2

Now go back to the site and select one of these titles!

...or search again 

Link to comment
Share on other sites

your html should be the following notice the g[] for the names this allows multiple checkboxes with same name to come as an array in php

 

<form action="result.php" method="get">

<h5>Genres:</h5>
<input id="1in"  name="g[]" type="checkbox" value="action"        /><input id="1ex"  name="g[]" style="display:none" type="checkbox" value="-action"        />action <br />
<input id="2in"  name="g[]" type="checkbox" value="adventure"     /><input id="2ex"  name="g[]" style="display:none" type="checkbox" value="-adventure"     />adventure<br />
<input id="3in"  name="g[]" type="checkbox" value="comedy"          /><input id="3ex"  name="g[]" style="display:none" type="checkbox" value="-comedy"          />comedy<br />
<input id="4in"  name="g[]" type="checkbox" value="drama"         /><input id="4ex"  name="g[]" style="display:none" type="checkbox" value="-drama"         />drama<br />
<input id="7in"  name="g[]" type="checkbox" value="fantasy"       /><input id="7ex"  name="g[]" style="display:none" type="checkbox" value="-fantasy"       />fantasy<br />
<input id="8in"  name="g[]" type="checkbox" value="magic"         /><input id="8ex"  name="g[]" style="display:none" type="checkbox" value="-magic"         />magic<br />
<input id="9in"  name="g[]" type="checkbox" value="supernatural"  /><input id="9ex"  name="g[]" style="display:none" type="checkbox" value="-supernatural"  />supernatural<br />
<input id="10in" name="g[]" type="checkbox" value="horror"          /><input id="10ex" name="g[]" style="display:none" type="checkbox" value="-horror"          />horror<br />
<input id="11in" name="g[]" type="checkbox" value="mystery"         /><input id="11ex" name="g[]" style="display:none" type="checkbox" value="-mystery"         />mystery<br />
<input id="12in" name="g[]" type="checkbox" value="psychological"   /><input id="12ex" name="g[]" style="display:none" type="checkbox" value="-psychological"   />psychological<br />
<input id="13in" name="g[]" type="checkbox" value="romance"         /><input id="13ex" name="g[]" style="display:none" type="checkbox" value="-romance"         />romance<br />
<input id="14in" name="g[]" type="checkbox" value="science fiction" /><input id="14ex" name="g[]" style="display:none" type="checkbox" value="-science fiction" />science fiction<br />
<input id="15in" name="g[]" type="checkbox" value="thriller"        /><input id="15ex" name="g[]" style="display:none" type="checkbox" value="-thriller"        />thriller<br />


<p>
<input type="submit" value="Search">
<input type="reset" value="Reset">
</p>
<form>

 

and your php should be the following, you'll have to insert some checks when nothing is selected

 

<?php

$action1 = array("Action Title1",
                "Action Title2");
$adventure1 = array("Adventure Title",
                "Adventure Title2");
$comedy1 = array("Comedy Title",
                "Comedy Title2");


$xml = '';
$xml .= '<br />Search results for: ' . implode(',', $_GET['g']) . '</annotation>' . PHP_EOL;
$xml .= '<br><br>' . PHP_EOL;

foreach ($_GET['g'] as $genre) {
  $xml .=  implode('<br />', ${$genre.'1'}) . '<br />';
}


  $xml .= '<br /><br>' . PHP_EOL;
  $xml .= 'Now go back to the site and select one of these titles!' . PHP_EOL;
  $xml .= "<br><br>" . PHP_EOL;
  $xml .= '...or search again' . PHP_EOL;

print $xml;

?>

 

hope its helpful

Link to comment
Share on other sites

Hey...is there any way of omitting duplicate "Titles"? Because some "Titles" have more than 1 genre so they'll show up in both. If I put them in just 1 genre...then the search misses it.

 

So is there a way of saying "if any duplicates in all arrays, remove all but 1"?

 

For example, I may have this:

 

$action1 = array("The Boat",
                "Girl");
$adventure1 = array("Country",
                "The Boat");

 

So if I search for "action" and "adventure" I'll get "The Boat" coming up twice. I only need it to come up once.

Link to comment
Share on other sites

your code should be something like this

 

<?php

$action1 = array("Action Title1",
                "Action Title2");
$adventure1 = array("Action Title1",
                "Adventure Title2");
$comedy1 = array("Action Title1",
                "Comedy Title2");


$xml = '';
$xml .= '<br />Search results for: ' . implode(',', $_GET['g']) . '</annotation>' . PHP_EOL;
$xml .= '<br><br>' . PHP_EOL;
$titles = array();
foreach ($_GET['g'] as $genre) {
  $titles = array_merge($titles, ${$genre.'1'});
}
$xml .= implode('<br />', array_unique($titles)) . '<br />';

  $xml .= '<br /><br>' . PHP_EOL;
  $xml .= 'Now go back to the site and select one of these titles!' . PHP_EOL;
  $xml .= "<br><br>" . PHP_EOL;
  $xml .= '...or search again' . PHP_EOL;

print $xml;

?>

Link to comment
Share on other sites

Hey, sorry for bugging you...but can you help me change 1 last thing?

 

The last script you posted works perfectly...but I rethought how it should handle an input. Instead of posting ALL of the stuff in the arrays for ALL of the genre, can you make it so it only posts the stuff within the arrays that show up in multiple genres?

 

For example, if these were the genres:

 

$action1 = array("Man",
                "Boy",
                "Girl");
$adventure1 = array("Female",
                "Man",
                "Boy");

 

and if the selection in the search form was "action" and "adventure", then it should output this:

 

Search results for: action, adventure

Boy
Man

Now go back to the site and select one of these titles!

...or search again 

 

This is what your last script would produce with those selections:

 

Search results for: action, adventure

Man
Female
Girl
Boy

Now go back to the site and select one of these titles!

...or search again 

 

 

The reason I'm asking for this change is because what it's currently searching is too broad.

Link to comment
Share on other sites

you can try this, its a bit complex you can break it up into a function or something for better understanding

 

<?php
$action1 = array("Man",
                "Boy",
                "Girl");
$adventure1 = array("Female",
                "Man",
                "Boy");

$comedy1 = array("Boy");

$xml = '';
$xml .= '<br />Search results for: ' . implode(',', $_GET['g']) . '</annotation>' . PHP_EOL;
$xml .= '<br><br>' . PHP_EOL;
$titles = array();
foreach ($_GET['g'] as $genre) {
  $titles = array_merge($titles, ${$genre.'1'});
}

//$xml .= implode('<br />', array_unique($titles)) . '<br />';
$xml .= implode('<br />', array_keys(array_filter(array_count_values($titles), create_function('$a','return ($a === count($_GET[\'g\'])) ? $a : null;')))) . '<br />';

  $xml .= '<br /><br>' . PHP_EOL;
  $xml .= 'Now go back to the site and select one of these titles!' . PHP_EOL;
  $xml .= "<br><br>" . PHP_EOL;
  $xml .= '...or search again' . PHP_EOL;

print $xml;

?>

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.