I need any suggestion how can i do that. Example what i trying todo:
I have option value called "Ban reason":
Cheating
Sample reason
If i choose "Cheating" i put reason "Chating" into mysql. That's ok with my code:
DB::table('server_bans')->insert(['reason' => Input::get('player_reason')
My view:
<select id="player_reason" name="player_reason"><option value="" selected="selected">-</option>
<option value="Cheating">Cheating</option>
<option value="No reason">No reason</option>
</select>
But i need to add into sql "Ban time" by reason using minutes. Example:
If i choose "Cheating" from option value, it goes reason and banlength for 360days by using minutes timer (518400 minutes)
Any suggestion how can i do that? Thanks for helping me!
You just need to switch on the reason and set a variable in your controller method when saving the ban reason.
public function banUser (Request $request) {
$reason = $request->get('player_reason');
switch ($reason) {
case 'Cheating':
$banlength = 518400;
break;
case 'No Reason':
$banlength = 1;
break;
default:
$banlength = 0;
break;
}
DB::table('server_bans')->insert(compact('reason', 'banlength'));
return back();
}
Related
I have a code where I would like to choose between four different possibility:
Query1
Query2
Query3
Query4
Once an user has choose his/her query my code has to execute a specific function in a different php file, the specific function should be activated depending on which index (1,2,3,4 that comes from the relative query) the user has chosen.
For instance if the user has chosen the Query1 the function:
function a($index){
}
where $index is 1, obtained directly form the query chosen.
Could you help me?
Thanks in advance
The Code Below might come in handy. The Idea here is that you Check within PHP if the Form was Submitted.... if it was, then simply get the Value of the Query (like: 1, 2, 3, 4). Then using Switch statement, You may then determine for yourself which Function should be called based on those Values: 1, 2, 3 and 4 respectively. NOTE: Be sure those functions are defined in your PHP Script as well...
<?php
if(isset($_POST['run'])){
$queryKey = isset($_POST['query']) ? $_POST['query'] : null;
if($queryKey){
switch($queryKey){
case "1":
callableFunctionForQuery1();
break;
case "2":
callableFunctionForQuery2();
break;
case "3":
callableFunctionForQuery3();
break;
case "4":
callableFunctionForQuery4();
break;
default:
}
}
}
?>
<html>
<head>
</head>
<body>
<form name="query-processor" action="" method="POST">
<label for="query" class="lbl" id="lbl-query">Please Select a Query to Run</label>
<select name="query" class="form-control" id="query">
<option value="1">Query1</option>
<option value="2">Query2</option>
<option value="3">Query3</option>
<option value="4">Query4</option>
</select>
<input type="submit" value="Run" id="run" name="run" />
</form>
</body>
</html>
OK so i think what you want is to assign a function to a variable
$function = function()
{
echo 'executing function';
};
then call the function by using the variable name
$function();
How can i make piece of select statement as php variable?
Example:
I have 2 options in select statement - edit and delete... I have those 2 options in dropdown menu... I want to make an mySQL query to edit or delete records/users from database...I tried to use switch and cases but it only gave me default as selecting.
<select name="editSelector">
<option name='edit'>Edit</option>
<option name='delete'>Delete</option>
</select>
<?php
if (select == edit)
{
edit query here
}
elseif (select == delete)
{
delete query here
}
?>
I know that this code doesn't have any variables etc. Because i have no idea how to make option as variable. It's connected to database already. Thanks for the answer in advance.
<?php
if($_REQUEST['editSelector'] == "edit"){
edit here;
}
else if ($_REQUEST['editSelector'] == "delete"){
delete here;
}
?>
Plus :
<select name="editSelector">
<option value='edit'>Edit</option>
<option value='delete'>Delete</option>
</select>
you need to place your select > option list in a form and process it the proper way using POST.
I would strongly advise against using ajax to facilitate any crud activity in your database. Unless you know what youre doing its VERY insecure.
heres a reasonable web resource if you need to look things up.
HTML file
<form name='option_form' action='someProcessingFile.php' method='post'>
<select name="editSelector">
<option value='edit'>Edit</option>
<option value='delete'>Delete</option>
</select>
<input type='submit' name='submit' value='Submit'/>
</form>
someProcessingFile.php
<?php
if(isset($_POST['editSelector']) {
switch($_POST['editSelector']) {
case 'edit':
// do something here, probably with the rest of the form
break;
case 'delete':
$id = htmlspecialchars($_POST['id']);
// do something here and delete the entry
break;
default:
// do something here to let yourself know that it all went wrong for some reason.
}
}
Suppose you have the following html select statement
<select>
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option>
</select>
Now I want to run a php if elseif statement that says,
if (option value = newest) {
// Run this
}
elseif ( option value = best sellers ) {
// Run this
}
etc. But I don't know what to put inside the if elseif statement. In other words instead of 'option value = newest' (which I know is incorrect), what can I put there so that if newest is selected it will execute the if statement, or if best sellers is selected it will execute the elseif statement?
Give name to your select.
<select name="selectedValue">
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option>
</select>
in your PHP, you will do:
$_POST['selectedValue'];
if I were you, I would prefer a switch-case incase, there are more than 2 conditions.
Example:
switch($_POST['selectedValue']){
case 'Newest':
// do Something for Newest
break;
case 'Best Sellers':
// do Something for Best seller
break;
case 'Alphabetical':
// do Something for Alphabetical
break;
default:
// Something went wrong or form has been tampered.
}
First put a name on your select:
<select name="demo">
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option>
</select>
Then
if ($_POST['demo'] === 'Newest') {
// Run this
}
elseif ( $_POST['demo'] === 'Best Sellers' ) {
// Run this
}
or
switch($_POST['demo']){
case 'Newest' :
//some code;
break;
case 'Best Sellers':
//some code;
break;
default:
//some code if the post doesn't match anything
}
The <select> should have a name attribute, e.g <select name="sortorder">. You could then say
if ($_REQUEST['sortorder'] == 'Newest') {
// TODO sortorder 'Newest' was selected.
}
If you know whether the form data is comming in via HTTP GET or HTTP POST, you could use $_GET['sortorder'] or $_POST['sortorder'] respectively.
I think the read-friendly version would be:
switch($option){
case 'Newest':
runNewestFunction();
break;
case 'Best Sellers':
runBestSellersFunction();
break;
case 'Alphabetical':
runAlphabeticalFunction();
break;
default:
runValidationRequest();
}
also, please add the name attribute <select name="option">
I made the dropdown with PHP which works fine. When the selected item changes, I call the function changePeriod().
$options[0] = '--period--';
$options[1] = 'Daily';
$options[2] = 'Weekly';
$options[3] = 'Monthly';
<td><?php echo form_dropdown('period', $options, '0', 'id="period" onchange="changePeriod()"'); ?></td>
Here you can see the HTML source code which is a result of the code above.
<td>
<select name="period" id="period" onchange="changePeriod()">
<option value="0" selected="selected">--period--</option>
<option value="1">Daily</option>
<option value="2">Weekly</option>
<option value="3">Monthly</option>
</select>
</td>
In this function I need to get the value of the selected item, but the 2 alerts above the switch both give Undefined as a result. Does anyone has an idea why I get undefined and not the values of the selected option? It worked with a dynamic dropdown filled with a foreach from the database.
function changePeriod() {
alert(document.getElementById("period").selectedIndex);
alert(document.getElementById("period").value);
switch (document.getElementById("period").selectedIndex) {
case 1:
alert("daily");
break;
case 2:
alert("weekly");
break;
case 3:
alert("monthly");
break;
}
}
Basically: Why can't I get the values of the selected option and how can I fix it?
Thanks for helping.
Is the HTML generated like this:
<select onchange="changePeriod()" id="period">
<option value="0">--period--</option>
<option value="1">Daily</option>
<option value="2">Weekly</option>
<option value="3">Monthly</option>
</select>
<script type="text/javascript">
function changePeriod(event) {
alert(document.getElementById("period").selectedIndex);
alert(document.getElementById("period").value);
switch (document.getElementById("period").selectedIndex) {
case 1:
alert("daily");
break;
case 2:
alert("weekly");
break;
case 3:
alert("monthly");
break;
}
}
</script>
If yes, it works for me.
Why don't you use jquery, you could do everything with less code.
EDIT:
The solution with jQuery:
$("#period").change(function(){
alert("The Value: "+$(this).val());
switch ($(this).val()) {
case 1:
alert("daily");
break;
case 2:
alert("weekly");
break;
case 3:
alert("monthly");
break;
}
});
take a look here: http://jsfiddle.net/rfWjd/
<? $config = parse_ini_file('/list.ini', true); ?>
<?echo'<select id="LBox" name="listbox" size="20" style="display:none;">';
foreach($config[MPR] as $id=>$label){
switch ($id)
{
case ($id==select):
echo'<option value="0" selected="selected"></option>';
break;
case ($id>0 && $id<=10):
echo'<optgroup label="'.$label.'">';
break;
case ($id>10 && $id<=20):
echo'</optgroup>';
break;
default:
echo'<option value="'.$id.'">'.$label.'</option>';
}
}
echo'</select>';?>
Above is code that builds a hidden list box and fills its options from an INI file. I would Like to replicate this for each section in my ini, but I am unsure of the best way to do this, other than copy and paste this 8 times with a new $config[x] value. Is there a way to replicate this for each section?
Write a function that does it and takes whatever changes (e.g. $config[x]) as an argument.