how to correctly accommodate the values? - php

I would like how to put the second result of the second query. the value is shows under row and I want that value is in front of the first result query.
The result should be shown where it is marked in red
$resultados=$reporte->facturaCompleto();
while($fila = mysqli_fetch_array($resultados))
{
$bandera=0;
echo "<tr style=''>";
echo '<td>'.utf8_encode($fila["factura"]).'</td>';
echo '<td>'.utf8_encode($fila["rfc_emisor"]).'</td>';
echo '<td></td>';
echo '<td></td>';
echo '</tr>';
//
$id_documento=$fila["id_documento"];
$reporte->set('id_documento',$id_documento);
$reporte->contador();
$contador=$reporte->get('contador');
$id_documento=$fila["id_documento"];
$reporte2->set('id_documento',$id_documento);
$resultadosmov = $reporte2->movimientos();
while($fila2 = mysqli_fetch_array($resultadosmov))
{
echo'<tr>';
echo '<td></td>';
echo '<td></td>';
echo '<td>'.utf8_encode($fila2["no_parcialidad"]).'</td>';
echo '<td>'.utf8_encode($fila2["importe_total"]).'</td>';
echo '</tr>';
}
}
echo '</table>';
echo "</td></tr></table>";

Echo all the columns when processing the second query.
$resultados=$reporte->facturaCompleto();
while($fila = mysqli_fetch_array($resultados))
{
$bandera=0;
//
$id_documento=$fila["id_documento"];
$reporte->set('id_documento',$id_documento);
$reporte->contador();
$contador=$reporte->get('contador');
$id_documento=$fila["id_documento"];
$reporte2->set('id_documento',$id_documento);
$resultadosmov = $reporte2->movimientos();
if ($resultadosmov->num_rows > 0) {
while($fila2 = mysqli_fetch_array($resultadosmov))
{
echo "<tr>";
echo '<td>'.utf8_encode($fila["factura"]).'</td>';
echo '<td>'.utf8_encode($fila["rfc_emisor"]).'</td>';
echo '<td>'.utf8_encode($fila2["no_parcialidad"]).'</td>';
echo '<td>'.utf8_encode($fila2["importe_total"]).'</td>';
echo '</tr>';
// Make 1st two columns blank in additional rows
$fila["factura"] = "";
$file["rfc_emisor"] = "";
}
} else {
echo "<tr style=''>";
echo '<td>'.utf8_encode($fila["factura"]).'</td>';
echo '<td>'.utf8_encode($fila["rfc_emisor"]).'</td>';
echo '<td></td>';
echo '<td></td>';
echo '</tr>';
}
}
BTW, what is the $bandera variable for? It's set but never used.
It would probably be even better if you could join the two queries, instead of doing queries in a loop.

Related

how to group the data having similar values in php?

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>";
}

Dynamic Table Continuous <td> Count

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
}

While loop within a while loop logically not working in PHP

