I want to create a display that only show the column with value larger than 0.
My code to display the field and value :
echo "<table><tr>";
while ($property = mysql_fetch_field($result))
{
if($property->name > '0')
echo "<td>Field name: " . $property->name . "</td>";
}
echo "<tr/>";
while ($row = mysql_fetch_assoc($result))
{
echo "<tr><td>".$row['a']."</td>";
echo "<td>".$row['b']."</td>";
echo "<td>".$row['c']."</td>";
echo "<td>".$row['d']."</td></tr>";
}
echo "</table>";
The output is like this :
Field name: a Field name: b Field name: c Field name: d
1 2 3 0
I want to have a display like below :
Field name: a Field name: b Field name: c
1 2 3
Where the field name: d is not display because the value is 0.
How can I do that? Can I wish for some examples from you?
Thanks.
Your code modified as for proper expected result.
echo "<table><tr>";
while ($property = mysql_fetch_field($result))
{
if($property->name > '0')
echo "<td>Field name: " . $property->name . "</td>";
}
echo "<tr/>";
while ($row = mysql_fetch_assoc($result))
{
echo "<tr>";
if($row['a'] > 0) echo "<td>".$row['a']."</td>";
if($row['b'] > 0) echo "<td>".$row['b']."</td>";
if($row['c'] > 0) echo "<td>".$row['c']."</td>";
if($row['d'] > 0) echo "<td>".$row['d']."</td>";
echo "</tr>";
}
echo "</table>";
try
while ($row = mysql_fetch_assoc($result))
{
echo "<tr>";
if($row['a'] >0) echo "<td>".$row['a']."</td>"; else echo"<td>-<td>";
if($row['b'] >0) echo "<td>".$row['b']."</td>"; else echo"<td>-<td>";
if($row['c'] >0) echo "<td>".$row['c']."</td>"; else echo"<td>-<td>";
if($row['d'] >0) echo "<td>".$row['d']."</td>"; else echo"<td>-<td>";
echo "</tr>";
}
$results = array();
$disp_col_a = false;
$disp_col_b = false;
$disp_col_c = false;
$disp_col_d = false;
while ($row = mysql_fetch_assoc($result))
{
if ($row['a'] > 0){
$disp_col_a = true;
}
if ($row['b'] > 0){
$disp_col_b = true;
}
if ($row['c'] > 0){
$disp_col_b = true;
}
if ($row['b'] > 0){
$disp_col_b = true;
}
$results[] = $row;
}
echo "<table><tr>";
if ($disp_col_a){
echo "<td>Field name: a</td>";
}
if ($disp_col_b){
echo "<td>Field name: b</td>";
}
if ($disp_col_c){
echo "<td>Field name: c</td>";
}
if ($disp_col_d){
echo "<td>Field name: d</td>";
}
echo "<tr/>";
foreach ($results as $results_row) {
{
echo "<tr>";
if ($disp_col_a){
echo "<td>";
if ($results_row['a'] > 0){
echo $results_row['a'];
}
echo "</td>";
}
if ($disp_col_b){
echo "<td>";
if ($results_row['b'] > 0){
echo $results_row['b'];
}
echo "</td>";
}
if ($disp_col_c){
echo "<td>";
if ($results_row['c'] > 0){
echo $results_row['c'];
}
echo "</td>";
}
if ($disp_col_d){
echo "<td>";
if ($results_row['d'] > 0){
echo $results_row['d'];
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
Not to be picky, putting aside that at first glance it certainly makes more sense to let mysql do the heavy lifting here - why would you want to output some number of rows in an HTML table that have different numbers of columns? Aside from the permanent maintenance headache, this would, by some, be considered Bad Design from a visual aesthetics perspective. :)
At the same time, I do understand the concept of "Good Enough Is," so I won't complain too much.
Related
I have a table in the database named 'transactions'. I want to list out the values from this table. The result should be displayed in HTML table format. The rows in the resulted table should be grouped as per the amount.There is a column named 'amount' in the 'transactions' table.
Here is my code:
$s1 = DB::table('transactions')
->select(DB::raw("GROUP_CONCAT(selected_seats SEPARATOR '') as selected_seats"),'userid','amount','show_id')
->where("show_id","=",$s)
->groupBy('userid')
->groupBy('amount')
->orderBy('userid','ASC')
->orderBy('amount', 'DESC')
->get();
I am retrieving the values from $s1 variable through loop.
Following is the output:
and i want the result like this:
Please click on the links given above. I am new to this forum so its not allowing me to add images in the question. Kindly help.
Here is the entire code:
if($s1 != null)
{
echo "<div class='panel-body'>";
echo "<table class='table table-responsive'>"; $c = 1;
echo "<th>Drama Title</th>";
echo "<th>Show Date</th>";
echo "<th>Seat booked</th>";
echo "<th>Amount</th>";
echo "<th>User</th>";
for($i=0;$i<count($s1);$i++)
{
echo "<tr><td>".$shows1[0]->show_title."</td>";
echo "<td>".$shows[0]->show_date."</td>";
echo "<td>";
echo $s1[$i]->selected_seats;
echo "</td>";
/*echo $s1[$i]->userid."<br/>";
echo $s1[$i]->amount."<br/>";*/
$transactions = DB::table('transactions')->where('userid',$s1[$i]->userid)->where('show_id',$s1[$i]->show_id)->get();
//var_dump($transactions);
$total_amount = 0;
//echo "<pre>Users<br/>"; var_dump($transactions);
foreach ($transactions as $transaction)
{
//echo "userid ".$s1[$i]->userid." "."show id: ".$transaction->show_id." "."<br/>";
// echo "amount: ".$transaction->amount." ";
$amount = $transaction->amount;
$total_amount = $total_amount + $amount; //echo $amount."<br/>";
$c = $c+1;
//echo "no. of seats:".$c;
$users = DB::table('users')->where('id',$s1[$i]->userid)->get();
}
echo "<td>".$total_amount."</td>";
//echo $s1[$i]->userid."<br/>";
//echo "<td>".$users[0]->name."</td>";
//echo "<pre>"; var_dump($users);
echo "</td>";
if(isset($users[0]))
{
//echo "values are set";
echo "<td>".$users[0]->name."</td></tr>";
}
else
{
//echo "null value";
continue;
}
}
/*echo $shows[0]->show_date."<br/>";
echo $shows[0]->show_title;*/
//echo "<pre>"; var_dump($s1);
//echo "<td>".$users[0]->name."</td>";
//echo "</tr>";
echo "</table>";
echo "</div>";
?>
}
else
{
echo "No records found";
}
Easiest way I think is calculate amounts in the loop you are printing data or readying data to send to a view. Since your data sorted by user_id, you always can change amount variable in the loop when user_id is changing. You can use count(explode($seat_variable)) to get number of seats in a single run of the loop. Look at the example code below.
$num_seat = 0; $amount = 0; $running_user_id = -1;
foreach ($row as $entry) {
...
if ($running_user_id != $entry['user_id']) { // check if the same user
$running_user_id = $entry['user_id'];
$num_seat = 0; $amount = 0; // resetting variable values for seperate user.
}
$num_seat += count(explode($entry['selected_seats']));
$amount += $entry['amount'];
...
}
And I assume you have missing data like emails in the query.
Code update after questioner added his code.
if($s1 != null) {
echo "<div class='panel-body'>";
echo "<table class='table table-responsive'>"; $c = 1;
echo "<tr>";
echo "<th>Drama Title</th>";
echo "<th>Show Date</th>";
echo "<th>Seat booked</th>";
echo "<th>Amount</th>";
// echo "<th>User</th>";
echo "</tr>";
$category = ""; $num_seats = 0;
for ($i=0; $i<count($s1); $i++) {
if ($category != $amount) {
if ($i > 0) { // print totals
echo "<tr>
<td colspan='3' align='right'>Total</td>
<td>".$num_seats."</td>
<td>".($num_seats * $amount)."</td>
</tr>";
echo "<tr><td colspan='5'> </td></tr>"; // empty row after totals printed
$num_seats = 0; //resetting number of seats per category
}
echo "<tr><td colspan='5'><h2>Category : ".$amount."</h2></td></tr>"; // printing category line in the given table
$category = $amount; // this logic is to prevent category line printing for each row
}
$transactions = DB::table('transactions')->where('userid',$s1[$i]->userid)->where('show_id',$s1[$i]->show_id)->get();
echo "<tr>";
$users = DB::table('users')->where('id', $s1[$i]->userid)->get();
// Check below values are correct or not
echo "<td>".$users[0]->name."</td>";
echo "<td>".$s1[$i]->userid."</td>";
echo "<td>".$s1[$i]->email."</td>"; // get user's email property here
echo "<td>".$s1[$i]->selected_seats."</td>";
$num_seats += count(explode(',', $s1[$i]->selected_seats));
echo "<td> </td></tr>"; // empty colomn for amount and the row end
}
echo "</table>";
echo "</div>";
}
I am stuck in a weird problem, I want to show the continuous number till the last in the row and cols,
instead of that its showing form 1-50 and then again the row starts with 1 up-to 50.
<?php
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td>$td</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Thanks
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
$style = "green";
if ($tr % 2 == 0) {
$style = "#ccc";
}
echo "<tr style='background-color:".$style."'>";
for($td=1;$td<=$cols;$td++)
{
echo "<td>$x</td>";
$x++;
}
echo "</tr>";
}
echo "</table>";
You are outputting $td which resets at every new tablerow.
Instead, you would like to output an incrementing $x, if I'm not mistaken.
<?php
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td>" . $x . "</td>";
$x++;
}
echo "</tr>";
}
echo "</table>";
?>
In response to your comment on another answer: if you would want alternating row colors, you could use $tr and check wether it is even or uneven:
if($tr % 2 == 0)
{
// use color1
}
else
{
// use color2
}
So I have a function that takes a number and outputs it as a placement. How do I go about making the function take in consideration ties. I have a ranking database and this php code echos out rankings on a table. right now it ranks but it doesn't consider ties. How do i go about doing this
<?php
function addOrdinalNumberSuffix($num) {
if (!in_array(($num % 100),array(11,12,13))){
switch ($num % 10) {
// Handle 1st, 2nd, 3rd
case 1: return $num.'st';
case 2: return $num.'nd';
case 3: return $num.'rd';
}
}
return $num.'th';
}
?>
<?php
$link = mysqli_connect("localhost", "citricide", "321213123Lol", "juneausmashbros");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM rankings ORDER BY points DESC";
$result = mysqli_query($link, $query);
echo '<article class="content grid_6 push_3">';
echo '<h1>';
echo 'Project M Summer Ranbat Rankings';
echo '</h1>';
echo '<section>';
echo '<center>';
echo '<table style="width:400px" class="rankslist">';
echo '<tr>';
echo '<th width="15%"><b>Rank</b></th>';
echo '<th width="45%"><b>Name</b></th>';
echo '<th width="45%"><b>Alias</b></th>';
echo '<th width="15%"><b>Points</b></th>';
echo '</tr>';
$ass = 0;
while($row = $result->fetch_array()) {
$ass++;
echo '<tr>';
if ($ass == 1) {
echo ' <center><td><B><font color=#FFD700>';
} else if ($ass == 2) {
echo ' <center><td><B><font color=#CCCCCC>';
} else if ($ass == 3) {
echo ' <center><td><B><font color=#cd7f32>';
} else {
echo '<td>';
}
echo addOrdinalNumberSuffix($ass);
echo ' </font></B</td></center>';
echo ' <td>'.$row['name'].'</td>';
echo '<td>'.$row['alias'].'</td>' ;
echo '<td>'.$row['points'].'</td>';
echo '</tr>';
}
echo '</table>';
echo '</center>';
echo '</section>';
echo '</article>';
?>
It seems like you need additional variables to track the rank and the previous value. By doing so, you can handle ties.
For example:
$ass = 0; // current record count
$rank = 0; // rank
$last_points = NULL; // variable to store last ranked value
while($row = $result->fetch_array()) {
$ass++;
// check if value changes and reset rank if it does
if ($row['points'] !== $last_points) {
$rank = $ass;
$last_points = $row['points'];
}
echo '<tr>';
if ($rank == 1) {
echo ' <center><td><B><font color=#FFD700>';
} else if ($rank == 2) {
echo ' <center><td><B><font color=#CCCCCC>';
} else if ($rank == 3) {
echo ' <center><td><B><font color=#cd7f32>';
} else {
echo '<td>';
}
echo addOrdinalNumberSuffix($rank);
echo ' </font></B</td></center>';
echo ' <td>'.$row['name'].'</td>';
echo '<td>'.$row['alias'].'</td>' ;
echo '<td>'.$row['points'].'</td>';
echo '</tr>';
}
So say your points looked like this:
100
100
90
80
The $rank value would be:
1
1
3
4
hey I make a simple php table using nested for loop ... and it will be like this...
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
but the problem is , i can not print this value using a loop inside the column .. so what will be the solution ??? please
my code :
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
echo "MY PROBLEM HERE...I cant print column numbers \n";
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
//echo "MY PROBLEM HERE...I cant print column numbers \n";
echo $col + ($row - 1) * 5;
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
To save several loops:
$rows = 3;
$cols = 5;
$table = '<table border="1">';
for($i=1;$i<=$rows;$i++){
$table .= '<tr><td>'.implode('</td><td>', range($cols*$i-$cols+1,$cols*$i)).'</td></tr>';
}
$table .= '</table>';
echo $table;
It's not $col + $row * 5 it has to be $row - 1
<?php
echo "<table border=1>\n";
for($row=1;$row<=3;$row++)
{
echo "<tr>";
for($col=1;$col<=5;$col++)
{
echo "<td>";
echo $col + ($row-1) * 5;
echo "</td>";
}
echo "</tr>";
}
echo "</table> \n";
?>
Hello i'm having problems adding numbered rows/serial numbers to my database query result. I've used $number to collect the actual number of rows.
Then another problem i'm trying to avoid is: Numbering of Column Headers.
Thanks for the help.
<?php
$number = mysql_num_rows($query);
for ($serial = 0; $serial < $number; $serial++)
{
echo "<tr>". $serial ."</tr>";
}
for ($i = 0; $i < $number_cols; $i++)
{
echo "<th>" . mysql_field_name($query, $i) . "</th>\n";
}
while ($row = mysql_fetch_row($query))
{
echo "<tr align=center>\n";
for ($i = 0; $i < $number_cols; $i++)
{
echo "<td>";
if (!isset($row[$i]))
{
echo "NULL";
}
else
{
echo $row[$i];
}
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>";
echo "</span>";
echo "</div>";
?>
trying my best to get something sane out of your terrific code.
<?php
$serial = 1;
while ($row = mysql_fetch_row($query))
{
echo "<tr align=center>\n";
echo "<td>";
echo $serial++;
echo "</td>\n";
foreach ($row as $value)
{
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}