Unable to get count of POST variable - php

I want to insert selected rows in the database but I am unable to get count of checked check-boxes. However in one of my form where I am updating records I could get count but here I couldn't. Code is as below
<tr class="odd gradeX">
<td>
<input type="checkbox" class="checkboxes" value="<?php echo $row['student_id']; ?>" name="ids[]" />
</td>
<td>
<?php echo $row['first_name']." ".$row['last_name'];?>
</td>
<td>
<?php echo $row['father_mobile'];?>
</td>
<td class="hidden-480">
<a href="mailto:<?php echo $row['email'];?>">
<?php echo $row['email'];?>
</a>
</td>
<td class="hidden-480">
<?php echo $row['address'];?>
</td>
<td class="center hidden-480">
<?php echo $row1['track_name'];?>
</td>
<td>
<?php echo $row2['level_name'];?>
</span>
</td>
</tr>
<?php
if(isset($_POST['move']))
{
echo "count is" . count($_POST["ids"]);
for($i=0; $i<count($_POST["ids"]); $i++){
echo $sql = "inset into tbl_student_batches set bacth_id='".$_POST['batch_id']."',student_id='".$_POST['ids']."'";
if (mysql_query($sql)) {
$msg = "Selected students moved to next batch successfully";
header("location:convert_level?msg=$msg");
}else{
$errmsg = ("Error while moving.". mysql_error());
}
}
}

you can try in this way
$studentIds = explode(' ', $_POST['ids']);
$totalId= count($studentIds);

Related

How to show a button on selecting the value in DropDown List

