table output php and mysql not getting result - php

I have the following: The result I am getting is the image attached. I would like the username to be listed only once and then the win, loose across the page. not a row for each result.
$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY 'username' asc ")
or die (mysql_error());
while ($row = mysql_fetch_array($sql_events)) {
$username = $row["username"];
$week = $row["win_loose"] ;
$row_color = ($row_count++ % 2 == 0 ? $color1 : $color2);
echo '<tr style="background-color: '.$row_color.';">';
echo '<td style="width: 100" align="center"><font size="2">'.$username.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';

you have to update your code. You are querying correctly but taking the data in a wrong way. You have to try associative array to solve your problem. Please try following codes-
$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY 'username' asc ") or die (mysql_error());
while ($row = mysql_fetch_array($sql_events)) {
$username[$row["username"]][] = $row["win_loose"];
}
foreach($username as $key=>$val){
echo '<td style="width: 100" align="center"><font size="2">'.$username.'</td>';
foreach($val as $value){
echo '<td style="width: 50" align="center"><font size="2">'.$value.'</td>';
}
}
One more thing to mention, it is not a good practice to use inline css on every table column. You could use a class and get the css to the css file attached to that class.
Hope it helps...:)

You're pre-echoing weeks for which you don't have data yet. The idea is, print a cell for each week as long as it's referring to the same user:
$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY username asc ")
or die(mysql_error());
$current_username = null;
while ($row = mysql_fetch_array($sql_events)) {
$username = $row["username"];
//User changed
if ($current_username == null || $current_username != $username) {
if ($current_username != null) {
echo '</tr>'; //Had another user before so end the row
}
$row_color = ($row_count++ % 2 == 0 ? $color1 : $color2);
echo '<tr style="background-color: ' . $row_color . ';">';
echo '<td style="width: 100" align="center"><font size="2">' . $username . '</td>';
$current_username = $username;
}
$week = $row["win_loose"];
echo '<td style="width: 50" align="center"><font size="2">' . $week . '</td>';
}
echo '</tr>';

Related

Total of column in displayed result in PHP

I have a table 'tblexam' which contains State,city,candidate.I want to fetch the data from this table using where clause.I am able to fetch the data but i want to add the sum of Candidate at the last row(As Shown In Picture) how can i do that .
$sql="SELECT *
FROM tblexam
WHERE state='UP'";
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result) {
$cnt=$cnt+1; ?>
<tr class="odd gradeX">
<td class="center"><?php echo htmlentities($cnt);?></td>
<td class="left"align="left"><?php echo htmlentities($result->state);?></td>
<td class="center" align="left"><?php echo htmlentities($result->city);?></td>
<td class="center"align="left"><?php echo htmlentities($result->candidate);?></td>
<?php } ?>
<td class="center"align="left"><?php echo htmlentities($result->Total);?></td>
</tbody>
</table>
Add total while iterating through rows and display it outside the loop, Sample code is given below
$sql = "SELECT * FROM tblexam WHERE city='UP'";
$result = $conn->query($sql);
$numRows = $result->num_rows;
if ($numRows> 0) {
$total = 0;
echo '<table border="1" cellpadding="5" cellspacing="0">';
echo '<tr>';
echo '<th>state</th>';
echo '<th>city</th>';
echo '<th>candidate</th>';
echo '</tr>';
while($row = $result->fetch_assoc()) {
$total+=$row['candidate'];
echo '<tr>';
echo '<td>'.$row['city'].'</td>';
echo '<td>'.$row['state'].'</td>';
echo '<td>'.$row['candidate'].'</td>';
echo '</tr>';
}
echo '<tr>';
echo '<td>Total</td>';
echo '<td> </td>';
echo '<td>'.$total.'</td>';
echo '</tr>';
echo '<table>';
}
Before you start looping the data you can create a variable which you set to 0, inside the loop you can add the result's total to this variable, after the loop, the variable will contain the grand total:
$total = 0;
foreach($results as $result) {
$total += (int)$result->Total;
...
}
// $total = 1425

Adding dynamic data to a table using php

