Is it possible to <br> every 10 table rows in CSS? - php

My script basically loads data from my database and I have to <br> somewhere but it wont work because its a huge table. So I need to format this table so every 10 rows there is a <br>.
<table align="center" cellspacing="0" cellpadding="0">
<?php
$date = mysql_query("SELECT DISTINCT `date` FROM `matches`");
while($daterow = mysql_fetch_assoc($date)){
echo ' <tr>
<td style="width:60px; background-color:#000000;"><font color="#FFFFFF" style="font-size:11px">'.$daterow['date'].'</font></td>
</tr>
';
$query = mysql_query("SELECT hour, team1, team2, goalsteam1, goalsteam2, competition FROM `matches` WHERE `date`='". $daterow['date'] ."'");
while($row = mysql_fetch_array($query)){
$teamtest = mysql_query("SELECT teamname FROM `teams` WHERE `team`='".$row['team1']."'");
$teamtestrow = mysql_fetch_assoc($teamtest);
$hour = substr($row['hour'],0,5);
echo '
<tr class="teamtable">
<td style="width:60px; font-size:11px;">'.$hour.'</td>
<td style="width:145px; font-size:11px;">'.$teamrow['teamtest'].'</td>
<td style="width:15px; font-size:11px;">'.$row['goalsteam1'].'</td>
<td style="width:15px; font-size:11px;">'.$row['goalsteam2'].'</td>
<td style="width:145px; font-size:11px;">'.$row['team2'].'</td>
<td style="width:120; font-size:11px;">'.$row['competition'].'</td>
</tr>';
}
}
?>
</table>
Or just <br> before every date. When I tried it will just leave a huge blank space up but still no space between the date and matches.

Not too sure if CSS can handle this. And unclear on where you expect a break to occur based on the code you are sharing. But in general you would use the modulus operator in PHP would be a good start. Knowing that maybe this would work for you:
<table align="center" cellspacing="0" cellpadding="0">
<?php
$date = mysql_query("SELECT DISTINCT `date` FROM `matches`");
$counter = 0;
while($daterow = mysql_fetch_assoc($date)){
echo ' <tr>
<td style="width:60px; background-color:#000000;"><font color="#FFFFFF" style="font-size:11px">'.$daterow['date'].'</font></td>
</tr>
';
$query = mysql_query("SELECT hour, team1, team2, goalsteam1, goalsteam2, competition FROM `matches` WHERE `date`='". $daterow['date'] ."'");
while($row = mysql_fetch_array($query)){
$teamtest = mysql_query("SELECT teamname FROM `teams` WHERE `team`='".$row['team1']."'");
$teamtestrow = mysql_fetch_assoc($teamtest);
$hour = substr($row['hour'],0,5);
echo '
<tr class="teamtable">
<td style="width:60px; font-size:11px;">'.$hour.'</td>
<td style="width:145px; font-size:11px;">'.$teamrow['teamtest'].'</td>
<td style="width:15px; font-size:11px;">'.$row['goalsteam1'].'</td>
<td style="width:15px; font-size:11px;">'.$row['goalsteam2'].'</td>
<td style="width:145px; font-size:11px;">'.$row['team2'].'</td>
<td style="width:120; font-size:11px;">'.$row['competition'].'</td>
</tr>';
$counter++;
// Do a modulus check.
if ($counter % 10 == 0) {
echo "<tr><td colspan="6"> </td></tr>";
}
}
}
?>
</table>
The key chunk of code is this:
$counter++;
// Do a modulus check.
if ($counter % 10 == 0) {
echo "<tr><td colspan="6"> </td></tr>";
}
On each loop $counter is incremented by 1. And using the modulus operator in the if() statement we check for every 10th item & then do something. In this case insert a row with an in it.

Related

Fixed column width with changing no. of columns

I have a horizontal table that have a changing number of columns.
How do I set a fixed column width? I have tried to set width in the main table setup and in the tr and td tags. No luck so far.
Any suggestions?
Here is my code
...mysql query
while ($row = mysql_fetch_assoc($result))
{
$file_no .= '<td>'.$row['File_no'].'</td>';
$user .= '<td>'.$row['User'].'</td>';
$movement .='<td>'.$row['Downloaded']. " &nbsp " .$row['Uploaded'].'</td>';
$i++;
}
echo '
<table border="1">
<tbody>
<tr align="center">
<td align="left"><b>File no.</b></td>'.$file_no .'
</tr>
<tr align="center">
<td align="left"><b>User</b></td>'.$user .'
</tr>
<tr align="center">
<td align="left"><b>Movement</b></td>'.$movement .'
</tr>
</tbody>
</table>
';

doing a loop inside a loop using mysql ressources

