Dividing the One Column Data into Four Columns - php

I have 35 records of statename in mysql table. I have divided the data into two columns
Here is my php code for the requirement
<td>
<table class="statetable">
<?
//******Fetching State Name Dynamically*****//
$fetchstate = mysql_query("SELECT LocationName FROM servicedesklocationmaster group by LocationName ASC");
$half1 = floor(mysql_num_rows($fetchstate)/2);
echo $half1;
$count = 0;
// First Half State
while($count <= $half1 && $row = mysql_fetch_array($fetchstate))
{
$statename = $row['LocationName'];
$count++;
?>
<tr>
<td>
<font size="1.5">
<input type="checkbox" name="Location[]" id="Location" checked value="<?php echo $statename;?>" onClick="CheckEachAsset()"><?php echo $statename;?><br>
</font>
</td>
<?
}
//echo $count;
?>
</tr>
</table>
</td>
<td>
<table class="statetable">
<?
// Second Half State
while($row = mysql_fetch_array($fetchstate))
{
$statename = $row['LocationName'];
?>
<tr>
<td>
<font size="1.5">
<input type="checkbox" name="Location[]" id="Location"
checked value="<?php echo $statename;?>" onClick="CheckEachAsset()"><?php echo $statename;?><br>
</font>
</td>
<?
}
?>
</tr>
</table>
</td>
Now as per my new requirement i want to divide this into 4 columns can anybody suggest me
how to achieve it

$quater = floor(mysql_num_rows($fetchstate)/4);
$count = 0;
while($row = mysql_fetch_array($fetchstate)) {
if($count < $quater) {
// echo table content1
}
if($count >= $quater && $count < $quater*2) {
// echo table content2
}
if($count >= $quater*2 && $count < $quater*3) {
// echo table content3
}
if($count >= $quater*3 && $count < $quater*4) {
// echo table content4
}
$count++;
}

<td>
<table class="statetable">
<?
$fetchstate = mysql_query("SELECT LocationName FROM servicedesklocationmaster group by LocationName ASC");
$quart= floor(mysql_num_rows($fetchstate)/4);
echo $quart;
$count = 0;
$times=0;
while($times<=4 && $row = mysql_fetch_array($fetchstate))
{
$statename = $row['LocationName'];
$count++;
$times++;
?>
<tr>
<td>
<font size="1.5">
<input type="checkbox" name="Location[]" id="Location" checked value="<?php echo $statename;?>" onClick="CheckEachAsset()"><?php echo $statename;?><br>
</font>
</td>
<?
if($count==$quart && $times!=4){
$count=0;
?>
</tr>
</table>
</td>
<td>
<table class="statetable">
<?
}
}
//echo $count;
?>
</tr>
</table>
</td>

Related

How to check more than one checkbox if value for each checkbox exists in database else unchecked

