How create multi dimensional HTML table with loop in PHP - html

I am new to HTML CSS, I have tried many time but fail. Please help.
I want to create table like this:
I have inserted relevant data in mysql but i am unable to show data in given format/table.
<tbody>
<?php
foreach($record as $row)
{
$pre_stop = $row['pre_stop'];
$current_stop_id = $row['current_stop'];
$cur_stop_name=mysql_query("SELECT stop_name_urd FROM stop WHERE stop_id = '$current_stop_id'");
while($cur_stop=mysql_fetch_array($cur_stop_name))
{
$stop_name=$cur_stop[0];
}
$inter_distance = $row['inter_distance'];
$total_distance += $row['inter_distance'];
echo "<tr>";
echo "<td class='border' style='text-align:center;'>" . $inter_distance . "</td>";
echo "<td class='border' style='text-align:center;'>" . $stop_name . "</td>";
echo "<td class='border' style='text-align:center;'>".$total_distance."</td>";
for ($i=0; $i < 1; $i++) {
echo "<td rowspan='".$count."' class='noBorder' style='text-align:center;'>" . "" . "</td>";
echo "<td class='noBorder'>" . $stop_name . "</td>";
}
}
//echo "<td class='noBorder' style='text-align:center;'>" . $stop_name . "</td>";
//echo "<td></td>";
?>
</tr>
</tbody>

If you're new to coding HTML, then I recommend you try to manually create a smaller version of your table (say 3 rows) so that you know how the resulting HTML code is supposed to look and that you're fetching the SQL data correctly. Then you can transfer that knowledge into PHP code.
Do watch out for your loop, though:
for ($i=0; $i < 1; $i++) {
echo "<td rowspan='".$count."' class='noBorder' style='text-align:center;'>" . "" . "</td>";
echo "<td class='noBorder'>" . $stop_name . "</td>";
}
This will only loop once, which seems to defeat your purpose, as your logical check is odd. Your for loop reads:
$i=0 - Make a variable named i
$i < 1 - Stop if i ever becomes
greater than or equal to 1
$i++ - Add 1 to the value of i
This means that it will go through the loop one time only and then break out of the loop.
I don't see from your code example how you're getting your data but I imagine you'd want something like this:
for ($i=0; $i < $resultCount; $i++) {
echo "<td rowspan='".$count."' class='noBorder' style='text-align:center;'>" . "" . "</td>";
echo "<td class='noBorder'>" . $stop_name . "</td>";
}
Where $resultCount is equal to the number of rows returned by your SQL query.

It looks like you reversed the order of the columns when echo-ing out your html. Also, it would be easier to use colspan vs rowspan in this approach. Not knowing the initial value of count, we shall assume the header is 0 and all other rows are 1 through m (where m is the number of rows since we read a matrix as m x n (rows x columns)).
In conclusion, try this:
$count = 0;
$total_distance = 0;
// the header row
echo "<tr>" . PHP_EOL;
// Apply the space on the left
echo "\t<th colspan='" . (count($record) - $count) . "' class='noBorder' style='text-align:right;'></th>" . PHP_EOL;
// Provide headers on the last three columns
echo "\t<th class='border' style='text-align:center;'>Total Distance</th>" . PHP_EOL;
echo "\t<th class='border' style='text-align:center;'>Stop Name</th>" . PHP_EOL;
echo "\t<th class='border' style='text-align:center;'>Inter-Distance</th>" . PHP_EOL;
echo "</tr>" . PHP_EOL;
foreach($record as $row)
{
// get the values
$pre_stop = $row['pre_stop'];
$current_stop_id = $row['current_stop'];
$inter_distance = $row['inter_distance'];
$total_distance += $inter_distance;
// Assume the last stop in the results has the desired stop name
$cur_stop_name=mysql_query("SELECT stop_name_urd FROM stop WHERE stop_id = '$current_stop_id'");
while($cur_stop=mysql_fetch_array($cur_stop_name))
{
$stop_name = $cur_stop[0];
}
// Start the row
echo "<tr>" . PHP_EOL;
// Apply the space on the left
echo "\t<td colspan='" . (count($record) - $count) . "' class='noBorder' style='text-align:right;'>" . $stop_name . "</td>" . PHP_EOL;
// Apply the last three columns
echo "\t<td class='border' style='text-align:center;'>".$total_distance."</td>" . PHP_EOL;
echo "\t<td class='border' style='text-align:center;'>" . $stop_name . "</td>" . PHP_EOL;
echo "\t<td class='border' style='text-align:center;'>" . $inter_distance . "</td>" . PHP_EOL;
// End the row
echo "</tr>" . PHP_EOL;
// increment the count
$count++;
}

