is this a good way of doing it? or should i use a date time stamp with seconds rather?
while i am on a page, the new message must come through without me refreshing the page
my code is written in php:
<div id='div_longgray_gradient2'>
<div id='div_float_img'>
<table width='96%' border='0'>
<tr>
<td rowspan='2' width='40px' align='left'>
<img src='/images/rainbow/arrow.png'>
</td>
<td align='left'>
<span class='spn_big_black_rbc'>MESSAGES</span>
</td>
</tr>
<tr>
<td align='left'>
<span class='spn_med_lightblue_rbc'>Inbox</span>
</td>
<td align='left'>
</td>
</tr>
</table>
<br/>
<br/>
<table width='80%' align='center'>
<tr>
<td class='td_show_contact_heading' align='left'><span class='spn_med_lightblue_rbc'>Received</span></td>
<td class='td_show_contact_heading' align='left'><span class='spn_med_lightblue_rbc'>Message</span></td>
<td class='td_show_contact_heading' align='left'><span class='spn_med_lightblue_rbc'>From</span></td>
</tr>
<?php
$cursor = $pager->getFirstIndice();
foreach ($pager->getResults() as $msg)
{
$has_freechat = false;
//changed id to withid here
$freechat_req_link="profiles/confirmfreechat?withid=".$msg->getRcProfileTableRelatedByProfileIdFrom()->getId();
$freechat_req_link=link_to('Freechat',$freechat_req_link,'class=link_small_dark');
$cc = sizeof ($fc_records);
for($i = 0; $i < $cc; $i++)
{
if($fc_records[$i]->getProfileIdWith() == $msg->getProfileIdFrom())
{
$has_freechat = true;
break;
}
}
$unique_code_from = $msg->getRcProfileTableRelatedByProfileIdFrom()->getUniqueCode();
$block_url = link_to('Block User',"blocklist/block?unqiue_code=$unique_code_from",'class=link_medium_blue');
echo "<tr>";
$date = add_date($msg->getCreatedAt(),$hr=2);
echo "<td class='td_show_contact_item' align='left'>".$date."</td>";
$opened_once = $msg->getOpenedOnce();
if($opened_once >= 1)
{
echo "<td class='td_show_contact_item' align='left'>".link_to($msg->getSubject(), 'messagebox/read?cursor='.$cursor,'class=link_medium_blue')."</td>";
}
else
{ ?>
<td align='left'>
<a href="<?php echo url_for('messagebox/read?cursor=').$cursor ?>" style='color:#ff0000 !important' class='spn_small_red_rbc'><?php echo $msg->getSubject();?></a>
</td>
<?php
}
echo "<td class='td_show_contact_item' align='left'>".$unique_code_from." ( $block_url )</td>";
echo "</tr>";
++$cursor;
}
function add_date($givendate,$hr=2)
{
$cd = strtotime($givendate);
$newdate = date('Y-m-d H:i:s', mktime(date('h',$cd)+$hr, date('i',$cd), date('s',$cd), date('m',$cd), date('d',$cd), date('Y',$cd)));
return $newdate;
}
?>
</table>
<br/>
Displaying results <?php echo $pager->getFirstIndice() ?> to <?php echo $pager->getLastIndice() ?>.
<br/>
<?php if ($pager->haveToPaginate()): ?>
<?php echo link_to('«', 'messagebox/list?page='.$pager->getFirstPage()) ?>
<?php echo link_to('<', 'messagebox/list?page='.$pager->getPreviousPage()) ?>
<?php $links = $pager->getLinks(); foreach ($links as $page): ?>
<?php echo ($page == $pager->getPage()) ? $page : link_to($page, 'messagebox/list?page='.$page) ?>
<?php if ($page != $pager->getCurrentMaxLink()): ?> - <?php endif ?>
<?php endforeach ?>
<?php echo link_to('>', 'messagebox/list?page='.$pager->getNextPage()) ?>
<?php echo link_to('»', 'messagebox/list?page='.$pager->getLastPage()) ?>
<?php endif ?>
<table width='96%' border='0'>
<tr>
<td rowspan='2' width='40px' align='left'>
<img src='/images/rainbow/arrow.png'>
</td>
<td align='left'>
<span class='spn_big_black_rbc'></span>
</td>
</tr>
<tr>
<td align='left'>
<span class='spn_med_lightblue_rbc'>Sent Items</span>
</td>
</tr>
</table>
<br/>
<br/>
<table width='80%' align='center'>
<tr>
<td class='td_show_contact_heading' align='left'><span class='spn_med_lightblue_rbc'>Sent</span></td>
<td class='td_show_contact_heading' align='left'><span class='spn_med_lightblue_rbc'>Message</span></td>
<td class='td_show_contact_heading' align='left'><span class='spn_med_lightblue_rbc'>To</span></td>
</tr>
<?php
$cursor2 = $pager2->getFirstIndice();
$br = sizeof ($block_records);
foreach ($pager2->getResults() as $item)
{
$link = link_to('Delete',"messagebox/deleteSentmsg?mid=".$item->getId(),'class=link_medium_blue');
$id = $item->getRcProfileTableRelatedByProfileIdTo()->getId();
?>
<tr>
<td class='td_show_contact_item' width='15%' align='left'><?php echo $date = add_date($item->getCreatedAt(),$hr=2); ?></td>
<td class='td_show_contact_item' width='45%' align='left'><?php echo $item->getMessage()?></td>
<td class='td_show_contact_item' width='25%' align='left'><?php echo $item->getRcProfileTableRelatedByProfileIdTo()->getUniqueCode()." ".$link;?></td>
</tr>
<?php
++$cursor2;
}
?>
</table>
<br/>
Displaying results <?php echo $pager2->getFirstIndice(); ?> to <?php echo $pager2->getLastIndice(); ?>.
<br/>
<?php if ($pager2->haveToPaginate()): ?>
<?php echo link_to('«', 'messagebox/list?page2='.$pager2->getFirstPage(),'class=link_big_dark') ?>
<?php echo link_to('<', 'messagebox/list?page2='.$pager2->getPreviousPage(),'class=link_big_dark') ?>
<?php $links = $pager2->getLinks(); foreach ($links as $page2): ?>
<?php echo ($page2 == $pager2->getPage()) ? $page2 : link_to($page2, 'messagebox/list?page2='.$page2,'class=link_big_dark') ?>
<?php if ($page2 != $pager2->getCurrentMaxLink()): ?> - <?php endif ?>
<?php endforeach ?>
<?php echo link_to('>', 'messagebox/list?page2='.$pager2->getNextPage(),'class=link_big_dark') ?>
<?php echo link_to('»', 'messagebox/list?page2='.$pager2->getLastPage(),'class=link_big_dark') ?>
<?php endif ?>
</div>
</div>
can anyone give some advise please?
i have no ajax/JS here so dont know how i would incorporate it
PHP is a server side language and will not update a page once it has been fetched by the browser. To do this you will need to use a combination of server side and client side scripts.
One way of making your page update it's content is to use AJAX (http://api.jquery.com/jQuery.ajax/) combined with a timer (setInterval() and setTimeout()) to query a PHP script and check for updates every x seconds. The updates can then be added to the page when they are available using JavaScript / jQuery.
Related
This question already has answers here:
How do I highlight table row when fetching all information with array?
(2 answers)
Closed 3 months ago.
Given the following code, I want to highlight the row of a table wherein the $listing->Full == '1'.
<table id="datatable-responsive" cellspacing="0" width="100%">
<thead>
<tr>
<th align="center">Qty</th>
<th align="center">Posted Date</th>
<th align="center">Expiration</th>
<th align="center">Full Pkg</th>
</tr>
</thead>
<tbody>
<?php foreach($listings as $listing):
?>
<tr>
<td align="center"><?php echo $listing->quantity; ?></td>
<td align="center"><?php
$pdate = new DateTime($listing->posted_at);
echo $pdate->format('m/d/y'); ?></td>
<td align="center"><?php
$date = new DateTime($listing->expdate);
echo $date->format('m/d/y');
?>
</td>
<td align="center"><?php
if($listing->full == '1'):
?>
<?php echo "Yes"; ?>
<?php else:
?>
<?php echo "No"; ?>
<?php endif; ?>
</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
So ideally, the highlight color would be FFFFE6. Any help is greatly appreciated. This is an MVC site, so changing the CSS isn't conveniently an option.
Test before you set the style. Here is an inline example:
<?php
if($listing->full == '1'):
?>
<td align="center" style="background:#FFFFE6">
<?php echo "Yes"; ?>
<?php else: ?>
<td align="center">
<?php echo "No"; ?>
<?php endif; ?>
</td>
<?php endif; ?>
Here is an example using a class in your CSS:
CSS
.highlight {
background: #FFFFE6;
}
PHP/HTML
<?php
if($listing->full == '1'):
?>
<td align="center" class="highlight">
<?php echo "Yes"; ?>
<?php else: ?>
<td align="center">
<?php echo "No"; ?>
<?php endif; ?>
</td>
<?php endif; ?>
abscondingsince is a date field with value 2016-08-24 and dateofcontactviaphone is a date field with value 2016-08-26 . when I try to echo timespan($row['absconding since'], $row['dateofcontactviaphone']); its giving 2016-08-2446 Years, 8 Months, 3 Days, 5 Hours, 43 Minutes as the output whereas it should have been 2 days and so on. I have no clue where I am making a mistake. My view code is as below:
<table cellpadding="10px" border="1" style="background:none;width:75%;" RULES="ROWS" class="tab_data">
<thead>
<th>Employee Name</th>
<th>Absconding Since</th>
<th>Absconding Days</th>
<th>Phone Log</th>
<th>Comments on call made</th>
<th>Date of Email Sent</th>
<th>Comments/responses to email recorded</th>
<th>View Absconding Checklist</th>
</thead>
<tbody>
<?php
$serial_no = 1;
$i = 0;
foreach($rows as $row){
$i++;
?>
<tr>
<td style="width:10%">
<?php echo $row['name']; ?>
</td>
</td>
<td style="width:10%">
<?php echo $row['abscondingsince'];
echo timespan($row['abscondingsince'], $row['dateofcontactviaphone']);
?>
</td>
<td style="width:10%">
<?php echo $row['dateofcontactviaphone']; ?>
</td>
<td style="width:10%">
<?php echo $row['commentsphone']; ?>
</td>
<td style="width:10%">
<?php echo $row['dateofcontactviaemail']; ?>
</td>
<td style="width:10%">
<?php echo $row['commentsemail']; ?>
</td>
<td style="width:10%">
<?php
if ($row['last_status'] == 'Accepted'){
echo anchor('exits/view_exit_checklist/'.$row['id'],"<i class='fa fa-eye edit_row' alt='View' title='Edit' rel='".$row['id']."' ></i>",array('rel'=>$row->id,'class'=>'edit_row'));
}
else
{
echo "NA";
}
?>
</td>
</tr>
<?php
} ?>
</tbody>
</table>
Use like this:-
echo timespan(strtotime($row['absconding since']),
strtotime($row['dateofcontactviaphone']));
I had Created a Web App in PHP Code igniter which generates table containing calculation of employees earning, deduction.
Since I am newbie to CI. I am trying to save the dynamic generated table data to database, but failed.
I am not getting that how to POST table data to Controller.
Any help is welcome.
This is My Controller To generate dynamic table
function index()
{
if($this->input->post("submit"))
{
$vals=$this->cpanel_model->esic();
if($vals){
$data['w_share']=$vals->w_share;
$data['e_share']=$vals->e_share;
}
$data["wshare"]="";
//echo $data['w_share'];
$city=$_POST["city"];
$v1= $data['w_share'];
$v2= '100';
$v3= $data['e_share'];
$arr=$_POST["wid"];
$arr1=$_POST["days"];
$arr2=$_POST["payment"];
$arr3=$_POST["wdays"];
for ($i=0; $i <count($arr1) ; $i++)
{
$net[$i]=($arr2[$i]/$arr3[$i])*$arr1[$i];
$wshare[$i]=($net[$i]/$v2)*$v1;
$eshare[$i]=($net[$i]/$v2)*$v3;
$total[$i]=$eshare[$i]+$wshare[$i];
$wdays[$i]=$arr3;
$days[$i]=$arr1;
}
$data["net"]=$net;
$data["wshare"]=$wshare;
$data["total"]=$total;
$data["eshare"]=$eshare;
$data["wdays"]=$arr3;
$data["days"]=$arr1;
$data["list"]=$this->payment_model->search($city);
$this->load->view("secure/php-version/esic",$data);
}
This Is My View to echo table values
<?php echo form_open('esic/insert',array("role"=>"form")); ?>
<div class="table-responsive">
<table border="1" id="t01" class="table table-striped table-bordered table-hover table-condensed">
<tr>
<th>Worker_id</th>
<th>Worker Name</th>
<th>Worker City</th>
<th>Designation</th>
<th>Working Days</th>
<th>Basic Payment</th>
<th>Net Payment</th>
<th>Present Days</th>
<th>Worker Share (1.75%)</th>
<th>Employee Share (4.75%)</th>
<th>Total</th>
</tr>
<?php $i=0; $sum2=0; ?>
<?php if(count($list)>0) { foreach ($list as $std) {?>
<tr>
<?php echo form_hidden("wid[]",$std["wid"]); ?>
<?php echo form_hidden("wdays[]",$wdays); ?>
<?php echo form_hidden("payment[]",$std["payment"]); ?>
<td><?php echo $std["wid"]; ?></td>
<td><?php echo $std["wname"]; ?></td>
<td><?php echo $std["city"]; ?></td>
<td><?php echo $std["designation"]; ?></td>
<td><?php echo $wdays[$i]; ?></td>
<td><?php echo $std["payment"]; ?></td>
<td><?php echo $net[$i]; ?></td>
<td><?php echo $days[$i]; ?></td>
<td><?php echo $wshare[$i]; ?></td>
<td><?php echo $eshare[$i]; ?></td>
<td><?php echo $total[$i]; ?></td>
<?php $sum2 += $std["payment"]; ?>
</tr>
<?php $i++;}}?>
<tr>
<?php
$sum=0;
$sum1=0;
$sum3=0;
$sum4=0;
$sum5=0;
$i=0;
foreach ($total as $value) {
$sum = $sum + $value;
$i++;
}
foreach ($days as $value) {
$sum1 = $sum1 + $value;
$i++;
}
foreach ($eshare as $value) {
$sum3 = $sum3 + $value;
$i++;
}
foreach ($wshare as $value) {
$sum4 = $sum4 + $value;
$i++;
}
foreach ($net as $value) {
$sum5 = $sum5 + $value;
$i++;
}
?>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum2; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum5; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum1; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum4; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum3; ?>
</td>
<td style="font-weight:bold; font-size:10pt;">
<?php echo $sum; ?>
</td>
</tr>
</table>
</div>
<?php echo form_submit(array("name"=>"submit","type"=>"submit","value"=>"Create ESIC")); ?>
<?php echo form_close(); ?>
Its not too clear on how you are currently generating your table, but regardless, when you generate your table you have to place it in a form, and assign keys to inputs. Try this approach:
HTML (these are the results you should get when you generate your table)
<form action="<?= base_url() ?>table_data" method="post">
<table>
<thead>
<tr>
<th>Name</th>
<th>Earnings</th>
<th>Deduction</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="hidden" name="table[0][name]" value="name1">name1</td>
<td><input type="hidden" name="table[0][earning]" value="earning1">earning1</td>
<td><input type="hidden" name="table[0][deduc]" value="deduc1">deduc1</td>
</tr>
<tr>
<td><input type="hidden" name="table[1][name]" value="name1">name1</td>
<td><input type="hidden" name="table[1][earning]" value="earning1">earning1</td>
<td><input type="hidden" name="table[1][deduc]" value="deduc1">deduc1</td>
</tr>
</tbody>
</table>
<input type="submit" value="Submit this table">
</form>
Controller:
function table_data() {
if ($this->input->post()) {
$table_data = $this->input->post('table');
$this->load->model('model_name');
$this->model_name->insert_table_data($table_data);
}
}
Model:
function insert_table_data($table_data){
$this->db->insert_batch('mytable', $table_data);
}
i need to fill a table with mysql data, but i want to add a new line for every 3 mysql records.
filling the table its easy. but how do i tell to add a <tr> to every 3 records??
so far i have this:
<table width="30%" border="0">
<tbody>
<tr>
<?php if($totalRows_rsSdk>0){
do{
?>
<td align="center" valign="middle"><input type="checkbox" class="css-checkbox" name="sdkcheck" id="sdkcheck<?php echo $row_rsSdk['id']; ?>"/><label for="sdkcheck<?php echo $row_rsSdk['id']; ?>" class="css-label2 radGroup3"><?php echo $row_rsSdk['nome']; ?></label></td>
<td align="center" valign="middle"><img src="Images/sdk-icons/<?php echo $row_rsSdk['icon']; ?>" height="70"></td>
<?php } while ($row_rsSdk = mysql_fetch_assoc($rsSdk));} ?>
</tr>
</tbody>
</table>
Try this code:
<table width="30%" border="0">
<tbody>
<tr>
<?php if($totalRows_rsSdk>0){
$counter = 0;
do{
?>
<td align="center" valign="middle"><input type="checkbox" class="css-checkbox" name="sdkcheck" id="sdkcheck<?php echo $row_rsSdk['id']; ?>"/><label for="sdkcheck<?php echo $row_rsSdk['id']; ?>" class="css-label2 radGroup3"><?php echo $row_rsSdk['nome']; ?></label></td>
<td align="center" valign="middle"><img src="Images/sdk-icons/<?php echo $row_rsSdk['icon']; ?>" height="70"></td>
<?php
$blockSize = 3;
$counter++;
if ($counter >= $blockSize) {
$counter = 0;
?>
</tr>
<tr>
<?php
}
} while ($row_rsSdk = mysql_fetch_assoc($rsSdk));} ?>
<?php echo str_repeat('<td></td>',($blockSize-$counter)); ?>
</tr>
</tbody>
</table>
It is very simple, but I cannot do it. Now it takes every information from my database and introduces it in the Nr. td. How can I increment the value with every row?
<?php while ($row=mysql_fetch_array($query)) { ?>
<tbody>
<tr>
<td (this is what I want to auto-increment)>
<?php
$i = 1;
foreach($row as $i){
$i++;
echo $i;
}
?>
</td>
<td width="120">
<?php echo $row['Name'];?>
</td>
</tr>
</tbody>
<?php } ?>
Thanks!
Try this:
Modify your code as given below:
<tbody>
<?php
$i = 1;
while ($row=mysql_fetch_array($query)) { ?>
<tr>
<td width="5">
<?php
echo $i;
$i++;
?>
</td>
<td width="120">
<?php echo $row['Name'];?>
</td>
</tr>
<?php }?>
</tbody>
Initialize $i before the while loop then echo in the td then increment it.
<?php
$i=1;
while ($row=mysql_fetch_array($query)) { ?>
<tbody>
<tr>
<td width="5">
<?php
echo $i;
$i++;
//You can join the above 2 lines into 1 like this:
// echo $i++;
// This way you're telling php, echo $i then increment it by 1
?>
</td>
<td width="120">
<?php echo $row['Name'];?>
</td>
</tr>
</tbody>
<?php } ?>
NB: if you're just learning PHP, you shouldn't be learning mysql_* as they are deprecated and will be removed in future releases. Check out mysqli or PDO.
initialize
$i = 1;
outside while loop
while ($row=mysql_fetch_array($query))
, so that it will increase.
Have your row counter ($rowNum) outside of the while
<?php
$rowNum = 1; ?>
<tbody>
<?php while ($row=mysql_fetch_array($query)) { ?>
<tr>
<td width="5">
<?php echo $rowNum; $rowNum++ ?>
</td>
<td width="120">
<?php echo $row['Name'];?>
</td>
</tr>
<?php } ?>
</tbody>