I would like make:
aaa | bbb | ccc | ddd etc
1 | 1 | 1 | 1
2 | 2 | 2 | 2
3 | 3 | 3 | 3
etc
for aaa, bbb etc i use FOREACH
<table><tr>
foreach ($data as $d){
echo "<td>" . $d . "</td>";
}
</tr>
for ($i = 0; $i < 20; $i++){
echo "<tr><td>" . $i . "</td></tr>";
}
but this working not ok. how can i use loop FOR for all data from foreach?
I think you can generate your table like this:
$columns = array('aaa','bbb','ccc','ddd');
$num_cols = count($columns);
echo "<table>";
echo "<tr>";
foreach($columns as $col)
{
echo "<td>$col</td>";
}
echo "</tr>";
for($i=1;$i<20;$i++)
{
echo "<tr>";
for($j=0;$j<$num_cols;$j++)
{
echo "<td>$i</td>";
}
echo "</tr>";
}
echo "</table>";
In general depends on what $data looks like.
<?php
$data = array('aaa', 'bbb', 'ccc', 'ddd'); // Assuming that $data is a columns storage
$rows = 10; // $rows = count($data); if you wish to have same number of columns and rows
echo '<table>';
echo '<tr>';
foreach ($data AS $item)
{
echo '<td>' . $item . '</td>';
}
echo '</tr>';
for ($idx = 0; $idx < $rows; $idx++)
{
echo '<tr>';
for ($col = 1, $col_num = count($data); $col <= $col_num; $col++)
{
echo '<td>' . $idx . '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
P.S. haven't tested the code.
Related
Error
Defined array in the controller, then transformed to the model which uses the implode function.
Problem = Data are inserted in the sql table in the same column. Eg: Coke,Pepsi,Slice (1 column)
t_id| product_name | unit | cost
1 | Coke,Pepsi,Slice | 5 | 1000
Solution Wanted = Data should be inserted in multiple column such as
t_id| product_name | unit | cost
1 | Coke | 5 | 1000
2 | Pepsi | 2 | 500
3 | Slice | 3 | 600
View
<div class="col-lg-6">
<?php
$getNameValue = $this->session->userdata('nameValue');
echo '<h3>' .$getNameValue['name'] .' paid Rs ' .$getNameValue['cash_amount']. '</h3>';
$getValue = $this->session->userdata('sessiondata');
if ($getValue != NULL){
echo '<table class="table table-bordered table-striped">';
echo '<tr>';
echo '<td><strong>Product Name</strong></td>';
echo '<td><strong>Unit</strong></td>';
echo '<td><strong>Cost</strong></td>';
echo "<form method='post' action=''>";
foreach ($getValue as $row)
{
echo '<tr>';
echo '<td><input type="hidden" name="allProduct[product_name][]" value="'.$row['product_name'].'">' .$row['product_name']. '</td>';
echo '<td><input type="hidden" name="allProduct[unit][]" value="'.$row['unit'].'">' .$row['unit'].'</td>';
echo '<td><input type="hidden" name="allProduct[cost][]" value="'.$row['cost'].'">' .$row['cost'].'</td>';
echo '</tr>';
}
echo '<tr><td></td><td><td><button type="submit" class="btn btn-success">POST</button></td></td></tr>';
echo "</form>";
echo '</table>';
}
?>
</div>
Controller
public function newSystemBatch($getBatch)
{
if ($data = $this->input->post('systemProduct')) {
$data['batch_id'] = $getBatch;
if (isset($data['sum'])) {
$data['cost'] = $data['cost'] / $data['unit'];
unset($data['sum']);
}
$productName = $data['product_name'];
$unit = $data['unit'];
$cost = $data['cost'];
$getValue = $this->session->userdata('sessiondata');
$getValue[] = array(
'product_name' => $productName,
'unit' => $unit,
'cost' => $cost
);
$this->session->set_userdata('sessiondata', $getValue);
$this->session->set_flashdata('message', $data);
redirect('inventory/newSystemMessage/' . $getBatch['batch_id']);
}
if ($data = $this->input->post('allProduct')) {
$this->header();
$this->footer();
$this->transaction->addNew($data);
$this->load->view("admin/newSystemTable", $data);
}
else {
$this->header();
$data['action'] = 'System';
$data['users_id'] = $this->user->users_id;
$this->load->view("admin/newSystemTable", $data);
$this->footer();
}
}
Model
function addNew($data)
{
if(is_array($data['product_name'])) $data['product_name'] = implode(",", $data['product_name']);
if(is_array($data['unit'])) $data['unit'] = implode(",", $data['unit']);
if(is_array($data['cost'])) $data['cost'] = implode(",", $data['cost']);
$this->db->insert('try', $data);
}
Model:
function addNew($data)
{
// Test that all all sub arrays are equal size
$length = array_unique(array_map('count', $data));
if (count($length) == 1) {
$length = current($length);
$keys = array_keys($data);
$res = array();
for($i = 0; $i < $length; $i++)
foreach($keys as $key)
$res[$i][$key] = $data[$key][$i];
$this->db->insert_batch('try', $res);
}
else {
// incorrect data
return false;
}
return true;
}
I have this code:
function random()
{
include('config/koneksi.php');
$result = mysql_query("select * from temp_hasil");
$n =mysql_num_rows(mysql_query("SELECT * FROM temp_hasil"));
for ($i = 1; $i <= $n; $i++)
{
for ($j = 1; $j <= $n; $j++)
{
$rows = mysql_fetch_array($result);
$this->table[$i][$j] = $i == $j ? INF : $rows['id'];
}
}
}
function __toString()
{
$str = '<table class="table table-bordered" id="tableInput"> <tbody>';
$str .= '<tr><td></td>';
foreach ($this->table as $rowName => $row)
{
$str .= "<td>$rowName</td>";
}
$str .= '</tr>';
foreach ($this->table as $rowName => $row)
{
$str .= "<tr><td>$rowName</td>";
foreach ($row as $columnName => $value)
{
$str .= "<td>";
$str .=
'<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' .
$columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>';
$str .= "</td>";
}
$str .= '</tr>';
}
$str .= '</tbody></table>';
return $str;
}
}
$str .= '</tr>';
foreach ($this->table as $rowName => $row)
{
$str .= "<tr><td>$rowName</td>";
foreach ($row as $columnName => $value)
{
$str .= "<td>";
$str .=
'<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' .
$columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>';
$str .= "</td>";
}
$str .= '</tr>';
}
$str .= '</tbody></table>';
return $str;
}`
and I have table "temp_hasil" such as:
id_temp | id |
1 | 8 |
2 | 5 |
3 | 7 |
If I run this code, it result :
1 2 3
1 | INF | 5 | 7 |
2 | | INF| |
3 | | | INF|
But I want end result such as:
1 2 3
1 | INF | 5 | 7 |
2 | 8 | INF| 7 |
3 | 8 | 5 | INF|
How do I code it in php? Is there anything wrong code? Thanks,... :)
Problem is - You can only fetch complete results once and can't get the first record in second loop. So instead, store the results in an array and use the array to create matrix.
This should work. Try this.
function random()
{
include('config/koneksi.php');
$result = mysql_query("select * from temp_hasil");
$n =mysql_num_rows(mysql_query("SELECT * FROM temp_hasil"));
$r=array();
for ($a = 1; $a <= $n; $a++)
{
$rows = mysql_fetch_array($result);
$r[]=$rows['id'];
}
for ($i = 1; $i <= $n; $i++)
{
for ($j = 1; $j <= $n; $j++)
{
$this->table[$i][$j] = $i == $j ? INF : $r[$j-1];
}
}
}
Simply, you can use a while loop and store to array and then use array sizeof($r) or count($r) in for loop. Which ever you prefer.
function random()
{
include('config/koneksi.php');
$result = mysql_query("select * from temp_hasil");
$r=array();
while($rows = mysql_fetch_array($result)) {
$r[]=$rows['id'];
}
for ($i = 1; $i <= sizeof($r); $i++)
{
for ($j = 1; $j <= sizeof($r); $j++)
{
$this->table[$i][$j] = $i == $j ? INF : $r[$j-1];
}
}
}
<table border="2">
<?php
foreach($array1 as $value)
{
echo '<tr><td>';
echo $value;
echo '</td></tr>';
}
?>
</table>
I have more array: array2,array3.
The above code only executes values of array1 but I want all the values of all array in the same table in separate columns!
array1 | array2 | array3
------------------------
| |
| |
| |
| |
I want something like this...
I tried modifying the code but no idea how to do it correctly.
$data = mysql_query(" SELECT * FROM user_pokemon_db WHERE user_id = '".$id."' ");
while($rows = mysql_fetch_assoc($data)) {
$db_id = $rows['id'];
$array[] = $db_id;
$level = $rows['level'];
$array1[] = $level;
$exp = $rows['exp'];
$array2[] = $exp;
$pkmn_id = $rows['pkmn_id'];
$data1 = mysql_query(" SELECT * FROM pokemons WHERE pk_id = '".$pkmn_id."' ");
while($rows = mysql_fetch_assoc($data1)) {
$poke = $rows['path']; $array3[] = $poke;
}
}
The above code fetches data from database and adds it to different arrays!
When you create your array, you don't specify keys, so they are generated automaticaly. So you can work on keys for your for loop instead of using a foreach in values:
We can see in your code that $array3 is longuer than the others. So we will base on it:
foreach($array3 as $key => $value)
{
if (isset($array1[$key])){
echo '<tr><td>'.$array1[$key].'</td>';
}else{
echo '<tr><td></td>';
}
if (isset($array2[$key])){
echo '<td>'.$array2[$key].'</td>';
}else{
echo '<td></td>';
}
echo '<td>'.$array3[$key].'</td></tr>';
}
It should work
try making a function that calls itself in the end. not sure if I got the syntax right here, but this should work.
$i = 0;
function test($i){
if($i < ar1.lenght && $i < ar2.lenght && $i< ar3.lenght){
echo "<tr><td>";
echo ar1[$i];
echo "</td><td>";
echo ar2[$i];
echo "</td><td>";
echo ar3[$i];
echo "</td></tr>";
$i++;
//calls itself with an incremented $i
test($i);
}//else do nothing.
}
--edit forgot some tags
I'm upgrading all my mysql_* to PDO. I can handle simple SELECTs and INSERTs. But I have one complex SELECT using nested loops that I am making a dog's dinner of. I'll post the mysql_* code, which outputs a two-column table with total registrations at A1 level in the first column and total registrations at A1 level who have paid in brackets in the second column.
-------------------------
Smithsville : A1 | |
10 | (7) |
-------------------------
Grange : A1 | |
4 | (4) |
-------------------------
Beau Ridge : A1 | |
23 | (16)|
-------------------------
Jonesboro : A1 | |
9 | (9) |
-------------------------
Lexing : A1 | |
3 | (1) |
-------------------------
In the full application further tables are genertated across the page for the other levels.
$levels = array('A1', 'A2', 'B1', 'B2');
$centres = array('Smithsville', 'Grange', 'Plateau Ridge', 'Jonesboro', 'Lexing');
for ($j = 0; $j < 1; $j++) /* Does not loop; selects only A1 in queries below*/
{
for ($k=0; $k<5; $k++) /* Cycles through the centres */
{
$show_count = mysql_query("SELECT COUNT(Centre)
FROM exam
WHERE exam.Centre='".$centres[$k]."'
AND exam.Level='".$levels[$j]."'");
$show_paid = mysql_query("SELECT COUNT(Centre)
FROM exam
JOIN personal
ON exam.P_ID=personal.P_ID
WHERE personal.Paid='1'
AND exam.Centre='".$centres[$k]."'
AND exam.Level='".$levels[$j]."'");
$result_rows = mysql_num_rows($show_count);
$result_rows2 = mysql_num_rows($show_paid);
if ($result_rows == 0)
{ echo 'No registrations';}
if ($result_rows2 == 0)
{ echo 'No payments';}
echo '<table class="Font3White">
<tr class="Title">
<td class="width8">'.$centres[$k].' : '.$levels[$j].'</td>
<tr>';
while (($row = mysql_fetch_row($show_count)) && ($row2 = mysql_fetch_row($show_paid)))
{
foreach ($row as $key => $value)
{
echo '<td>';
echo $value;
echo '</td>';
}
foreach ($row2 as $key2 => $value2)
{
echo '<td> (';
echo $value2;
echo ')</td>';
}
echo '</tr>';
}
echo '</table>';
}
}
My PDO attempt is
for ($j = 0; $j < 1; $j++) /* Does not loop; selects only A1 */
{
for ($k=0; $k<5; $k++) /* Cycles through the centres */
{
/*//////////////////////////*/
/* Prepares statements */
/*//////////////////////////*/
$stmt1 = $db->query("SELECT COUNT(Centre)
FROM exam
WHERE exam.Centre=:centre
AND exam.Level=:level");
$stmt2 = $db->query("SELECT COUNT(Centre)
FROM exam
JOIN personal
ON exam.P_ID=personal.P_ID
WHERE personal.Paid='1'
AND exam.Centre=:centre
AND exam.Level=:level");
/*/////////////////////*/
/* Binds parameters */
/*/////////////////////*/
$stmt1->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt1->bindParam(':level', $level, PDO::PARAM_STR);
$stmt2->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt2->bindParam(':level', $level, PDO::PARAM_STR);
/*//////////////////////////*/
/* Executes statements */
/*//////////////////////////*/
$stmt1->execute(array($centre, $level));
$stmt2->execute(array($centre, $level));
echo '<table class="Font3White">
<tr class="Title">
<td class="width8">'.$centres[$k].' > '.$levels[$j].'</td>
<tr>';
while (($row = $stmt1->fetch(PDO::FETCH_ASSOC)) && ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)))
{
foreach ($row as $key => $value)
{
echo '<td>';
echo $value;
echo '</td>';
}
foreach ($row2 as $key2 => $value2)
{
echo '<td> (';
echo $value2;
echo ')</td>';
}
echo '</tr>';
}
echo '</table>';
}
}
I know this is terrible but I hope it can be worked with.
I know it's wrong to make you the dirty work, but that's it.
I made some changes to your code, so compare mine with yours.
Hope it helps you.
$levels = array('A1', 'A2', 'B1', 'B2');
$centres = array('Smithsville', 'Grange', 'Plateau Ridge', 'Jonesboro', 'Lexing');
foreach ($levels as $level) {
foreach ($centres as $centre) {
// As you said, PREPARE
$stmt1 = $db->prepare("SELECT COUNT(Centre)
FROM exam
WHERE exam.Centre=:centre
AND exam.Level=:level");
$stmt2 = $db->prepare("SELECT COUNT(Centre)
FROM exam
JOIN personal
ON exam.P_ID=personal.P_ID
WHERE personal.Paid='1'
AND exam.Centre=:centre
AND exam.Level=:level");
$stmt1->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt1->bindParam(':level', $level, PDO::PARAM_STR);
$stmt2->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt2->bindParam(':level', $level, PDO::PARAM_STR);
//You don't need to pass parameters here just on PDOStatement::bindParam
$stmt1->execute();
$stmt2->execute();
echo '<table class="Font3White">
<tr class="Title">
<td class="width8">' . $centre . ' > ' . $level . '</td>
<tr>';
// Fetch stuffs
$row = $stmt1->fetch(PDO::FETCH_ASSOC);
$row2 = $stmt2->fetch(PDO::FETCH_ASSOC);
// Don't use while with PDOStatement::fetch, use fetchAll then foreach it.
if ($row && $row2) {
foreach ($row as $key /* <= Do you need it? */ => $value) {
echo '<td>';
echo $value;
echo '</td>';
}
foreach ($row2 as $key2 /* <= Do you need it? */ => $value2) {
echo '<td> (';
echo $value2;
echo ')</td>';
}
echo '</tr>';
}
echo '</table>';
}
}
Can you show us how you set $centre and $level variables? Maybe they are arrays, not strings? If they are - you may have to create new variable and assign value from array to it. Like this:
$value_to_bind = $centres[$k];
Execute does not need parameters if you used bindParam() before.
I never tried to run 2 queries with same parameter names and same variables binded to them. Maybe this is a problem?
I have a MySQL query that returns data using PHP.
My problem is that I need to populate my html table (with 3 columns) with the data returned by the query but the data should be populated Columnwise.
Like first the first column should be populated .. then the second and finally the third one.
Also I would like to know that is it possible to do so for an unlimited set of data?
Following the screen shot of the desired layout
you can use while.
<table>
$sql=mysql_query("select * from table");
while($s=mysql_fetch_array($sql))
{
$x=$s["x"];
<tr>
<td ><?php echo $x; ?></td>
<td ><?php echo $y; ?></td>
<td ><?php echo $z; ?></td>
</tr>
}
</table>
guybennet's answer is correct but if you want it to work for an unlimited amount of columns you could do this: (I also threw in some column headers for readability)
echo '<table>';
$counter = 0;
$result = mysql_query("select * from table");
while($row = mysql_fetch_array($result)) {
$counter++;
if($counter == 1) {
echo '<tr>';
foreach($row as $key => $val) {
echo "<th>$key</th>";
}
echo '</tr>';
}
echo '<tr>';
foreach($row as $key => $val) {
echo "<td>$val</td>";
}
echo '</tr>';
}
echo '</table>';
Also of course you should use mysqli or PDO I'm just showing a quick example.
If you don't care about how it's organized, you can try something like this:
echo "<table><tr>";
$i=0;
while($row = mysql_fetch_array($sql))
{
echo "<td>".$row[0]."</td>\n";
$i++;
if($i==3)
{
echo "</tr>\n<tr>";
$i=0;
}
}
echo "</tr></table>";
Otherwise, I'd suggest putting it all into an array and then putting it into the table.
$data = array();
$result = mysql_query("SELECT * FROM your_table");
while ($row = mysql_fetch_array($result)) {
$data[] = $row;
}
$itemsAmount = count($data);
$ceilAmount = ($itemsAmount - $itemsAmount % 3) / 3;
$lastAmount = $itemsAmount % 3;
$firstArray = array_slice($data, 0, $itemsAmount);
$secondArray = array_slice($data, 0, $itemsAmount*2);
$thirdArray = array_slice($data, 0, $lastAmount);
$output = "<table>";
foreach ($data as $key => $value) {
$output .= "<tr>";
$output .= "<td>" . $firstArray[$key][0] . "</td>";
$output .= "<td>" . $secondArray[$key][0] . "</td>";
if (empty($thirdArray[$key])) {
$str = '';
} else {
$str = $thirdArray[$key][0];
}
$output .= "<td>" . $str . "</td>";
$output .= "</tr>";
}
$output .= "</table>";
echo $output;
You need to check all the results returned, then print them as you won't print in order:
First get an array with all the results
<?php
$sql= mysql_query('SELECT * FROM table');
$num= mysql_affected_rows($sql);
$items = array();
$i=0;
while($item=mysql_fetch_array($sql)){
$items[++$i]=$item['data'];
}
Now start printing
int $num_rows;
$num_rows=$num%3;
echo '<table>';
for ($i=0;$i<$num_rows;$i++){
echo '<tr>';
for ($j=0;$j<2,$j++){
$n=$i+1+($j*$num_rows);
if($items[$n]!==null)
echo '<td>'.$items[$n].'</td>';
else
echo '<td></td>';
}echo '</tr>';
}echo'</table>';
?>