wattsup88 Posted December 14, 2006 Share Posted December 14, 2006 Ok I was just wondering how to achieve this goal... I need to pass a row returned from mysql to a variable then to another page after its clicked on... I have tried two methods... The $SESSION method and the $_POST method... Both return the last value of the row and not the actual row im clicking on... Here is the code for the $_POST method post1.php:[code]<form name="viewjobs" action="post2.php><?php$result = mysql_query("SELECT * FROM jobs ORDER BY id");//query displayedwhile ($row = mysql_fetch_array($result)) { $id = $row['id'];print "<table border=\"1\">";print "<tr>";print "<td width=\"200\"><input type=\"hidden\" name=\"id\" value=\"$id\"><input type=\"submit\" value=\"View Job\">";print "<td width=\"200\">$id";print "</td>";print "</table>";}?></form>[/code]post2.php:[code]<?phpecho $_POST['id'];?>[/code]It posts the value "8" as that is the last value of the table... The next method does the same thing... However, keep in mind that I am trying to make the value of the row "clicked on" appear on the next page...Here is the $_SESSION method used...session1:[code]<a href="session2.php">Click Here</a><?php$result = mysql_query("SELECT * FROM jobs ORDER BY id");//query displayedwhile ($row = mysql_fetch_array($result)) { $id = $row['id'];print "<table border=\"1\">";print "<tr>";print "<td width=\"200\">$_SESSION['id'] = "$id";print "<td width=\"200\">$id";print "</td>";print "</table>";}?>[/code]session2:[code]<?phpecho $_SESSION['id'];?>[/code]As i stated earlier it returns the value "8"... My best guess is that the variable is overritten every time a value from the database is returned and the last value written is the last value in the table... If anyone has a method that would be functional that would be fantistic... i know i posted something about this before but now its a little more specific so you can understand...Thanks in advance-Jake Quote Link to comment Share on other sites More sharing options...
SharkBait Posted December 14, 2006 Share Posted December 14, 2006 You need to set your session like this:[code=php:0]$_SESSION['id'] = $id;[/code]On a line by itself in your code you are just displaying $_SESSION and whatever $id is equal to.To display all rows that the query found you can do:[code]<?php$result = mysql_query("SELECT * FROM jobs ORDER BY id");echo "<table>";//query displayedwhile ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $id = $row['id']; // Create multi-dimension array to store all ids $_SESSION['id'][$i] = $id; // Display IDs in a table echo " <tr> <td width=\"200\">{$id}</td> </tr>"; $i++;}echo "</table>";?>[/code]Not sure if it is totally correct, but the idea is there :) Quote Link to comment Share on other sites More sharing options...
uktrips007 Posted December 14, 2006 Share Posted December 14, 2006 why don't you store the $row array to session variable and access it in the other page like as follows$_SESSION['row']=$row;and access this in other page like$_SESSION['row'][0] for first element of row similerly for other element of row. Quote Link to comment Share on other sites More sharing options...
.josh Posted December 14, 2006 Share Posted December 14, 2006 just to clarify, you are only getting the last row because you keep assigning row info to the same variable and then overwriting it on each iteration of the loop. You need to do like in sharkbait's example, where you create a new element in an array each iteration: the part where he has $i and it increments each time. Or you could just leave that off and do this:$_SESSION['id'][] = $id;because php knows to create the next element without you explicitly specifying it (like with the $i) Quote Link to comment Share on other sites More sharing options...
wattsup88 Posted December 14, 2006 Author Share Posted December 14, 2006 One more question... How do i return the value clicked on the next page... Quote Link to comment Share on other sites More sharing options...
.josh Posted December 14, 2006 Share Posted December 14, 2006 [code]echo $_SESSION['id'][0]; // echos 1st oneecho $_SESSION['id'][1]; // echos 2nd one// echo all of them in a loopforeach ($_SESSION['id'] as $val) { echo "$val <br/>";}[/code] Quote Link to comment Share on other sites More sharing options...
wattsup88 Posted December 16, 2006 Author Share Posted December 16, 2006 I keep getting this message:Warning: Invalid argument supplied for foreach() ... obviously wrong for each statement but i dont know why... Quote Link to comment Share on other sites More sharing options...
wattsup88 Posted December 16, 2006 Author Share Posted December 16, 2006 oh and i used the foreach statement that crayon violet gave me in the post before my last one... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.