separation of month and year from database by using php - php

I was trying to get month and year from database when I click on the specific month from select option it should display month and as well as year, the result should fetch from database.
below is my code
<?php
$mon_arr=array("Jan"=>"01","Feb"=>"02","Mar"=>"03","Apr"=>"04","May"=>"05","Jun"=>"06","Jul"=>"07","Aug"=>"08","Sep"=>"09","Oct"=>"10","Nov"=>"11","Dec"=>"12");
?>
<select name="month" id="monthr">
<?php foreach($mon_arr as $x=>$xval){ ?>
<option value="<?php echo $xval; ?>" <?php if($m == $xval) echo "selected=selected"; ?>><?php echo $x; ?></option>
<?php } ?>
</select>
<?php //echo $xval; ?>
<?php //echo $m; ?>
<?php //echo $x; ?>
<select name="year" id="yearr">
<?php for($yi=2014;$yi<=2025;$yi++){ ?>
<option value="<?php echo $yi; ?>" <?php if($y == $yi) echo "selected=selected"; ?>><?php echo $yi; ?></option>
<?php } ?>
</select>
</div>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Description</th>
<th>File</th>
<th>Submitted By</th>
<th>Date</th>
<th></th>
</tr>
</thead>
<tbody>
<?php $sel_month=mysql_query("SELECT * FROM ".TABLE_MONTHLY." WHERE pid='".$_GET['pid']."' ORDER BY m_date DESC ");
$f=new all_calls();
while($res_month=mysql_fetch_array($sel_month))
{
?>
<tr>
<td></td>
<td><?php echo $res_month['m_desc']; ?></td>
<td><?php $pathsi=explode(',',$res_month['m_file']);
for($ji=1;$ji<count($pathsi);$ji++) {
?>
Download<br />
<?php
}
?></td>
<td><?php echo $f->get_username($res_month['m_by']); ?></td>
<td><?php echo $res_month['m_date']; ?></td>
<td><i class="fa fa-pencil"></i></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
class monthly_report{
function validate_and_insert($data,$mid,$mfile)
{
//var_dump($mid); die;
if(empty($data['mdesc'])) $err='Description Required';
if(empty($err))
{
//echo 'hello'; die;
//var_dump($mid); die;
if(strlen($mid)==0)
{
$insert=mysql_query("INSERT INTO ".TABLE_MONTHLY." SET pid='".$_POST['hide_pid']."', m_desc='".$data['mdesc']."', m_file='".$mfile."', m_by='".$_SESSION['ID']."', m_date=CURDATE()");
if($insert)
{
return 0;
}
}
else
{
$a=new all_calls();
$paths=$a->get_monthly_file($mid);
if(strlen($mfile)==0)
{
$newfile=$paths;
}
else
{
$newfile=$paths.','.$mfile;
}
//var_dump($mid);die;
$update=mysql_query("UPDATE ".TABLE_MONTHLY." SET m_desc='".$data['mdesc']."', m_file='".$newfile."' WHERE mid='".$mid."' ");
if($update)
{
return 0;
}
}
}
}
}
?>
Can anyone help me out with this issue.

Related

Input type hidden passing only last value of foreach loop

I have a foreach and a table and im trying to send the id of the specific row when button pressed, but its always sending the data of the last row in the input type hidden "solicitudId" id. At the "input type hidden animales[]" is passing all the elements, not the elements of the selected row.
`
<?php if (isset($solis) && !empty($solis)) { ?>
<thead>
<th>Opcion</th>
<th>Nombre Dueño</th>
<th>Nombre Guardian</th>
<th>Desde</th>
<th>Hasta</th>
<th>Direccion de guarda</th>
<th>Nombre de mascota</th>
<th>Especie</th>
<th>Raza</th>
<th>Observaciones</th>
</thead>
<tbody>
<?php if ($_SESSION['tipo'] == 'g') { ?>
<form action="<?php echo FRONT_ROOT ?>Guardian/operarSolicitud" method="POST">
<?php } else { ?>
<form action="<?php echo FRONT_ROOT ?>Dueno/cancelarSolicitud" method="POST">
<?php } ?>
<?php foreach ($solis as $solicitud) {
$count = 0;
?>
<?php foreach ($mascXsoli as $tabla) {
if ($tabla->getIdSolicitud() == $solicitud->getId()) {
$idMascotaX = $tabla->getIdMascota();
foreach ($mascotas as $masc) {
if ($masc->getId() == $idMascotaX) {
$count++;
}
} //contar para hacer el rowspan
}
} ?>
<tr>
<td rowspan="<?php echo $count; ?>">
<input type="hidden" name="solicitudId" value="<?php echo $solicitud->getId(); ?>">
<?php if ($_SESSION['tipo'] == 'g') { ?>
<button type="submit" name="operacion" value="aceptar" class="btn btn-danger"> Aceptar </button>
<button type="submit" name="operacion" value="rechazar" class="btn btn-danger"> Rechazar </button>
<?php } else { ?>
<button type="submit" class="btn btn-danger"> Cancelar </button>
<?php } ?>
</td>
<td rowspan="<?php echo $count; ?>"><?php echo $solicitud->getNombreDueno(); ?></td>
<td rowspan="<?php echo $count; ?>"><?php echo $solicitud->getNombreGuardian(); ?></td>
<td rowspan="<?php echo $count; ?>"><?php echo $solicitud->getFechaInicio(); ?></td>
<td rowspan="<?php echo $count; ?>"><?php echo $solicitud->getFechaFin(); ?></td>
<td rowspan="<?php echo $count; ?>"><?php echo $solicitud->getDireccionGuardian(); ?></td>
<?php foreach ($mascXsoli as $tabla) {
if ($tabla->getIdSolicitud() == $solicitud->getId()) {
$idMascotaX = $tabla->getIdMascota();
foreach ($mascotas as $masc) {
if ($masc->getId() == $idMascotaX) { ?>
<td><?php echo $masc->getNombre(); ?></td>
<td><?php echo $masc->getEspecie(); ?></td>
<td><?php echo $masc->getRaza(); ?></td>
<td><?php echo $masc->getObservaciones(); ?></td> <? //TAMAÑO Y FOTOS
?>
<?php if ($_SESSION['tipo'] == 'g') { ?>
<input type="hidden" name="animales[]" value="<?php echo $masc->getId(); ?>">
<?php } ?>
</tr>
<?php
}
}
}
}
}
} else {
echo " <h4>NO HAY SOLICITUDES ENVIADAS!</h4>";
?>
Si ha enviado una solicitud anteriormente, puede que esta haya sido aceptada y
deba pagar su confirmacion.
<?php } ?>
`
I use this structure in a lot of views, and all have the same problem.

How to merge tabular data in PHP

I have a table with weeks of a year and some data adjacent to each week, and i can't seem to merge the values correctly. Week rows with no data should stay empty
This is where i list the weeks
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td><?php echo $leadCount->theweek ?>
/ <?php echo $leadCount->theyear ?></td>
</tr>
<?php }
And this is where i try to merge
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td style="text-align: center">
<?php if ($getFtdCountDMm[$index]->theweek == $leadCount->theweek && !empty($getFtdCountDMm[$index]->ftdcount)) {
echo $getFtdCountDMm[$index]->ftdcount;
} else {
echo '0';
} ?>
</td>
</tr>
<?php } ?>
it should look like this
but it looks like this
Instead of running two loops run one loop like this.
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td><?php echo $leadCount->theweek ?>
/ <?php echo $leadCount->theyear ?>
</td>
<td style="text-align: center">
<?php if ($getFtdCountDMm[$index]->theweek == $leadCount->theweek && !empty($getFtdCountDMm[$index]->ftdcount)) {
echo $getFtdCountDMm[$index]->ftdcount;
} else {
echo '0';
} ?>
</td>
</tr>
<?php }

