i have a column called Status in my table, and the possible values for this column are 1 or 0, 1 means that the row is activated and 0 means that the row is disabled.
But inside my backoffice, i have one html table to edit all the values inside this table.
To edit the values, the admin just need to select some rows(i'm using checkboxes), and click at the button to edit.
This button will show a new html table , inside this html table, i inserted all the values that i have inside my database table, into textboxes to change and edit.
The problem is that i need to show the current Status column value, example:
The status is activated, the value from status is 1.
The value will appear like this:
To change that, i used str_replace
if ($val['status'] == '1'){
$val['status'] = str_replace("1","Active","1");
}else{
$val['status'] = str_replace("0","Disable","0");
}
But when i submit the str_replace changes the value, and sends to my database Active instead 1, or Disabled instead 0.
I'm using a selectbox to chose if the status must be active or disabled
<select name='u_status[".$val['id']."]' class='txtedit'>
<option value=".$val['status']." style='display:none;'>".$val['status']."</option>
<option value='1'>Active</option>
<option value='0'>Disabled</option>
</select>
Thank you.
Create an array with the statuses:
$status = array(
'Disabled',
'Enabled'
);
Then use $val['status'] as the key to get the value you want from that array:
<option value=".$val['status']." style='display:none;'>".$status[$val['status']]."</option>
Don't use str_replace. This is just a boolean.
$val['status'] = ($val['status'] == '1' ? 'Active' : 'Disabled');
or even:
if ($val['status'] == '1') {
$val['status'] = 'Active';
} else {
$val['status'] = 'Disabled';
}
Related
dropmenu is <select name="dropbox"> with 3 options - admin, activate, delete. The below snippet of code shows if the activate <option> is selected and submitted by the submit button then echo etc. I have a <select name="dropbox"> on every row for each user. My code only works if i change the last drop box.
if(isset($_POST['submit']))
{
if(isset($_POST['dropmenu']) && $_POST['dropmenu'] == 'activate')
{
echo 'is activated';
}
else{
echo 'fail';
}
}
Is there a way i can use foreach drop box with a value selected?
One of the possible ways is to connect the name of the select element with user id like
<select name="dropbox_[userId]">
In this case you simply know how to build an input name for checking it's value in $_POST table.
If you don't interate by users on submit code then you can use a regular expression to get an inforamtion abut user connected with element.
I have a buttons which are displayed from sql query:
$username = new User();
$name = $username->data()->username;
$sql1 = DB::getInstance()->query("SELECT names FROM list WHERE username = '$name'");
if (!$sql1->count()) {
echo 'No data';
} else {
foreach ($sql1->results() as $sql1) {
?>
<p><button class="" > <?php echo $sql1->names; ?></button></p>
<?php
}
}
This displays two buttons which match the conditions from the query, so I'm trying to disable one of the displayed buttons if it doesn't match another condition.
For example, there are two buttons, John and Poodle. And a query to match if one of the buttons is an animal.
So if the button john does not match the query, it should be disabled.
I will try to express the idea sketched in the comments to the question, as you asked me to and explain it step by step:
foreach ($sql1->results() as $set) {
echo sprintf('<p><button %s>%s</button></p>'."\n",
in_array($set->name, array('poodle','cat','sheep')) ? 'disabled' : '',
$set->name);
}
Here $set is a different object (set of attributes) for each iteration of the foreach loop. You said you have two entries in that queries result, so two buttons get generated. Each $set has a name ($set->name) if you understand your code correct (I don't know your database...). This name is used twice for generating each button: first the name is used as text in the button and second it is used in a conditional to decide if the button should be disabled or not. That condition is implemented as a trinary expression, line 3 in the example above. In the line a function is called: in_array(). That returns true or false. If true, then the attribute "disabled" is added to the button, if false then the empty string ('') is added instead, so the button does not have the disabled property.
This is obviously not finished code. It is meant to give you the idea, so you should understand it, not just copy and try it. Feel free to ask if questions arise!
Try the following:
<button class="<?php if($sql1->names != "required_name") echo "disabled" ?>">
<?php echo $sql1->names; ?>
</button>
To disable a button ith php+html, you can try with a query value returned "disabled".
For example: in your query use a CASE WHEN statement to return disabled value when your condition is defined:
SELECT filed1,
CASE
WHEN field1= '19' THEN 'disabled'
WHEN field1='20' THEN ''
WHEN filed1='21' THEN ''
WHEN field1='22' THEN ''
ELSE 'disabled'
END AS for_button
FROM table1 AS tbl1
WHERE tbl1.field1_param = '$param'
Put in the HTML TAG button this:
<input type="submit" value="Button OnOff" <?=$disableButton['for_button']; ?>>
If the value returned is disabled, in the for_button variable, write the valure returned from query and the button will are disabled.
I works on the an data-edit form in codeigintier. And the problem is about re-populate checkbox
It works if it is an add form (that means I need not concern about the value in database):
<?= set_checkbox('is_default', '1'); ?> for checkbox
The problem is, in the edit form:
I can't repopulate the checkbox
<?php if ($customer_group[0]['is_default'] == "1") echo "checked"; set_checkbox('is_default', '1'); ?>
The checkbox will check even I have not check it in the edit => fail to validate in the form, thanks for helping
I have already set the validation rule in controller, the code in the add form is working , but how to handle the case for edit form?
In order to re-populate checkbox following code might be helpful:
set_checkbox('fieldName', 'fieldValue');
Where 'value' is the second parameter of the form_checkbox call. Like this:
form_checkbox('fieldName[]', 'value', set_checkbox('fieldName', 'value'));
Now if you are on edit form then below code might help you
$getVal=$valFromDb; //$valFromDb is actually value of the filed from db as you are on edit page
if($getVal!=0){
{
echo form_checkbox('fieldName[]', 'value', true);
}
else
{
echo form_checkbox('fieldName[]', 'value', false);
}
set_checkbox takes a third argument to set the default state, so basically you have to do something like this
echo set_checkbox('is_default', 1, $customer_group[0]['is_default'] == "1");
Can give one suggestion??
1. Hide all the checked value of checkbox in input box when you are directed towards edit page.
If checked box is checked in edit page, edit the value of hidden input field of textbox value.
Submit it, when validation failed, checked or repopulate the checkbox value according to hidden field value. send checkbox value of checked box field through array from controller to edit page view like this. e.g $data['repopulate_checks'] = $this->input->post('array name of checkboxs');
In view :
getit like this
$catch_checkbox = $repopulate_checks;
You can directly get through $repopulate_checks also.
Hope this help you.
You can use form_checkbox() function: Guide
$isChecked = False; // or True for default value
If have stored data then:
$isChecked = $customer_group[0]['is_default'];
echo form_checkbox('input_name', 'value', $isChecked);
or the hard way:
set_checkbox():
The first parameter must contain the name of the checkbox, the second
parameter must contain its value, and the third (optional) parameter
lets you set an item as the default (use boolean TRUE/FALSE)
<input type="checkbox" name="is_default" value="1" <?php echo ($customer_group[0]['is_default']) ? set_checkbox('is_default', '1') : '' ; ?>/>
set_checkbox takes a third argument to set the default state, so basically you have to do something like this
$checked = FALSE; if($customer_group[0]['is_default']){ $checked = TRUE; }
echo set_checkbox('is_default', 1, $checked);
okay, im new in this site also new in php and can't get the logic on this,
i have a product page that shows the name, quantity and an add to cart button in each row of product
i made this just cutted of some code
while($showProducts = mysql_fetch_array($products))
{
$currenQuantity = $showProducts['current_quantity'];
$prodid = $showProducts['product_id'];
echo"<select name='quan'>";
for ($x=0;$x<=$currenQuantity;$x++)
{
if($currenQuantity != 0)
{
echo "<option value=$x> $x </option>";
}
}
echo"</select><br/>";
}
now the problem is every time i tried to get the value by using $_POST['quan'] the value that i always get is the default value 1 even i select a different value of quantity of a certain product, and i'm blanked with ideas.
You can't use the same name for an input/select field in a form.
You have to specify a diffrent name or create an indexed array:
<select name="quan[$prodid]">
You can acces it via
$_POST['quan'][$prodid]
Just a wild guess: has each of your product rows a select named 'quan'?
Then you're getting the last select named 'quan' in your POST data.
Prefix each of the quantities selects with name of the product, or some kind of a (scrambled) id.
Additionally:
If you don't want to have a 0 in your select, don't start the for loop with 0
for ($x=1;$x<=$currenQuantity;$x++)
if the current quantity is 0, the for loop doesn't get executed.
If you have more selectboxes with the same name (quan) in one form, you are getting value of the last one.
You should change the selectbox definition to:
echo"<select name='quan[" . $prodid . "]'>";
then you can access the values using:
$_POST["quan"][$some_product_id]
$_POST["quan"][$another_product_id]
I have several links on one page which all direct to the same page, but each of them passes a different variable in the URL (http://sitename.com/thispage.php?id=3). I set new variables to true or false depending on which value is passed for "id", then put the true/false variables in to set the default option on an option tag like so:
if (isset($_GET['id']))
$id = ($_GET['id']);
if ($id = 4)
$id4 = true;
else
$id4 = false;
...
<option value="Value" selected="<?php echo "$id4"; ?>">
Value
</option>
Excuse me if it seems messy.
I've got 12 of those setup, one for each menu option. I can't seem to get it to display the correct one as the default selection though, as it always selects the 2nd to last menu item.
One thing you should correct is putting true and false in quotation marks, otherwise they will be converted from boolean to string and end up as a 1 or 0.
Or you could replace the whole thing with this
<option<?php if (isset($_GET['id']) && $_GET['id'] == 4){ echo ' selected'; } ?>>
value
</option>