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.
Related
I have an option menu with several options including show all records. The option menu is fed by a query on a MySQL table. I am trying to echo the option selected and everything works until I select all records. Then the echo shows as %. Would like for it to echo ALL when All records is selected from option menu. I am using the following to echo:
<?PHP echo isset($_POST['selGrade']) ? $_POST['selGrade'] : 'ALL'; ?>
Currently the default option is all records, which echos ALL correctly with my PHP statement above. Problem happens when I select another option and then select all records again. Then it echos "%". What can I add to the PHP statement to echo ALL when a user selects All records?
With the help of Peter Darmis, the solution to my question is:
<?PHP
if(!isset($_POST['selGrade']) || empty($_POST['selGrade']) ||
$_POST['selGrade']=="%"){
$text = "ALL";
} else {
$text = $_POST['selGrade'];
}
echo $text;
?>
I asked a similar question at Option Menu Showing Undefined Index. Thanks to all.
I'm guessing "all records" empties the $_POST['selGrade'] but doesn't unset it. Consider using !empty($_POST['selGrade']) instead of isset().
Function empty()
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);
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';
}
I'm trying to make a simple vote script in PHP and need help. The problem I'm having is that I've created a form with two buttons, like and dislike, which correspond to an if else statement.
If the like button is clicked do an update, else the dislike button was clicked do an update.
Here is the code I have so far, there is more above it, but not necessary to my problem:
while($row = $q2->fetch())
{
$up_vote= $row['up_votes']; /*Current Up Votes*/
$down_vote= $row['down_votes']; /* Current Down Votes*/
echo '<form method="post" action="">';
echo '<input type="button" id="up_vote" name="up_vote" value="Like" /> OR';
echo '<input type="button" id="down_vote" name="down_vote" value="Dislike" />';
echo '</form>';
echo round($row['rating'], 0);
echo '% of voters Liked this';
}
$up_votes = $up_vote +1;/*$up_vote referes to original # of up_votes, $up_votes adds 1 to the number of votes*/
$down_votes = $down_vote +1;/*$down_vote referes to original # of down_votes, $up_votes adds 1 to the number of votes*/
if ($something);/*upvote clicked*/
{
$vote_update = "UPDATE images
SET up_votes:up_votes
WHERE vote_item_id: vote_item_id";
$q3 = $conn-> prepare($vote_update);
$q3->execute(array(':up_votes'=>$up_votes, /*$up_vote referes to original # of up_votes, $up_votes adds 1 to the number of votes*/
':vote_item_id'=>$current_vote_item_id));
/* ALSO create INSERT INTO votes_done to track the user vote*/
$conn = null;
}
else ($something2);/*down vote clicked*/
{
$vote_update = "UPDATE images
SET down_votes:down_votes
WHERE vote_item_id: vote_item_id";
$q3 = $conn-> prepare($vote_update);
$q3->execute(array(':down_votes'=>$down_votes, /*$down_vote referes to original # of down_votes, $up_votes adds 1 to the number of votes*/
':vote_item_id'=>$current_vote_item_id));
/* ALSO create INSERT INTO votes_done to track the user vote*/
}
What should the if and else statements look like? I'm not sure how to go about this, can't have two submits, should I have two different forms, one for each button? Either way how can I say if like was clicked go to if and run code, or if dislike was clicked go to else and run code?
Thanks
Syntax errors aside, you can give the two buttons the same name, but different values.
echo '<form method="post" action="___URL OF YOUR PAGE___">';
echo '<input type="submit" id="up_vote" name="vote" value="Like" /> OR';
echo '<input type="submit" id="down_vote" name="vote" value="Dislike" />';
echo '</form>'
...
if($_POST['vote'] == "Like") {
...
} else if ($_POST['vote'] == "Dislike") {
...
}
Note that I changed the input's type to submit instead of button. This will cause the data to send itself to the page (button won't do anything unless you start adding JavaScript).
You can read more about this in these answers:
https://stackoverflow.com/a/6129301/1451957
html button v.s. html submit?
PHP is server side, while you want to control events triggered in client side (a user clicks one button, or another).
Javascript/Jquery are client side, there you can control and trigger events that call to PHP scripts that will do the query.
Also else(){ is not valid in PHP, you should use elseif(){.
This sounds like an instance where using JavaScript would be more useful, but if you really want to use PHP you can either use two separate forms or change the buttons to links that look like buttons and add URL parameters.
Also: you've got some syntax errors. Examples: you shouldn't be putting semicolons after your if/elseif conditions, else() should be elseif(), you're using two different variable names when trying to increment $up_votes and $down_votes, etc.
Why dont't you add an 'onclick' event to each of the like and dislike buttons, and execute different script?
I have been working on this one topic for weeks. I'm creating a webpage that pulls information from MsSQL server. My problem is where there is a section with few check boxes.
The checkboxes are suppose to be checked if they are found in the SQL database.
If the borrower used money from "Savings", "Checking" and "Stock" those checkboxes should be checked in HTML page. In my case it is only checking whatever is on the first row of the SQL search list. So if the list has "Saving" on the first row, only the "Saving" checkbox will be checked not the rest on the list. I tried using loop (while($r->EOF)), but then it picks whatever is on the end of the list. Here is what I am using to pull data from the SQL server. Thank you in advance for your help. Really appreciate it!
function __construct($_ldid, $_lrid)
$this->ldid = $_ldid;
$this->lrid = $_lrid;
//$loan is defined in the PHP.class (which is shown below).
$q = ("SELECT * FROM Tabel_name where loan_id = 885775")
$v = array($_ldid, $_lrid);
$r = $db->Execute($q, $v);
$this->downpaymenttype = $r->fields; //<- I think I have this line done wrong because it is suppose to store the outputs to an array.
//So wherever the "$loan" is in HTML page, it will take the outputs from the above statement.
//The above code, which is in PHP.class file, outputs the following(or suppose to):
1 885775 Checking
2 885775 Saving
3 885775 Gift
In the HTML webpage, the following codes should check mark the boxes according to the outputs:
<input type="checkbox" name="DPS1[]" id="DPS1-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Checking"} checked {/if}/>
<input type="checkbox" name="DPS2[]" id="DPS2-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Saving"} checked {/if}/>
<input type="checkbox" name="DPS3[]" id="DPS3-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Gift"} checked {/if}/>
Only the "checking" checkbox is getting checked because that's the one on the first row.
I also tried to loop, but it is not storing in array (I guess I don't know how to use the array for session)
$q = ("SELECT Tabel_name FROM loan_downpaymentsource where loan_id = 885775");
$v = array($_ldid, $_lrid));
$r = $db->Execute($q, $v);
while(!$r->EOF) {
$this->downpaymenttype = $r->fields;
$r->MoveNext();
}
Add a loop to go through your results before displaying and set flags. $r->fields is only going to grab the fields for the current row. You need to go through your entire rowset to see if the others are set.
Something like (It looks like you're using adodb or something like that)
$checking = false;
$saving = false;
$gift = false;
while(!$r->EOF){
$row = $r->fields;
switch($row->downpaymenttype){
case 'Checking':
$checking=true;
break;
case 'Saving':
$saving=true;
break;
case 'Gift':
$gift=true;
break;
default:
break;
}
$r->moveNext();
}
Now on your view check the flags. I've not used smarty but you'll have to pass those flags to your template as well as I'm sure you know, and then change your checkbox lines to check if each flag is true/false.
Hope that helps!