foreach in table format using codeigniter [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
<tbody>
<?php foreach($result as $row){ ?>
<?php foreach($result1 as $row1) {?>
<tr>
<td >
<span><?php echo $row->state; ?></span>
</td>
<td >
<span><?php echo $row->breakdown_grants; ?></span>
</td>
<td >
<span><?php echo $row1->breakdown_grants; ?></span>
</td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
I need to display my coding in table format.in that first foreach contain state and breakdown_grants value.
state actual provisional
$row->state $row->breakdown_grants $row1->breakdown_grants
this is my table format.what i need to change in my foreach.in $result and $result1 i'll get different data.

Better you use for loop like
<?php for($i=0;$i<count($result);$i++) { ?>
<tr>
<td>
<span><?php echo $result[$i]->state; ?></span>
</td>
<td>
<span><?php echo $result[$i]->breakdown_grants; ?></span>
</td>
<td>
<span><?php echo $result1[$i]->breakdown_grants; ?></span>
</td>
</tr>
<?php } ?>
Make sure that $result and $result1 are of same indexes and of same lengths.

Related

how to get the total of the earnings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
the code is expected to calculate the total amount of the "Net Earning" row. How do i write a code to do that. The $stmt variable is associated to a query from the database
<div class="block-card-body">
<div class="my-table table-responsive">
<table class="table align-items-center table-flush mb-0">
<thead class="thead-light">
<tr>
<th>Order ID</th>
<th>Amount</th>
<th>Fee</th>
<th>Net Earning</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<?php
foreach ($stmt as $val) {
?>
<td><?php echo $val['PAY_ID']; ?></td>
<td class="text-color">$<?php echo $val['PAY_AMOUNT']; ?></td>
<td class="text-danger">$<?php echo ($val['PAY_AMOUNT']) * 0.1 ; ?></td>
<td class="text-success">$<?php echo $val['PAY_AMOUNT'] - (($val['PAY_AMOUNT']) * 0.1) ; ?></td>
<td><?php echo $val['PAY_DATE']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div><!-- end block-card-body -->
Create a variable to use as an accumulator, initialise it to zero.
Then in the loop, calulate the net and add it to your accumulator. How you display it is up to you later.
Also note, I moved the <tr> inside the loop so you get a well formed table row
<tbody>
<?php
$netTotal = 0;
foreach ($stmt as $val) {
?>
<tr>
<td><?php echo $val['PAY_ID']; ?></td>
<td class="text-color">$<?php echo $val['PAY_AMOUNT']; ?></td>
<td class="text-danger">$<?php echo ($val['PAY_AMOUNT']) * 0.1 ; ?></td>
<?php
$t = $val['PAY_AMOUNT'] - (($val['PAY_AMOUNT']) * 0.1);
$netTotal += $t;
?>
<td class="text-success">$<?php echo $t;?></td>
<td><?php echo $val['PAY_DATE']; ?></td>
</tr>
<?php
}
?>
</tbody>

How to view data based on values ​from database ci3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I want to make a recapitulation of absent data, which I display using a table in CI. The problem that occurs to me is that the data that appears in the table is not in date order.
I want to make a report like this
enter image description here
The absence data does not match based on the date data above the table
How to display the data according to the date data that I called from the database?
Here is my code:
<tbody>
<tr class="text-center">
<td>Nip</td>
<td>Nama Lengkap</td>
<?php
foreach ($tampiltgl as $data) :?>
<td>
<?php echo $data['waktu']; ?>
</td>
<?php endforeach; ?>
</thead>
<tbody>
<?php
$no=1;
foreach ($tampil as $data) :?>
<tr>
<td><?php echo $data['nip']; ?> </td>
<td>
<?php echo $data['nm_lengkap']; ?>
</td>
<?php
date_default_timezone_set("Asia/Jakarta");
$timestamp = date('m');
$opd=$this->session->userdata('opd');
$tgl=$this->db->query ("SELECT DISTINCT waktu FROM absen_pagi where opd='$opd'AND MONTH(waktu) ='$timestamp'")->result_array();
$absen = $this->db->query ("SELECT DISTINCT waktu, status from absen_pagi where nip='".$data['nip']."' AND opd='$opd' AND MONTH(waktu) ='$timestamp'")->result_array();
foreach ($absen as $key) :?>
<td><?php echo $key['status']; ?> </td>
</td>
<?php endforeach; ?>
<?php endforeach; ?>
</tbody>
</table>
I think you're looking for this:
<table>
<thead>
<tr class="text-center">
<td>Nip</td>
<td>Nama Lengkap</td>
<?php
foreach ($tampiltgl as $data) :?>
<td>
<?php echo $data['waktu']; ?>
</td>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
$no=1;
foreach ($tampil as $data) :?>
<tr>
<td><?php echo $data['nip']; ?> </td>
<td>
<?php echo $data['nm_lengkap']; ?>
</td>
<?php
date_default_timezone_set("Asia/Jakarta");
$timestamp = date('m');
$opd=$this->session->userdata('opd');
$tgl=$this->db->query ("SELECT DISTINCT waktu FROM absen_pagi where opd='$opd'AND MONTH(waktu) ='$timestamp'")->result_array();
$absen = $this->db->query ("SELECT DISTINCT waktu, status from absen_pagi where nip='".$data['nip']."' AND opd='$opd' AND MONTH(waktu) ='$timestamp'")->result_array();
foreach ($tampiltgl as $tampiltgldata) {
$celldata = '';
foreach ($absen as $key) {
if ($tampiltgldata['waktu'] == $key['waktu']) {
$celldata = $key['status'];
break;
}
}
echo "<td>$celldata</td>";
} ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
When displaying the $absen data from the database, loop over the $tampiltgl array that you used to print the dates in the header.
For each date in the $tampiltgl array, check if there is an entry in the $absen array with the same date. If a match is found, print the status. If no match was found print an empty table cell.

Generating an HTML table with PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've run into a problem with php, I have this table:
<?php
$s=(" SELECT * FROM my_tbl");
$W=mysql_query($s);
?>
<table>
<tr>
<td> <center><strong>Member </strong></center></td>
</tr><tr>
<td> <center><strong> Total Meal </strong></center></td>
</tr>
<?php while ( $r=mysql_fetch_array($W)) {$i++; ?>
<tr > <td> <?= $r["name"]; ?> </td></tr>
<tr>
<td> <?= $r["tmeal"]; ?> </td>
</tr><?php } ?>
</table>
But I'm looking for this output:
How can I improve my code so I can get this result?
$conn = mysqli_connect('server','username','password',"DB NAme") or die("error");
$queryString = "select * from TableNAme";
$queryres = mysqli_query($conn, $queryString) or die("not fire");
echo "<table>";
while($row = mysqli_fetch_array($queryres))
{
echo "<tr><td>$row[0]</td><td>$row[0]</td><td>$row[0]</td></tr>";
}
echo "</table>";
This may help you
<?php
$con = mysql_connect("localhost", "root", "mypass") or
die("Could not connect: " . mysql_error());
mysql_select_db("your_db");
$s=(" SELECT * FROM my_tbl");
$W=mysql_query($s); ?>
<table>
<tr>
<td> <center><strong>Member </strong></center></td>
<td> <center><strong> Total Meal </strong></center></td>
</tr>
<?php while ( $r = mysql_fetch_assoc($W)) { ?>
<tr > <td> <? php echo $r['name']; ?></td>
<td> <? php echo $r['tmeal']; ?> </td>
</tr><?php } ?>
</table>

Display image using php with <img src="" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have stupid problem with the html/php rules. I'm trying to show an image from an apache server with this code using a table:
<?php
//code
while($row = mysqli_fetch_array($result)) {
echo '
<tr>
<td> '.$row['x'].' </td>
<td> '.$row['y'].' </td>
<td> '.$row['z'].' </td>
<td> '.$row['f'].' </td>
<td> '.$row['g'].' </td>
<td> '.$row['d'].' </td>
<td><img src=\"<?php echo $url; ?>\"/></td>
</tr>';
}
//code
?>
But obviusly the inner php script is considered as normal text and no run!
Don't echo large blocks of HTML in PHP like that. Its bad practice. No, actually, its horrible practice. Instead learn to open and close the PHP tag as needed, like:
<?php
//code..code...code...
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td> <?php echo $row['x']; ?> </td>
<td> <?php echo $row['y']; ?> </td>
<td> <?php echo $row['z']; ?> </td>
<td> <?php echo $row['f']; ?> </td>
<td> <?php echo $row['g']; ?> </td>
<td> <?php echo $row['d']; ?> </td>
<td><img src="<?php echo $url; ?>"/></td>
</tr>
<?php
}
//code..code...code
?>
There are several benefits to this, including that its less likely to break syntax highlighting and your code is not defaced with as many \" all over the place.
You're already inside PHP - you shouldn't open another <?php scope:
echo '
<tr>
<td> '.$row['x'].' </td>
<td> '.$row['y'].' </td>
<td> '.$row['z'].' </td>
<td> '.$row['f'].' </td>
<td> '.$row['g'].' </td>
<td> '.$row['d'].' </td>
<td><img src="' .$url . '"/></td>
</tr>';
Just change this:
<td><img src=\"<?php echo $url; ?>\"/></td>
to:
<td><img src="' . $url .'"/></td>
Try it:
<?php
while($row = mysqli_fetch_array($result)){
echo '
<tr>
<td> '.$row['x'].' </td>
<td> '.$row['y'].' </td>
<td> '.$row['z'].' </td>
<td> '.$row['f'].' </td>
<td> '.$row['g'].' </td>
<td> '.$row['d'].' </td>
<td><img src="'.$url.'"/></td>
</tr>';
}
?>
It just a problem in concatenation, you need just combine those strings, not set another 'echo script'.
you can simply close the php section and put plain html, then reopen php when needed like this :
<?php
$test = array( "a test","also","a","test");
$itteraror = 0;
$url = "#";
?>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB" lang="en-GB">
<head>
</head>
<body>
<table>
<?php
while($itteraror<sizeof($test)) {
?>
<tr>
<td> <?php echo $test[$itteraror] ; ?></td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td><img src="<?php echo $url ; ?>" alt="my image"></td>
</tr>
<?php
$itteraror++;
}
?>
</table>
</body>
</html>
i did make some modification to your code in order for it to be standalone for testing purpose.

I have created a search function using php and mysql but it will only search one word? how can i make it search a phrase

I have created a search function but it only works by entering in one key word. How can I let someone enter in a phrase and it will display results? at the minute im having to use _ and telling customers to use _ instead of space.
<div class="post">
<h1 class="title">Search Results for: <?php echo $_POST['keywords']; ?></h1>
<table style="width: 100%">
<tr>
<td></td>
</tr>
<?php
$searchterm = $_POST['keywords'];
include 'db.inc.php';
$query = mysql_query("SELECT * FROM search WHERE keywords like '%$searchterm%'");
while($row = mysql_fetch_array($query))
{
?>
<tr>
<td style="font-size:x-large"><a href="./<?php echo $row['link']; ?>">
<?php echo $row['title']; ?> </a></td>
</tr>
<tr>
<td><?php echo $row['description']; ?></td>
</tr>
<tr>
<tr>
</tr>
<?php
}
?>
</table>
</div>
<div class="post">
</div>
</div>

Categories