I'm trying to do some loops in loop using mysql but it seems that there not interpreted
it returns only the first result then nothing even If I have many records in my database, It should return everything
below is my code that display the results
while ($cr = mysql_fetch_assoc($result)):
$i = 0;
++$i;
?>
<div style="width:200px" class="fif"><table style="width:100%">
<tr class="fbgreybox">
<td colspan="3" style="text-align: center;font-weight: bold">
<img src="images/calendar_2.png"> Séance du : <?php echo date('d-m-Y', strtotime($cr['record'])); ?>
</td>
</tr>
<?php
$sql = "SELECT `exercise` FROM `workouts` WHERE `record` = '{$cr['record']}' AND `user`= {$_SESSION['userid']} GROUP BY `exercise`";
$result = mysql_query($sql);
while ($exo = mysql_fetch_assoc($result)) :
?>
<tr class="fbinfobox">
<td colspan="3" style="text-align: left">
<img src="images/Sport-dumbbell.png"> ////<?php echo exerciseName($exo['exercise']); ?>
</td>
</tr>
<tr>
<td style="text-align: center;font-weight: bold">Séries</td>
<td style="text-align: center;font-weight: bold">Reps</td>
<td style="text-align: center;font-weight: bold">Poids</td>
</tr>
<?php
$rqt = "SELECT `set_number`, `reps`, `weight` FROM `workouts` WHERE `exercise` = {$exo['exercise']} AND `record` = '{$cr['record']}' AND `user` = {$_SESSION['userid']} ORDER BY `set_number`";
$result2 = mysql_query($rqt);
while ($detail = mysql_fetch_assoc($result2)):
?>
<tr>
<td>
Série ////<?php echo $detail['set_number']; ?>
</td>
<td>
<?php echo $detail['reps']; ?>
</td>
<td>
<?php echo $detail['weight']; ?>
</td>
</tr>
<?php
endwhile;
endwhile;
?>
</table>
</div>
<?php
endwhile;
there is not mistakes in the code so I really do not understand why it is not interpreted.
anykind of help will be much appreciated.
You are using $result in both mysql_fetch_assoc() function. AVOID THAT.
In inner loop you can use another variable:
$sql = "SELECT `exercise` FROM `workouts` WHERE `record` = '{$cr['record']}' AND `user`= {$_SESSION['userid']} GROUP BY `exercise`";
$workoutsResult = mysql_query($sql);
while ($exo = mysql_fetch_assoc($workoutsResult )) :
If I were you, I would first loop through the first query and put it in an array.
Then you loop through this array, and search for the inner queries and also put in arrays inside the other array.
Then, you loop through them to build your table.
It'll make your code cleaner, and will avoid problems like the one you're having, the one #Suresh pointed out.

Switch mysql query to compare results

I have a table containing several columns, one of which is a date column (data1).
The mysql query used is
SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()
We then take the data from each row, run some calculations and store this as a separate variables.
I would now like to compare this data with data from last week (in the same table), i.e. by changing the SELECT query.
Let me expand...
Query for getting this weeks data from table:
$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()";
Now we run through extracting the data
$result = mysql_query($sqld) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
$referred = $referred + $row['referred'];
$invalidated = $invalidated + $row['invalidated'];
$tobequalified = $tobequalified + $row['tobequalified'];
}
(the above is just a snippet of the calculations we need to run to demonstrate).
Now we display the results based on this weeks data
<h4>Totals for this week</h4>
<table class="table stat-table">
<tbody>
<tr>
<td class="value"><? echo $num_rows; ?></td>
<td class="full">Total leads</td>
</tr>
<tr>
<td class="value"><? echo $referred; ?></td>
<td class="full">Referred</td>
</tr>
<tr>
<td class="value"><? echo $invalidated; ?></td>
<td class="full">Invalidated</td>
</tr>
<tr>
<td class="value"><? echo $tobequalified; ?></td>
<td class="full">To be qualified</td>
</tr>
</tbody>
</table>
I'd like to now change the $sqld query above to select rows in the table that fall into last week, run the same calculations above and display the results below so we can compare the two.
<h4> Totals for last week</h4>
<table class="table stat-table">
<tbody>
<tr>
<td class="value"><? echo $num_rows; ?></td>
<td class="full">Total leads</td>
</tr>
<tr>
<td class="value"><? echo $referred; ?></td>
<td class="full">Referred</td>
</tr>
<tr>
<td class="value"><? echo $invalidated; ?></td>
<td class="full">Invalidated</td>
</tr>
<tr>
<td class="value"><? echo $tobequalified; ?></td>
<td class="full">To be qualified</td>
</tr>
</tbody>
</table>
Is there any way of achieving this without copying everything and changing the $sqld query?
I think you can create a function for repeting data and call it with some parameter to change the query like below
somefunction("Previous");
somefunction();
function somefunciton($query = "current") {
if($query == "Current")
$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()";
else
$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 2 WEEK) and (now(),INTERVAL 1 WEEK)";
$result = mysql_query($sqld) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
$referred = $referred + $row['referred'];
$invalidated = $invalidated + $row['invalidated'];
$tobequalified = $tobequalified + $row['tobequalified'];
}
<h4>Totals for this week</h4>
<table class="table stat-table">
<tbody>
<tr>
<td class="value"><? echo $num_rows; ?></td>
<td class="full">Total leads</td>
</tr>
<tr>
<td class="value"><? echo $referred; ?></td>
<td class="full">Referred</td>
</tr>
<tr>
<td class="value"><? echo $invalidated; ?></td>
<td class="full">Invalidated</td>
</tr>
<tr>
<td class="value"><? echo $tobequalified; ?></td>
<td class="full">To be qualified</td>
</tr>
</tbody>
</table>
}
$result1 = mysql_query("SELECT * FROM leads");
$result2 = mysql_query("SELECT * FROM other table");
$arr1 = new Array();
$arr2 = new Array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$arr1['FirstName'] = $row['your column name'];
}
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$arr2['FirstName'] = $row['your column name'];
}
now you have your two arrays($arr1 and $arr2) and you compare as per you want...