Related

YII2 how to get each record after joinwith

How to get each record in an active record after multiple joinWiths?
$model = \common\models\opcr\OpcrKra::find()
->joinWith('opcrRoObjectives')
->joinWith('opcrRoObjectives.opcrFdObjectives')
->joinWith('opcrRoObjectives.opcrFdObjectives.opcrIndividuals');
I get a Trying to get property of non-object error.
foreach ($model->all() as $row) {
echo "<tr>"
. "<td></td>"
. "<td>" . $row->kra . "</td>"
. "<td>" . $row->opcrRoObjectives->id . "</td>" // error here
. "</tr>";
}
I tried to display the $row->count() and it gave me just the exact number of records I expected.
I solved this using the eager loading.
$authors = Author::find()->with('posts')->all(); // fetches the authors with their posts
foreach($authors as $author) {
echo "<h2>Author : " . $author->name . "</h2>";
echo "<ul>";
foreach($author->posts as $post) { // no query executed here
echo "<li>" . $post->title . "</li>";
}
echo "</ul>";
}
You just try this solution:
$model = \common\models\opcr\OpcrKra::find()
->joinWith('opcrRoObjectives')
->joinWith('opcrRoObjectives.opcrFdObjectives')
->joinWith('opcrRoObjectives.opcrFdObjectives.opcrIndividuals')
$result = $model->asArray()->all();
Then in your view:
foreach ($model as $row) {
echo "<tr>"
. "<td></td>"
. "<td>" . $row->kra . "</td>"
. "<td>" . $row['opcrRoObjectives']['id'] . "</td>"
. "</tr>";
}

Yii2.0 export function using kartik-v

I am new in using kartik. Can I just check with you whether kartik is able to do below things? And how to do it?
Display date in different format than in db. i.e. in db it is displayed as YYYY-MM-DD"T"HH:MM:SS" whereas on screen, I want to display it as YYYY-MM-DD.
Show subtotal and total?
Since in my report screen, im using a normal html to display data since from my understanding, the gridview does not have the subtotal function.
Below is my html:
$reports = $dataProvider->getModels();
$i=1;
echo "<table class='table table-striped table-bordered'>";
echo "<tr><th>#</th><th>Item</th><th>Total Summary Usage Qty</th><th>Current Stock On Hand</th><th>Measurement</th></tr>";
foreach ($reports as $report) {
$item_key = $report['report_item_key'];
$item_model = Item::find()->where(['item_key' =>$item_key])->one();
$item_e_desc_tx = $item_model->desc_e_tx;
$item_c_desc_tx = $item_model->desc_c_tx;
$item_qty_no = $item_model->qty_no;
$item_measurement_tx = $item_model->measurement;
echo "<tr>";
echo "<td>" . $i++ . "</td>";
echo "<td>" . Html::a($item_key, array('stockmovementreportdetailbyitem', 'item_key' => $item_key, 'search' => 'true', 'stockmovement_summary_from_sales_date' => $stockmovement_summary_from_sales_date, 'stockmovement_summary_to_sales_date'=>$stockmovement_summary_to_sales_date)) . "<br>" . $item_e_desc_tx . "<br>" . $item_c_desc_tx ."</td>";
echo "<td>" . $report['total_summary_usage_qty'] . "</td>";
echo "<td>" . $item_qty_no . "</td>";
echo "<td>" . $item_measurement_tx . "</td>";
echo "</tr>";
}
echo "</table>";
echo \yii\widgets\LinkPager::widget([
'pagination'=>$dataProvider->pagination,
]);

Having trouble using result of mysql Min() in php

