mardi 21 juin 2016

Retrieve variable from one page to display on second page in PHP

I have a "standings" table which ranks teams from 1-42 through a mysql query. What I am trying to do is retrieve the rank from the first page to display on each team's page via php. I was able to get this to work using a second GET method through the team.php link, but it just doesn't seem right having the rank in the url along with the team id.

Here is my current code on the standings page:

for($i = 0; $i < $rows; $i++) {
$result->data_seek($i);
$row = $result->fetch_array(MYSQLI_NUM);
$rank = $row[12];
$teamid = $row[0];

echo "<tr>";
echo "<td class='rank'>" . $rank . "</td>";
echo "<td class='left'><a href='team.php?team_id=$teamid&rank=$rank'>$row[1]</a></td>";
for($j = 2; $j < 11; $j++) echo "<td>$row[$j]</td>";
echo "</tr>";
}

And then the individual team's page:

$teamid = $_GET['team_id'];
$rank = $_GET["rank"];

I then just echo the $rank variable to display that team's rank, which works fine. However, is there a better way to retrieve the rank variable without sending it through the href? So I could change this:

echo "<td class='left'><a href='team.php?team_id=$teamid&rank=$rank'>$row[1]</a></td>";

Back to:

echo "<td class='left'><a href='team.php?team_id=$teamid'>$row[1]</a></td>";

While retrieving the rank in a different way?

I should note I also attempted to use a SESSION variable to retrieve the rank from the first page. However, each user's page displays the rank of '42', as the for loop overwrites the SESSION variable until the last row. As shown below:

session_start();
for($i = 0; $i < $rows; $i++) {
$result->data_seek($i);
$row = $result->fetch_array(MYSQLI_NUM);
$_SESSION["rank"] = $row[12];
$teamid = $row[0];

echo "<tr>";
echo "<td class='rank'>" . $_SESSION["rank"] . "</td>";
echo "<td class='left'><a href='team.php?team_id=$teamid'>$row[1]</a></td>";
for($j = 2; $j < 11; $j++) echo "<td>$row[$j]</td>";
echo "</tr>";
}

Aucun commentaire:

Enregistrer un commentaire