I really need someone to help me on the scenario below.

I fetch all the rows in a database table with the mysql_fetch_array() and it was successful. I placed a button on each row in order to click and take the user to another page. On click of the button, i want session to take the values of all columns on that particular row to the other page.

But i discovered that on click on any of the button, session only registered the last row of the table not minding maybe i click on the button of the first row or any other row. I need someone to put me through on this and i will really appreciate all suggestions.

Thanks in advance.

below is a copy of my script for your understanding.

$select = "select * from platform";
		  $result = mysql_query($select) or die(mysql_error());
		  
		  echo "<table border = '1' bordercolor = '#CC6600' cellspacing = '0'>";
echo "<tr><th>Category</th><th>Code</th><th>Game</th><th>Valid Time</th></tr>";

			while($row = mysql_fetch_array($result)){
			
			echo '<form name = "form2" method = "post" action = "predictproc.php">';
			
			echo "<tr><td width = '100'>";
	echo $row['category'];

	echo "</td><td width = '100'>";

	echo $row['competitioncode'];

	echo "</td><td width = '250'>";

	echo $row['teama']; echo '&nbsp;&nbsp;'; echo "vs"; echo '&nbsp;&nbsp;'; echo $row['teamb'];

	echo "</td><td width = '100'>";

	echo $row['kickoff'];

	echo "</td><td>";

	echo '<input type = "Submit" name = "Submit" value =  "Play This" class = "button">';

	echo "</td></tr>";

		$gamecode = $row['competitioncode'];

	
			echo '</form>';
			
		//my problem starts here, I want session variables to be created for each row if I click on the button of that particular row. but no matter what button i click, it retrieves record of only the last row on the other page.  
			
				$predict = "select * from platform where competitioncode = '$gamecode'";

				$predict_result = mysql_query($predict) or die("can't select for predict");

				if($predict_result)
				{
					if(mysql_num_rows($predict_result) == 1)
					{
						$predictrow = mysql_fetch_array($predict_result);

					$_SESSION['category'] = $predictrow['category'];

				$_SESSION['competitioncode'] = $predictrow['competitioncode'];

				$_SESSION['teama'] = $predictrow['teama'];

				$_SESSION['teamb'] = $predictrow['teamb'];

				$_SESSION['kickoff'] = $predictrow['kickoff'];	
					}

				}
				
						
			}		
				echo "</table>";
				
	
				
					
	?>

Recommended Answers

All 8 Replies

you should keep an index or row number

$i=0;
while($row = mysql_fetch_array($result)){
echo "<form>";
$_SESSION[$i]['category'] = $predictrow['category'];
echo "<input type=hidden name=clickindex id=clickindex value={$i}>";
$i++;
echo "</form>";
}

now in your action page(predictproc.php) you can use clickindex variable

<?php
$index=$_POST['clickindex];
echo $_SESSION[]['category'];
?>

$predictrow = mysql_fetch_array($predict_result);

$predictrow = mysql_fetch_assoc($predict_result);

assoc = associative array (with key names)
array = plain array (with only values)

change

$index=$_POST;
echo $_SESSION[[B]$index[/B]];

Thanks man, but the session will not only print the category column but on click on the button attached to each row, all the columns of the row should be transferred with session.

like the category, gamecode, teama, teamb.

please help me out and am also try on my part here but never solved the problem.


Thanks in advance.

category was just example you need to set what ever number of columns you want
only important is index of row which is clicked

i find it hard to understand what you are asking, from what urtrivedi said your problem is that the session variable is just overwritten everytime cause it has the same name, you want to add an extra index like above.

$_SESSION[0] = $predictrow;
$_SESSION[0] = $predictrow;
$_SESSION[0] = $predictrow;
$_SESSION[0] = $predictrow;

$_SESSION[1] = $predictrow;
$_SESSION[1] = $predictrow;
$_SESSION[1] = $predictrow;
$_SESSION[1] = $predictrow;

etc..

that's interesting mysql_fetch_array is pulling an associative array for you though, i always thought it didn't must of been how the first server i used was setup

so do you advice me to use assoc instead of array?

so do you advice me to use assoc instead of array?

Nah it will make no difference, i looked up on php.net and mysql_fetch_array defaults to pulling both associative and numbered arrays.

-> http://www.php.net/manual/en/function.mysql-fetch-array.php

it should save processing time using assoc though


Using the session to store them values seems pointless to me, this whole section should just be removed off this page and added on the next page:

//my problem starts here, I want session variables to be created for each row if I click on the button of that particular row. but no matter what button i click, it retrieves record of only the last row on the other page.  
 
				$predict = "select * from platform where competitioncode = '$gamecode'";
 
				$predict_result = mysql_query($predict) or die("can't select for predict");
 
				if($predict_result)
				{
					if(mysql_num_rows($predict_result) == 1)
					{
						$predictrow = mysql_fetch_array($predict_result);
 
					$_SESSION['category'] = $predictrow['category'];
 
				$_SESSION['competitioncode'] = $predictrow['competitioncode'];
 
				$_SESSION['teama'] = $predictrow['teama'];
 
				$_SESSION['teamb'] = $predictrow['teamb'];
 
				$_SESSION['kickoff'] = $predictrow['kickoff'];	
					}
 
				}
 
 
			}

then edit the submit slightly to pass the competition code onto the next page

<?php
....

<td>";
 	echo '<input type="hidden" name="compcode" value="'.$row['competitioncode'].'"/>';
 	echo '<input type = "Submit" name = "Submit" value =  "Play This" class = "button">';
 
	echo "</td>
....
?>

then on your next page the competition code will be accessed via $_POST;
and do the mysql query on that page to get the data

commented: Yes you are right, I was just trying to explain how you can make session array, But your suggestion is better to have less load on sessions +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.