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>
Related
How can i show data in two row separately as shown in image
I have tried this but i am not getting what i want. Also i don't want to use two loops separately.
<table class="tbl1">
</thead>
<tbody>
<tr>
<td>
<h1> date</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++;?>
<td>
<?php echo $value[ 'date']; ?>
<?php } ?>
</tr>
<tr>
<td>
<h1>Status</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++; ?>
<td>
<?php echo $value[ 'status']; ?>
</td>
<?php } ?>
</tr>
</tr>
</tbody>
</table>
Do a single loop and in that loop include 2 <tr>s for date and status.
<table class="tbl1">
<tbody>
<?php foreach ($student as $value) { ?>
<tr>
<td><h1> date</h1> </td>
<td><?php echo $value['date']; ?>
</tr>
<tr>
<td><h1>Status</h1></td>
<td><?php echo $value['status']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Also i don't see the purpose of using $i here and what about the rest of the column in front of date and status.
Or if you want only 2 rows and display all data column wise then you could do it like
<?php
$dateHTML = '';
$statusHTML = '';
foreach ($student as $value) {
$dateHTML .= '<td>'.$value['date'].'</td>';
$statusHTML .= '<td>'.$value['status'].'</td>';
}
?>
<table class="tbl1">
<tbody>
<tr>
<td><h1> date</h1> </td>
<?php echo $dateHTML;?>
</tr>
<tr>
<td><h1>Status</h1></td>
<?php echo $statusHTML;?>
</tr>
</tbody>
</table>
So, i've table like in picture
I want put looping row person on left side to singgle row select box on right side, like by color. And this is a code table right side.
<table>
<tr>
<td>No.</td>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<?php if(!empty($query)) { $number=1 ; foreach($query as $row) { ?>
<tr>
<td>
<?php echo $number++ ?>.</td>
<td>
<?php echo $row->column_a ?></td>
<td>
<?php echo $row->column_b ?></td>
<td>
<?php echo $row->column_c ?></td>
</tr>
<?php }} ?>
</table>
How to control looping iteration, to make this is happen?
Tanks a lot.
My friend just help to fix my problem,
<table>
<tr>
<td>No.</td>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<?php if(!empty($query)) { $number=1;$count=0; foreach($query as $row) { ?>
<?php if($count % 3 == 0){ ?>
<tr>
<td>
<?php echo $number++ ?>.</td>
<?php } ?>
<td>
<?php echo $row->column_a ?></td>
<td>
<?php echo $row->column_b ?></td>
<td>
<?php echo $row->column_c ?></td>
<?php $count++; if($count % 3 == 0){ ?>
</tr>
<?php }}} ?>
</table>
Ok, I have a variable called $total_bal that is the answer of a simple equation made from two queries stored in variables x and y
for example
$y = $row->amount_one;
$z = $row->amount_two;
$total_bal = $z + $y;
However I have many entries in amount_one and amount_two.
As I am using codeigniters active records
I tried
echo $this->db->count_all($total_bal);
But this dose not work, any idea of the best way to do this ?
So Im after a way to add all the $total_balup, for more incite into my code see bellow.
<?php
if (isset($records)) : foreach ($records as $row) :
$x = $row->amount_two;
$y = $row->ammount_one;
$total_bal = $z + $y;
?>
<table>
<tbody>
<tr>
<td>amount one</td>
<td>amount two</td>
</tr>
<tr>
<td>
<?php echo $x;?>
</td>
<td>
<?php echo $y;?>
</td>
<td>
<?php echo $$total_bal;?>
</td>
</tr>
<!-- <tr>-->
<!-- <td>-->
<!-- --><?php //echo $this->db->count_all('$total_bal'); ?>
<!-- </td>-->
<!-- </tr>-->
</tbody>
</table>
<?php endforeach; ?>
<?php else : ?>
<h3>You Have No Accounts</h3>
<h4>Why No Add A Account?</h4>
<?php endif; ?>
One way to do this is to use an accumulator variable. Take for example, a $grandTotal variable. You set it to 0 outside the foreach, over each iteration of the loop, you add the $rowTotal to the $grandTotal. Eventually when the loop ends, you have a total value of all row totals.
The benefit to this method is that it doesn't require any additional calls to the database and since you are already looping through the values to display them, accumulating them is minimal processing.
<?php if (isset($records)) : ?>
<table>
<thead>
<tr>
<th>Amount One</th>
<th>Amount Two</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php $grandTotal = 0; ?>
<?php foreach ($records as $row) : ?>
<?php
// Add field values to get row total
$rowTotal = $row->amount_one + $row->amount_two;
?>
<?php
// Add row total to grand total
$grandTotal += $rowTotal;
?>
<tr>
<td>
<?php echo $row->amount_one;?>
</td>
<td>
<?php echo $row->amount_two;?>
</td>
<td>
<?php echo $rowTotal;?>
</td>
</tr>
<?php endforeach; ?>
<tr>
<td></td>
<td></td>
<td><?php $grandTotal; ?></td>
</tr>
</tbody>
</table>
<?php else : ?>
<h3>You Have No Account</h3>
<h4>Why Not Add An Account?</h4>
<?php endif;?>
I'm trying to catch the last loop in my CodeIgniter for-loop. I'm trying to generate a < hr /> under every news item but the last one. This is my code:
<table class="news">
<?php foreach($news as $news_item): ?>
<tr>
<td class="headline"><?php echo $news_item['title']; ?></td>
</tr>
<tr>
<td class="text"><?php echo $news_item['text']; ?></td>
</tr>
<tr>
<td><hr /></td>
</tr>
<?php endforeach; ?>
</table>
Уou can try something like this:
<?php for($i = 0, $lastIDX = count($news)-1; $i<=$lastIDX; $i++): ?>
<!-- html code ... $news_item is $news[$i] now -->
<? if ($i !== $lastIDX) : ?>
<tr>
<td><hr /></td>
</tr>
<?php endif; ?>
<?php endfor; ?>
But it would be better to use CSS :last-child selector.
You can use this one as a solution:
$last = end($news);
<table class="news">
<?php foreach($news as $news_item): ?>
<tr>
<td class="headline"><?php echo $news_item['title']; ?></td>
</tr>
<tr>
<td class="text"><?php echo $news_item['text']; ?></td>
</tr>
<tr>
<td><?=(($last == $news_item)?"":"<hr />")?></td>
</tr>
<?php endforeach; ?>
</table>
Where the $last is the last array of your array XD.
Get the last key of the array beforehand and compare it the current one while looping:
$keys = array_keys($news);
$last_key = end($keys);
foreach ($news as $news_key => $news_item) {
if ($news_key == $last_key) {
// at the last item
} else {
// not at the last item
}
}
Also you could count the elements in your array beforehand, and have a counter inside the loop that increments on every round, so you can tell if you are at the end or not.
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.