I have a problem in updating multiple checkboxes
I have the following ,
if the price is checked then I will calculate its total price if not then the total price is 0
so I have this in the view
<?php echo form_open("/lab-forms/save/$id_form") ?>
<table>
<tr>
<td><input type="checkbox" name="approved[]" <?php if($price->is_checked==1) { echo 'checked="checked"';}?> value=<?php echo $price->id; ?>></td>
<td><?php echo $price->price; ?></td>
<td><?php echo $price->qt; ?></td>
<td><?php echo $price->total_price; ?></td>
</tr>
</table>
</form>
I want to get the checked and the unchecked ones cause the default on the database is checked
Thanks
echo ($price->is_checked == 1 ? "checked='checked'" : "")
This is exactly like
if($price->is_checked == 1) {
echo "checked='checked'";
}
else {
....you know....
}
Just easier yo use for situations like these one
Related
here is my php code in which i want some more configurations.....
<?php
for ($i=0; $i < sizeof($json['TrainRoute']); $i++) { ?>
<tr>
<td><?php echo $json['TrainRoute'][$i]['SerialNo']; ?></td>
<td><?php echo $json['TrainRoute'][$i]['StationName']; ?></td>
<td><?php echo $json['TrainRoute'][$i]['ScheduleArrival']; ?> / <?php echo $json['TrainRoute'][$i]['ScheduleDeparture']; ?></td>
<td class="train-mobile"><?php echo $json['TrainRoute'][$i]['ActualArrival']; ?> / <?php echo $json['TrainRoute'][$i]['ActualDeparture']; ?></td>
<td class="train-mobile"><?php echo $json['TrainRoute'][$i]['Day']; ?></td>
<td style="color: red;"><?php echo $json['TrainRoute'][$i]['DelayInDeparture']; ?></td>
</tr>
<?php } ?>
In the above code, i want to insert the following condition..
<td style="color: green;">
<?php
if ( $json['TrainRoute']['StationName'] = $json['CurrentStation']['StationName'] ) {
echo $json['CurrentStation']['StationName'];
}
?>
</td>
So that when i display, below code comes with the above code but the below code will display only once.
When I simply paste below code in above code, below code display with every repetition of above code, but I want it to display only once at the current position.
How can I solve this?
Within your if statement, you are assigning a value to the array this will always evaluate to true, and thus the if statement will never fail;
$json['TrainRoute']['StationName'] = $json['CurrentStation']['StationName']
This is because you're using a single equals
It should be a double or triple equals for comparison, or strict comparison respectively;
$json['TrainRoute']['StationName'] == $json['CurrentStation']['StationName']
I cannot find the solution to my issue on this forum. I'm trying to give the submit button a PHP value (unique ID) by extracting data from my database. I would also like to store the value in a SESSION. The submit button doesn't work when I I give it something other than a String as value.
Student View:
<pre>
<?php foreach ($tabinternships as $internship) { ?>
<tr>
<td><span class="html"><?php echo $internship->id_internship() ?></span></td>
<td><?php echo $internship->email_teacher1_internship() ?></span></td>
<td><?php echo $internship->email_teacher2_internship() ?></span></td>
<td><?php echo $internship->email_responsible_internship() ?></span></td>
<td><?php echo $internship->id_promoter_internship() ?></span></td>
<td><?php echo $internship->name_enterprise_internship() ?></span></td>
<td><?php echo $internship->stage_internship() ?></span></td>
<td><input type="radio" name="apply" value="<?php $tabinternship->id_internship() ?>"></td> <!-- Method calls an Object rather than an Array because I'm only displaying the attributes of 1 Object therefore the Array is not required here-->
</tr>
<?php } ?>
</pre>
StudentController:
<pre>
$tabinternships = Db::getInstance()->select_internships();
if(isset($_POST['apply']) && isset($_POST['application'])){
$_SESSION['apply']= $_POST['apply'];
$_SESSION['application']= $_POST['application'];
$_SESSION['redirect']='redirect';
}
if(!empty($_SESSION['application']) && !empty($_SESSION['apply']) && !empty($_SESSION['redirect'])){
$_SESSION['redirect']='';
header("Location: index.php?action=formdeux");
die();
}
</pre>
Thanks for your help.
I have found the error, I wasn't using echo when giving a value to the radio input
echo $tabinternship->id_internship()
I am trying to echo out the user level depending on whether the user level is either 1 or 5 based on SQL data results. Here:
<?php while ($rrows = mysql_fetch_array($rs_results)) {?>
<tr>
<td><input name="u[]" type="checkbox" value="<?php echo $rrows['id']; ?>" id="u[]"></td>
<td><?php echo $rrows['id']; ?></td>
<td><?php echo $rrows['date']; ?></td>
<td><?php echo $rrows['user_name'];?></td>
<td><?php echo $rrows['user_email']; ?></td>
<td><?php ?></td>
So I need a sort of if statement to select the user level from $rrows['id'] then if that selected data is 1 echo out "Network" and if it is "5" echo out "Administrator". How can this be done?
Seems like you need something like that:
$user_levels = array('Network','role2','role3','role4','Administrator');
<?php while ($rrows = mysql_fetch_array($rs_results)) {?>
<tr>
<td><input name="u[]" type="checkbox" value="<?php echo $rrows['id']; ?>" id="u[]"></td>
<td><?php echo $rrows['id']; ?></td>
<td><?php echo $rrows['date']; ?></td>
<td><?php echo $users_levels[(int)$rrows['id']-1]; ?></td>
<td><?php echo $rrows['user_name'];?></td>
<td><?php echo $rrows['user_email']; ?></td>
<td><?php ?></td>
The reason I am using -1 in the array is because the array is 0 based and your roles start with 1.
If you need to use the 'user_level' simply replace the row with
<td><?php echo $users_levels[(int)$rrows['user_level']-1]; ?></td>
Hope this helps!
I wonder how you got that far without an if statement but anyway, you also could do this right in you sql query. I always try to outsource as much logic to mysql as possible for a balanced load distribution.
SELECT level, IF(level = 1, 'Network', 'Administrator') as level FROM table
I think you can nest if statements to have more options.
Adapted from this answer: 'IF' in 'SELECT' statement - choose output value based on column values
I wouldn't do it with a if statement. I'd do it like that:
$levels = array(1 => "Network", 5 => "Administrator");
echo $levels[$rrows['user_level']];
That way, if you want to add other levels, you just add a value to the array and that's it.
I am trying to code a php script that automaticly selects the selected users (checkboxes)
I don't know what it is i am missing, maybe you guys can help out. What i am trying is:
$get_users = $mysqli->query("SELECT * FROM users WHERE user_type='5' AND user_id_parent='". get_uid() ."'");
$get_checkedusers = $mysqli->query("SELECT * FROM modules_users_availability WHERE module_id='". $mid ."' AND shopid='". get_uid() ."'
");
while ($row = $getcheckedusers->fetch_assoc()) {
$chosenCategory[] = $row['userid'];
}
while($users = $getusers->fetch_assoc()){
foreach($chosenCategory as $chosencategories){
if($users['user_id'] == $chosenCategory){
echo $checked = "checked";
}
}?>
<tr>
<td><input type="checkbox" <?php echo $selected; ?> name="access[]" value="<?php echo $users['user_id']; ?>" /></td>
<td><?php echo $users['name']; ?></td>
<td><?php echo $users['email']; ?></td>
</tr>
<?php } ?>
My problem is that i can't use the getcheckedusers array correctly in getusers while.
It returns trible result if there is 3 users, and double result if here is 2. Maybe i'm missing something? :)
while($users = $getusers->fetch_assoc()){
$checked="";
if(in_array($users['user_id'],$chosenCategory)){
$checked = "checked";
}?>
<tr>
<td><input type="checkbox" <?php echo $checked; ?> name="access[]" value="<?php echo $users['user_id']; ?>" /></td>
<td><?php echo $users['name']; ?></td>
<td><?php echo $users['email']; ?></td>
</tr>
<?php } ?>
If condition is wrong.. I hope it should like this
if ($users['user_id'] == $chosencategories) {}
--Edited for clarity.
Database:
tblModule, contains a list of modules that can be enabled or disabled.
tblData, contains a list of trusts and the modules they have enabled. This links to tblModule on tblData.M01 = tblModule.mod_key
The PHP page is accessed from an index page and passes a variable lstModTrust to this page, to limit the returned records from tblData for a single trust. tblData.trust_key
A query runs, qryModuleList which returns a list of all modules. This is used to generate a table of all available modules. Each row shows module name tblModules.mod_name, module code tblModules.mod_code and a checkbox.
qryModData will return a list of the modules that are enabled for a single trust, and the corresponding checkboxes need to be ticked in the table.
This page will then be used to enable and disable modules for a trust. If a module is unticked the entry will be deleted from tblData, if it is ticked an entry will be inserted, and if no change then no change in the DB.
At the moment I'm having trouble getting the checkboxes ticked correctly based on qryModData
Any thoughts anyone?
--Edited to include code--
<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Module</td>
<td>Module Code</td>
<td> </td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_qryModuleList['mod_name']; ?></td>
<td><?php echo $row_qryModuleList['mod_code']; ?></td>
<td><input <?php if (!(strcmp($row_qryModData['M01'],$row_qryModuleList['mod_code']))) {echo "checked=\"checked\"";} ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
</tr>
<?php } while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)); ?>
</table>
Then there's two SQL queries, one that generates the list to build the table, and the second which I'm trying to use to set the boxes to ticked.
qryModuleList
SELECT *
FROM tblmodules
ORDER BY mod_name ASC
qryModData
SELECT *
FROM tbldata
WHERE trust_key = varTrust
varTrust is pulled from a URL variable.
Apologies for not including the code in the first place.
--Edit for new code.
<?php while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)) { ?>
<tr>
<td><?php echo $row_qryModuleList['mod_name']; ?></td>
<td><?php echo $row_qryModuleList['mod_code']; ?></td>
<td><input <?php if (strcmp($row_qryModData['M01'],$row_qryModuleList['mod_key']) != 0) {echo "checked=\"checked\"";} ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
</tr>
<?php } ; ?>
--Edited for new Code.
<tr class="tblHead">
<td>Module</td>
<td>Module Code</td>
<td>Enabled\Disabled</td>
</tr>
<?php
$mod_data = array();
while ($row_qryModData = mysql_fetch_assoc($qryModData))
array_push($mod_data, $row_qryModData['M01']);
$currentRow = 0;
while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)) {
?>
<tr bgcolor="<?php echo($currentRow++ % 2)?"#CCFFFF":"#FFCCFF";?>">
<td><?php echo $row_qryModuleList['mod_name']; ?></td>
<td><?php echo $row_qryModuleList['mod_code']; ?></td>
<td><input <?php if (false !== (array_search($mod_data, $row_qryModuleList['mod_key']))) echo "checked=\"checked\""; ?> name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
</tr>
<?php } ; ?>
I think I'm understanding this now. What you need to do is place all of the allowed modules into an array and use the array_search() function to find it.
Example:
$mod_data = array();
while ($row_qryModData = mysql_fetch_assoc($qryModData)) array_push($mod_data, $row_qryModData['M01']);
That will get all the available modules into one array.
Next, while you cycle through the 'ModuleList' query, use the array_search() method to try and find the 'ModKey' variable in it. If you do, check the box. If not, do nothing.
Example:
<td><input <? if (false !== (array_search($mod_data, $row_qryModuleList['mod_code'])) echo "checked=\"checked\" "; ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
(The reason I use a "!==" is because the function can return false or something that might equal false and might not. Read the page I've linked above for more)