As a beginner in PHP.
I have a dropdownlist of event names and a button, And I tried to display it only when I selecting any value > 0 only. Please help me out.
Thanks in advance
<?Php
#$event=$_GET['event'];
if(strlen($event) < 0 ){
echo "Data Error";
exit;
}
$sql="SELECT id,event_name FROM wp_evr_event order by id";
if(isset($event) and strlen($event) > 0){
$quer="SELECT id,fname,status,email,event_id FROM wp_evr_attendee where event_id=$event order by fname";}
else{$quer="SELECT id,fname,status,email,event_id FROM wp_evr_attendee order by event_id";}
echo "<form method=post name=Reload action=''>";
echo "<select name='event' onchange=\"reload(this.form)\"><option value=''>please select..</option>";
foreach ($con->query($sql) as $row){
if($row['id']==#$event){echo "<option selected value='$row[id]'>$row[event_name]</option>"."<BR>";}
else{echo "<option value='$row[id]'>$row[event_name]</option>";
}
}
echo "</select>";
?>
</div>
<div class="container">
<table>
<tr>
<td class='c_td'>id</td>
<td class='c_td'>name</td>
<td class='c_td'>status</td>
<td class='c_td'>email</td>
<td class='c_td'>event_id</td>
<td class='c_td'>edit</td> <td>delete</td>
</tr>
<?php
$i=1;
foreach ($con->query($quer) as $row2) {
$i=$i+1;
?>
<tr>
<td class='i_td'><?php echo $row2['id'] ?> </td>
<td class='i_td'><?php echo $row2['fname'] ?> </td>
<td class='i_td'><?php echo $row2['status'] ?> </td>
<td class='i_td'><?php echo $row2['email'] ?> </td>
<td class='i_td'><?php echo $row2['event_id'] ?> </td>
</tr>
<?php
}
?>
Because only the names of attendees will be exported for the selected event
<?php
echo '<td><a class="btn" href="test3.php?event=' . $row2['event_id'] . '">Export To Excel</a> </td>';
?>
I tried this
select:invalid + button {
display: none;
}

Sum values to show in php

In my admin panel i have a view of orders of day, individually is ok, but i want to count and sum the values (count number of orders) and (sum value from orders).
$pedido->valor_pedido (is the individual price from order)
My code is like this.
if(count($pedidos) > 0){ foreach($pedidos as $pedido){ $totalHoje += $valorCompra; ?>
<tr onclick="window.location='<?=base_url();?>index.php/adm/pedidos/alterar/<?=$pedido->id;?>'" style="cursor:pointer" target='_blank'>
<td width="50">
<?=$pedido->id;?>
</td>
<td class="pedidonome">
<?=strtolower($pedido->nome ? $pedido->nome : $pedido->razao_social);?>
</td>
<td>
<?php echo getPagamento($pedido->pagamento);?>
</td>
<td>R$
<?=$pedido->frete;?>
</td>
<td>R$
<?=$pedido->valor_pedido;?>
</td>
<td><span class="<?=$pedido->situacao;?>"><?=$pedido->situacao;?></span></td>
</tr>
If i understand you the right way you need to get a sum of orders, that's how you can do this:
<?php if(count($pedidos) > 0): ?>
<?php $sum = 0.00; ?>
<?php foreach($pedidos as $pedido): ?>
<tr onclick="window.location='<?=base_url();?>index.php/adm/pedidos/alterar/<?=$pedido->id;?>'" style="cursor:pointer" target='_blank'>
<?php $sum += $pedido->valor_pedido; ?>
<td width="50">
<?=$pedido->id;?>
</td>
<td class="pedidonome">
<?=strtolower($pedido->nome ? $pedido->nome : $pedido->razao_social);?>
</td>
<td>
<?php echo getPagamento($pedido->pagamento);?>
</td>
<td>R$
<?=$pedido->frete;?>
</td>
<td>R$
<?=$pedido->valor_pedido;?>
</td>
<td><span class="<?=$pedido->situacao;?>"><?=$pedido->situacao;?></span></td>
</tr>
...................
<?php endforeach; ?>
<?= 'Total: ' . $sum ?>
<?php endif; ?> //end if statement
<?php
$con = mysqli_connect("localhost","username","pwd","db name);
$result = mysqli_query($con,"select count(1) FROM tablename");
$row = mysqli_fetch_array($result);
$total = $row[0];
echo "Total rows: " . $total;
?>

Radio Button Group Produce a Zero Value When Form Method=POST

I have a group of radio buttons placed in a form with method post. But anytime the form is post the value from the radio button is zero. Below is the code
<form method="POST" action='insert_per.php'>
<table >
<caption>
Grant Permissions
</caption>
<thead>
<tr>
<th class="center"><strong>Name</strong></th>
<th class="center"><strong>Email</strong></th>
<?PHP if($_SESSION['user_group'] == 63){
echo '<th class="center"><strong>System Admin</strong></th>';
echo '<th class="center"><strong>Admin</strong></th>';
echo '<th class="center"><strong>User</strong></th>';
} ?>
<th class="center"><strong></strong></th>
</tr>
</thead>
<?php
if($stmt->execute()){
// output data of each row
while($rows = $stmt->fetch(PDO::FETCH_ASSOC)){ ?>
<tr>
<td border="0" type="hidden" style="display:none;"><input type="hidden" name="hidden" value=<?php echo $rows['member_id']; ?></td>
<td class="center"><?php echo $rows['username']; ?></td>
<td class="center"><?php echo $rows['email']; ?></td>
<?PHP if($_SESSION['user_group'] == 63){ ?>
<td class="center"><input type='radio' id=<?php echo $rows['username']; ?> name="gp" value="system admin"
<?php echo ($rows['permission']== 31 )?'checked':'' ?>></input> </td>
<td class="center"><input type='radio' id=<?php echo $rows['username']; ?> name="gp" value="admin"
<?php echo ($rows['permission']== 15 )?'checked':'' ?> ></input> </td>
<td class="center"><input type='radio' id=<?php echo $rows['username']; ?> name="gp" value="user"
<?php echo ($rows['permission']== 1 )?'checked':'' ?> ></input> </td> <?PHP } ?>
<td class="center"><button name="SUBMIT" type="SUBMIT">submit</button></td>
</tr>
<?php
}
}
?>
</table>
</form>
Below is the code for the insert_per.php
echo "this is the gp "+ $_POST['gp'];
echo "this is the gp "+ $_POST['hidden'];
try{
$stmt= $dbh->prepare("UPDATE `members_groups` SET `group_id`=(SELECT group_id FROM groups WHERE
group_name = ? ) WHERE member_id = ?");
$stmt->bindParam(1, $_POST['gp'] , PDO::PARAM_STR);
$stmt->bindParam(2 , $_POST['hidden'] , PDO::PARAM_INT);
if($stmt->execute()) {
echo " the privilege was granted successfully ".'<BR>';
echo "<a href='user_acc.php'>View result</a>";
}
} catch(PDOException $e)
{
echo "An error occured ".$e->getMessage();
}
The radio buttons are expected to produce one of the following values
"system admin"
"admin"
"user"
but it always produces zero "0" value.

Cannot update the form to the database

hod_view.php ## // the view of apply form from database
<?php
$sql= "SELECT * FROM form WHERE id='$formID'";
$result = mysql_query($sql) or die('Cannot get ID. ' . mysql_error());
$row=mysql_fetch_array($result);
$staffID=$row['staff_id'];
$sql2= "SELECT * FROM users WHERE staff_id='$staffID'";
$result2 = mysql_query($sql2);
$row2=mysql_fetch_array($result2);
?>
<form action="viewProcess.php?action=modifyView&id=<?php echo $row['id']?>" method="post" enctype="multipart/form-data" >
<h3 class="underlineLongest">USER APPLICATION FORM </h3>
<p align="right">Reference Number : <b> <?php echo $row['ref_no']; ?> </b> </p>
<table width='100%'>
<tr>
<td colspan='2' bgcolor="#C7C7C7"> Applicant Details</td>
</tr>
<tr>
<td width='30%'>Date of Application </td>
<td width='70%'> <textColor> <?php echo $row['app_date']; ?> </textColor> </td>
</tr>
<tr>
<td width='30%'>User Full Name </td>
<td width='70%'> <textColor> <?php echo $row2['name']; ?> </textColor> </td>
</tr>
<tr>
<td width='30%'>Designation/Staff ID </td>
<td width='70%'> <textColor> <?php echo $staffID; ?> </textColor> </td>
</tr>
<tr>
<td width='30%'>Department/Division </td>
<td width='70%'> <textColor> <?php echo $row2['department'].'/'.$row2['division']; ?> </textColor> </td>
</tr>
<tr>
<td width='30%'>Telephone Ext. No </td>
<td width='70%'> <textColor> <?php echo $row2['ext']; ?> </textColor> </td>
</tr>
<tr>
<td colspan='2' bgcolor="#C7C7C7" > Application Service Required </td>
</tr>
<tr>
<td width='30%'>Type of Application </td>
<?php
$type= $row['type'];
$result4 = mysql_query(" SELECT * FROM type WHERE type_code='$type'");
$row4=mysql_fetch_array($result4); ?>
<td width='70%'> <textColor> <?php echo $type.' - '.$row4['description']; ?> </textColor> </td>
</tr>
<tr>
<td width='30%'> Type of Facility </td>
<td width='70%'>
<textColor> <?php
$facility=explode(';',$row['facility']);
foreach($facility as $i =>$key)
{
echo $key .' - ';
$result5 = mysql_query(" SELECT * FROM facility WHERE fac_code='$key'");
while($row5=mysql_fetch_array($result5))
{
echo $row5['description'].' <br> ';
}
} ?>
</textColor> </td>
</tr>
<tr>
<td colspan='2' bgcolor="#C7C7C7" > Endorsed Status (Head of Department) </td>
</tr>
<tr>
<td width='30%'>Endorsed By </td>
<td width='70%'> <textColor> <?php echo $row['endorsed_by']; ?> </textColor> </td>
</tr>
<tr>
<td width='30%'>Status </td>
<td class="alt3">
<input name="radiobutton1" type="radio" value="APPROVED" checked >Approve
<input name="radiobutton2" type="radio" value="REJECTED" >Reject
</td>
</tr>
</tr>
<tr>
<td width='30%'>Date & Time </td>
<td width='70%'> <textColor> <?php echo $row['endorsed_time']; ?> </textColor> </td>
</tr>
<tr>
<td width='30%'>Remarks </td>
<!--<td width='70%'><textarea name="endorsed_remark" id = "endorsed_remark"></textarea> </td> -->
<td><input type="text" name="endorsed_remark" id="endorsed_remark" /></td>
</tr>
</table>
<br><br>
<center><p><input class="btnSuccess" type ="submit" name="submit" value="Submit" onclick="modifyView();" >
<input class="btnEdit" type="button" name="btnCancel" value="Cancel" onclick="goBack()" > </p>
This hod_view.php is the view form that have been applied by the user. Then, the approver need to approve this form by updating the endorsed_status and endorsed_remark into the database.
Unfortunately, the form is not updated in the database.
viewProcess.php ## // the update query to database
<?php
include 'includes/initial.php';
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action)
{
case 'modifyView' :
break;
default :
header('Location: index.php');
}
function modifyView()
{
if(isset($_GET['id']) && $_GET['id'] > 0)
{
$formID = $_GET['id'];
}
else
{ // redirect to index.php if id is not present
header('Location: index.php');
}
echo $formID;
$sql = "UPDATE form SET endorsed_status='{$_POST['radiobutton1']}',endorsed_remark='{$_POST['endorsed_remark']}' WHERE id='$id'";
$result = mysql_query($sql) or die('Cannot update. ' . mysql_error());
if ($_POST['radiobutton2']=='REJECTED')
{
$sql = "UPDATE form SET endorsed_status='{$_POST['radiobutton2']}',endorsed_remark='{$_POST['endorsed_remark']}' WHERE id={$id} ";
$result = mysql_query($sql) or die('Cannot update. ' . mysql_error());
}
}
**This viewProcess.php is the sql query (update). Can you guys help me to make this coding correct?
Try this.
<?php
include 'includes/initial.php';
$action = isset($_POST['action']) ? $_POST['action'] : '';
switch ($action)
{
case 'modifyView' :
break;
default :
header('Location: index.php');
}
function modifyView()
{
if(isset($_POST['id']) && $_POST['id'] > 0)
{
$formID = $_POST['id'];
}
else
{ // redirect to index.php if id is not present
header('Location: index.php');
}
echo $formID;
$sql = "UPDATE form SET endorsed_status='{$_POST['radiobutton1']}',endorsed_remark='{$_POST['endorsed_remark']}' WHERE id='$id'";
$result = mysql_query($sql) or die('Cannot update. ' . mysql_error());
if ($_POST['radiobutton2']=='REJECTED')
{
$sql = "UPDATE form SET endorsed_status='{$_POST['radiobutton2']}',endorsed_remark='{$_POST['endorsed_remark']}' WHERE id={$id} ";
$result = mysql_query($sql) or die('Cannot update. ' . mysql_error());
}
}

Pull and update data from database in PHP CodeIgniter foreach loop

I have a view which contains the data which is displayed from the database by a foreach loop and i want is to get the data which is edited from the view and update it in the database one by one but the problem is that database will be updated if there is a where clause but in my case i don't know how to get the key(tid) from view to controller here is the controller function which displays the data
public function bulk()
{
$pid = '';
if($this->input->post('pid'))
$pid = $this->input->post('pid');
$template['menu'] = $this->shared_model->get_flightmenus();
if($pid == '')
{
if($this->uri->segment(3))
$pid = $this->uri->segment(3);
else
$pid = $template['menu'][0]['tid'];
}
$template['pid'] = $pid;
// echo $pid;
// exit();
$template['airline'] = $this->shared_model->get_records('eat_airline');
$template['terminal'] = $this->shared_model->get_records('eat_terminal');
$template['data'] = $this->shared_model->get_recordbyvalue('eat_flight','menu_tid = '.$pid);
$template['main_content'] = $this->load->view('flight/flight_bulk_view', $template, true);
$this->load->view('includes/template', $template);
}
here is the view
<h4><?php echo anchor('login/logout', 'Logout'); ?></h4>
<?php echo anchor('/home', 'Home'); ?>/Flights
<h2>Flights</h2>
<div>
<?php echo form_open('/flight/') ?>
<?php foreach($menu as $row){ $options[$row['tid']] = $row['m_name']; } ?>
<?php $js = 'name="pid" id="pid" onChange="this.form.submit();"'; ?>
<?php echo form_dropdown('pid', $options, $pid, $js); ?>
<?php echo form_close(); ?>
</div>
<?php echo anchor('/flight/display/1/0/'.$pid, 'Create'); ?>
<?php echo anchor('flight/bulk', 'Bulk Update'); ?>
<table>
<tr>
<td>
Name
</td>
<td>
Airline
</td>
<td>
From
</td>
<td>
Stopover
</td>
<td>
Destination
</td>
<td>
Price
</td>
<td>
Unit
</td>
<td>
Tax
</td>
<td>
Protection
</td>
<td>
Fare
</td>
<td>
Week Offer
</td>
<td>
Menu
</td>
<td>
Sort
</td>
<td>
Edit
</td>
<td>
Delete
</td>
</tr>
<?php foreach($data as $row){ ?>
<tr>
<td>
<?php echo $row['f_name'] ?>
</td>
<td>
<?php $options1[0] = ''; foreach($airline as $row1){ $options[$row1['tid']] = $row1['a_name']; } ?>
<?php echo $options[$row['airline_tid']]; ?>
</td>
<td>
<?php $options1[0] = ''; foreach($terminal as $row1){ $options[$row1['tid']] = $row1['t_name']; } ?>
<?php echo $options[$row['terminal_from_tid']]; ?>
</td>
<td>
<?php echo $row['t_stopover'] ?>
</td>
<td>
<?php $options1[0] = ''; foreach($terminal as $row1){ $options[$row1['tid']] = $row1['t_name']; } ?>
<?php echo $options[$row['terminal_destination_tid']]; ?>
</td>
<td>
<?php echo $row['f_price'] ?>
</td>
<td>
<?php echo $row['f_unit'] ?>
</td>
<td>
<?php if($row['f_tax']>0) echo 'Yes'; else echo 'No'; ?>
</td>
<td>
<?php echo $row['f_protection'] ?>
</td>
<td>
<?php echo $row['f_fare'] ?>
</td>
<td>
<?php if($row['f_weekoffer']>0) echo 'Yes'; else echo 'No'; ?>
</td>
<!--
<td>
<?php $options[0] = ''; foreach($menu as $row1){ $options[$row1['tid']] = $row1['m_name']; } ?>
<?php echo $options[$row['menu_tid']]; ?>
</td>
-->
<td>
<?php echo $row['sortindex'] ?>
</td>
<td>
<?php echo anchor('flight/display/2/'.$row['tid'].'/'.$pid, 'Edit'); ?>
</td>
<td>
<?php echo anchor('flight/display/3/'.$row['tid'].'/'.$pid, 'Delete'); ?>
</td>
</tr>
<?php } ?>
</table>
i am unable to come up with the logic of how it will be done
any help will be appreciated
all i can see is that you want to pass the data from your view to controller you can do that by passing your data to url and then use it from the controller for future use.
You are getting values from DB row by row. Each row in your database has a unique ID (hopefully). That is your main criteria for the WHERE clause.
I don't see any inputs in your HTML, so I don't really understand how you are saving the data, but you can either do a hidden input in each row like <input type="hidden" id="[your_row_id_from_DB]" /> or if you have multiple inputs per row - add a name with id appended to it, like <input type="text" name="flight_your_row_id" /> . That way you can parse your names and know which values are submitted.

Categories