How to avoid duplicates in nested foreach loop php

Why are values are getting printed multiple times (see images below). I need to print them only once.
<?php foreach($patchData3 as $tes3){?>
<?php foreach($patchData1 as $tes){?>
<tr class="<?php if($tes->PATCH == $tes3->PATCH) {echo "red_color"; } ?>">
<td><?php echo $tes->HOSTNAME;?></td>
<td><?php echo $tes->VERSION;?></td>
<td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
</tr>
<?php } ?>
<?php }?>
<?php foreach($patchData1 as $tes){ ?>
<tr class="<?php if(checkfunction($tes->PATCH,$patchData3) == TRUE) { echo "red_color"; } ?>">
<td><?php echo $tes->HOSTNAME;?></td>
<td><?php echo $tes->VERSION;?></td>
<td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
</tr>
<?php } ?>
<?php
function checkfunction($patch,$patchData3){
foreach($patchData3 as $tes3){
if($patch == $tes3->PATCH){
return true;
}
}
}
?>
I used a function to overcome the duplication. Please comment if it does not work.

Table Province District and Subdistrict with PHP

I have table Province District and Subdistrict in my database and i want to print as output in html table as below:
but i get this:
here are my code:
<?php
$orderProv = 1;
//$QueryProvinces = Query Province;
foreach ($QueryProvinces as $QueryProvince) {
?>
<tr>
<td> <?php echo $orderProv; ?></td>
<td> <?php echo $QueryProvince->nameProv; ?></td>
<?php
//$QueryDistricts = Query District;
foreach ($QueryDistricts as $QueryDistrict ) {
?>
<td> <?php echo $QueryDistrict ->nameDist; ?></td>
<?php
}
?>
</tr>
<?php
}
?>
Please help me,
Thanks a lot.
You need to close and then open a new row with each loop of foreach ($QueryDistricts as $QueryDistrict ). Try something like this -
<?php
$orderProv = 1;
//$QueryProvinces = Query Province;
foreach ($QueryProvinces as $QueryProvince) {
?>
<tr>
<td> <?php echo $orderProv; ?></td>
<td> <?php echo $QueryProvince->nameProv; ?></td>
<?php
//$QueryDistricts = Query District;
$i = 0; // simple counter
foreach ($QueryDistricts as $QueryDistrict ) {
if($i>0){ // if not the 1st row, echo new row start
?>
<tr>
<td> </td>
<td> </td>
<?php
}
?>
<td> <?php echo $QueryDistrict ->nameDist; ?></td>
</tr>
<?php
$i++;
}
}
?>
Table Result:
html source code:
Complete code:
<table id="example1" class="table table-bordered table-condensed table-hover">
<thead>
<tr>
<th>No.</th>
<th width="20%">Provinsi</th>
<th>Kabupaten/Kotamadya</th>
<th>Kecamatan</th>
<th>Pendamping</th>
<th>Telepon</th>
<th>Email</th>
<th>Status Laporan</th>
</tr>
</thead>
<tbody>
<?php
$thecolor = array ("olive", "navy", "aqua", "maroon", "green", "yellow", "orange", "purple", "light-blue", "red");
//$infLokasis = Query;
$nomorProv = 1;
$thecolorProv = $thecolor;
$colorProv = 0;
foreach ($infLokasis as $infLokasi) {
# code... cetak nama provinsi
if ($colorProv >= 10) {
# code... warna set
$colorProv = 0;
}
?>
<tr>
<td><strong class="text-<?php echo $thecolorProv[$colorProv];?> small"><?php echo $nomorProv.". ";?></strong></td>
<td><strong class="text-<?php echo $thecolorProv[$colorProv];?> small"><?php echo $infLokasi->lokasi_nama;?></strong></td>
<?php
$i = 0; // simple counter
//$infKabkotas = Query;
$nomorKabkota = 1;
$thecolorKabkota = $thecolor;
$colorKabkota = 0;
foreach ($infKabkotas as $infKabkota) {
# code... cetak nama kabupaten
if ($colorKabkota >= 10) {
# code... warna set
$colorKabkota = 0;
}
if($i>0){ // if not the 1st row, echo new row start
?>
<tr>
<td></td>
<td></td>
<?php
}
?>
<td><strong class="text-<?php echo $thecolorKabkota[$colorKabkota];?> small"><?php echo $nomorProv.".".$nomorKabkota.". ".$infKabkota->lokasi_nama; ?></span></td>
<?php
$j = 0;
$infKecamatans = Query;
$nomorKecamatan = 1;
$thecolorKecamatan = $thecolor;
$colorKecamatan = 0;
foreach ($infKecamatans as $infKecamatan) {
if ($colorKecamatan >= 10) {
# code... warna set
$colorKecamatan = 0;
}
if($j>0){ // if not the 1st row, echo new row start
?>
<tr>
<td></td>
<td></td>
<td></td>
<?php
}
?>
<td><strong class="text-<?php echo $thecolorKecamatan[$colorKecamatan];?> small"><?php echo $nomorProv.".".$nomorKabkota.". ".$nomorKecamatan.". ".$infKecamatan->lokasi_nama; ?></strong></td>
<?php
$k = 0;
$idDaerah = substr($infKecamatan->lokasi_kode, 0,10);
$infPendampings = $db->get_results("SELECT id_p, nama_p, telepon_p, email_p FROM pendamping WHERE id_d LIKE '$idDaerah%' ");
$nomorPendamping = 1;
$thecolorPendamping = $thecolor;
$colorPendamping = 0;
if ($infPendampings) {
foreach ($infPendampings as $infPendamping) {
if($k>0){ // if not the 1st row, echo new row start
?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<?php
}
?>
<td>
<?php echo "<span class=\"text-".$thecolorPendamping[$colorPendamping]." small\">".$nomorPendamping.". "; ?></span>
<?php echo "<a href=\"vpendamping.php?str=".encryptor('encrypt', $infPendamping->id_p)."\" class=\"text-".$thecolorPendamping[$colorPendamping]." small\" target=\"_blank\">".namaGelar($infPendamping->nama_p); ?>
</td>
<td class="small"><?php echo $infPendamping->telepon_p; ?></td>
<td class="small"><?php echo $infPendamping->email_p; ?></td>
<td class="small"><?php echo "<i class=\"fa fa-fw fa-square-o text-red\"></i> Kosong"; ?></td>
<?php
$nomorPendamping++;
$colorPendamping++;
$k++;
}
}
else {
# code... row kosong
#if i romove 4 line code below... Provinsi name and Kabupaten/Kotamadya name should appear
echo "<td></td>\n";
echo "<td></td>\n";
echo "<td></td>\n";
echo "<td></td>\n";
}
?>
</tr>
<?php
$nomorKecamatan++;
$colorKecamatan++;
$j++;
}
?>
<?php
$i++;
$colorKabkota++;
$nomorKabkota++;
} // End infKabkota
$nomorProv++;
$colorProv++;
}
?>
</tbody>
</table>

