noobstar Posted December 10, 2006 Share Posted December 10, 2006 Good day everyone,Here is a small code part from my script:[code]while ($line = mysql_fetch_assoc($result)){echo "<div align=center><form method=post action=threads.php style=display:inline><table width=700 border=0><tr><td>";echo "<input type=submit value='-=".$line['category']."=-' class=link>";$_SESSION['forum_id'] = $line['forum_id'];echo $_SESSION['forum_id'];echo "<tr><td valign='middle' align=center bgcolor=#86c9e3><span class='style8'>";echo $line['subject'];echo "</span></td></tr></table></form></div>";}[/code]What im tring to do is simple. It displays all the categories and next to them is the forum_id now when a user clicks on the perticular category it send the session with the forum_id to a place that displays all the threads. However, the session is flawd it only grabs the last value from the forum_id field in the database so in other words it only displays the threads that are in the 2nd forum.This is the problem, when it runs this part ...[code]$_SESSION['forum_id'] = $line['forum_id'];[/code]... however, it only saves the last value found in forum_id.My question is, is there a way to send across the correct session value with the corresponing forum_id?Thank you for any replies or suggestions :) Quote Link to comment https://forums.phpfreaks.com/topic/30102-session-problem/ Share on other sites More sharing options...
.josh Posted December 10, 2006 Share Posted December 10, 2006 it only saves the last one because you keep over-writing the same variable. change it to [code=php:0]$_SESSION['forum_id'][] = $line['forum_id'];[/code]this will make an array of session forum_id to be accessed later on like so:[code=php:0]echo $_SESSION['forum_id'][0]; // echo 1st oneecho $_SESSION['forum_id'][1]; // echo 2nd oneecho $_SESSION['forum_id'][2]; // echo 3rd one// you get the picture[/code] Quote Link to comment https://forums.phpfreaks.com/topic/30102-session-problem/#findComment-138394 Share on other sites More sharing options...
noobstar Posted December 10, 2006 Author Share Posted December 10, 2006 Yea i knew it would have to do something with arrays, im still fairly new to php afterall.I tried what you said however, this is what i got ...Fatal error: [] operator not supported for strings in C:\wamp\www\forum\forum.php on line 23I also tried echoing out but the same error came up :S Quote Link to comment https://forums.phpfreaks.com/topic/30102-session-problem/#findComment-138397 Share on other sites More sharing options...
noobstar Posted December 10, 2006 Author Share Posted December 10, 2006 Sorry, i got it to work but its not the thing i was afterIt does the job in a way however i would have to specify the forum_id manually i need something that puts in automatically something like this code would do:[code] while ($line = mysql_fetch_assoc($result)) { echo "<div align=center><form method=post action=threads.php style=display:inline><table width=700 border=0><tr><td valign=middle align=center bgcolor=#58a7c6>"; echo "<input type=submit value='-=".$line['category']."=-' class=link>"; echo "<input type=hidden name=forum_id value=".$line['forum_id'].">"; echo "<tr><td valign='middle' align=center bgcolor=#86c9e3><span class='style8'>"; echo $line['subject']; echo "</span></td></tr></table></form></div>"; }[/code]This brings in the forum_id ($line['forum_id']) and when i click on my forum category that is sent to threads and then it is used in a query that displays all the related threads to the forum_id.I hope you can understand me :( Quote Link to comment https://forums.phpfreaks.com/topic/30102-session-problem/#findComment-138435 Share on other sites More sharing options...
alpine Posted December 10, 2006 Share Posted December 10, 2006 Try to set $_SESSION['forum_id'] as an array first, and CV's way is fine:[code]<?php$_SESSION['forum_id'] = array();// then run the while()?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/30102-session-problem/#findComment-138437 Share on other sites More sharing options...
noobstar Posted December 10, 2006 Author Share Posted December 10, 2006 The output is exactly the same as from my first reply by which it only passes the last value of forum_idO well never mind i guess i can stick my other way this would have saved quite a bit of coding though but what can i dothanks again though from both of you i do appreciate the help, its just im still a bit green on php and i haven't tried alot out :) Quote Link to comment https://forums.phpfreaks.com/topic/30102-session-problem/#findComment-138442 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.