I have this while loop within another while loop in my PHP:
$selecty = mysql_query("SELECT * FROM followers WHERE userid='".$_SESSION['id']."'");
$rowsy = mysql_num_rows($selecty);
echo '<td>'. $table["username"]. '</td>';
echo '<td>';
while ($tables = mysql_fetch_assoc($selecty)) {
if($tables['followerid']!=$table['id']) {
echo ''.'';
} else {
echo ''.'';
}
}
echo '</td>';
echo "<tr>";
This is more of a logic question and whether or not a nested while loop is the right way to do it. What I'm trying to say is if the 'followerid' from 'user followers table' is not the same as the 'id' from users table (which is from the previous loop) - echo the follow button, else echo the following button.
This is working file while I have data in the followers table but If I don't nothing shows (as there are no rows) - How could I implement this in my PHP? So also if there are no rows in 'followers table' echo follow button?
you can try do it like that
$selecty = mysql_query("SELECT * FROM followers WHERE userid='".$_SESSION['id']."'");
$rowsy = mysql_num_rows($selecty);
echo '<td>'. $table["username"]. '</td>';
echo '<td>';
while ($tables = mysql_fetch_assoc($selecty)) {
if($tables['followerid']!=$table['id'] and $tables['followerid'] != '') {
echo '';
} else if($tables['followerid'] =$table['id'] and $tables['followerid'] !='') {
echo '';
} else {
echo what you like here when $tables['followerid'] = ''
}
}
echo '</td>';
echo "<tr>";
edit
class="follow">'.'</a>'
^------------you dont have to make point and single quotes here
$selecty = mysql_query("SELECT * FROM followers WHERE userid='".$_SESSION['id']."'");
$rowsy = mysql_num_rows($selecty);
echo '<table><tr>';
echo '<td>'. $table["username"]. '</td></tr>';
if ($tables['followerid'] !== ''){
while ($tables = mysql_fetch_assoc($selecty)) {
echo '<tr><td>';
if($tables['followerid']!=$table['id'] and $tables['followerid'] != '') {
echo '</td></tr>';
} else if($tables['followerid'] =$table['id'] and $tables['followerid'] !='') {
echo '</td></tr>';
} else {
echo "what you like here </td></tr>";
}
}
}
else {
echo "do your code here " ;
}
echo "</table>";
Put a boolean (FALSE) at the start of the 'followers' loop such that if it gets crossed make it TRUE. If you get outside the loop and it's still FALSE then add the button anyway.
$trip = FALSE;
while ($tables = mysql_fetch_assoc($selecty)) {
if($tables['followerid']!=$table['id']) {
echo ''.'';
} else {
$trip = TRUE;
echo ''.'';
}
}
if( !$trip ) echo '<a href="#" data-userid="'.$_SESSION['id'].'" class="follow">'.'</a

Retrieve only column with value larger than 0

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.

Applying different CSS to top 10 results of a query

The table below prints of the results of a query called $sqlStr3. It works fine. The query is set to return the top 25 results.
I would like to apply a unique CSS class (called "class1" for purposes of this question) to the top ten results in the query. How can I do this?
Thanks in advance,
John
$result = mysql_query($sqlStr3);
$count = 1;
$arr = array();
echo "<table class=\"samplesrec1edit\">";
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td class="sitename1edit2a">'.$count++.'.</td>';
echo '<td class="sitename1edit1">'.stripslashes($row["username"]).'</td>';
echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
echo '</tr>';
}
echo "</table>";
use this:
$result = mysql_query($sqlStr3);
$count = 1;
$arr = array();
echo "<table class=\"samplesrec1edit\">";
while ($row = mysql_fetch_array($result)) {
if($count < 11){
echo '<tr class="top-10">';
}else{
echo '<tr class="non-top-10">';
}
echo '<td class="sitename1edit2a">'.$count++.'.</td>';
echo '<td class="sitename1edit1">'.stripslashes($row["username"]).'</td>';
echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
echo '</tr>';
}
echo "</table>";
access the td of top10 using this css
.top-10 td{
//definitions
}
and access the td of non top10 lines with
.non-top-10 td{
//definitions
}
... class="...<?php if ($count <= 10) { ?> class1<?php } ?>" ...
And move the increment to the end of the loop.
you can do it in the server side via php like:
echo '<tr '.($count <=10 ? 'myclass' : '').' > ;
or in jquery in front side like:
$("someTableSelector").find("tr:lt(10)").addClass('myclass')
How about assigning each result from TOP10 a unique class called top_result_[n]?
while ($row = mysql_fetch_array($result)) {
echo '<tr'.($count++ <=10 ? ' class="top_result_'.$count.'"' : '').'>';
echo '<td class="sitename1edit2a">'.$count.'.</td>';
echo '<td class="sitename1edit1">'.stripslashes($row["username"]).'</td>';
echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
echo '</tr>';
}
echo "</table>";
I'm not sure wether you want to apply ONE class for all 10 results or UNIQUE class for each from 10 results, as for the first case, the code would be:
while ($row = mysql_fetch_array($result)) {
echo '<tr'.($count++ <=10 ? ' class="top_result"' : '').'>';
echo '<td class="sitename1edit2a">'.$count.'.</td>';
echo '<td class="sitename1edit1">'.stripslashes($row["username"]).'</td>';
echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
echo '</tr>';
}
echo "</table>";
Use CSS:
.samplesrec1edit tr:nth-child(-n+10) {
cssrule:value;
}
This technique only works in newer browsers however. The following link has compatibility charts.
http://reference.sitepoint.com/css/pseudoclass-nthchild

Categories