By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,889 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,889 IT Pros & Developers. It's quick & easy.

help: include file with variable inside div tag

P: 28
I have a simple message board I created a while back.

Recently they've asked me to add threading support for messages.

so I created a seperate thread page, which gives me each threaded message after querying the database.

Then, on the main page, what I want it to do is display the original message,
then display the threads under it, after you click on the "show threads" link im trying to put somwhere around the original thread. Then display the next message, with threads under it (of course after the thread button is clicked). I don't want the page to refresh if possible and dont mind if i need to pull all the data first, then format it after, then display the page.

I'll paste pertinent code below, if im missing anything, i'm glad to throw any code requested:

main page
Expand|Select|Wrap|Line Numbers
  1. <head>
  2.  
  3. <style type="text/css">
  4. div {
  5. color: white;
  6. display: none;
  7. }
  8. </style>
  9.  
  10. <script language="JavaScript">
  11. function setVisibility(id, visibility) {
  12. document.getElementById(id).style.display = visibility;
  13. }
  14. </script>
  15.  
  16. </head>
  17.  /* more page data */
  18.  
  19. // database query to show messages withought the thread
  20. $result=mssql_query("SELECT * FROM Message_Board WHERE $searchby LIKE '%$search2%' 
  21. AND thread_number=0
  22. ORDER BY TStamp DESC");
  23.  
  24. // loop to display message without thread
  25. $i=0;
  26. while ($i < $num) {
  27.  
  28. $First_Name=mssql_result($result,$i,"First_Name");
  29. $Category=mssql_result($result,$i,"Category");
  30. $TStamp=mssql_result($result,$i,"TStamp");
  31. $msg=nl2br(mssql_result($result,$i,"msg"));
  32. $id=mssql_result($result,$i,"id");
  33. $thread_number=mssql_result($result,$i,"thread_number");
  34. $thread_id=mssql_result($result,$i,"thread_id");
  35.  
  36. ?>
  37.  
  38. <? echo $msg; ?>
  39. <br>
  40. <input type=button name=type value='Show Layer' onclick="setVisibility('sub3', 'inline');";><input type=button name=type value='Hide Layer' onclick="setVisibility('sub3', 'none');";> 
  41.  
  42. <div id="sub3"><? include('threadtest.php?thread_id=$thread_id'); ?></div>
  43. <?
  44.  
  45. $i++;
  46.  
  47. }
  48.  
  49. mssql_close();
  50. ?>
  51.  
here's pertinent code for 'threadtest.php:
Expand|Select|Wrap|Line Numbers
  1. <? $thread_id=$_GET['thread_id']; ?>
  2.  
  3. // database query
  4. $result=mssql_query("SELECT * FROM Message_Board 
  5. WHERE $searchby LIKE '%$search2%' 
  6. AND thread_id=$thread_id
  7. AND thread_number != 0
  8. ORDER BY TStamp ASC");
  9.  
  10. // loop to display threaded messages for the message with "thread_id"
  11. <?
  12. $i=0;
  13. while ($i < $num) {
  14.  
  15. $First_Name=mssql_result($result,$i,"First_Name");
  16. $Category=mssql_result($result,$i,"Category");
  17. $TStamp=mssql_result($result,$i,"TStamp");
  18. $msg=nl2br(mssql_result($result,$i,"msg"));
  19. $id=mssql_result($result,$i,"id");
  20. $thread_number=mssql_result($result,$i,"thread_number");
  21. $thread_id=mssql_result($result,$i,"thread_id");
  22.  
  23. ?>
  24.  
  25. <tr>
  26. <td>
  27. <hr width=50%>
  28. <? echo $msg; ?><br><font size=1><tt><? echo $First_Name; ?> at <? echo $TStamp; ?></tt></font> 
  29. </td>
  30. </tr>
  31.  
  32. <?
  33.  
  34. $i++;
  35.  
  36. }
  37.  
  38. mssql_close();
  39.  
I put the code for the loops in so you see what I'm trying to do.
If the message has a thread number of 0, its the original message, any message with higher values would be threads for that particular message. in which I don't want to be displayed unless clicked on. And just an fyi, the thread_id is the same on the original message and any threaded messages off that particular original message.

i also know from reading forums all day that i can't put variables in an include statement
Expand|Select|Wrap|Line Numbers
  1. include('threadtest.php?thread_id=$thread_id');
, but i wanted to show what i am looking to return, and the fact that the threadtest.php page needs the thread_id (which would be different for each original message) and then hits the database again with it (should I be hitting the database twice like that?).

I left out a lot of code i figured would be useless for this question. Plz assume the database connects correctly and im getting the results from the database that i want.

When i browse to www.website.com/threadtest.php?thread_id=2345 ,for example,
it displays exactly what i want to include on the main page. i only want it to display tho when the user clicks.

I believe i would be able to code it with the page refreshing, but i'm sure there must be a way to do what i'm trying to.

My other problem (non php related?) is in the list of original messages, it doesnt matter which "click to view thread" button i hit, it always displays in the first message only ....i.e., i click on the "view thread" button for message number 3, but the drop down appears in message number 1.

any help appreciated =) thx
Oct 2 '08 #1
Share this Question
Share on Google+
5 Replies


Dormilich
Expert Mod 5K+
P: 8,639
If you don't want to use refresh you can use ajax for that, another advantage of this is, that you can call a script with parameters.

regards ← has to break-fast now
Oct 2 '08 #2

Markus
Expert 5K+
P: 6,050
Put the thread id in a constant, before you include the file. Then you can access the constant in the included file. Simple.

Cheers,
Markus has an inkling he may be of no help because he has just woke up. :D
Oct 2 '08 #3

P: 28
Put the thread id in a constant, before you include the file. Then you can access the constant in the included file. Simple.

Cheers,
Markus has an inkling he may be of no help because he has just woke up. :D
Thanks for the response, I go back on sunday so I will give it a shot.

Here's what I'm thinking I'm going to try then...

from lines 34 - 43 on the main page code, looks like I already set the thread_id, so just removing the variable in the include statement may work =)
likt this:
Expand|Select|Wrap|Line Numbers
  1. $thread_id=mssql_result($result,$i,"thread_id"); 
  2.  
  3. ?> 
  4.  
  5. <? echo $msg; ?> 
  6. <br> 
  7. <input type=button name=type value='Show Layer' onclick="setVisibility('sub3', 'inline');";><input type=button name=type value='Hide Layer' onclick="setVisibility('sub3', 'none');";>  
  8.  
  9. <div id="sub3"><? include('threadtest.php'); ?></div> 
  10. <? 
  11.  
