I've looked through a lot of questions, so forgive me if there is one that may help, but I've not been able to figure this out.
So I have a simple HTML form, that takes the users input for three categories: multiplayer, platform, and genre. This is the html code
<form method="post" action="gamebuddy.php">
<div class="players">
Please select one (or both) of the following: <br>
<input type="checkbox" name="players[]" value="No">Single Player<br>
<input type="checkbox" name="players[]" value="Yes">Multiplayers<br>
</div>
<div class="platform">
Please select your game system(s): <br>
<input type="checkbox" name="platform[]" value="XBOX">Xbox<br>
<input type="checkbox" name="platform[]" value="PS3">PS3<br>
<input type="checkbox" name="platform[]" value="PC">PC<br>
<input type="checkbox" name="platform[]" value="Wii">Wii<br>
</div>
<div class="genre">
Please select the genre(s) of game you would like: <br>
<input type="checkbox" name="genre[]" value="Action" >Action<br>
<input type="checkbox" name="genre[]" value="Casual">Casual<br>
<input type="checkbox" name="genre[]" value="Roleplaying">Role-Playing<br>
<input type="checkbox" name="genre[]" value="Shooter">Shooter<br>
<input type="checkbox" name="genre[]" value="Sports">Sports<br>
</div>
<div class="submit">
<input type="submit">
</div>
And then I have a PHP file that is used when the user clicks submit. Ideally, it takes the form inputs as a variable, and uses the SQLite statement to find the games the user can play based on his choices.
Here's the PHP code:
<div class="displaygames">
Based on your choices, these games seem suitable for you: <br>
<?php
if(!empty($_POST['players'])) {
foreach($_POST['players'] as $players) {
echo $players; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
if(!empty($_POST['platform'])) {
foreach($_POST['platform'] as $platform) {
echo $platform; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
if(!empty($_POST['genre'])) {
foreach($_POST['genre'] as $genre) {
echo $genre; //echoes the value set in the HTML form for each checked checkbox.
}
}
//This is to connect to the database
$db = new SQLite3('gamebuddy.db');
//Statement that uses variables to create list
$results = $db->query("SELECT * FROM games where multiplayer = '$players' and platform = '$platform' and genre is '$genre'");
//Displays List
while ($row = $results->fetchArray()) {
echo '<ul>';
echo $row['game'];
echo '</ul>';
}
?>
So everything works fine if you put in one answer for each category (for example, the user clicks "No" to multiplayer, "PS3" to platform, and "action" to genre). BUT if the user selects "Action" AND "Role-Playing", for some reason it only takes the last one the user selects, in this instance "role-playing".
So my question is how do I get the statement to show ALL of the games when there are multiple inputs.
Thank you for your help, I will answer any questions there may be, and of course mark the answer as solved if it helps. Thanks!
If u have array in for example $players, u can use implode function and "IN" statement in SQL:
"SELECT * FROM games WHERE multiplayer IN (".implode(",", $players).")"
Related
I am trying to make a random tournament generator, where I can select names from a list with checkboxes and then randominze them into a different order.
I have the following form:
<form method="post" action="<?php echo ROOT ?>HomeController/createTournament/" enctype="multipart/form-data">
<div class="form-group">
<label for="participants">Select participants</label><br>
<?php foreach($players as $p): ?>
<input type="checkbox" name="participants" value="<?php echo $p['name'];?>"> <?php echo $p['name'];?><br>
<?php endforeach; ?>
</div>
<button type="submit" class="btn btn-primary btn-block" name="create">Show participants</button>
</form>
This form show's a checkbox and behind the checkbox the name of the participant.
This is my method:
public function createTournament() {
if(isset($_POST["create"])) {
$participants = $_POST['participants'];
}
include('app/views/showTournament.php');
}
That means I am saving the checked ones into $participants, right?
In the file showTournament, I know have access to $partipants.
I try to var_dump $particpants and it shows me:
string(6) "Onlyoneselected name"
So I tried a foreach, to get ALL of the selected names.
<?php
foreach($participants as $p) {
echo $p;
}
;?>
The foreach isn't showing anything, but the file has access to $participants. I want all the names on my screen, so I can start randomizing them. What do I do wrong?
<input type="checkbox" name="participants"
This line here is the root of your problems.
Because every checkbox has the same name, the value of $_POST['participants'] gets overridden for each checkbox in the list.
If you change that snippet to:
<input type="checkbox" name="participants[]"
Then $_POST['participants'] becomes an array of all checked values.
You need multiple checkbox values.
And therefore, HTML name of the input should be multiple (array)
<input type="checkbox" name="participants" will return string, only latest submitted value.
<input type="checkbox" name="participants[]" will return array of all submitted values.
So, replacing name="participants" to name="participants[]" will work.
Hello I am using various check boxes with different values and than storing the vales inside DataBase as all the checkboxes values are store inside Array
<input type="checkbox" value="Gym" id="details_10" name="utilities[]">
<input type="checkbox" value="Spa" id="details_11" name="utilities[]">
and than store in the database like: Gym, Spa.
Later I am making MySQL query inside edit form where I will be able to edit those and other parameters. For the other inputs I am using:
<?php if($row['data'] == 'Sample Data'){echo 'selected="selected"';}?>
In this case I would like to use something like the following:
<input type="checkbox" value="Gym" id="details_10" name="utilities[]"<?php if($row['utilities'] == 'Gym'){echo 'checked"';}?>>
Any help to achieve this result will be very welcome.
I've always used this in these cases.
<?php
if($row['utilities'] == 'Gym')
{
?>
<input type="checkbox" value="Gym" id="details_10" name="utilities[]" checked="checked">
<?php
}
else
{
?>
<input type="checkbox" value="Gym" id="details_10" name="utilities[]">
<?php
}
?>
I am storing content into a database table. One table column is called attrbutes and has a list of values such as (ex: 1, 3, 5) based on the checkboxes that were checked.
<form>
<input type="checkbox" name="attribute" value="1">Attr 1<br>
<input type="checkbox" name="attribute" value="2">Attr 2<br>
<input type="checkbox" name="attribute" value="3">Attr 3<br>
<input type="checkbox" name="attribute" value="4">Attr 4<br>
<input type="checkbox" name="attribute" value="5">Attr 5<br>
<form>
Couple of questions on how to integrate checkboxes with PHP...
1) How do I check to see if at least 1 checkbox is checked on form submit?
2) How do I turn the checked checkboxes into a list like 1, 3, 5 if checkboxes 1, 3, and 5 are selected.
3) As a reverse to #2, on page load I need to figure out how to check each checkbox that's value is listed in the database column. if 1, 3, 5 is listed in table column, I need checkboxes 1 3 and 5 checked on page load.
I know how to code the basic queries for inserting, updating, and removing etc...but I've never worked with checkboxes and storing values from checkboxes using php before.
Change you html:
<input type="checkbox" name="attribute[]" value="1">Attr 1<br>
<input type="checkbox" name="attribute[]" value="2">Attr 2<br>
<input type="checkbox" name="attribute[]" value="3">Attr 3<br>
<input type="checkbox" name="attribute[]" value="4">Attr 4<br>
<input type="checkbox" name="attribute[]" value="5">Attr 5<br>
1)
$checkedAttr = $_POST['attribute'];
if(count($checkedAttr) > 0)
echo "At least one checkbox is selected";
2)
$checkboxList = implode(',', $checkedAttr);
3)
$checkedAttr = explode(',', $yourQueryResultStringContainingTheCheckedList);
<input type="checkbox" name="attribute[]" value="1" <?php if(in_array('1', $checkedAttr)) echo 'checked=\"checked\"'; ?>Attr 1<br>
...
1, 2) You can treat form elements as arrays with name="attribute[]" then loop through the posted values in your php as an array.
For example:
<?php
$attributes = $_POST['attribute'];
if(empty($attributes)) {
echo "No attributes selected";
} else {
// echo whole array
print_r($attributes);
// loop through array
foreach($attributes as $attribute) {
echo $attribute." ";
}
// create list as one whole string
$list = implode(',', $attributes);
}
?>
3) When you are building the form (using php) you can check each value in a loop. Note that I also made your labels proper labels so they will also activate the checkbox if clicked.
<?php
// need some code to get db values into array
$attributes = array(1,3,5); // your list
// loop through the amount of checkboxes you want
for($i=1; $i <= 5; $i++) {
if(in_array($i, $attributs) { // check for a match with current checkbox
$checked = " checked";
} else {
$checked = "";
}
echo'<input type="checkbox" name="attribute[]" id="attribute'.$i.'" value="'.$i.'"'.$checked.'><label for="attribute'.$i.'">Attr 1</label><br>'
}
?>
Using WordPress, so PHP, HTML, CSS, JavaScript what is the best method of populating the results of a form upon submission? I could have a form with ddl, radio buttons, etc.
<form>
<input type="radio" name="sex" value="male">Male<br>
<input type="radio" name="sex" value="female">Female
<input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
<input type="checkbox" name="vehicle" value="Car">I have a car
<input type="submit" value="Submit">
</form>
What is the best way of populating the results i.e. "67% of users are Female" and "30% ride bikes" on the same page once the submit button is triggered?
Try something along these lines:
script.php
----------
<html>
<body>
<form method=post action=script.php>
your form here...
</form>
<? if($_SERVER["CONTENT_LENGTH"]>0) {
//form was submitted to script, so process form inputs here and display results...
}
?>
</body>
</html>
<form action="phpfile.php" method="Post">
...form here
</form>
phpfile.php:
<?php
if(isset($_POST['sex'])){
$sex = $_POST['sex'];
}//etc..
//To show the result, simply echo it:
echo $sex;
You'll need some sort of storage system to be able to calculate the amount of each.
So what you would need to do is write a simple query where the value of the field is male or female. Then you can easily calculate it.
What you will need to do is add the form results to the database, then find the total number of results for both categories, then calculate the percent female and the percent bike.
The form page:
<html>
<?php
if(isset($_GET['status']) && $_GET['status'] == "values") {
//connect to DB and select table
$male = count( mysql_query("SELECT gender FROM Table WHERE gender='male'"));
$female = count (mysql_query("SELECT gender FROM Table WHERE gender='female'"));
$car = count (mysql_query("SELECT vehicle FROM Table WEHRE vehicle='car'"));
$bike = count (mysql_query("SELECT vehicle FROM Table WEHRE vehicle='bike'"));
$totalResults = $male + $female;
$totalVehicles = $car + $bike;
$percentFemale = 100 * ($female / $totalResults);
$percentFemale = number_format($percentFemale, 0);
$percentBike = 100 * ($bike / $totalVehicals);
$percentBike = number_format($totalBike, 0);
echo "<p>" . $percentFemale . "% of users are female. </p>";
echo "<p>" . $percentBike . "% of users use bikes. </p>";
} else { ?>
<form action="formProcessor.php" method="POST"><!-- You could also use GET -->
<input type="radio" name="sex" value="male">Male<br>
<input type="radio" name="sex" value="female">Female
<input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
<input type="checkbox" name="vehicle" value="Car">I have a car
<input type="submit" value="Submit">
</form>
<?php } ?>
</html>
The formProcessor.php Page:
<?php
if(isset($_POST["sex"]) && isset($_POST['vehicle'])) {
//Connect to MySQL DB and select table here
$sex = $_POST['sex'];
$vehicle = $_POST['vehicle'];
mysql_query("INSERT INTO Table(sex, vehicle) VALUES($sex, $vehicle)"); //You may also want to add an id column, but...
//Close mysql connection
header("Location: /form.php?status=values");
die();
} else {
die("There was an error in your submission.");
}
?>
I think this answers your question, you've got a way to find the percent of female users and users on bikes. If you need that to be dynamic and show only the greater amount (or show both or something) just add a comment. This also assumes you are not using PDO, if you are, I can adjust the code. I just wrote the code, so I don't know for sure if it works, but here you go!
When my user searches for game in my DB, I present them with a list of the games that match their search term, and then i want to add a button to each item that allows the user to select "have it" or "want it" then have a single "add" button that adds their selections to their profile.
because you cant have it and want it at the same time, i assumed a radio button would be the best choice.
but now i am lost as to how to handle the buttons. When i click on one selection from the search, it will deselect when i choose have or want on another game.
I understand that i should be creating a separately name form for each search result, but then how would i manage the data when sending it to my controller? Maybe i need incrementing form names and then count how many results and use that in my controller?
Also, i need the gameID associated with the selections so i need to send a hidden value with that data for each selection
maybe i am going about this the wrong way...
heres my code
echo '<div id="UserSearchGameResults">';
echo '<form action="#" method="Post">';
$x = 0;
while($gamesLike != null)
{
$x++;
echo '<div id="game_search_list_item">'.$x.'. '.$gamesLike['title'].'</div>
<span class="gamelistblue">
<input type="radio" name="haveOrWant" value="have" />Have it
</span>
<span class="gamelistorange">
<input type="radio" name="haveOrWant" value="want" />Want it
</span>
<input type="hidden" name="gameID" value="'.$gamesLike['ID'].'" />';
$gamesLike = $statement->fetch();
}
$statement->closeCursor();
echo '<br /> <input type="submit" value="Add Game(s)" />';
echo '</div></form>';
any help is appreciated with the subject
new ideas on how to handle my needs are welcome too.
Dan
Use arrays for the input names. Something like
<input type="radio" name="game' . $gamesLike['ID'] . '[haveOrWant]" value="have" />Have it