Put a checkbox in each table cell - php

I'm trying to add checkboxes to each cell in my table.
If $columns['mything$record'] is equal to 1, I want a checkbox that's checked to show up.
If $columns['mything$record'] is equal to 0, I want a blank checkbox to show up. I've created if statements which one can view below, but they don't put a checkbox in each table cell with the respective value, only one blank checkbox appears at the top of the page.
How can I put checkboxes in each cell that are checked and unchecked based on the cell value?
[![screenshot][1]][1]
$stmt = $sql->prepare("SELECT record,shortName FROM dms
where dms.isInactive=0
ORDER BY lastName asc, firstName asc");
$stmt->execute();
$shortNameArray=array();
while ($row = $stmt->fetch()) {
// print_r($row);
$shortNameArray[$row['record']]=$row['shortName'];
}
//print_r($shortNameArray);
$columns = array(
1 => array(
"selectFieldName" => "nrs.record nrRecord",
"resultFieldName" => "nrRecord",
"headerName" => "NT#",
"defaultSortOrder" => -1,
),
array(
"selectFieldName" => "nrs.title",
"resultFieldName" => "title",
"headerName" => "Title",
"defaultSortOrder" => 1,
),
);
$selectedColumnNumbers = array(1,2,3);
$counter=4;
foreach($shortNameArray as $record=>$name){
if($record>0){
$columns[]=array(
"selectFieldName" => "CASE WHEN $record IN (GROUP_CONCAT(dm_nr_links.dmRecord)) THEN 1 ELSE 0 END mything$record",
"resultFieldName" => "mything$record",
"headerName" => "$name",
"defaultSortOrder" => 1,
);
$selectedColumnNumbers[]=$counter;
$counter++;
if ($columns['mything$record'] == 1) {
echo "<input type='checkbox' name='PLJan' checked='checked' />";
}
if ($columns['mything$record'] == 0) {
echo "<input type='checkbox' name='JLFeb' />";
}
}
}
$selectedColumns = array();
$selectedFields = array(); // contains all the fields to be selected
foreach ($selectedColumnNumbers as $columnNumber){
$selectedColumns[$columnNumber] = $columns[$columnNumber];
if (strlen($columns[$columnNumber]["selectFieldName"]) > 0)
$selectedFields[] = $columns[$columnNumber]["selectFieldName"];
}
$selectedFields = array_unique($selectedFields); // Remove all duplicate fields for selection
///// Process Filter Cookies
$filterWhereString = "";
$filterBindValues = array();
for ($c=1; $c<=5; $c++) {
if (isset($_COOKIE["nrsfl$c"])
&& isset($_COOKIE["nrsfl$c"."Op"])
&& isset($_COOKIE["nrsfl$c"."Val"])
&& strlen($_COOKIE["nrsfl$c"."Val"]) > 0
) {
$columnNumber = $_COOKIE["nrsfl$c"];
$filterOperator = $_COOKIE["nrsfl$c"."Op"];
$filterValue = $_COOKIE["nrsfl$c"."Val"];
if (isset($columns[$columnNumber])){
if ($c >= 2) {
$lastC = ($c-1);
if (strlen($filterWhereString) > 0) // If string exists, add conjunction
{
if (isset($_COOKIE["nrsfl$lastC"."C"])){
$conSelect = $_COOKIE["nrsfl$lastC"."C"];
} else $conSelect = 1;
if ($conSelect == 1) $conjunction = "AND";
else $conjunction = "OR";
$filterWhereString .= "$conjunction ";
}
}
$column = $columns[$columnNumber];
$fieldName = $column['resultFieldName'];
$filterWhereString .= "$fieldName ";
switch ($filterOperator) {
case 1:
$operator = "=";
break;
case 2:
$operator = "LIKE";
break;
case 3:
$operator = ">=";
break;
case 4:
$operator = "<=";
break;
case 5:
$operator = ">";
break;
case 6:
$operator = "<";
break;
case 8:
$operator = "NOT LIKE";
break;
default:
$operator = "!=";
}
if ((strcmp($operator, "NOT LIKE") == 0) ||(strcmp($operator, "LIKE") == 0)) $filterField = "CONCAT('%',:filter".$c.",'%')";
else $filterField = ":filter$c";
$filterWhereString .= "$operator $filterField ";
if (substr_compare($fieldName, "date", 0, 4) == 0) { // Convert dates
$filterValue = strtotime($filterValue);
$filterValue = date("Y-m-d",$filterValue);
}
if ($fieldName=="isRestricted") { // Convert binaries
if(strtolower($filterValue)=='y')$filterValue="1";
if(strtolower($filterValue)=='n')$filterValue="0";
}
$filterBindValues[":filter$c"] = $filterValue;
}
}
}
/////
?>

Replace your code line
echo "<input type='checkbox' name='PLJan' ";
echo "checked='checked'";
with
echo "<input type='checkbox' name='PLJan' checked='checked' />";
and
echo "<input type='checkbox' name='JLFeb' ";
with
echo "<input type='checkbox' name='JLFeb' />";

Related

Disable a radio button if previous value is false in while loop PHP

i'm sorry if my english is not perfect but i will explain the situation with my best, basically my goal is to disable/hide the radio button if the previous loop value of status is pending, below is my code
$allPaid = true
while($row2 = mysqli_fetch_assoc($sql2Q))
{
$bill = $row2['billPay_id'];
$amount = $row2['billPay_amount'];
$interest = $row2['billPay_interest'];
$payAmount = $row2['billPay_amountPaid'];
$payInterest = $row2['billPay_interestPaid'];
$makeUp = $row2['billPay_makeUp'];
$billStatus = $row2['billPay_status'];
$date = $row2['billPay_collectOn'];
$date = date("d-m-Y",strtotime($date));
$PayDate = $row2['billPay_payOn'];
if($PayDate == "0000-00-00 00:00:00")
{
$PayDate = "";
}
else
{
$PayDate = date("Y-m-d",strtotime($PayDate));
}
if($billStatus == 0)
{
$status = "Pending";
}
else
{
$status = "Paid";
}
echo "
<tr>
<form>
<td>$no</td>
<td>$date</td>
<td>$PayDate</td>
<td>$amount</td>
<td>$payAmount</td>
<td>$payInterest</td>
<td>$makeUp</td>
<td>$status</td>
";
if(($status == "Pending")&&($allPaid == true))
{
echo "<td><input type='checkbox' name='colDate' value='$date' onclick='payData()'></input></td>";
$allPaid = false;
}
else
{
echo "<td></td>";
$allPaid = true;
}
echo " </form>
</tr> ";
$date = date("y-m-d",strtotime($date));
$no++;
}
what i am doing right now is making a payment system where customer need to pay the bill before able to pay the next bill, so before paying the current bill the next bill radio button will not appear or hidden, in this system the radio button is act as selector,is there any way to achieved that? any help will be very helpful, thank you
UPDATED
i have update the code based on Anggara suggestion, but the output is a little bit off
If your page showing only one bill for one person, you can add a variable indicating the person has paid all previous bills.
$allPaid = true; // declare here
while($row2 = mysqli_fetch_assoc($sql2Q))
{
$bill = $row2['billPay_id'];
$amount = $row2['billPay_amount'];
$interest = $row2['billPay_interest'];
$payAmount = $row2['billPay_amountPaid'];
$payInterest = $row2['billPay_interestPaid'];
$makeUp = $row2['billPay_makeUp'];
$billStatus = $row2['billPay_status'];
$date = $row2['billPay_collectOn'];
$date = date("d-m-Y",strtotime($date));
$PayDate = $row2['billPay_payOn'];
if($PayDate == "0000-00-00 00:00:00")
{
$PayDate = "";
}
else
{
$PayDate = date("Y-m-d",strtotime($PayDate));
}
if($billStatus == 0)
{
$status = "Pending";
$allPaid = false; // there is a bill unpaid
}
else
{
$status = "Paid";
}
echo "
<tr>
<form>
<td>$no</td>
<td>$date</td>
<td>$PayDate</td>
<td>$amount</td>
<td>$payAmount</td>
<td>$payInterest</td>
<td>$makeUp</td>
<td>$status</td>
";
if($status == "Pending" && $allPaid) // show only if previous bills are paid
{
echo "<td><input type='checkbox' name='colDate' value='$date' onclick='payData()'></input></td>";
}
else
{
echo "<td></td>";
}
echo " </form>
</tr> ";
$date = date("y-m-d",strtotime($date));
$no++;
}
If i understand correctly, this should do what you want
replace
if($status == "Pending")
{
echo "<td><input type='checkbox' name='colDate' value='$date' onclick='payData()'></input></td>";
}
else
{
echo "<td></td>";
}
with
echo "<td><input type='checkbox' name='colDate' value='$date' onclick='payData()' ". (($status == 'Pending')? 'disabled':'') ."></input></td>";
if you want to hide it you can replace disabled with hidden

Form only submitting some rows to database

I have a form, populated by an uploaded CSV, and upon submit it saves to my database staging/temp table. However, it's only saving the first 4 of 7 rows.
The form:
$file = $_FILES["file"]["tmp_name"];
$handle = fopen($file, "r");
$maxPreviewRows = PHP_INT_MAX; // this will be ~2 billion on 32-bit system, or ~9 quintillion on 64-bit system
$hasHeaderRow = true;
echo "<form method='post' action='/form-submit' >";
echo '<table>';
/*WE WILL NEED TO QA CONDITIONS AND HIGHLIGHT IN RED HERE. ALSO NEED BORDER STYLINGS*/
if ($hasHeaderRow) {
$headerRow = fgetcsv($handle);
echo '<thead><tr>';
foreach($headerRow as $value) {
echo "<th>$value</th>";
}
echo '</tr></thead>';
}
echo '<tbody>';
$rowCount = 0;
while ($row = fgetcsv($handle)) {
$colCount = 0;
echo '<tr>';
foreach($row as $value) {
echo "<td><input name='row[".$rowCount."][".$colCount."]' type='text' value='$value' /></td>";
$colCount++;
}
echo '</tr>';
if (++$rowCount > $maxPreviewRows) {
break;
}
}
echo '</tbody></table>';
echo "<input type='submit' name='confirm' value='confirm'>";
echo '</form>';
}
The submission:
foreach ($_POST['row'] as $rowValue)
{
if( is_array($rowValue) && count($rowValue) > 0 )
{
$sqlFull = "INSERT INTO staging VALUES( DEFAULT, ". substr( str_repeat ( '?, ', count($rowValue) ), 0, -2 ) .")";
if( !$stmt = $connect->prepare($sqlFull) )
{
echo "Prepare failed: (" . $connect->errno . ") " . $connect->error;
}
$stmt->bind_param( str_repeat ( 's', count($rowValue) ), ...$rowValue );
$stmt->execute();
}
}
So it's definitely working but for some reason it's stopping on the fourth row every time. I edit the first field of each row each time and when I check my file dashboard I'll see only the first 4 from each submission.
ANy ideas why this is ending on the 4th row?
UPDATE:

PHP: both if and else statements are fired when I use post

when I use $_POST, both the if and else statements are executed. But the weird thing is that only the foreach loop is executed and "Flag is 0" is printed to the screen. If I replace $Flag = 1 with $Flag = 0, in the else statement, the loop does not get executed. When I replace "isset($_POST['Back'])" with "1", again the loop does not execute and all is well. So how come both my else and my if statement execute when I use $_POST? Thank you for any input
File #1
$Flag = 0;
if (isset($_POST['Back']))
{ echo "Flag is 0";
$Value = sqlsrv_fetch_array(sqlsrv_query($dbhandle,'SELECT MAX([Protocol Branch]) FROM Import_Trial_Information'),SQLSRV_FETCH_NUMERIC);
$Value = $Value[0];
}
else {$Flag = 1; echo "Flag is 1"; }
if ($Flag)
{
foreach ($Database_Tables AS $Delete)
{
$Query = 'DELETE FROM Import_' . $Delete;
sqlsrv_query($dbhandle,$Query);
if ($Delete == 'Paper_Information') {$Protocol = array('','');}
else {$Protocol = array(',[Protocol Branch]',',1');}
$Query = 'INSERT INTO Import_' . $Delete . '([Paper ID]' . $Protocol[0] . ') VALUES (' . (string)$ID . $Protocol[1] .')';
sqlsrv_query($dbhandle,$Query);
}
$Value = 1;
}
File #2
<div id = "Back_Button">
<form action="Import_Article_Advanced.php" method = "POST"><button name = "Back" ID = "Back" Value = "Back">Return to Previous Page</button></form>
</div>
try this
if (isset($_POST['Back']) OR !empty($_POST['Back']) OR $_POST['Back']!="" )
{ echo "Flag is 0";
$Value = sqlsrv_fetch_array(sqlsrv_query($dbhandle,'SELECT MAX([Protocol Branch]) FROM Import_Trial_Information'),SQLSRV_FETCH_NUMERIC);
$Value = $Value[0];
}
else {$Flag = 1; echo "Flag is 1"; }