and then from threadtest.php remove:
Expand|Select|Wrap|Line Numbers
  1. <? $thread_id=$_GET['thread_id']; ?> 
=======================================

If you don't want to use refresh you can use ajax for that, another advantage of this is, that you can call a script with parameters.

regards ← has to break-fast now
Thanks Dorm, I will check on ajax for the non refresh
Oct 2 '08 #4

Markus
Expert 5K+
P: 6,050
Yep, I gave it a quick glance and looks fantabbydoosy!

See you 'round
Oct 2 '08 #5

P: 28
thanks guys for all the help,

i needed to make a couple changes to make it work, but all basically based on the above changes.

i had to change the variables in the second loop (in threadtest.php) because i think the loop variables in threadtest were switching the loop variables in the main page.....particularly "$i" , "$result", and "$num".

looks like i also had a "mssql_close();" at the end of threadtest.php which may have been stopping the loop from the main page, removed it along with a "session_start();", and an extra database connect string i had in threadtest.php.

I also fixed the problem where the thread was only displaying on the first message, i used the value of $id to add to the id name in the div tag, worked perfectly for each message, and it doesn't reload the page, and I didn't have to use ajax, altho i want to learn more about ajax for future reference, once the fixes were in, the page actually workd =)

Here's the changed code for anyone interested, once again really appreciate the help and i hope i can help someone else out at some point =)


Main Page:
Expand|Select|Wrap|Line Numbers
  1. <head> 
  2.  
  3. <style type="text/css"> 
  4. div { 
  5. color: white; 
  6. display: none; 
  7. </style> 
  8.  
  9. <script language="JavaScript"> 
  10. function setVisibility(id, visibility) { 
  11. document.getElementById(id).style.display = visibility; 
  12. </script> 
  13.  
  14. </head> 
  15.  /* more page data */ 
  16.  
  17. // database query to show messages withought the thread 
  18. $result=mssql_query("SELECT * FROM Message_Board WHERE $searchby LIKE '%$search2%'  
  19. AND thread_number=0 
  20. ORDER BY TStamp DESC"); 
  21.  
  22. // loop to display message without thread 
  23. $i=0; 
  24. while ($i < $num) { 
  25.  
  26. $First_Name=mssql_result($result,$i,"First_Name"); 
  27. $Category=mssql_result($result,$i,"Category"); 
  28. $TStamp=mssql_result($result,$i,"TStamp"); 
  29. $msg=nl2br(mssql_result($result,$i,"msg")); 
  30. $id=mssql_result($result,$i,"id"); 
  31. $thread_number=mssql_result($result,$i,"thread_number"); 
  32. $thread_id=mssql_result($result,$i,"thread_id"); 
  33.  
  34. ?> 
  35.  
  36. <? echo $msg; ?> 
  37. <br> 
  38. <input type=button name=type value='Show Layer' onclick="setVisibility('sub<? echo $id; ?>', 'inline');";><input type=button name=type value='Hide Layer' onclick="setVisibility('sub<? echo $id; ?>', 'none');";>  
  39.  
  40. <div id="sub<? echo $id; ?>"><? include('threadtest.php'); ?></div> 
  41. <? 
  42.  
  43. $i++; 
  44.  
  45.  
  46. mssql_close(); 
  47. ?> 
  48.  
  49.  

threadtest.php:
Expand|Select|Wrap|Line Numbers
  1. // database query 
  2.  
  3. // renamed loop variables so as not to get confused with index.php
  4.  
  5. $result2=mssql_query("SELECT * FROM Message_Board 
  6. WHERE thread_id=$thread_id
  7. AND thread_number != 0
  8. ORDER BY TStamp ASC");
  9.  
  10. // loop to display threaded messages for the message with "thread_id" 
  11. $num2=mssql_num_rows($result2);
  12.  
  13. $i2=0;
  14. while ($i2 < $num2) {
  15.  
  16. // removed unnecessary variables
  17.  
  18. $First_Name=mssql_result($result2,$i2,"First_Name");
  19. // $Category=mssql_result($result,$i,"Category");
  20. $TStamp=mssql_result($result2,$i2,"TStamp");
  21. $msg=nl2br(mssql_result($result2,$i2,"msg"));
  22. // $id=mssql_result($result,$i,"id");
  23. // $thread_number=mssql_result($result,$i,"thread_number");
  24. // $thread_id=mssql_result($result,$i,"thread_id");
  25.  
  26. ?>
  27.  
  28.     <hr width=50%>
  29.         <? echo $msg; ?><br><font size=1><tt><? echo $First_Name; ?> at <? echo $TStamp; ?></tt></font> 
  30.  
  31. <?
  32.  
  33. $i2++;
  34.  
  35. }
  36.  
  37. ?>
  38.  
  39. // removed line - shouldn't close database yet
  40. // mssql_close(); 
  41.  
Oct 5 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.