Database schema looks like
ID Link_name Description
and wish The link name from the database to load in the table as
-----------------------------
Link_name1 | Link_name2 |
------------------------------
-----------------------------
Link_name3 | Link_name4 |
------------------------------
But the code
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
require('config.php');
$datae = mysql_query("SELECT * FROM `products` ORDER BY id DESC")or die(mysql_error()); while($infoe = mysql_fetch_array( $datae )) {?>
<tr>
<td width="7%"> </td>
<td width="93%" class="main_text">
<h3><?php echo $infoe['Link_name']; }?><hr><br></h3>
</td>
</tr>
</table>
Output as
--------------
Link_name1 |
--------------
---------------
Link_name2 |
--------------
Not sure what are you asking for, but just a guess:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
require('config.php');
$datae = mysql_query("SELECT * FROM `products` ORDER BY id DESC")or die(mysql_error());
$leftRight = 'left';
while($infoe = mysql_fetch_array( $datae )) {
if ( $leftRight == 'left') {
echo '<tr><td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td>';
$leftRight = 'right';
} else {
echo '<td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td></tr>';
$leftRight = 'left';
}
}
if ( $leftRight == 'right') echo '<td></td><td></td></tr>'; ?>
</table>
EDIT If you need to format N column table:
$current = 1;
$columnLimit = 3; //you can set any number of column to output
while($infoe = mysql_fetch_array( $datae )) {
if ( $current == 1) {
echo '<tr><td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td>';
$current++;
} elseif ( $current == $columnLimit) { {
echo '<td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td></tr>';
$current = 1;
} else {
echo '<td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td>';
$current++;
}
}
while ( $current!=1 && $current <= $columnLimit) {
if ($current != $columnLimit)
echo '<td></td>';
else
echo '<td></td></tr>';
$current++;
}
?>
Use the modulus operator (http://php.net/manual/de/language.operators.arithmetic.php)
With that you know if your current data entry is a odd or even number. so you can say something like: If your a even dataset then close the current row and open a new one.

print data with auto increment in paging

i know it's a silly question, but it is creating a problem for me....
I want to print data from mysql with auto increment, but when i use the below code, it auto increments the value in the same page but i want it to be continued to the next pages also, here is my code
<?php
$perpage = 10;
$start = (isset($_GET['id'])) ? $_GET['id'] : 0;
$TotalRec = mysql_result(mysql_query("SELECT COUNT(*) FROM register where
r_bid='".$_SESSION["id"]."'"), 0);
$select = "SELECT * FROM register where r_bid='".$_SESSION["id"]."' LIMIT
$start,$perpage";
$result = mysql_query($select) or die(mysql_error());
$res = mysql_query("select * from regi_balic where b_id='".$_SESSION["id"]."'");
$row1=mysql_fetch_array($res);
$i=1;
while($row = mysql_fetch_array($result))
{
echo '<tr>
<td align="center" width=5%><font size=3>'.$i.'</font></td>
<td width=12%><font size=3>'.$row1['name'].'</font></td>
<td align="center" width=5%><font size=3><a href="edit_detail.phpid='.$row["r_id"].'
&cand_id='.$_SESSION["id"].'&email='.$row["email"].'">'.$row['name'].'</a></font></td>
<td align="center" width=5%><font size=3>'.$row['reference'].'</font></td>
<td align="right" style="padding-right:8px" width=12%>
<fontsize=3>'.$row['age'].'</font></td>
<td align="right" style="padding-right:8px" width=12%>
<fontsize=3>'.$row['occupation'].'</font></td>
<td width=12%><font size=3>'.$row['mob_no'].'</font></td>
<td width=2%><a href="process_del_client.php?id='.$row['r_id'].'"
onClick="returnConfirmSubmit(\'Are You sure ?\')"><img src = "images/delete.png">
</a></td>
</tr>';
}
$i++;
echo '</table>';
if($start == 0)
{
echo "<br>Previous Page ";
}
else
{
echo '<br><a href='."./view.php?id=" . ($start - $perpage) . '>'.
"PreviousPage".'</a> ';
}
if($start + $perpage >= $TotalRec)
{
echo " Next Page<br>";
}
else
{
echo ' <a href='."./view.php?id=" . ($start + $perpage) . '>'."Next Page".'
</a><br>';
}
?>
update value in a session variable and use that onto another page for updation .
Ex..
$_SESSION['value']=0
$_SESSION['value']=$_SESSION['value']++;
try changing
$i=1;
to
$i=1+$start;

Alternative to using PHP-GMP to populate two column table

A page is using the PHP GNU Multiple Precision (GMP) to determine how many rows should end up in each column. While the code works, we would like to migrate the application to a new host that does not support the PHP5-GMP module. That said, what might be the best alternative to generating and populating a two column table that may not always have an even number of results? Further the table design is probably not the best so I am open to alternatives.
The code below taken from here gets us close but since it uses mysql_fetch_array, it places the items like:
a b
c d
instead of
a c
b d
Is there a way to have it display in the first example?
Newer code:
<head>
<style>
.container { width: 400px; float: left; }
.container .item { width: 50%; float: left; height: someFixedHeight; }
</style>
</head>
<?php
mysql_select_db("database",$db);
$cat= $_GET["cat"];
echo '<div class="container">';
$q = mysql_query("SELECT * FROM name WHERE Field4 = '$cat'",$db);
while ($res = mysql_fetch_array($q)){
echo '<div class="item">' . $res['Field1'] . '</div>';
}
echo '</div>';
?>
Original code:
<div id="bit-list" align="center" >
<table class="list" width="600" border="0" align="center">
<tr>
<td width="300" height="72" valign="top" border="1">
<div align="left">
<?php
$result = mysql_query("SELECT * FROM name WHERE field4 = '$cat' ",$db);
$myrow3 = mysql_fetch_row($result);
$num_rows = mysql_num_rows($result);
$d3 = gmp_div_q("$num_rows", "2", GMP_ROUND_PLUSINF);
$i = 1;
$result8 = mysql_query("SELECT * FROM name WHERE field4 = '$cat' ",$db);
while ($myrow3 = mysql_fetch_row($result8))
{
if ($i <= gmp_strval($d3))
{
echo "<p>";
echo '<a href="detail.php?';
echo 'page=';
echo "$myrow3[2]";
echo '&';
echo 'pnum=';
echo "$myrow3[6]";
echo '">';
echo "$myrow3[1]";
echo "</p>";
$i = $i + 1;
}
else
{
}
}
?>
</div></td>
<td width="300" valign="top" border="1">
<div align="left">
<?php
$result = mysql_query("SELECT * FROM name WHERE field4 = '$cat' ",$db);
$myrow3 = mysql_fetch_row($result);
$num_rows = mysql_num_rows($result);
$d4 = gmp_div_q("$num_rows", "2", GMP_ROUND_MINUSINF);
$d3 = gmp_div_q("$num_rows", "2", GMP_ROUND_PLUSINF);
$j = 1;
$result18 = mysql_query("SELECT * FROM name WHERE field4 = '$cat' ",$db);
while ($myrow3 = mysql_fetch_row($result18))
{
if ($j <= gmp_strval($d3))
{
$j=$j+1;
}
else
{
echo "<p>";
echo '<a href="detail.php?';
echo 'page=';
echo "$myrow3[2]";
echo '&';
echo 'pnum=';
echo "$myrow3[6]";
echo '">';
echo "$myrow3[1]";
echo "</p>";
$j = $j + 1;
}
}
?>
</div>
</td>
</tr>
</table>
</div>
If i read well, you use gmp_div_q for rounding only? Then, you should check round().
The only case, that round() cannot cover is GMP_ROUND_ZERO, but you don't use that (and you could cover it with a simple if).

PHP MYSQL, How to show records in four columns ,

Here is my code the records shows in four columns but if my records is blank it shows three balng images, any suggestions?
$query = mysql_query("SELECT * from rbf_events_images where event_id='".$_GET['id']."'");
echo '<table border="1">';
if(count(mysql_num_rows($query)>0)):
$tropentags='<tr>';
$troclosingtags='</tr>';
$formTags="";
$tdTags="";
$count=1;
while($row = mysql_fetch_array($query)){
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
if ($count>3)
{
$formTags.=$tropentags.$tdTags.$troclosingtags;
$tdTags="";
$count=0;
}
$count=$count+1;
}
if ($count>0)
{
for($i = 1; $i <= (4-$count) ; $i++)
{
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
}
$formTags.=$tropentags.$tdTags.$troclosingtags;
}
echo $formTags;
endif;
Thanks for your help!really appreciated!
I noticed that on lines like this one:
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
You are delimiting the string with single quotes ('), and you are also trying to embed a variable in the string that uses single quotes. I'm not sure how you did not get compile errors for that. I would switch to:
$tdTags= '<td align="left" valign="middle" class="td">' . $row['image'] . '</td>';
Here's what I usually do to put records in columns:
$id = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * from rbf_events_images where event_id='$id'");
echo '<table border="1"><tbody><tr>';
if (mysql_num_rows($query) > 0) {
$count = 0;
while ($row = mysql_fetch_array($query)) {
if ($count && $count % 4 == 0) echo '</tr><tr>';
echo '<td align="left" valign="middle" class="td">'.$row['image'].'</td>';
$count++;
}
}
echo '</tr></tbody></table>';

Categories