help with array for last field in php or codeigniter

I have been on this for a day now and still cant solve it though it should be quite simple. I have a php code.
foreach($cart as $line=>$item)
{
echo form_open("sales/edit_item/$line");
?>
<td style="align:center;"><?php echo $item['name']; ?></td>
<?php if ($items_module_allowed)
{
?>
<td><?php echo form_input(array('name'=>'price','value'=>$item['price'],'size'=>'6'));?></td>
<?php
}
else
{
?>
<td><?php echo $item['price']; ?></td>
<?php echo form_hidden('price',$item['price']); ?>
<?php
}
?>
<td>
<?php
if($item['is_serialized']==1)
{
echo $item['quantity'];
echo form_hidden('quantity',$item['quantity']);
}
else
{
echo form_input(array('name'=>'quantity','value'=>$item['quantity'],'size'=>'2'));
}
?>
</td>
</div></div>
<td><?php echo to_currency($item['price']*$item['quantity']-$item['price']*$item['quantity']*$item['discount']/100); ?></td>
<?php
if($item['allow_alt_description']==1)
{
}
else
{
if ($item['description']!='')
{
}
else
{
}
}
?>
</td>
<td> </td>
<td style="color:#2F4F4F";>
<?php
if($item['is_serialized']==1)
{
}
?>
</td>
<td colspan=3 style="text-align:left;">
<?php
if($item['is_serialized']==1)
{
}
?>
</td>
</tr>
<tr style="height:3px">
<td colspan=8 style="background-color:white"> </td>
</tr> </form>
<?php
}
}
?>
This creates fields with relevant data and works ok. I just need to get the last field and echo it, I have tried everything and it still loops through the array, or does not work. This might be simple to someone else but it has confused me for a day now.
When you use a loop, such as for or foreach or while, it will iterate over every single child element of the array until it reaches the end. You don't need to loop, you simply need to access the last member of the array, like so:
$lastLine = end( array_keys($cart) );
echo form_open("sales/edit_item/{$lastLine}");
Edit: Now that I understand a bit better:
$lastItem = array_slice($cart, -1, null, true);
$line = key($lastItem);
$item = reset($lastItem);
echo form_open("sales/edit_item/{$line}");
?>
<!-- do all your html-ish stuff here. -->

Categories