How to compare and get the difference between 2 multidim arrays in PHP

Could somebody pls help me with this.
I have 2 arrays arr1 and arr2
<?php
$arr1[0]['name'] = "Ben";
$arr1[0]['level'] = "3";
$arr1[0]['age'] = "10";
$arr1[0]['gender'] = "M";
$arr1[1]['name'] = "Chris";
$arr1[1]['level'] = "12";
$arr1[1]['age'] = "4";
$arr1[1]['gender'] = "F";
$arr2[0]['name'] = "Jack";
$arr2[0]['grade_level'] = "3";
$arr2[0]['age'] = "10";
$arr2[0]['gender'] = "F";
$arr2[1]['name'] = "Lily";
$arr2[1]['level'] = "2";
$arr2[1]['age'] = "7";
$arr2[1]['gender'] = "F";
$arr2[2]['name'] = "Chris";
$arr2[2]['level'] = "12";
$arr2[2]['age'] = "4";
$arr2[2]['gender'] = "M";
?>
I need to compare the difference between arr1 and arr2 (and vice versa) on entries that have the same level and age values and return/highlight the columns that didn't match.
ex:
Ben->3->10->M in arr1 will be compared to
A.1 Jack->3->10->F in arr2 because they have the same level and age values
Chris->12->4->F in arr1 will be compared to
A.2 Chris->12->4->M in arr2 because they have the same level and age values
I should be able to know which columns didn't match
for A.1 the return should be name & gender
for A.2 the return should be gender
Desired output:
http://s7.postimage.org/8r3tf24rf/img.png
I have tried array_diff and several array functions & manipulations but i can't come up with the desired result :(
pls help. Thanks!
You could for example use "helper arrays" ($hl highlighter) that'd look like this:
<?php
$arr1[0]['name'] = "Ben";
$arr1[0]['level'] = "3";
$arr1[0]['age'] = "10";
$arr1[0]['gender'] = "M";
$arr1[1]['name'] = "Chris";
$arr1[1]['level'] = "12";
$arr1[1]['age'] = "4";
$arr1[1]['gender'] = "F";
$arr2[0]['name'] = "Jack";
$arr2[0]['level'] = "3";
$arr2[0]['age'] = "10";
$arr2[0]['gender'] = "F";
$arr2[1]['name'] = "Lily";
$arr2[1]['level'] = "2";
$arr2[1]['age'] = "7";
$arr2[1]['gender'] = "F";
$arr2[2]['name'] = "Chris";
$arr2[2]['level'] = "12";
$arr2[2]['age'] = "4";
$arr2[2]['gender'] = "M";
$hl1 = array();
$hl2 = array();
foreach($arr1 as $x => $tester)
{
foreach($arr2 as $y => $testing)
{
if(($tester['level'] == $testing['level']) AND ($tester['age'] == $testing['age']))
{
foreach($tester as $key => $value)
{
if($testing[$key] != $value)
{
$hl1[$x]['pos'] = true;
$hl1[$x]['key'][] = $key;
$hl2[$y]['pos'] = true;
$hl2[$y]['key'][] = $key;
}
}
}
}
}
$printheader = true;
echo "<table border='1'>";
foreach($arr1 as $pos => $printer)
{
echo "<tr>";
if($printheader)
{
$header = array_keys($printer);
foreach($header as $key)
{
echo "<td>". $key ."</td>";
}
echo "</tr><tr>";
$printheader = false;
}
foreach($printer as $key => $value)
{
if(isset($hl1[$pos]) AND in_array($key, $hl1[$pos]['key']))
{
echo "<td style='background-color: ff0000;'>". $value ."</td>";
}
else
echo "<td>". $value ."</td>";
}
echo "</tr>";
}
echo "</table>";
$printheader = true;
echo "<table border='1'>";
foreach($arr2 as $pos => $printer)
{
echo "<tr>";
if($printheader)
{
$header = array_keys($printer);
foreach($header as $key)
{
echo "<td>". $key ."</td>";
}
echo "</tr><tr>";
$printheader = false;
}
foreach($printer as $key => $value)
{
if(isset($hl2[$pos]) AND in_array($key, $hl2[$pos]['key']))
{
echo "<td style='background-color: ff0000;'>". $value ."</td>";
}
else
echo "<td>". $value ."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
looks like this: http://anyimg.com/view/8pj5q71/table_out.png

Magento - Bad attributes when loading product?

I'm creating custom rules to automagically add products to categories. The below code is from my observer. The problem I encounter is that upon running this code, products without an attribute ('shirt_color' for example) get added to my category.
Can anyone shed some light as to why this is happening? For some reason, 'charcoal grey' is still being attached to the product w/o a 'shirt_color'. When compared ('charcoal grey' == 'charcoal grey') it resolves to 'true' and that is why $r == 1;
Thanks.
Example output:
#product w/ a 'shirt_color' attribute
productEntityId=628
productName=Rogue GRAY International Shirt XXL
productSKU=HW0003-XXL
string ==
product attributeSTRTOLOWER='charcoal grey'
value STRTOLOWER='charcoal grey'
r=1
#product w/o a 'shirt_color' attribute
productEntityId=629
productName=O'Neill Hyperfreak White
productSKU=
string ==
product attributeSTRTOLOWER='charcoal grey'
value STRTOLOWER='charcoal grey'
r=1
Code:
public function onCategoryRuleSave($observe)
{
$model = Mage::getModel('catalog/product');
$collection =
$model->getCollection()
->addAttributeToSelect('entity_id');
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
foreach ($collection as $product) {
echo "productEntityId=" . $product->getId() . "<br>";
$product = $product->load($product->getId());
$productAttributeValue =
$model->load($product->getId())
->getAttributeText( $observe['attribute_code'] );
$r = 0;
echo "productName=" . $product->getName() . "<br>";
echo "productSKU=" . $product->getSku() . "<br>";
if ( is_numeric($observe['value']) ) {
echo "operator= " . $observe['operator'] . "<br>";
switch($observe['operator']) {
case "<":
echo "numeric <<br>";
$r = ($productAttributeValue < $observe['value']) ? 1 : 0;
break;
case ">":
echo "numeric ><br>";
$r = ($productAttributeValue > $observe['value']) ? 1 : 0;
break;
case "<=":
echo "numeric <=<br>";
$r = ($productAttributeValue <= $observe['value']) ? 1 : 0;
break;
case ">=":
echo "numeric >=<br>";
$r = ($productAttributeValue >= $observe['value']) ? 1 : 0;
break;
case "==":
echo "numeric ==<br>";
$r = ($productAttributeValue == $observe['value']) ? 1 : 0;
break;
case "!=":
echo "numeric !=<br>";
$r = ($productAttributeValue != $observe['value']) ? 1 : 0;
break;
}
}
else {
switch($observe['operator']) {
case "==":
echo "string ==<br>";
echo "product attributeSTRTOLOWER='" . strtolower($productAttributeValue) . "'<br>";
echo "value STRTOLOWER='" . strtolower($observe['value']) . "'<br>";
$r = (
strtolower($productAttributeValue) == strtolower($observe['value'])
) ? 1 : 0;
echo "r=" . $r . "<br>";
break;
case "!=":
echo "string !=<br>";
echo "product attributeSTRTOLOWER='" . strtolower($productAttributeValue) . "'<br>";
echo "value STRTOLOWER='" . strtolower($observe['value']) . "'<br>";
$r = (
strtolower($productAttributeValue) != strtolower($observe['value'])
) ? 1 : 0;
echo "r=" . $r . "<br>";
break;
}
}
echo "<br>";
if ($r==1) {
$write->query(
"REPLACE INTO `catalog_category_product` (`category_id`, `product_id`)
VALUES (" . $observe['category_id'] . "," . $product->getId() . ")"
);
}
}
die();
}
Passed from the controller:
Mage::dispatchEvent(
'category_rule_save',
array(
'rule_id' => $id,
'attribute_code' => $data['attribute_code'],
'operator' => $data['operator'],
'value' => $data['value'],
'category_id' => $data['category'],
'store_id' => $data['store_id']
)
);
Figured out the problem. Because I declared $model = Mage::getModel('catalog/product'); earlier, something was messing up later on. Instead of
$model->load($product->getId())
I now have
Mage::getModel('catalog/product')->load($product->getId()).
I did the same for Mage::getModel('catalog/product')->getCollection().

Categories