Jump to content

Add up numbers then echo


flynryan692

Recommended Posts

Ok, so..I think I may sorta know how to do this..but I am unsure...I'm a n00b at php :/

 

So, on my site when the person joins then can transfer hours which is called thours in mysql. Then, as a member they fill out a report will all the details blah blah great. Well, so far it all works, however I want it so when a person looks at the roster is displays the transfer hours but then adds up all the hours from each report. So, I guess what I am asking is how would I write it so that is gathers up the duration (dur in mysql) from each report and adds it to thours for each person. I'll post the code from the page called hub. I want to add $dur (on the pirep page) with thours on the hub page... I hope me posting this code doesn't make it super long and annoying..I'm just unsure of how much of the code will be needed so you guys know what to do to help...this code is the hub page.

 

<?
if($hub==''){echo'No Hub Slected';}elseif($hub == 'KLAX'){?><style type="text/css">

<? } ?>

<? 
if ($_GET[sort]){ $sort = $_GET[sort]; } else { $sort = "login $view"; }


if ($_GET[view]){ $view = $_GET[view]; } else { $view = "ASC"; }



if ($view == "ASC"){ $newview = "DESC"; } elseif ($view == "DESC"){ $newview = "ASC"; } else { $view = "DESC"; }

if ($_GET[hub]){ $hub = "hub='$_GET[hub]' AND"; } else { $hub = ""; }



if ($sort == "login"){ $mlist = mysql_query("SELECT * FROM `pilots` WHERE  status = '1' AND hub='$hub' ORDER BY login $view") or die(mysql_error()); } else {

$mlist = mysql_query("SELECT * FROM `pilots` WHERE status = '1' AND hub='$phub' ORDER BY $sort $view")or die(mysql_error()); }

$mlrows = mysql_num_rows($mlist);  
  
  
  
  
  $hubstaff = mysql_query( "SELECT * FROM `pilots` WHERE hm = '1' AND hub = '$phub' LIMIT 1" )
  or die("MySQL Said:".mysql_error());
  $hubs_num = mysql_num_rows($hubstaff);
  
   if($hubs_num == 0){echo'<table widith=100%>No hub manager selected for this hub currently</table>'; } elseif ($hubs_num == '1'){
   
   
   
   $hubr = mysql_fetch_assoc($hubstaff);
   
   $fname = $hubr["fname"];
   $lname = $hubr["lname"];
   $hlogin = $hubr["login"];
   $hemail = $hubr["semail"];
   

   
   echo"San Jose Operations Manager:  $hlogin $fname $lname<br />
   Email :  $hemail";
   
   }
  
?>
  <table width=100% cellspacing=0>


<?  if($mlrows == 0){
  echo'No Pilots in this hub';
  } else {
  echo"

<tr bgcolor=#034e75>
  <td colspan=7 align=center><div align=center><span class=style1><font color=white>SnapJet $phub Pilot Roster</font></span></div></td>
</tR>
<tr bgcolor=#EEEEEE align=center>
<td><a href=#>Login</font></a></td>
<td><a href=#>First Name</a></font></td>
<td><a href=#>Last Name</font></a></td>
<td><a href=#>Rating</font></a></td>
<td><a href=#>Hours</font></a></td>
</tr>
<br /><br />
";
  
  while ($alr = mysql_fetch_array($mlist)){




$counter = $counter + 1;



if ($counter % 1 == 0){ $bgcolor = "#FFFFFF"; }

if ($counter % 2 == 0){ $bgcolor = "#EEEEEE"; }

?>
</tr>


<tr align=center bgcolor=<? echo"$bgcolor";?>>

<td width=10%><a href="?page=profile&login=<? echo"$alr[login]";?>"><? echo"$alr[login]";?></a></td>

<td width=15%><? echo "$alr[fname]"; ?></td>

<td width=15%><? echo "$alr[lname]"; ?></td>

<td width=15%><? echo "$alr[rating]"; ?></td>

<td width=10%><? echo "$alr[thours]"; ?></td>




<? }} ?>
</table>



  <br />

<center>

</center>

Link to comment
Share on other sites

Well, I have figured this much out..however it is showing the same amount for each person and for some reason it isn't adding either...

 

Any ideas?

 

$plink = mysql_query("SELECT sum( dur ) as H FROM pireps WHERE login='$glogin' AND status='1' ")
  or die("MySQL Said:".mysql_error());
  
$presult = mysql_fetch_assoc($plink);


$dur = $presult['H'];

$hlink = mysql_query("SELECT ( thours ) as HR FROM `pilots` WHERE status = '1' AND hub='$phub' ")or die(mysql_error()); 

$hresult = mysql_fetch_assoc($hlink);

$hours = $hresult['HR'];

$test = $dur + $hours;

Link to comment
Share on other sites

If I'm understanding you properly, your problem lies within the fact that you're basically displaying the last person's hours for everyone, because of the way your queries are set up.

 

You need to build it so that one query runs all the users, then within that there's a query that runs the dur and one that runs the thours and adds them up and then produces the row for the table. The user query finishes, moves on to the next and does it again. Something like this:

 

echo "<table>";

$user_query = mysql_query("SELECT * FROM users_table") or die(mysql_error());

while($row = mysql_fetch_array($user_query)) {

$dur_query = mysql_query("SELECT SUM(dur) AS H FROM pireps WHERE login='$glogin' AND status='1' ") or die(mysql_error());

while($row = mysql_fetch_array($dur_query)) {

$dur = $row['H'];

}

$thour_query = mysql_query("SELECT thours AS HR FROM `pilots` WHERE status = '1' AND hub='$phub' ") or die(mysql_error());

while($row = mysql_fetch_array($user_query)) {

$thours = $row['HR'];

}

$total = $thours + $dur;

echo "<tr><td>user x</td><td>".$total."</td></tr>";

}

echo "</table>";

 

That should work if I'm understanding the issue. Not sure if you wanted thours to be summed as well when pulled from the DB, if so, just add SUM before the () on the query.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.