Adding serial number to the table - php

I have this PHP code that displays data from the MySQL database. I would like to add a serial number for which I tried with the following code but it doesn't work as it's not in the loop and also it doesn't know endpoint and so I have max at 12:
<?php foreach (range(0, 12) as $number) { echo $number; }?>
Here's the main PHP code. How do I add a serial number to the <td> block:
<tbody>
<?php foreach ($items as $i => $item) : ?>
<tr id="tablelist_item_<?php echo $i; ?>" class="<?php echo $fc_item_classes.' row'.($i%2 ? 1 : 0); ?>" <?php echo $microdata_itemtype_code; ?>>
<?php if ( $buttons_exists || $show_title || count($item->css_markups) ) : ?>
<td><?php foreach (range(0, 12) as $number) { echo $number; }?></td>
<td class="fc_title_col">
<?php if ($show_title) : ?>
<!-- BOF item title -->
<span class="fc_item_title" itemprop="name">
<?php if ($link_titles) : ?>
<?php echo $item->title; ?>
<?php else : ?>
<?php echo $item->title; ?>
<?php endif; ?>
</span>
<!-- EOF item title -->
<?php endif; ?>
<div class="fcclear fc_beforemarkups"></div>
</td>
<?php endif; ?>
<!-- BOF item fields -->
<?php foreach ($columns as $name => $label) : ?>
<td><?php echo isset($item->positions['table']->{$name}->display) ? $item->positions['table']->{$name}->display : ''; ?></td>
<?php endforeach; ?>
<!-- EOF item fields -->
</tr>
<?php endforeach; ?>
</tbody>

I ended up with this: <td><?php echo ($i+1); ?></td>
and it works :)

Related

foreach PHP something wrong

I have this code in PHP
<?php foreach($this->group('filter') as $i => $fields): ?>
<?php $newField = $this->field('name#'.$i)->value(); ?>
<?php $finalvar = explode(",", $newField); ?>
<a data-filter=<?php foreach($finalvar as $fletter) : ?>".filter_<?php echo standardize($fletter); ?>">
<?php endforeach; ?>
<?php if($this->field('icon#'.$i)->value()): ?><i class="<?php echo $this->field('icon#'.$i)->value(); ?>"></i><?php endif; ?>
<span class="name"><?php if($this->field('label_items#'.$i)->value()): ?><?php echo $this->field('label_items#'.$i)->value(); ?><?php else: ?><?php echo $this->field('name#'.$i)->value(); ?><?php endif; ?>
</span>
</a>
<?php endforeach; ?>
PHP code works but I have a problem on the HTML code. The generated HTML code is this:
<a data-filter=".filter_country">
".filter_capital;
<span class="name">Australia</span>
</a>
My goal is to implement this one:
<a data-filter=".filter_country,.filter_capital"
<span class="name">Australia</span>
</a>
I know it's something in front of me but I cannot see it. Does anyone have an idea concerning the syntax?
Check your syntax:
<?php foreach ($this->group('filter') as $i => $fields) : ?>
<?php $newField = $this->field('name#' . $i)->value(); ?>
<?php $finalvar = explode(",", $newField); ?>
<a data-filter="
<?php
$i = 1;
foreach ($finalvar as $fletter) { ?>
.filter_<?php echo standardize($fletter); ?>
<?php if ($i < count($fletter)) { ?>, <?php } ?>
<?php $i++; } ?>
">
<?php if ($this->field('icon#' . $i)->value()) : ?><i class="<?php echo $this->field('icon#' . $i)->value(); ?>"></i><?php endif; ?>
<span class="name"><?php if ($this->field('label_items#' . $i)->value()) : ?><?php echo $this->field('label_items#' . $i)->value(); ?><?php else : ?><?php echo $this->field('name#' . $i)->value(); ?><?php endif; ?>
</span>
</a>
<?php endforeach; ?>

how to allow more than 1 rows to appear horizontally

This is an example of what I want to achieve
but what I was able to display is 1 product per row.
This is what I have done and but is not working as intended.
<ol>
<?php
$no = 1;
foreach($projects as $project):?>
<?php if($project['finish'] == 'no'):?>
<li><?=$no?></li>
<li><?=$project['type']?></li>
<li><?=$project['date_started']?></li>
<li><?=$project['brief_description']?></li>
<li><?=$project['full_description']?></li>
<li>
<?php foreach($images as $image):?>
<?php if($image['project_id'] == $project['id']):?>
<img src="img/<?=$image['name']?>">
<?php endif;?>
<?php endforeach;?>
</li>
<li>
Contact admin
</li>
<?php endif;?>
if($no % 5 === 0):
echo "<br>";
endif;
<?php $no++; ?>
<?php endforeach;?>
How can I get the code to display 5 lists per row
<html>
<body>
<table>
<?php
echo '<tr>';
for($i=1;$i<100;$i++)
{
echo '<td>'.$i.'</td>';
if($i%5==0)
{
echo $i;
echo '</tr><tr>';
}
}
?>
</table>
</body>
</html>
This will you the idea

PHP foreach: data as value less than a number