$minTime = "Select MIN(categoryTime) FROM List WHERE personStatus='Atendiendo' AND depName='Admisiones'";
$Time = mysqli_query($con,$minTime);
echo "<table>
<tr>
<th>Main Record</th>
<th>Tiempo Categoria</th>
<th>Tiempo Estimando</th>
<th>Time IN </th>
</tr>";
while($min = mysqli_fetch_array($Time))
{
echo "<tr>";
echo "<td>" . $min['categoryTime'] . "</td>";
echo "<td>" . $min['categoryTime'] . "</td>";
echo "<td>" . $min['categoryTime'] . "</td>";
echo "<td>" . $min['categoryTime'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
If I delete the MIN() function the query works perfectly...
The query work if I delete the Min() fuction. I tried Least() with no success.
Several issues here.
I think the snippet is missing:
$Time =mysqli_query($con,$minTime);
where $con is the connection.
When you add the MIN function around the categoryTime field, the label of the column in the resultset changes, so the reference to $min['categoryTime'] no longer returns anything. Try changing query to
SELECT MIN(categoryTime) as minCat ...
then change the php array column reference to $min['minCat']
HTH

How to: Format a MySQL query result with currency signs using PHP

I have a MySQL query that fetches a numeric value VARCHAR (20) from a table, and I need to format the result to include thousands separators (,) and decimal separators (.) to two positions.
I've searched a lot for a simple query, so now I want to know if this is possible using CSS.
Here is the table data:
Income
-------------------
1234456789
234456000
987456000
And so on. Here's the query:
// Fetch data
$sql="SELECT * FROM scoreboard WHERE codcliente = '".$q."' OR nombre LIKE '%".$q."%'";
$result = mysql_query($sql);
if(mysql_num_rows($result) == 0){
echo '<div align="center" style="background-color:#CCCCCC; font-weight:bold; color:#C0504D;">Record no existe. <input id="srchcreate" type="button" value="Crear" /></div>';
return;
}
//echo $result;
And the result:
// Construct the table
echo "<table>";
// Construct the array
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['codcliente'] . "</td>";
echo "<td>" . $row['nombre'] . "</td>";
// echo "<td>" . $row['ejecutivo'] . "</td>";
// echo "<td>" . $row['banca_as400'] . "</td>";
// echo "<td>" . $row['banca_real'] . "</td>";
// echo "<td>" . $row['ingresos'] . "</td>";
echo "<td>" . $row['ciiu'] . "</td>";
// echo "<td>" . $row['division'] . "</td>";
echo "<td>" . $row['actividad'] . "</td>";
echo "<td>" . $row['riesgo_industria'] . "</td>";
echo "<td>" . $row['riesgo_cliente'] . "</td>";
echo "<td>" . $row['fecha'] . "</td>";
echo "<td>" . $row['analista'] . "</td>";
echo "</tr>";
}
echo "</table>";
This gets inserted to a PHP file and displayed as a table.
No, CSS isn't designed to do that.
JavaScript does have that capability.
<?php
echo number_format(123456789, 2);
?>
123,456,789.00
echo "<td>".number_format($row['ingresos'], 2)."</td>\n"

retrieve two database table values in a single html table

I have a table(webmeasurementsuite) in my database(probe_config).I have written an php coding to retrieve the datas from the database and display it in the html table.Can anyone correct my errors please.when I load the php page to the browser,I am getting a blank page.
<?php
$con = mysql_connect("localhost","root","mysql");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM webmeasurementsuite");
echo "<table border='1'>
<tr>
<th>replication</th>
<th>wait</th>
<th>timeout</th>
<th>clearcache</th>
<th>name</th>
<th>wms</th>
</tr>;
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['replication'] . "</td>";
echo "<td>" . $row['wait'] . "</td>";
echo "<td>" . $row['timeout'] . "</td>";
echo "<td>" . $row['clearcache'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['wms'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
I think you need to learn more about Web Development.
HTML is used only for rendering the information in an appropriate format. For accessing the database records, we got to use server side Scripting Languages or Web Technologies(J2EE, JSP, ASP, and more) to access the database. That one can not be done with HTML alone.
If am wrong, please brief your question bit more.