I want to save my input values into a 2D array.
<table>
<?php
$kriteria = array('IP', 'SE', 'PE', 'BE');
?>
<thead>
<tr>
<th>Kriteria</th>
<?php
foreach ($kriteria as $val) {
echo '<th>' . $val . '</th>';
}
?>
</tr>
</thead>
<tbody>
<?php $n = count($kriteria); ?>
<?php for ($i = 0; $i < $n; $i++): ?>
<tr>
<th>
<?= $kriteria[$i] ?>
</th>
<?php for ($j = 0; $j < $n; $j++): ?>
<td><input type="text" class="form-control" id="<?= $kriteria[$j] . $kriteria[$i] ?>" name="<?= $j. $i ?>" value=""></td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</tbody>
</table>
I tried with the POST method but it didn't work.
I want to use the value from the input for counting in another page.
this the result
I want to use the value from the input for counting in another page.
public function page1()
{
$kriteria = json_encode(array('IP', 'SE', 'PE', 'BE'));
$temp = sys_get_temp_dir() . '/tmp_file_123';
file_put_contents($temp, $kriteria);
}
public function page2()
{
$temp = sys_get_temp_dir() . '/tmp_file_123';
$kriteria = file_get_contents($temp);
}
Related
I have three tables created by uploaded data from the previous file. I would like to insert data in defined places, so that each service is a separate record in the table and has a separate quantity and amount assigned.
My php function:
function listService()
{
$service_chcecked = $_POST['service_chcecked'];
$quantity = $_POST['quantity_chcecked'];
$net_price = $_POST['net_price_chcecked'];
for ($x = 0; $x < count($service_chcecked); ++$x) {
echo '<tr><td>id:' . $x . '</td><td>name:' . $service_chcecked[$x] . '</td>';
for ($y = 0; $y < count($net_price); ++$y) {
echo '<td>price:' . $net_price[$y] . '</td>';
for ($z = 0; $z < count($quantity); ++$z) {
echo '<td>quantity:' . $quantity[$z] . '</td>';
};
};
echo '</tr>';
}
}
And my html place:
<div class='services'>
<table>
<tr>
<th><span>NO.</span></th>
<th><span>Service name</span></th>
<th><span>Net price</span></th>
<th><span>Quantity</span></th>
</tr>
<?php listServiceName(); ?>
</table>
</div>
Now it displays to me this way, with repeated data at the end :/
broken table
That's because you are nesting your for loops. You create the <tr> and the "id" and "name" <td> for each item in service_chcecked. Then you create a "price" <td> for each service_chcecked * net_price_chcecked. And your 3rd nested loop creates a "quantity" column for each service_chcecked * net_price_chcecked * quantity_chcecked. That's why you end up with that broken table. It depends on how you receive the POST data, but if your three arrays always have the same length, you could do it all in one loop:
function listService()
{
$service_chcecked = $_POST['service_chcecked'];
$quantity = $_POST['quantity_chcecked'];
$net_price = $_POST['net_price_chcecked'];
for ($x = 0; $x < count($service_chcecked); ++$x) {
echo '<tr><td>id:' . $x . '</td><td>name:' . $service_chcecked[$x] . '</td>';
echo '<td>price:' . $net_price[$x] . '</td>';
echo '<td>quantity:' . $quantity[$x] . '</td>';
echo '</tr>';
}
}
Its hard to understand whats happening but less is more and try not to mix your data & html inside your function its going to confuse you later on.
<?php
$services = $_POST['service_checked'];
$qtys = $_POST['quantity_checked'];
$prices = $_POST['net_price_checked'];
?>
<div class='services'>
<table>
<tr>
<th>NO.</th>
<th>Service name</th>
<th>Net price</th>
<th>Quantity</th>
</tr>
<? foreach($services as $k=>$service){?>
<tr>
<td><?=$k?></td>
<td><?=$service?></td>
<td><?=$qtys[$k]?></td>
<td><?=$prices[$k]?></td>
</tr>
<? }?>
</table>
</div>
I have an PHP for loop; which looks at a number in database and outputs the tables rows to number of times.
Here is my HTML:
<?php
for($x=0; $x < 5; $x++){
$row_count =1;
?>
<table class="table table-bordered" id="ticktable">
<tr class=" <?php echo $row_count; ?>">
<?php
for($w=0; $w < 5; $w++){
?>
<td>
<img class="yellow-sign" src="http://www.backgroundsy.com/file/large/yellow-sign.jpg" width="100">
</td>
<?php
}
?>
</tr>
</table>
<?php
$row_count ++;
}
?>
The above code display this:
How I can assign unique class to each of the rows?
Labelled Image
Example:
<?php foreach($data as $row) { ?>
<tr id="data<?php echo $row['id']; ?>"> </tr>
<?php } ?>
I hope it will help you.
If you are trying to get unique class to tr then do this
for ($x = 0; $x < 5; $x++) {
$row_count = 1;
$randClass = rand(1111,9999).$row_count;
?>
<table class="table table-bordered" id="ticktable">
<tr class="<?php echo $randClass; ?>">
<?php
for ($w = 0; $w < 5; $w++) {
?>
<td>
<img class="yellow-sign <?php echo $randClass; ?>" src="http://www.backgroundsy.com/file/large/yellow-sign.jpg" width="100">
</td>
<?php
}
?>
</tr>
</table>
<?php
$row_count++;
}
rand(1111,9999) will generate random number between 1111 and 9999
EDIT
i have also added $row_count so that the number can not be repeated even if there is large data. What I've done is: <tr class="<?php echo rand(1111,9999).$row_count; ?>">
UPDATE
first store the random string in a variable $randClass = rand(1111,9999).$row_count;
then echo out where you want the variable like this:
$randClass = rand(1111,9999).$row_count;
and <img class="yellow-sign <?php echo $randClass; ?>" >
Here is the updated code for you. Check this
<?php
$color_class[] = array("yellow-sign","red-sign","green-sign","blue-sign","black-sign");
$row_count = 0;
for($x=0; $x < 5; $x++){
?>
<table class="table table-bordered" id="ticktable">
<tr class=" <?php echo $row_count; ?>">
<?php
for($w=0; $w < 5; $w++){
?>
<td>
<img class="<?php echo $color_class[$row_count]; ?>"
src="http://www.backgroundsy.com/file/large/yellow-sign.jpg" width="100">
</td>
<?php
}
?>
</tr>
</table>
<?php
$row_count ++;
}
?>
This question already has answers here:
How to float 3 divs side by side using CSS?
(16 answers)
Closed 6 years ago.
My table has rows that's looped in a non-specific length because the values in the cells may be added or removed anytime. Anyway, here's the code:
<?php
$i = 1;
foreach($items as $item => $itemValue) {
if ($itemValue['item_id'] == $parentItemValue['id']) {
if (fmod($i,7)) echo '<tr>';
echo '<td class="inner-td"><input type="checkbox" id="itemId">'.$itemValue['item'].'</td>';
if (!fmod($i,7)) echo '</tr>';
$i++;
}
?>
The above code displays this:
I also tried if (!fmod($i,7)) echo '<tr>' and if (!fmod($i,8)) echo '</tr>' and gives me this:
Also, if (!fmod($i,10)) echo '<tr>' and if (!fmod($i,11)) echo '</tr>' and gives me this:
I want my table to look like this:
Is there a way that the cells will fill in the entire row before making a new one?
You can try this. Just change the $maxcol value for how many columns you want.
<?php
$tmp = array('test1','test2','test3','test4');
echo '<table border="1">';
$x = 0;
$maxcol = 2; // Max column
foreach($tmp as $i=>$v)
{
echo $x === 0 ? '<tr>' : '';
echo '<td>'.$v.'</td>';
echo $x === ($maxcol-1) ? '</tr>' : '';
$x++;
$x = $x == $maxcol ? 0 : $x;
}
echo '</table>';
?>
Try to follow this structure.
Note that all fields and rows are made up by myself.
<tbody>
<?php
require_once ('connectionWithDB.php');
$table = "members";
$array = $admin_query->viewTableData($table);
foreach ($array as $row){
print_r($array);
?>
<tr>
<td> <?php $row[member_id] ?> </td>
<td> <?php $row[member_password] ?> </td>
<td> <?php $row[member_first_name] ?> </td>
<td> <?php $row[member_last_name] ?> </td>
<td> <?php $row[member_DOB] ?> </td>
<td> <?php $row[member_address] ?> </td>
<td> <?php $row[member_email] ?> </td>
<td> <?php $row[member_phone] ?> </td>
<td> <?php $row[member_gender] ?> </td>
</tr>
</tbody>
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>
I am trying to use for loops to create a table which dynamically returns something like this: Note how the td content have been arranged
<table>
<tr>
<td>1</td>
<td>3</td>
<td>5</td>
<td>7</td>
<td>9</td>
</tr>
<tr>
<td>2</td>
<td>4</td>
<td>6</td>
<td>8</td>
<td>10</td>
</tr>
</table>
among What I have tried so far is
$row=2;
$col=20;
$x=0;
echo '<table>';
for($i=0;$i<$row;$i++){
echo '<tr>';
for($k=0;$k<$col;$k++)
{
echo '<td>'.echo $x+=1.'</td>';
}
echo '</tr>';
}
In this case I get something different and which is not what I want.
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
</table>
Kindly someone help me map this.Thanks
<?php
# how high to count
$count = 10;
# how many rows in the table
$rows = 2;
# figure out how many columns
$columns = ceil($count/$rows);
# could also go the other way and declare there to be 5 columns and then
# divide to get the rows, but since you're counting down the columns,
# I thought this made more sense. Either way.
?><table><?php
for ($i=0; $i<$rows; ++$i) {
?><tr><?php
for ($j=0; $j<$columns; ++$j) {
# calculate which number to show in column $j of row $i. Each column adds
# $rows numbers to the total, while each row just adds one more. And we
# want to start at 1 instead of 0.
$n = $j * $rows + $i + 1;
?><td><?= $n ?></td><?php
}
?></tr><?php
}
?></table>
$total_count = 10;
$total_rows = 2;
$table = array();
//building table array
for($i=1;$i<=$total_count;$i++) {
$row = $i % $total_rows;
$row = $row == 0 ? $total_rows : $row;
$table[$row][] = $i;
}
//generate table based on array
echo "<table>";
for($row=1;$row<=$total_rows;$row++) {
echo "<tr>";
foreach($table[$row] as $cell) {
echo "<td>".$cell."</td>";
}
echo "</tr>";
}
echo "</table>";
This isnt as complicated as people are making it seem
Start the inner loop at whatever row you're currently on and add 2 each time.
<?php
$rows=2;
$cols=10;
?>
<table>
<?php for($i=1;$i<=$rows;$i++): ?>
<tr>
<?php for($k=$i;$k<$cols;$k+=2): ?>
<td><?php echo $k ?></td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</table>
Id probably use range and foreach though
<?php
$rows=2;
$cols=10;
?>
<table>
<?php foreach( range( 1, $rows ) as $row ): ?>
<tr>
<?php foreach( range( $row, $cols, 2 ) as $col ): ?>
<td><?php echo $col ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
This approach will split the data itself
function array_chunk_vertical($array = null,$cols = 3, $pad = array(null))
{
if (is_array($array) == true and !empty($array))
{
$rows = ceil(count($array)/$cols);
$array = array_chunk($array,$rows);
if (count($array) < $cols)
{
$array = array_pad($array,$cols,$pad);
}
foreach($array as $key => $value)
{
$array[$key] = array_pad($value,$rows,null);
}
foreach($array as $key => $value)
{
foreach($value as $sub_key => $sub_value)
{
$output[$sub_key][$key] = $sub_value;
}
}
return $output;
}
return $array;
}
$data = array(1,2,3,4,5,6,7,8,9,10,11);
echo '<table border="1">';
foreach(array_chunk_vertical($data,ceil(count($data)/2)) as $row)
{
echo '<tr>';
foreach($row as $col)
{
echo '<td>' . $col . '</td>';
}
echo '</tr>';
}
echo '</table>';