Jump to content

Recommended Posts

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 :)
Link to comment
https://forums.phpfreaks.com/topic/30102-session-problem/
Share on other sites

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 one
echo $_SESSION['forum_id'][1]; // echo 2nd one
echo $_SESSION['forum_id'][2]; // echo 3rd one
// you get the picture
[/code]
Link to comment
https://forums.phpfreaks.com/topic/30102-session-problem/#findComment-138394
Share on other sites

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 23

I also tried echoing out but the same error came up :S
Link to comment
https://forums.phpfreaks.com/topic/30102-session-problem/#findComment-138397
Share on other sites

Sorry, i got it to work but its not the thing i was after

It 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 :(
Link to comment
https://forums.phpfreaks.com/topic/30102-session-problem/#findComment-138435
Share on other sites

The output is exactly the same as from my first reply by which it only passes the last value of forum_id

O 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 do

thanks 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 :)
Link to comment
https://forums.phpfreaks.com/topic/30102-session-problem/#findComment-138442
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.