Same results from Database

I need to add "Last member topics" in the members profile, so that it shows the last 5 topics that member wrote.
I used this code:
$last_topic = mysql_query("SELECT * FROM " .prefix. "TOPICS WHERE T_AUTHOR = '".$ProMemberID."' ORDER BY T_DATE DESC LIMIT 5") or die (mysql_error());
$result = mysql_num_rows($last_topic);
if ($result > 0){
$rslast = mysql_fetch_array($last_topic);
$topic_id = $rslast['TOPIC_ID'];
$topic_subject = $rslast['T_SUBJECT'];
$topic_mes = $rslast['T_MESSAGE'];
$t_ret = $rslast['T_ENUM'];
$i=0;
while ($i < $result){
echo '<tr>
<td colSpan="2" align="center" class="userdetails_data" width="100%">
<table border="0" width="100%">
<tr>
<td>
<font style="font-size: 9pt" face="Arial"><a href="index.php?mode=t&t='.$topic_id.'">
'.$topic_subject.'</a>
</td>
<td class="editedby" width="130">
<font face="Arial" style="font-size: 9pt"> Edited
:
'.$t_ret.'</font></td>
</tr>
</table>
</td>
</tr>
';
++$i;
}
}else {
echo '<tr><td colSpan="2" align="center" class="userdetails_data" width="100%">
<span lang="ar-eg">
<font color="#FF0000" style="font-size: 9pt" face="Arial">This member has no topics yet! </font></span></td></tr>';
}
It works, but showing the same topic in the 5 rows??!!
any solutions?
its because you are assigning a $rslast one time and then using this variable in loop ,
try this
$last_topic = mysql_query("SELECT * FROM " .prefix. "TOPICS WHERE T_AUTHOR = '".$ProMemberID."' ORDER BY T_DATE DESC LIMIT 5") or die (mysql_error());
$result = mysql_num_rows($last_topic);
if ($result > 0){
while($rslast = mysql_fetch_array($last_topic)) {
$topic_id = $rslast['TOPIC_ID'];
$topic_subject = $rslast['T_SUBJECT'];
$topic_mes = $rslast['T_MESSAGE'];
$t_ret = $rslast['T_ENUM'];
echo '<tr>
<td colSpan="2" align="center" class="userdetails_data" width="100%">
<table border="0" width="100%">
<tr>
<td>
<font style="font-size: 9pt" face="Arial"><a href="index.php?mode=t&t='.$topic_id.'">
'.$topic_subject.'</a>
</td>
<td class="editedby" width="130">
<font face="Arial" style="font-size: 9pt"> Edited
:
'.$t_ret.'</font></td>
</tr>
</table>
</td>
</tr>
';
}
}else {
echo '<tr><td colSpan="2" align="center" class="userdetails_data" width="100%">
<span lang="ar-eg">
<font color="#FF0000" style="font-size: 9pt" face="Arial">This member has no topics yet! </font></span></td></tr>';
}
You are getting same value because your $result var has total rows from query which in this case is 5 while you are saving the value to variable $rslast, instead use while loop so that you can loop over array values.
$last_topic = mysql_query("SELECT * FROM " .prefix. "TOPICS WHERE T_AUTHOR = '".$ProMemberID."' ORDER BY T_DATE DESC LIMIT 5") or die (mysql_error());
$result = mysql_num_rows($last_topic);
if ($result > 0){
while($rslast = mysql_fetch_array($last_topic,MYSQL_ASSOC)) {
//^-----while Loop added to loop through arrays, ^----associative array
$topic_id = $rslast['TOPIC_ID'];
$topic_subject = $rslast['T_SUBJECT'];
$topic_mes = $rslast['T_MESSAGE'];
$t_ret = $rslast['T_ENUM'];
echo '<tr>
<td colSpan="2" align="center" class="userdetails_data" width="100%">
<table border="0" width="100%">
<tr>
<td>
<font style="font-size: 9pt" face="Arial"><a href="index.php?mode=t&t='.$topic_id.'">
'.$topic_subject.'</a>
</td>
<td class="editedby" width="130">
<font face="Arial" style="font-size: 9pt"> Edited :'.$t_ret.'</font></td>
</tr>
</table>
</td>
</tr>';
}}else {
echo '<tr><td colSpan="2" align="center" class="userdetails_data" width="100%">
<span lang="ar-eg">
<font color="#FF0000" style="font-size: 9pt" face="Arial">This member has no topics yet! </font></span></td></tr>';
}

php splitting records in multiple pages is not working properly

i'm facing problem in getting the apprpriate values for some cells ....In the first page i'm getting the appropriate result..but in the next pages the values for product information,total price and profit from order all have value 0 but all the other fields are containing correct information.
Five records are shown per page.. The code is given below:
<?include"dbconnect.php"?>
session_start();
?>
<?include "adminhead.php"?>
<?include "adminleftnav.php"?>
<div id="seasonal">
<table width="635" border="0" align="left" bgcolor="#CCCCCC" >
<tr>
<td>
<table width="635" border="0" align="left" >
<tr>
<td width="52" bgcolor="#E0EBED">Order Number</td>
<td width="150" bgcolor="#E0EBED">Product Information</td>
<td width="100" bgcolor="#E0EBED">Total Price(TK)</td>
<td width="90" bgcolor="#E0EBED">Order Date</td>
<td width="80" bgcolor="#E0EBED">Payment Status</td>
<td width="100" bgcolor="#E0EBED">Profit from order(TK)</td>
</tr>
</table>
</td>
</tr>
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1)*5 ;
$sql = "Select distinct order_no,date,payment_status from product_order_info order by order_no ASC LIMIT $start_from,5";
$query_for_order1 = mysql_query ($sql);
?>
<tr>
<td>
<table id="foo" border="1" bgcolor="#f4eddb" cellspacing="2" cellborder="2" width=635>
<? while($row_for_my_order1=mysql_fetch_assoc($query_for_order1))
{
?>
<tr>
<td width=52>
<?echo $row_for_my_order1['order_no'];?>
</td>
<td width=156>
<?
$sql1 = "Select * from product_order_info where order_no= '$row_for_my_order1[order_no]' order by order_no ASC LIMIT $start_from,5";
$query_for_order = mysql_query($sql1);
while($row_for_my_order=mysql_fetch_assoc($query_for_order))
{
$query_for_product_details=mysql_query("select * from product where product_no='$row_for_my_order[product_id]' ");
$row_for_product_details=mysql_fetch_array($query_for_product_details);
?>
<br>
<?echo $row_for_product_details['product_name'];
?>
<br>
Quantity
<?echo $row_for_my_order['quantity'];
}
?>
</td>
<td width=106>
<?
$total=0;
$sql1 = "Select * from product_order_info where order_no= '$row_for_my_order1[order_no]' order by order_no ASC LIMIT $start_from,5";
$query_for_order = mysql_query($sql1);
while($row_for_my_order2=mysql_fetch_assoc($query_for_order))
{
$query_for_product_details=mysql_query("select * from product where product_no='$row_for_my_order2[product_id]' ");
$row_for_product_details=mysql_fetch_array($query_for_product_details);
$total = $total+ $row_for_product_details['sell_price'] * $row_for_my_order2['quantity'];
}
echo $total;
?>
</td>
<td width=90>
<?echo $row_for_my_order1['date'];?>
</td>
<td>
<?echo $row_for_my_order1['payment_status'];?>
</td>
<td width=100>
<? $total=0;
$sql1 = "Select * from product_order_info where order_no= '$row_for_my_order1[order_no]' order by order_no ASC LIMIT $start_from,5";
$query_for_order = mysql_query($sql1);
while($row_for_my_order3=mysql_fetch_assoc($query_for_order))
{
$total=$total+$row_for_my_order3['profit'];
}
echo $total; ?>
</td>
</tr>
<?
}
?>
</table>
</div>
<?php
$sql = "SELECT DISTINCT COUNT(order_no) FROM product_order_info";
$rs_result = mysql_query($sql);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 5);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='view_order.php?page=".$i."'>".$i."</a> ";
};
?>
<?include "footer1.php"?>
Please help me in this regard...
You should delete LIMIT $start_from,5 from every query except the first one. As Dagon pointed out, you should consider the possibility to use a single query, to reduce the number of queries to the SQL server.

Categories