...

View Full Version : PHP Sessions Problem



user55
11-19-2007, 03:16 AM
Hi,

I have three PHP files:
=====================
standings.php
totals.php
totals_search.php

Standings.php should display a table like this:

http://i219.photobucket.com/albums/cc48/jdfw00/one.gif

But instead, it will display it like this: The last four columns (QUOTA,MEMBERS,PERCENTAGE and SCORES) all show the same result for each DIVISION.

http://i219.photobucket.com/albums/cc48/jdfw00/2.gif

It gets this information through sessions from totals.php. You can see it below.


<?php

//more code...

foreach ($resultArray as $val) {
$datarow_color = ($datarow_count % 2) ? $color1 : $color2;

echo ('<tr bgcolor='. $datarow_color .'>');//first row
if (($rowcount%2) == 0) {
$css_class = "\"rows\"";
} else {
$css_class = "\"odd\"";
}
echo ('<td class=' . $css_class . ' align=default >'.($val[45] == 'Y' ? 'X' : '').'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.($val[47] == 'Y' ? 'X' : '').'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[0].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[1].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[2].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[4].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[6].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[7].'&nbsp</td>');

if ($val[9] == 'Y') $temp = 'X';
elseif ($val[9] == 'N') $temp = '';
else $temp = $val[9];

echo ('<td class=' . $css_class . ' align=default >'.$temp.'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=center >'.($val[10] == 'Y' ? 'X' : '').'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=center >'.($val[11] == 'Y' ? 'X' : '').'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=center >'.($val[12] == 'Y' ? 'X' : '').'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=center >'.($val[14] == 'Y' ? 'X' : '').'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=center >'.($val[56] == 'Y' ? 'X' : '').'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=center >'.($val[16] == 'Y' ? 'X' : '').'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[18].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=center >'.($val[20] == 'Y' ? 'X' : '').'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=center >'.($val[22] == 'Y' ? 'X' : '').'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[24].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[25].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[26].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[27].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[28].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[29].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[31].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[33].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[35].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[37].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=center >'.($val[39] == 'Y' ? 'X' : '').'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[52].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[53].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[54].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[41].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[43].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[49].'&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >'.$val[50].'&nbsp</td>');
echo ('</tr>');
$datarow_count++;
}

//begin totals
$empty1 = 0; $sum = 0; $sum3 = 0; $avg4 = 0; $empty5 = 0; $empty2 = 0; $empty3 = 0; $empty4 = 0;
foreach ($resultArray as $val) {
if(empty($val[45]) || $val[45] == 'N') $empty1++;
if(empty($val[47]) || $val[47] == 'N') $empty2++;
if(empty($val[0])) $empty3++;//id,division,location
if(empty($val[4])) $empty4++;//quota
if(empty($val[6])) $empty5++;//members
if(empty($val[9]) || $val[9] == 'N') $empty6++;
if(empty($val[10])) $empty7++;
if(empty($val[11])) $empty8++;
if(empty($val[12])) $empty9++;
if(empty($val[14])) $empty10++;
if(empty($val[56]) || $val[56] == 'N') $empty11++;
if(empty($val[16]) || $val[16] == 'N') $empty12++;
if(empty($val[20]) || $val[20] == 'N') $empty13++;
if(empty($val[22]) || $val[22] == 'N') $empty14++;
if(empty($val[24]) || $val[24] == 'N') $empty15++;
if(empty($val[25])) $empty16++;
if(empty($val[26])) $empty17++;
if(empty($val[27])) $empty18++;
if(empty($val[28])) $empty19++;
if(empty($val[29])) $empty20++;
if(empty($val[33])) $empty21++;
if(empty($val[35])) $empty22++;
if(empty($val[37])) $empty23++;
if(empty($val[39])) $empty24++;
if(empty($val[52])) $empty25++;
if(empty($val[53])) $empty26++;
if(empty($val[54])) $empty27++;
if(empty($val[41])) $empty28++;
if(empty($val[43])) $empty29++;
if(empty($val[49])) $empty30++;
if(empty($val[50])) $empty31++;
$sum++;
$sum3 += $val[4];//total quota
$sum4 += $val[6];//total members
$sum5 += $val[18];
$sum6 += $val[31];
$avg4 += $val[7];//average percentage
}

//begin division report totals
echo "<tr class=\"divisiontotals\">
<td>".($empty1 == 0 ? 'X' : '')."</td>
<td>".($empty2 == 0 ? 'X' : '')."</td>
<td colspan=\"3\">DIVISION REPORTS</td>
<td></td>
<td></td>
<td></td>
<td>".($empty6 == 0 ? 'X' : '')."</td>
<td>".($empty7 == 0 ? 'X' : '')."</td>
<td>".($empty8 == 0 ? 'X' : '')."</td>
<td>".($empty9 == 0 ? 'X' : '')."</td>
<td>".($empty10 == 0 ? 'X' : '')."</td>
<td>".($empty11 == 0 ? 'X' : '')."</td>
<td>".($empty12 == 0 ? 'X' : '')."</td>
<td></td>
<td>".($empty13 == 0 ? 'X' : '')."</td>
<td>".($empty14 == 0 ? 'X' : '')."</td>
<td>".($empty15 == 0 ? 'X' : '')."</td>
<td>".($empty16 == 0 ? 'X' : '')."</td>
<td>".($empty17 == 0 ? 'X' : '')."</td>
<td>".($empty18 == 0 ? 'X' : '')."</td>
<td>".($empty19 == 0 ? 'X' : '')."</td>
<td>".($empty20 == 0 ? 'X' : '')."</td>
<td></td>
<td>".($empty21 == 0 ? 'X' : '')."</td>
<td>".($empty22 == 0 ? 'X' : '')."</td>
<td>".($empty23 == 0 ? 'X' : '')."</td>
<td align=center>".($empty24 == 0 ? 'X' : '')."</td>
<td>".($empty25 == 0 ? 'X' : '')."</td>
<td>".($empty26 == 0 ? 'X' : '')."</td>
<td>".($empty27 == 0 ? 'X' : '')."</td>
<td>".($empty28 == 0 ? 'X' : '')."</td>
<td>".($empty29 == 0 ? 'X' : '')."</td>
<td>".($empty30 == 0 ? 'X' : '')."</td>
<td>".($empty31 == 0 ? 'X' : '')."</td>
</tr>";
//end division report totals

echo "<tr class=\"trtotals\">
<td>".($sum-$empty1)."</td>
<td>".($sum-$empty2)."</td>
<td colspan=\"3\">".($sum-$empty3)." Total</td>
<td>$sum3</td>
<td>$sum4</td>
<td>".number_format($avg4/$sum, 2)."%</td>
<td>".($sum-$empty6)."</td>
<td>".($sum-$empty7)."</td>
<td>".($sum-$empty8)."</td>
<td>".($sum-$empty9)."</td>
<td>".($sum-$empty10)."</td>
<td>".($sum-$empty11)."</td>
<td>".($sum-$empty12)."</td>
<td>$sum5</td>
<td>".($sum-$empty13)."</td>
<td>".($sum-$empty14)."</td>
<td>".($sum-$empty15)."</td>
<td>".($sum-$empty16)."</td>
<td>".($sum-$empty17)."</td>
<td>".($sum-$empty18)."</td>
<td>".($sum-$empty19)."</td>
<td>".($sum-$empty20)."</td>
<td>$sum6</td>
<td>".($sum-$empty21)."</td>
<td>".($sum-$empty22)."</td>
<td>".($sum-$empty23)."</td>
<td>".($sum-$empty24)."</td>
<td>".($sum-$empty25)."</td>
<td>".($sum-$empty26)."</td>
<td>".($sum-$empty27)."</td>
<td>".($sum-$empty28)."</td>
<td>".($sum-$empty29)."</td>
<td>".($sum-$empty30)."</td>
<td>".($sum-$empty31)."</td>
</tr>";


echo "<tr class=\"trpoints1\">";//1st row of scores
echo ('<td>'.($sum-$empty1).'</td>');
echo ('<td>'.($sum-$empty2).'</td>');
echo "<td colspan=\"3\">Scores</td>";
echo ('<td>'.number_format($resultArray[0][61]/$sum3 * $sum4, 2).'</td>');
echo "<td colspan=\"2\"></td>";
echo "<td colspan=\"4\">".number_format(8 / (4*$sum-$empty3) * $sum-$empty6, 2)."</td>";
echo "<td colspan=\"3\">".number_format((3 / $sum-$empty3) * $sum-$empty10, 2)."</td>";
echo "<td>".number_format(9 / (20*$sum-$empty3), 2)."</td>";
echo "<td>".number_format((3/$sum-$empty3) * ($sum-$empty13), 2)."</td>";
echo "<td>".number_format((3/$sum-$empty3) * ($sum-$empty14), 2)."</td>";
echo "<td colspan=\"6\">".number_format(3/(6*$sum-$empty3) * ($sum-$empty15+$sum-$empty16+$sum-$empty17+$sum-$empty18+$sum-$empty19+$sum-$empty20), 2)."</td>";
echo "<td>".number_format(3/ (9*$sum-$empty3) * ($sum-$empty14), 2)."</td>";
echo ('</tr>');


$scorestotal = ($sum-$empty1)+($sum-$empty2)+($resultArray[0][61]/$sum3 * $sum4)+(8 / (4*$sum-$empty3) * $sum-$empty6)+((3 / $sum-$empty3) * $sum-$empty10)+(9 / (20*$sum-$empty3))+
((3/$sum-$empty3) * ($sum-$empty13))+((3/$sum-$empty3) * ($sum-$empty14))+(3/(6*$sum-$empty3) * ($sum-$empty15+$sum-$empty16+$sum-$empty17+$sum-$empty18+$sum-$empty19+$sum-$empty20))+(3/ (9*$sum-$empty3) * ($sum-$empty14));


$_SESSION["sum3"] = $sum3; // <-----this will show in standings.php
$_SESSION["sum4"] = $sum4; // <-----this will show in standings.php
$_SESSION["avgdsum"] = $avg4/$sum; // <-----this will show in standings.php
$_SESSION["scorestotal"] = $scorestotal; // <-----this will show in standings.php

//more code...

?>

Here is some of the code from standings.php:


<?php
if ($num_rows > 0)
{
?>

<?php


echo "<strong>Standings</strong> ";
echo "<br />";

echo "<table cellpadding=\'2\' cellspacing=\'0\'>";

echo "<tr>";
echo "<td class=\"ThRows\" >ORDER</td>";
echo "<td class=\"ThRows\" >DIVISION</td>";
echo "<td class=\"ThRows\" >MANAGER</td>";
echo "<td class=\"ThRows\" >QUOTA</td>";
echo "<td class=\"ThRows\" >MEMBERS</td>";
echo "<td class=\"ThRows\" >%QUOTA</td>";
echo "<td class=\"ThRows\" >SCORE</td>";
echo "</tr>";


session_start();


$query = "SELECT scores.division, scores.a55, managers.division, managers.name ".

"FROM scores, managers ".

"WHERE scores.division = managers.division";

$result = mysql_query($query);

if($result && mysql_num_rows($result) > 0)
{
$standings = 1;
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
$resultArray = mysql_fetch_array($result);

echo ('<tr>');//first row
if (($rowcount%2) == 0) {
$css_class = "\"row\"";
} else {
$css_class = "\"alt\"";
}


echo ('<td class=' . $css_class . ' align=default >'.$standings.'</td>');
echo ('<td class=' . $css_class . ' align=default >' . $resultArray[0] . '&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >' . $resultArray[3] . '&nbsp</td>');
echo ('<td class=' . $css_class . ' align=default >' . $_SESSION["sum3"] .'</td>');
echo ('<td class=' . $css_class . ' align=default >' . $_SESSION["sum4"] .'</td>');
echo ('<td class=' . $css_class . ' align=default >' . round($_SESSION["avgdsum"],2) . '%</td>');
echo ('<td class=' . $css_class . ' align=default >' . round($_SESSION["scorestotal"],2) . '</td>');
echo ('</tr>');
$standings++;


} // end if results
$i++;
}

echo "</table>";

echo "<br />";

session_write_close();

?>

I can post all of the code for all three PHP files if needed.

user55
11-19-2007, 03:18 AM
More info on the problem:

The totals.php file only displays the totals for one division at a time:

There are 12 links in totals.php that when a user clicks on any of them it will displaly the data for that DIVISION.
It stays on totals.php when clicked on and only the url and data change.

totals.php?search_fd1=1&&'> <-- DIVISION 1

totals.php?search_fd1=2&&'> <-- DIVISION 2

and so on...

Those totals in the SESSIONS above display a total according to which DIVISION you are looking at.


The links look like this:

echo "<ul>
<li><a href='http://www.mysite.com/totals.php?search_fd1=1&&'>1</a>
<li><a href='http://www.mysite.com/totals.php?search_fd1=2&&'>2</a>
<li><a href='http://www.mysite.com/totals.php?search_fd1=3&&'>3</a>
<li><a href='http://www.mysite.com/totals.php?search_fd1=4&&'>4</a>
<li><a href='http://www.mysite.com/totals.php?search_fd1=5&&'>5</a>
<li><a href='http://www.mysite.com/totals.php?search_fd1=6&&'>6</a>
<li><a href='http://www.mysite.com/totals.php?search_fd1=7&&'>7</a>
<li><a href='http://www.mysite.com/totals.php?search_fd1=8&&'>8</a>
<li><a href='http://www.mysite.com/totals.php?search_fd1=9&&'>9</a>
<li><a href='http://www.mysite.com/totals.php?search_fd1=10&&'>10</a>
<li><a href='http://www.mysite.com/totals.php?search_fd1=11&&'>11</a>
<li><a href='http://www.mysite.com/totals.php?search_fd1=12&&'>12</a>
</ul>";

I got those links by doing a search on totals_search.php for each division and then posted them in totals.php.

That is how I get those links. I guess thats why I keep getting the same results in those session, but I am not sure.

user55
11-19-2007, 04:54 AM
The data in the second column "DIVISION" is wrong. It should be the same as the column "ORDER". Sorry about that.

Any help is appreciated. Thanks, Sara.

Inigoesdr
11-19-2007, 06:19 AM
This may or may not be related to your problem, but you seem to be missing a closing bracket for your for() loop. Also, you set the variable for each column only once, so that's why you have the same data on every row. You really should just generate the averages on the standings page to avoid this problem altogether.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum