hi, I have a table of movies and it has nearly 1000 entries.
CREATE TABLE movies (
movie_id INT,
movie_name VARCHAR(255),
type_id INT,
movie_region CHAR(3),
release_date DATE
) DEFAULT CHARACTER SET utf8’;
and I want to be able to display them (depending on what pre-search criteria they select such as genre) as options for the user to select in a html form such as this:
nah that’s kinda all I have: table full of movies and the form. just after the basics to point me in the right direction, so I can figure it out. like, do you just echo it somehow?
it all works fine but I’m just wondering, how do I get the results in the last form to be based off the selections in the first 2 forms? for example if I select a different region and/or genre, the list of movies in the final form would be different.
I’m guessing I need to define region and genre somehow so that I can use them in the ‘SELECT movie_name FROM movies’ query - but how? This is driving me nuts, been trying all day.
This part of your code creates the $movies array. It doesn’t do anything with the region or genre data.
You need to build the SQL query dynamically, so that it will filter the results based on existing POST data
$result = mysqli_query($link, "SELECT movie_name FROM movies");
if (!$result)
{
$error = 'Error fetching info from database!';
include 'error.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$movies[] = array('movie_name' => $row['movie_name']);
}
Try modifying it like this:
$query = "SELECT movie_name FROM movies WHERE 1=1 "; //note trailing space
//Region
if(isset($_POST['region']) AND in_array($_POST['region'], array('NA', 'JP', 'AU'))) {
$query .= "AND movie_region='{$_POST['region']}' ";
}
//Debug
# echo $query;
$result = mysqli_query($link, $query);
if(!$result) {
// ....
}
So now $query is a string, by default it will select ALL movies. If a region was selected (and is a safe known value) the query will change
I’m not sure how to do the same with genre because I can’t see where your DB identifies a genre for each movie. Is that what type_id is for?
oh awesome thank you, I really just had no idea how to do that (still learning heh) - makes sense to me now I think. will play some. one part I don’t understand - what does 1=1 mean?
and for genres, I have the genre table with ‘id’ and ‘genre_name’
in my movie table I have ‘genre_id’ to link to that table
this is what I changed it to, but it’s loading the full list of movies and then not changing when I select different regions…
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/connect.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/functions.php';
$query = "SELECT movie_name FROM movies WHERE 1=1 ";
if(isset($_POST['region']) AND in_array($_POST['region'], array('US', 'JP', 'AU'))) {
$query .= "AND movie_region='{$_POST['region']}' ";
}
$result = mysqli_query($link, $query);
if (!$result)
{
$error = 'Error fetching info from database!';
include 'error.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$movies[] = array('movie_name' => $row['movie_name']);
}
?>
all the forms are the same as they were, underneath that PHP code there. not sure what I’m doing wrong. probably something to do with the order of things… hm.
I’m guessing, maybe, that upon selecting a different region, the list would have to re-list itself somehow, because as it is now it’s loading all the movies and leaving it at that, regardless of what I select afterwoods… due to the code already being finished… I think? hehe
1=1 is a WHERE condition that will always be true (1 is always equal to 1). The reason I included it was so that the query would not break when there is no region or genre selected.
It’s a bit like a placeholder so the query always has a WHERE clause. If the region is specified the “AND movie_region…” part gets added.
If nothing is added the query will still work, and select all rows.
The code below it can add additional parts to the WHERE clause without knowing if there were any others before it.
it’s loading the full list of movies and then not changing when I select different regions
Selecting a radio button doesn’t affect PHP at all. You need to SUBMIT the form (i.e POST it back to the server) for the $_POST varialbes to exist, and your movie list to get limited.
You’ll probably need region and genre to share a form. When that form is submitted you build the final one with a list of relevant movies.
As for the genres you need to three things:
Select the ID as well as the genre name from your database to build the <select> field
Use that ID as the <option> value (e.g <option value=“6”>Horror</option>)
use zero as the value if you want to have an ‘Any Genre’ <option>
Update the code that builds $query in a similar way, so that if there is a non-zero genre value you limit by it.
For example:
$query = "SELECT movie_name FROM movies WHERE 1=1 ";
//Region
if(isset($_POST['region']) AND in_array($_POST['region'], array('US', 'JP', 'AU'))) {
$query .= "AND movie_region='{$_POST['region']}' ";
}
//Genre
if(isset($_POST['genre']) AND $_POST['genre'] != 0) {
$query .= "AND genre_id = " . (int)$_POST['genre'] . ' ';
}
This is why step #2 above is important. $_POST[‘genre’] needs to be the genre ID, not the text name.
Thanks for your reply, it’s making a lot of sense to me now.
I’m playing around with it and have gotten it working to the point where each form is working individually (region form, genre form) to refresh the movie list, I’m just not sure how to implement region AND genre in the same form… so that I can update the movie list with BOTH choices and not just one or the other. so trying to figure that out… then I think I’m set.
now the only thing to do with this that I’m wondering about is the fact that when I submit that form, yes it refreshes the movie list appropriately, awesome - but the options selected are cleared when the page is reloaded after submitting the form. does anyone know if there any way to have the form show the options that were selected after the form is submitted? hope that makes sense. lol
oknow - to preselect a radio it needs a checked=“checked” attribute and for the <option> tags it’s selected=“selected”.
Also, there is no point having one label refer to all 3 radios. That really defeats the purpose and is bad for accessibility. Each radio should have its own label tag that refers to the radio ID
Similarly the label for genre points to ‘platform’ which doesn’t exist.
Do the same with region. If you make an array of regions to loop over your code will be a bit cleaner as you won’t have to do the if/else multiple times for each hard-coded region.
edit: solved that myself, had to update a database column
thanks very much for the help, going to work on adding the ‘remember selection’ stuff now, seeing the code it makes sense to me. really, really appreciate the help!
one more step to learn after this and my practice site is about done.