Retrieve variable from one page to display on second page in PHP - 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>";
}

Actually, the way you tried is correct.. But, if you're still looking for an alternative, one method would be store each team rank in a separate session variable... something like $_SESSION[$teamid] = $rank;
for($i = 0; $i < $rows; $i++) {
$result->data_seek($i);
$row = $result->fetch_array(MYSQLI_NUM);
$rank = $row[12];
$teamid = $row[0];
$_SESSION[$teamid] = $rank;
echo "<tr>";
echo "<td class='rank'>" . $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>";
}
Then on you individual page you can try something like:
$teamid = $_GET['team_id'];
$rank = $_SESSION[$teamid];

Related

PHP get min and max values from 50 random numbers?

<?php
echo "<table border='1'><br />";
for ($row = 0; $row < 10; $row ++) {
echo "<tr>";
for ($col = 0; $col < 5; $col ++) {
$rand = rand (1, 200);
echo "<td>", $rand, "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Here is code but my question is how can I find max and min from values of the table? Do I have to make random numbers somehow into array?
You can do as following without changing much of your code
<?php
$array = array(); // <-- added code
echo "<table border='1'><br />";
for ($row = 0; $row < 10; $row ++) {
echo "<tr>";
for ($col = 0; $col < 5; $col ++) {
$rand = rand(1, 200);
$array[] = $rand; // <-- added code
echo "<td>", $rand, "</td>";
}
echo "</tr>";
}
echo "</table>";
$min = min($array); // <--- added code
$max = max($array); // <--- added code
?>
Check it out the max and min value of an aray in PHP using library function.
for($col = 0; $col < 5; $col ++) {
$rand[] = rand (1, 200);
}
echo $max = max($rand)."<br/>";
echo $min = min($rand)."<br/>";
print_r($rand);

display first set of result in row

I want to iterate an array of data using foreach loop.
However, I want the first 4 results to display two items per row.Thereafter I want the rest of the data to display I item per row
i found a possible solution here
$data = range(1, 30);
for($count = 0; $count < count($data);)
{
echo "<tr>\n";
for($i = 0; $count < count($data) && $i < 2; $count++, $i++) {
echo "\t<td>$data[$count]</td>\n";
}
for(; $i < 2; $i++) {
echo "\t<td>-</td>\n";
}
echo "</tr>\n";
}
how the problem with this code is that it displays all the data in rows of two.i however only want the first 4 results to display that way.
You can do something like this:
$data = range(1, 30);
for($count = 0; $count < count($data); ++$count){
if($count < 4){
if($count % 2 == 0){
echo "<tr><td>" . $data[$count] . "</td>";
}else{
echo "<td>" . $data[$count] . "</td></tr>";
}
}else{
echo "<tr><td>" . $data[$count] . "</td></tr>";
}
}
Here's the demo
You're using the already incremented $i variable twice.
$i is never < 2
Add this instead
for($j = 0; $j < 2; $j++) {
echo "\t<td>-</td>\n";
}
Test here
Try with -
$data = range(1, 30);
for($count = 0; $count < count($data); $count++)
{
echo "<tr>";
if($count < 2) {
for($i = 1; $i <= 2; $i++) {
echo "<td>" . $data[$count] . "</td>";
if($i == 1) {
echo "</tr><tr>";
}
}
} else {
echo "<td>" . $data[$count] . "</td>";
}
echo "</tr>";
}
What about something like this?
echo '<table border=1>';
$data = range(1, 30);
for($count = 0; $count < count($data); $count++){
echo "<tr>\n";
if($count < 4) {
echo "\t<td>$data[$count]</td>\n";
$count++;
echo "\t<td>$data[$count]</td>\n";
} else {
echo "\t<td colspan=2>$data[$count]</td>\n";
}
echo "</tr>\n";
}
echo '</table>';

Loop through a PHP Multidimensional Array

Im trying to create a 2D array with a specific number of rows and cols which I have stored as the variable $n ; so if $n was 5, I would have 5 rows and 5 cols, all with random numbers. I have created a for loop (as shown below) that generates the correct amount of rows but I cannot figure out how to do the same with the columns at the same time. The code I have at the moment is shown below.
<?php
$n = 3;
for($i=0; $i<=$n; $i++) {
$value[$i][0] = rand(1,20);
$value[$i][1] = rand(1,20);
$value[$i][2] = rand(1,20);
$value[$i][3] = rand(1,20);
}
print "<table>";
for($j=0; $j<$n; $j++) { // Runs the loop times $n
print "<tr>";
for($k=0; $k<$n; $k++) { // Runs the loop times $n
print "<td>" . $value[$j][$k] . "</td>";
}
print "</tr>";
}
print "</table>";
?>
Any help would be appreciated in learning to create this loop of the array. Thanks in advance.
You need a second loop inside the first loop:
for($i=0; $i<=$n; $i++) { // rows
for($j=0; $j<=$n; $j++) { // columns
$value[$i][$j] = rand(1,20);
}
}
Try this instead :
$n = 3;
for($i=0; $i<=$n; $i++) {
$value[$i][0] = rand(1,20);
$value[$i][1] = rand(1,20);
$value[$i][2] = rand(1,20);
$value[$i][3] = rand(1,20);
}
print "<table>";
foreach($value as $row){
print "<tr>";
foreach($row as $cell){
print "<td>" . $cell . "</td>";
}
print "</tr>";
}
print "</table>";
And using a for loop :
print "<table>";
for($i=0; $i<=$n; $i++) { // rows
print "<tr>";
for($j=0; $j<=(count($value[$i])-1); $j++) { // columns
echo "<td>".$value[$i][$j]."</td>";
}
print "</tr>";
}
print "</table>";

Why is this for loop only doing one iteration?

For some reason this loop only does one iteration and makes only 1 row. Can anyone spot an error?
for($i = 0; $i < 7; $i++){
echo "<tr>";
echo "<td>" . ($min + ($i * 5)) . "</td>";
for($i = 1; $i < 6; $i++){
echo "<td>" . $min . "</td>";
}
echo "</tr>";
}
You are using same variable in both loops ($i). Use another variable in second loop.

php array with in array names don't output

Is this possible to do?
i'm looking to build a grid of values, but I was wondering if this is possible
$section_array = array(
$array_1[$i],
$array_2[$i],
$array_3[$i]
);
for ($i = 1; $i <= 31; $i++) {
echo "<tr>";
for ($j = 0; $j < 3; $j++) {
echo "<td>" . $section_array[$j] . "</td>"; // 3
}
echo "</tr>";
}
This is basically the idea, I want to print out 1 value of each section (array_1, array_2) etc
then move to the next day, and print out the values of day 2 and so on. is it possible without having to just list every single one in the 2nd for loop?
Are you looking for something like this? Loop through your array of arrays and print 2 values from the sub array.
$arr = //insert data here
foreach($arr as $subarr) {
echo '<tr><td>' . $subarr['myval1'] . '</td><td>' . $subarr['myval2'] . '</td></tr>';
}
$secname_array = array($val1, $val2, $val3);
$daycount = 31;
for ($i = 1; $i <= $daycount; $i++){
echo"<tr>
<td>$i</td>";
foreach ($secname_array as $v)
{
echo "<td>". $v[$j]. "</td>";
}
$j++;
echo"</tr>";
}
?>

Categories