0

I have 4 different MySQL query's and I want to be able to choose which one is used based on which radio button is clicked. I was wondering what would be the best way to do this?

<form action="">
<input type="radio" name="query1" value="">Rating<br>
<input type="radio" name="query2" value="">Genre<br>
<input type="radio" name="query3" value="">Year<br>
<input type="radio" name="query4" value="">Rating,Genre,Year
</form>

Do I store the query's on individual php pages and then call them using the radio button or ....?

The query's select movie info from a database and then order them by either rating, year, genre or all three.

10
  • 2
    You can use isset() with functions, and do it all inside one page if you want. Commented Apr 7, 2015 at 20:10
  • Gee, should I have posted that as an answer? I did get an upvote ^ "No Fred, not without a graphical representation, you silly Sam." Commented Apr 7, 2015 at 20:12
  • By the way, you're working too hard with those radio buttons. Commented Apr 7, 2015 at 20:15
  • 1
    This is far too broad but that's basically it; sure. If I put in an answer, it could grow into even more comments. So, you'll need to post your SQL/PHP, or take it up with the person who has given you an "answer" below. Commented Apr 7, 2015 at 20:17
  • 1
    This question is a little short on information. Can you share what you have tried, and what problems have you run into? ¯\_(ツ)_/¯ Commented Apr 7, 2015 at 20:21

2 Answers 2

3

Set all your radio buttons to hold the same name attribute but with different values, then upon submit and choosing the desired radio button, query for what is matched.

<?php 

if(isset($_POST['submit'])){

    if(!isset($_POST['query'])){
    echo "You chose nothing";
    }

    if($_POST['query'] == "Rating"){
    // query for Rating
        echo "Rating";
    }

    if($_POST['query'] == "Genre"){
    // query for Genre
        echo "Genre";
    }

    if($_POST['query'] == "Year"){
    // query for Year
        echo "Year";
    }

    if($_POST['query'] == "Rating,Genre,Year"){
    // query for Rating,Genre,Year
        echo "Rating,Genre,Year";
    }

} // brace for if(isset($_POST['submit']))

?>
<form action="" method="post">
<input type="radio" name="query" value="Rating">Rating<br>
<input type="radio" name="query" value="Genre">Genre<br>
<input type="radio" name="query" value="Year">Year<br>
<input type="radio" name="query" value="Rating,Genre,Year">Rating,Genre,Year
<br>
<input type="submit" name="submit" value="Submit query">
</form>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I will give it a go now and see if I can get it working, I will let you know how thing go, again Thanks
@McNoodles Nota: This will work. If it doesn't, then it would have been because of how you setup your queries. That, would be another question. You basically put your related queries inside // query for Rating etc.
1

Radio buttons must have same name.

Without a method="post" the <form> method will be "get" so the PHP would use $_GET not $_POST

<form action="">
<input type="radio" name="query" value="1">Rating<br>
<input type="radio" name="query" value="2">Genre<br>
<input type="radio" name="query" value="3">Year<br>
<input type="radio" name="query" value="4">Rating,Genre,Year
</form>

I am not a big fan of using IF ELSE branching structures and avoid them whenever possible.

I prefer passing integer values to be used in arrays.

PHP

$q = intval($_GET['query']);

The intval() will return a zero if ($_GET['query'] return no value.

$queries = array(
0 => $query4,
1 => $query1,
2 => $query2,
3 => $query3,
4 => $query4);

$sql = $queries[$q];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.