I have three Table their structure is added down there. i want to show all role on one screen. and when click on one any role it will take you to next screen where (all the permission shown in check boxes) permission that is given to that role checked and other unchecked. and i want to that dynamically, as it can be given and taken by using check boxes. there check boxes should contain role_id and perm_id so it can b use for crud operations.table has many to many relation.
i have the role_id bu using the isset($_GET['role_id']) metthod and perm_id by calling a function that will give me the perm_id
<?php if (isset($_GET['role_id'])) {
$role_id = $_GET['role_id'];
} ?>
user_permission.php page
<?php $row = $role->allPermission($role_id); if(!empty($row)){
?>
<table class="table table-bordered table-striped" id="datatable-editable">
<thead>
<tr>
<th><center>Add</center></th>
<th><center>Update</center></th>
<th><center>Delete</center></th>
<th><center>View</center></th>
</tr>
</thead>
<tbody>
<tr>
<?php $all_permissions = $permission->allPermissions();
while ($row=mysqli_fetch_assoc($all_permissions)) {
$perm_id = $row['perm_id'];
$check_permission = $permission->checkPermission($role_id,$perm_id);
while ($row=mysqli_fetch_assoc($check_permission)){
?>
<td><center><input type="checkbox" perm_id="<?php echo $row['perm_id']?>" value="<?php echo $row['permission']?>"> <?php echo $row['permission']?>
</center>
</td>
<?php
}} ?>
</tr>
<tr>
<?php foreach ($row as $val) {?>
<td>
<?php if ($val['permission'] == 'add' || $val['permission'] == 'update' || $val['permission'] == 'delete' || $val['permission'] == 'view'){ ?>
<input type="checkbox" checked="checked" value="<?php echo $val['id']?>">
<?php } else { ?> <input type="checkbox" value="<?php echo $val['id']?>" > <?php }?>
</td>
<?php } ?>
<!-- foreach ($value as $key => $val) {
// echo $key . ' => ' . $val." / ";
echo $key['prem'];
} -->
<!-- <td>
<?php if (in_array('add', $row)){ ?>
<input type="checkbox" checked="checked" value="<?php echo $id?>">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td>
<td>
<?php if (in_array('update', $row)){ ?>
<input type="checkbox" checked="checked">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td>
<td>
<?php if (in_array('delete', $row)){ ?>
<input type="checkbox" checked="checked">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td>
<td>
<?php if (in_array('view', $row)){ ?>
<input type="checkbox" checked="checked">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td> -->
</tr>
</tbody>
</table>
allPermissions()
public function allPermissions()
{
$query="SELECT * from permission where status = 1";
$conn=$this->Connection();
$result = mysqli_query($conn,$query);
$num_rows = mysqli_num_rows($result);
if ($num_rows>0) {
return $result;
}
}
chechkPermission()
public function checkPermission($role_id,$perm_id)
{
$query = "select * from role_perm rp where role_id = '$role_id' and perm_id = '$perm_id' ";
$conn=$this->Connection();
$result = mysqli_query($conn,$query);
$num_rows = mysqli_num_rows($result);
if ($num_rows>0) {
return $result;
}
}
its been three(now) days i'm stuck here, please help. and sorry for my bad english
Table Structure
This is how i want it
you mean something like this? <input type="checkbox" <?= ($permission >= 3 ? "checked" : "") ?>

Getting Array Value from Checkbox in PHP

I am trying to build a list where user can select any checkbox and it can send all select values for the row to the next page.
Here is what I have:
first page:
<table border="0">
<tr>
<td>Check All | Uncheck All</td>
<td>Item Name</td>
<td>Item Description</td>
</tr>
<?php
if ($num_rows == 0) {
return "No Data Found";
}else{
while ($row = dbFetchAssoc($result)) {
$item_id = $row['item_id'];
$item_name = $row['item_name'];
$item_desc = $row['item_desc'];
$item_qty = $row['item_qty'];
$item_upc = $row['item_upc'];
$item_price = $row['item_price'];
$vendor_id = $row['vendor_id'];
?>
<tr>
<td> <input type="checkbox" name="area[]" value="<?=$item_id?>" /></td>
<td><input type="text" name="item_name[]" value="<?=$item_name?>"></td>
<td><input type="text" name="item_desc[]" value="<?=$item_desc?>"></td>
</tr>
<?php
}
}
?>
<tr><td colspan="3"><input type="submit"></td></tr>
</table>
</form>
Next Page:
<table>
<tr><td colspan="3"><?php "Total Item(s) selected: "; echo count($_POST['area']); ?></td></tr>
<?php
if(!empty($_POST['area'])) {
foreach($_POST['area'] as $check) {
echo "<tr><td>".$check."</td><td>".$_POST['item_name']."</td><td>".$_POST['item_name']."</td></tr>";
}
}
?>
</table>
I need to read the item_name and item_desc value as well. How do I get it?
You need the key:
if(!empty($_POST['area'])) {
foreach($_POST['area'] as $key => $check) {
echo "<tr><td>".$check."</td><td>".$_POST['item_name'][$key]."</td><td>".$_POST['item_desc'][$key]."</td></tr>";
}
}
As Marc said in his answer it would probably be better to set key's in your loop creating the input.
<?php
if ($num_rows == 0) {
return "No Data Found";
}else{
$counter = 0;
while ($row = dbFetchAssoc($result)) {
$counter++;
$item_id = $row['item_id'];
$item_name = $row['item_name'];
$item_desc = $row['item_desc'];
$item_qty = $row['item_qty'];
$item_upc = $row['item_upc'];
$item_price = $row['item_price'];
$vendor_id = $row['vendor_id'];
?>
<tr>
<td> <input type="checkbox" name="area[<?=$counter?>]" value="<?=$item_id?>" /></td>
<td><input type="text" name="item_name[<?=$counter?>]" value="<?=$item_name?>"></td>
<td><input type="text" name="item_desc[<?=$counter?>]" value="<?=$item_desc?>"></td>
</tr>
<?php
}
}
?>
foreach($_POST['area'] as $key => $check) {
echo $check . $_POST['item_name'][$key];
}
assuming that there's an exact 1:1 correspondence between your three submitted arrays. Remember that checkboxes which are NOT checked do NOT get submitted with the form, so this is most likely NOT true and this code will not work as written.

Want to update multiple records using checkboxes and save button

I am searching some records using two text boxes and then updating the selected records in database. i am able to see the value row id of the selected checkbox but when i want to get the value for updation in database it gives 0, i.e showing no record in array
Here is my code
if($_POST["search"])
{
$nitnumber = $_POST["nitnumber"];
$workno = $_POST["workno"];
$query = "select * from print where nit = $nitnumber and work = $work";
$result = mysql_query($query) or die ("<font color =red>NIT Number and/or Work Number is Missing</font>");
$count = mysql_num_rows($result);
if($count == 0)
echo "<font color=red>Record not found</font>";
else
{
while($record = mysql_fetch_assoc($result))
{
?>
<tr class="odd">
<td><div align="center"><?php echo $record['a']; ?></div></td>
<td><div align="center"><?php echo $record['b']; ?></div></td>
<td><div align="left"><?php echo $record['c']; ?></div></td>
<td> <div align="left">
<?php
enter code hereecho $record["d"];
?>
</td>
<td>
<input name="checkbox[]" id="checkbox[]" type="checkbox" value="<?php echo $record[$id];?>">
<?php echo $record["id"];?>
</td>
<?php } } }?>
<tr>
<td colspan="5" align="right"> <input type="submit" value="Save" name="save"> </td>
</tr>
<?php
if ($_POST['save'])
{
$num_chkboxes=count($_POST['checkbox']);
for($i=0; $i<$num_chkboxes; $i++){
$complete = intval($checkbox[$i]);
echo $complete;
var_dump($complete);
echo $updateSQL = "UPDATE toDo SET complete=1, WHERE toDoId=$complete";
//$Result1 = mysql_query($updateSQL, $FamilyOrganizer) or die(mysql_error());
}
}
?>
<?php
if ($_POST['save'])
{
$checkbox1 = $_POST['chk1'];
$selected_checkbox = "";
foreach ($checkbox1 as $checkbox1)
{
$selected_checkbox .= $checkbox1 . ", ";
}
$selected_checkbox = substr($selected_checkbox, 0, -2);
$updateSQL = "" // your update query here
}
?>
<input type="checkbox" name="chk1[]" value=""> // take checkboxes like this
First, the problem started when you fill the values of the checkboxes:
<input name="checkbox[]" id="checkbox[]" type="checkbox" value="<?php echo $record[$id];?>" />
you must gave them the value like
value="<?php echo $record['id'] ?>"
Second, you don't get the values of the checkboxes as you should:
for($i = 0; $i < count($_POST['checkbox']); $i++){
$complete = intval($_POST['checkbox'][$i]);
//echo $complete;
//var_dump($complete);
echo $updateSQL = "UPDATE toDo SET complete=1, WHERE toDoId=$complete";
}
Another thing you should take care about is the assigning of the elements ids:
id="checkbox[]"
you must give unique ids for each element:
id="checkbox-1"
id="checkbox-2"

How to insert data in php/mysql in one save click?

I have table player_points with player_name, runs, sixes, match_no, fiftys, hundreds, point_scored.
I make form with above fields where i insert runs and sixes and calculate fiftys, hundreds and point_scored using runs & sixes, give extra 10 points for every sixex after 3 six.
Its works properly but when i insert runs and sixes and save it only shows calculation in fiftys, hundred and point-scored textbox not saved into database table. After i saved it second time its save into database. My code is like below:
<tr><td colspan="9"><hr style="border:1px #999 dashed;"></td></tr>
<tr>
<th>Player</th>
<th>Status</th>
<th>Runs</th>
<th>6's</th>
<th>50's</th>
<th>100's</th>
<th>TP</th>
<th>Action</th>
</tr>
<tr><td colspan="9"><hr style="border:1px #999 dashed;"></td></tr>
<?php
$classObj->getPlayerPointTable();
//$matchid = $_GET['mid'];
$condpl = "player_name='".$player1."' AND match_no = '".$matchid."'";
$resultpl = $classObj->selectSql($condpl);
//$recordcount = $classObj->getAffectedRows($result);
$rowpl = $classObj->fetchData($resultpl);
foreach($rowpl as $rowspl)
{
$Status1 = $rowspl['status'];
$runs1 = $rowspl['run_scored'];
$sixes1 = $rowspl['sixs'];
$fifty1 = $rowspl['fifty'];
$hundred1 = $rowspl['hundred'];
$total_score1 = $rowspl['Total_Score'];
$total_points1 = $rowspl['point_scored'];
$firstPoint1 = $rowspl['FirstPoint'];
$allocation1 = $rowspl['allocation'];
}
?>
<?php
$bonus1 = 10;
$bonus2 = 20;
$bonus3 = 25;
$bonus4 = 50;
/*condition for fifty 0 & 1 start */
if ($runs1 >= 50 && $runs1 <= 99 || $runs1 >= 150 && $runs1 <= 199 || $runs1 >= 250 && $runs1 <= 299) {
$fifty1 = 1;
} else {
$fifty1 = 0;
}
if ($runs1 >= 50 && $runs1 <= 499) {
$tfif1 = $runs1 + $bonus3;
} else {
$tpor1 = $runs1;
}
/*condition for fifty 0 & 1 end */
/*condition for hundred 0 & 1 start */
if ($runs1 >= 100 && $runs1 <= 199) {
$hundred1 = 1;
}
else if ($runs1 >= 200 && $runs1 <= 299) {
$hundred1 = 2;
}
else {
$hundred1 = 0;
}
/*condition for hundred 0 & 1 end */
/*Condition start for Six excess to 3 sixes*/
$i1 = $sixes1 - 3;
if($i1 >= 1 && $i1 <= 500)
{
$tsix1 = $i1 * $bonus1;
}
else
{
$tsix1 == 0;
}
/*Condition start for Six excess to 3 sixes*/
if($fifty1 == 1 && $hundred1 >= 1)
{
$tsixhnrd1 += $bonus4;
}
/*condition for hundred 0 & 1 end */
if ($runs1 >= 100 && $runs1 <= 500) {
$trun1 += $bonus3;
}
$total_points1 = $tfif1+$tsix1+$tsixhnrd1+$trun1+$tpor1;
$total_score1 = $runs1;
/*veriable for Balling team database insertions end*/
?>
<form enctype="multipart/form-data" method="post" name="formA1">
<tr>
<td><div style="width:auto;padding-left:7px;font-size:17px;"><?php echo $player1;?></div>
<input type="hidden" name="player1" id="player1" value="<?php echo $player1;?>" />
<input type="hidden" name="matchid" id="matchid" value="<?php echo $matchid;?>" />
</td>
<?php
if($Status1=='P'){
?>
<td align="center">
<select id='status1' name='status1' onChange="dis_ableA1(this)">
<option value='NP'>NP</option>
<option value='P' selected>P</option>
<option value='O'>Out</option>
</select>
</td>
<td align="center">
<input type="text" onkeypress="return blockNonNumbers(this, event, false, false);" style="width:50px;height:25px;text-align:right;background:#f5c0c0;" name="runs1" id="runs1" value="<?php echo $runs1;?>" size="3" maxlength="3" />
</td>
<td align="center">
<input type="text" onkeypress="return blockNonNumbers(this, event, false, false);" style="width:30px;height:25px;text-align:right;background:#f5c0c0;" name="sixes1" id="sixes1" value="<?php echo $sixes1;?>" size="2" maxlength="2" />
</td>
<?php
} else if($Status1=='O'){
?>
<td align="center">
<select id='status1' style="width:50px;height:25px;" name='status1' onChange="dis_ableA1(this)">
<option value='NP'>NP</option>
<option value='P'>P</option>
<option value='O' selected>Out</option>
</select>
</td>
<td align="center">
<input type="text" onkeypress="return blockNonNumbers(this, event, false, false);" style="width:50px;height:25px;text-align:right;background:#ccc;" disabled name="runs1" id="runs1" value="<?php echo $runs1;?>" size="3" maxlength="3" />
</td>
<td align="center">
<input type="text" onkeypress="return blockNonNumbers(this, event, false, false);" style="width:30px;height:25px;text-align:right;background:#CCC;" disabled name="sixes1" id="sixes1" value="<?php echo $sixes1;?>" size="2" maxlength="2" />
</td>
<?php } else { ?>
<td align="center">
<select id='status1' style="width:50px;height:25px;" name='status1' onChange="dis_ableA1(this)" tabindex="1">
<option value='NP'>NP</option>
<option value='P'>P</option>
<option value='O'>Out</option>
</select>
</td>
<td align="center">
<input type="text" onkeypress="return blockNonNumbers(this, event, false, false);" style="width:50px;height:25px;text-align:right;" name="runs1" disabled id="runs1" value="<?php echo $runs1;?>" size="3" maxlength="3" />
</td>
<td align="center">
<input type="text" onkeypress="return blockNonNumbers(this, event, false, false);" style="width:30px;height:25px;text-align:right;" name="sixes1" disabled id="sixes1" value="<?php echo $sixes1;?>" size="2" maxlength="2" />
</td>
<?php }?>
<td align="center">
<label style="width:30px;height:25px;text-align:right;"><?php echo $fifty1;?></label>
<input style="width:30px;height:25px;text-align:right;" type="hidden" id="fifty1" name="fifty1" value="<?php echo $fifty1;?>" size='2' maxlength='2' />
</td>
<td align="center">
<label style="width:30px;height:25px;text-align:right;"><?php echo $hundred1;?></label>
<input style="width:30px;height:25px;text-align:right;" type="hidden" id="hundred1" name="hundred1" value="<?php echo $hundred1;?>" size='2' maxlength='2' />
</td>
<td align="center">
<label style="width:30px;height:25px;text-align:right;"><?php echo $total_points1;?></label>
<input style="width:30px;height:25px;text-align:right;" type="hidden" id="total_points1" name="total_points1" value="<?php echo $total_points1;?>" size='5' maxlength='5' />
<input type="hidden" name="allocation1" id="allocation1" value="pending" />
</td>
<?php
if($Status1=='P'){
?>
<td align="center">
<input type="submit" name="submitA1" id="submitA1" value="Save" />
</td>
<?php } else { ?>
<td align="center">
<input type="submit" disabled="disabled" name="submitA1" id="submitA1" value="Save" />
</td>
<?php } ?>
</tr>
</form>
<tr><td style="height:5px"></td></tr>
<!-- Insert Code Starts Submit1-->
<?php
if(isset($_POST['submitA1']))
{
$classObj->getPlayerPointTable();
$matchid = $_GET['mid'];
$cond = "player_name='".$player1."' AND match_no = '".$matchid."'";
$result = $classObj->selectSql($cond);
$count = $classObj->getAffectedRows($result);
//echo $count;
if($count > 0)
{
$player1up['status'] = $postedData['status1'];
//$player1up['player_name'] = $postedData['player1'];
$player1up['run_scored'] = $postedData['runs1'];
$player1up['sixs'] = $postedData['sixes1'];
$player1up['fifty'] = $postedData['fifty1'];
$player1up['hundred'] = $postedData['hundred1'];
$player1up['Total_Score'] = $postedData['total_score1'];
$player1up['point_scored'] = $postedData['total_points1'];
$player1up['allocation'] = $postedData['allocation1'];
$player1up['FirstPoint'] = $postedData['total_points1'];
$classObj->getPlayerPointTable();
$classObj->userPostedData = $player1up;
$cond = "match_no= '".$matchid."' AND player_name='".$player1."'";
$insert_query = $classObj->updateData($cond);
echo "<script> alert ('Player 1 update successfully.')</script>";
echo "<script>window.location='FirstIn.php?mid=$matchid'</script>";
}
else if($count <= 0)
{
$player['player_name'] = $postedData['player1'];
$player['match_no'] = $postedData['matchid'];
$player['status'] = $postedData['status1'];
$player['run_scored'] = $postedData['runs1'];
$player['sixs'] = $postedData['sixes1'];
$player['fifty'] = $postedData['fifty1'];
$player['hundred'] = $postedData['hundred1'];
$player['Total_Score'] = $postedData['total_score1'];
$player['point_scored'] = $postedData['total_points1'];
$player['FirstPoint'] = $postedData['total_points1'];
$classObj->userPostedData = $player;
$insert_query = $classObj->insertData();
if($insert_query == 'success')
{
echo "<script> alert('Player 1 score added.')</script>";
echo "<script>window.location='score.php?mid=$matchid'</script>";
}
}
else
{
echo "<script> alert ('Error Occured')</script>";
}
}
?>
Assuming that your class' getAffectedRows() method really fetches the affected rows from the last statement, it must be noted that a SELECT does not set this, it will be zero.
If you want to know how many lines were selected, use something like mysqli_num_rows.

arrange checkbox values in two columns

I have a form with check box values i want to arrange the checkbox in two columns instead of
one single long column.
How can i split it to two columns ?
Here is the code :
<form id="form" name="form" method="post" action="">
<table width="502" border="0">
<tr>
<td>
Name :
<input type="textbox" name="rcv_group_name" value="">
<input type="hidden" name="add" value="add" />
</td>
</tr>
<tr>
<td align="left">
<strong>HEADING</strong>
<?php
$q = "select * from Config_RCV";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) > 0)
{
$k=0;
while ($row = mysqli_fetch_array($r,MYSQLI_NUM))
{
?>
<br> <input type="checkbox" name ="rcv_val[]" value ="<? echo $row[0];?>" /> <? echo $row[1];?>
<?
$k++;
}
}
?>
</td>
</tr>
<tr>
<td align="right"><input type="submit" name="Submit" id="Submit" value="Submit" style="background-color:#999" />
< /td>
<td height="26" align="left"> </td>
<td> </td>
</tr>
</table>
<form>
add another table and make 2 columns by splitting 2'nd result:
if (mysqli_num_rows($r) > 0) {
$k=0;
echo '<table><tr>';
while ($row = mysqli_fetch_array($r,MYSQLI_NUM)) {
?>
<td><input type="checkbox" name ="rcv_val[]" value ="<?php echo $row[0];?>" /> <?php echo $row[1];?> </td>
<?php
$k++;
if($k%2 == 0){
echo '</tr><tr>';
}
}
echo '</table>';
}
use your $k
if ($k%2==0){
echo "<br />";
}
instead of the lone now
Using tables like above will probably be better for looks
You could just add a second column to your table:
echo "<tr>";
$i = 0;
while ($row = mysqli_fetch_array($r,MYSQLI_NUM)) {
if ($i++%2==0) {
echo "</tr><tr>";
}
echo "<td>".$CELL_CONTENT_HERE."</td>";
}
echo "</tr>";

Categories