I need the code to be executed only if value of $entry[0] is less than 10, but it doesn't work.
I know the error is here: $data as $entry[0] < 10
The data is an excel worksheet and $entry[0] is serial numbers from 1 to 100 and $entry[1] is text field (domain names).
<?php $data = wp_excel_cms_get("top100"); ?>
<?php foreach($data as $entry[0] < 10): ?>
<?php echo $entry[0]." ";?>
<a href ="<?php echo "http://". $entry[1]; ?>" target="_blank">
<?php echo $entry[1];?></a><br />
<hr />
<?php endforeach; ?>
You are using foreach in a wrong way. First, iterate over data, then apply your logic in your sheet row data:
<?php $data = wp_excel_cms_get("top100"); ?>
<?php foreach($data as $entry): ?>
<?php if($entry[0] < 10): ?>
<?php echo $entry[0]." ";?><?php echo $entry[1];?><br />
<?php endif; ?>
<hr />
<?php endforeach; ?>
<?php
$data = wp_excel_cms_get("top100");
foreach($data as $entry[0] ){
if ($entry[0] < 10){ echo $entry[0]." ";?>
<?php echo $entry[1];?><br />
<?php } ?>
<hr>
<?php endforeach; ?>

PHP loop interject

Consider the following loop:
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value != ''): ?>
<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</div>
<?php endif; ?>
<?php endforeach; ?>
I wanted to wrap the first 12 items in a div and then the last 2 items in a div. The problem is there are not always 12 items exactly in the first div. There can be between 2 AND 12 items.
How would I manipulate this loop to achieve such? Many thanks
Just use a counter inside the loop to see how many times you've been through it.
<?php $count = 1; ?>
<?php $break= count($this->item->extra_fields) - 2; ?>
<?php echo "<div>"; ?>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value != ''): ?>
<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</div>
<?php $count++; ?>
<?php endif; ?>
<?php if ($count == $break) : ?>
<?php echo "</div><div>"; $count ==0; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php echo '</div>'; ?>

Display rows including empty rows in symfony 1

I'm a newbie in web development, and i want to modify an existing project.I want to display rows including a null value,a default value of zero or the empty one.This is the code in action..
public function executeDetails(sfWebRequest $r) {
if($this->getUser()->hasAttribute('extend_term_ch')) {
$this->getUser()->getAttributeHolder()->remove('extend_term_ch');
}
$this->loan = Doctrine_Core::getTable('Loans')->findOneById($r->getParameter('id'));
$this->debits = Doctrine_Core::getTable('CreditLogs')->getByLoanId($r->getParameter('id'));
}
and in templates
<?php $totalAmount = 0 ?>
<?php foreach($loan->Collections as $i=>$c): ?>
<?php $totalAmount += $c->amount ?>
<tr class="<?php echo ($i%2==0)?'even':'odd' ?>">
<td>
<?php echo $c->mode_of_payment ?>
<?php if($c->mode_of_payment == 'Cheque'): ?>
<?php $cReferencedObj = $c->Cheques ?>
<div class="small_detail info">
<span class="bold">(<?php echo $cReferencedObj->bank ?>, <?php echo $cReferencedObj->branch ?>) <?php echo $cReferencedObj->reference_number ?></span>
</div><br />
<?php endif; ?>
<?php $replaced = $c->getReplacedCheque() ?>
<?php if(!empty($replaced)): ?>
<div class="small_detail info">
Payment for <span class="bold">(<?php echo $replaced->bank ?>, <?php echo $replaced->branch ?>) <?php echo $replaced->reference_number ?></span>
</div><br />
<?php endif; ?>
</td>
<td><?php echo $c->batch_no ?></td>
<td>
PHP <?php echo number_format($c->amount, 2) ?>
<?php if($c->mode_of_payment == 'Cheque' && !empty($c->Cheques->replaced_with)): ?>
<?php $replaced = $c->Cheques->Replaced ?>
<div class="small_detail info">
Replaced with <span class="bold">(<?php echo $replaced->bank ?>, <?php echo $replaced->branch ?>) <?php echo $replaced->reference_number ?></span>
</div>
<?php endif; ?>
</td>
<td><?php echo date('F d, Y', strtotime($c->date_created)) ?></td>
<td><?php echo $c->created_by ?></td>
<td><?php echo $c->date_created ?></td>
<td>
<?php if(in_array($c->mode_of_payment, array('Cheque', 'ATM', 'Direct Bank Transfer'))): ?>
<span class="info">
View Details
</span>
<?php else: ?>
<div class="small_detail info">
Collector: <span class="bold"><?php echo $c->Collectors->lastname.', '.$c->Collectors->firstname ?></span>
</div>
<?php endif; ?>
<?php if(in_array($loan->mode_of_payment, array('Cash', 'ATM')) && $sf_user->hasCredential('COLLECTIONS_ROLLBACK')): ?>
<div class="small_detail bullet_go">
Rollback Collection
</div>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="6">Total Paid</td>
<td>PHP <?php echo number_format($totalAmount, 2) ?></td>
</tr>
This will only display rows with data..how about displaying the empty row?For example a person has to pay his debts in 40 days from April 1, 2014 up to March 10,2014, then for some reason, he did not pay April 4, 6, 8 and so on.I want to display those dates 4, and 6 and 8 and display some errors for every date he failed to pay it like "No payment for this date".

Categories