News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Getting Variables from the Forum? How

Started by blood618^_^, July 25, 2007, 01:24:24 AM

Previous topic - Next topic

blood618^_^

Lets say I wanna get this Variables from the forum What should I do?

Username
User's number of post
User's Profile Information
User's Avatar

Users own Profile, My Message link and if in a certain group member (i.e. Admin, moderator, etc) a Certain link will show up.

Recent Topics from a certain forum board (example show only news and announcement post) just the Subject line and its link

Latest Post just the subject line and its link.

Things like that.

i was looking at the files and sources and what i've found out is the

global $content and some other stuff.. The page is not really inside the forums. but the templates its using will be same as the board default. how would this be possible? thanks :D

spottedhog

print_r($context);
--or--
echo '<pre>', print_r($context, true), '</pre>'; //<--goes in the template somewhere

//or for users
print_r($user);

blood618^_^

#2
how did you get this code? what are the possible variables that are inside those $codes?

what globals and files i should use to make this work? should i copy index.php ?

::EDIT::

Im sorry if im this much of an idiot. Im really lost with this even though i wanna understand it. please help. thx.

spottedhog

Where did I get it???  lol   ;D

I am here nearly every day, reading, and when I find something I think may be useful sometime, I save it.  I got this long ago.

The key is probably to use the print_r($context); code on the page where you wish to call the variables.  This way you will see an array of everything available.  Each page may contain different things, so it is good not to assume...   :)

blood618^_^

#4
i see. this is a php file right ? so it should be in the same folder where everyone goes? hmmm *testing*


::EDIT::

I got this Code


<?php

echo '<pre>', print_r($context, true), ' </pre>';

?>


actually this is your code it didnt work. is there any files i need to suplement this code? security.php? source code like that?

spottedhog

you need to make sure that $context is called in the globals.....  Above that code, put:

globals $context;

blood618^_^

umm no required files even outside the forum folder?

you need to require settings and other files right? before it can load up the variables? ... and the SQL ? im just guessing right now because my pc is having some problems...

spottedhog

you do need to require....

like this:

<?
require_once('path/to/SSI.php');
global $context;
echo '<pre>', print_r($context, true), ' </pre>';
?>


blood618^_^

Thanks for that .. it works! :D

how about latest threads and topics???

i've seen global $topic, $board what is the file required for this one?  and also how can i include permissions to work on this specific page?


I've been reading the index files and load.php hoping to learn more about this... again thanks... right now... im lookin for the topics and the possible permissions to exist when  certain thinggies occur...

Dragooon

You can use SSI functions for latest threads, topics
Or make custom DB queries.

blood618^_^

#10
Im really sorry if im such a burden... I really looking for the solution on this...

require_once('forums/SSI.php');
global
$context, $topic, $board;

echo '<pre>', print_r($topic, true), ' </pre>
<br><br><br><br><br>'
;

echo '<pre>', print_r($board, true), ' </pre>
<br><br><br><br><br>'
;


It didnt work... although my globals already indicate them the latest threads and topics didnt show...

what i want to do is to make the a specific board(e.g. News and Announcements) show its latest 5(Number of results) topics titles along with its link...

also i want to show my users the 10 latest threads which is only in thier permissions... somewhat like the ones from the bottom of the board (latest threads) where you can see a link of a thread...

Those topics will only show a # of characters so that it wont move the certain table...

does anyone get what i mean? im sorry if im vague in my information... please ask if you are confused with this... thanks...
==============================
I was reading the SSI.php but i havent read it completely yet...  although i've read some interesting stuff already...


::EDIT::

Umm.. Once i call the SSI.php that means i can use its functions right ? can i just add new functions in my newfile to coordinate with the SSI?

how do you call functions in SSI ?

(and yes sadly... i still want some answers on my questions before the ::EDIT:: message... :( )

Dragooon

#11

require('/path/to/SMF/SSI.php');
require('/path/to/SMF/Settings.php');
global $scripturl, $db_prefix, $context,$txt;

$result = mysql_query("
SELECT
m.id_msg, m.subject, m.body, t.id_topic, t.id_board,
b.name AS bname, m.id_member
FROM {$db_prefix}topics AS t, {$db_prefix}messages AS m, {$db_prefix}members AS mem, {$db_prefix}boards AS b
WHERE t.id_board = 1
AND b.id_board = 1
AND m.id_msg = t.id_first_msg
AND mem.id_member = m.id_member
ORDER BY t.id_topic DESC
LIMIT 5") or die(mysql_error());

while ($row = mysql_fetch_array($result))
{
$bodylen = 200;
// Limit the length of the message, if the option is set.
if (strlen(str_replace('<br />', "\n", $row['body'])) > $bodylen)
$row['body'] = htmlspecialchars(substr(str_replace('<br />', "\n", un_htmlspecialchars($row['body'])), 0, $bodylen-3) . '...', ENT_QUOTES);


censorText($row['body']);
censorText($row['subject']);


$context['news][] = array(
'title' => $row['subject'],
'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
'description' => $row['body'],
'author' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
'comments' => $scripturl . '?action=post;topic=' . $row['id_topic'] . '.0',
'guid' => $row['id_msg'],
                        'replies' => $row['numReplies']
);

You can use this code to call 5 latest topics from a Board. It will show first 200 letters from it.
Edit WHERE t.id_board = 1
AND b.id_board = 1

To determine your board ID.
Edit $bodylen = 200; to determine your character limit
Edit LIMIT 5 to limit the no. of topics.

Now to throw it out you need
foreach( $context['ghinfo'] as $info)
echo'
<strong><br><a href="', $info['link'], '">', $info['title'], '</strong></a>
<span>', $info['description'];
)
( I didnt properly tested this thing so it may give errors.)

For Recent topics
global $scripturl, $db_prefix, $context, $smfFunc, $txt, $user_info, $modSettings;
//Main query
$result = db_query("
SELECT
m.id_msg, m.subject, m.body, t.id_topic, t.id_board,
b.name AS bname,m.id_member,b.memberGroups
FROM {$db_prefix}topics AS t, {$db_prefix}messages AS m, {$db_prefix}members AS mem, {$db_prefix}boards AS b
WHERE t.id_board = b.id_board
AND m.id_msg = t.id_first_msg
AND mem.id_member = m.id_member
AND b.memberGroups = b.memberGroups
AND $user_info[query_see_board]
AND ID_BOARD != $modSettings[recycle_board]
ORDER BY t.id_topic  DESC
LIMIT 5") or die(mysql_error());

while ($row = mysql_fetch_assoc($result))
{      //The variable which decides the MAX letters it can have.
$bodylen = 200;
// Limit the length of the message, if the option is set.
if (strlen(str_replace('<br />', "\n", $row['body'])) > $bodylen)
$row['body'] = htmlspecialchars(substr(str_replace('<br />', "\n", un_htmlspecialchars($row['body'])), 0, $bodylen-3) . '...', ENT_QUOTES);


censorText($row['body']);
censorText($row['subject']);

$context['rp'][] = array(
'title' => $row['subject'],
'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
'description' => $row['body'],
'category' =>  $scripturl .'?board=' .$row['id_board'] . '.0',
'bname' => $row['bname']
);
}
foreach( $context['rp'] as $info)
echo'
<dt><br><a href="', $info['link'], '">', $info['title'], '</a></dt>
<dd>', $info['description'], '<br>posted in <a href ="',$info['category'],'">',$info['bname'],'</a></dd>
</dlv>';


This code will show 5 recent posts from any board.Its maximum letter limit would be 200 if crossed it will be "...".

Customization :
To limit the number of topics shown:
See that LIMIT 5 and edit the 5 to no. of posts you want.

To show posts from 1 board.
See that t.id_board = b.id_board.Edit it and change it to t.id_board = <Your Board ID> AND b.id_board = <Your Board ID>
Make the Board ID to match the ID of the board you want to show.Remember BOTH entries must be same.

To limit the length of max body letters which can be shown:
Find $bodylen = 200; and edit 200 with what you want.

To remove a description remove ', $info['description'], '

spottedhog

Here... let me throw this out....  The code you placed in a reply:

echo '<pre>', print_r($topic, true), ' </pre>



<br><br><br><br><br>';



echo '<pre>', print_r($board, true), ' </pre>



<br><br><br><br><br>';


...is not "globaled" like $context.  $context is set up in one of the Sources file (forget which one) and this is why you can call it to see all the variables.  There is no such thing for $topic or $board, mainly because it is not needed.  $context contains all info possible.

Dragooon

$context is not stored in 1 single file. It is stored in nos of files.

ioszilla

Quote$context is set up in one of the Sources file (forget which one)

It's initialized in index.php (in root installation folder):

Line #77:

$context = array();

Dragooon

No
have a closer look. $context is defined at many different places. The root $context is at index.php but you really cannot use a heck out of it.

ioszilla


felcon

Quote from: wordzilla on August 13, 2007, 07:27:34 AM
Quote$context is set up in one of the Sources file (forget which one)

It's initialized in index.php (in root installation folder):

Line #77:

$context = array();

$context is only "defined" in index.php but it's been assigned values in various files. mostly in .php files in the source directory.

blood618^_^

#18
I fixed the script... yeah there were some missing brackets and quotes but its fixed and nicely done... i turned it into functions and i received this error

QuoteDatabase Error: No database selected
File: /home/blood618/public_html/forums/Sources/Load.php
Line: 2017

::EDIT::

This was that line:


function sessionDestroy($session_id)
{
global $db_prefix;

if (preg_match('~^[A-Za-z0-9]{16,32}$~', $session_id) == 0)
return false;

// Just delete the row...
return db_query("
DELETE FROM {$db_prefix}sessions
WHERE session_id = '" . addslashes($session_id) . "'
LIMIT 1", __FILE__, __LINE__);
}


::EDIT::

This is the whole code fixed and stuff...


<?php

require('---MYFORUMPATH/SSI.php');
require(
'---MYFORUMPATH/Settings.php');

function
recent_news()
{
global
$scripturl, $db_prefix, $context,$txt;

$result = mysql_query("
SELECT
m.id_msg, m.subject, m.body, t.id_topic, t.id_board,
b.name AS bname, m.id_member
FROM
{$db_prefix}topics AS t, {$db_prefix}messages AS m, {$db_prefix}members AS mem, {$db_prefix}boards AS b
WHERE t.id_board = 1
AND b.id_board = 1
AND m.id_msg = t.id_first_msg
AND mem.id_member = m.id_member
ORDER BY t.id_topic DESC
LIMIT 5"
) or die(mysql_error());

while ($row = mysql_fetch_array($result))
{
$bodylen = 20;
// Limit the length of the message, if the option is set.
if (strlen(str_replace('<br />', "\n", $row['body'])) > $bodylen)
$row['body'] = htmlspecialchars(substr(str_replace('<br />', "\n", un_htmlspecialchars($row['body'])), 0, $bodylen-3) . '...', ENT_QUOTES);


censorText($row['body']);
censorText($row['subject']);


$context['news'][] = array(
'title' => $row['subject'],
'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
'description' => $row['body'],
'author' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
'comments' => $scripturl . '?action=post;topic=' . $row['id_topic'] . '.0',
'guid' => $row['id_msg'],
                       
'replies' => $row['numReplies']
);
}

}

function
recent_topics()
{
global
$scripturl, $db_prefix, $context, $smfFunc, $txt, $user_info, $modSettings;
//Main query
$result = db_query("
SELECT
m.id_msg, m.subject, m.body, t.id_topic, t.id_board,
b.name AS bname,m.id_member,b.memberGroups
FROM
{$db_prefix}topics AS t, {$db_prefix}messages AS m, {$db_prefix}members AS mem, {$db_prefix}boards AS b
WHERE t.id_board = b.id_board
AND m.id_msg = t.id_first_msg
AND mem.id_member = m.id_member
AND b.memberGroups = b.memberGroups
AND
$user_info[query_see_board]
AND ID_BOARD !=
$modSettings[recycle_board]
ORDER BY t.id_topic  DESC
LIMIT 5"
) or die(mysql_error());

while ($row = mysql_fetch_assoc($result))
{      //The variable which decides the MAX letters it can have.
$bodylen = 20;
// Limit the length of the message, if the option is set.
if (strlen(str_replace('<br />', "\n", $row['body'])) > $bodylen)
$row['body'] = htmlspecialchars(substr(str_replace('<br />', "\n", un_htmlspecialchars($row['body'])), 0, $bodylen-3) . '...', ENT_QUOTES);


censorText($row['body']);
censorText($row['subject']);

$context['rp'][] = array(
'title' => $row['subject'],
'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
'description' => $row['body'],
'category' =>  $scripturl .'?board=' .$row['id_board'] . '.0',
'bname' => $row['bname']
);
}
foreach($context['rp'] as $info)
echo'
<dt><br><a href="'
, $info['link'], '">', $info['title'], '</a></dt>
<dd>'
, $info['description'], '<br>posted in <a href ="',$info['category'],'">',$info['bname'],'</a></dd>
</dlv>'
;
}



?>


again thanks... im really getting things now step by step... (btw whats nos?)

ioszilla

Quoterequire('---MYFORUMPATH/SSI.php');
require('---MYFORUMPATH/Settings.php');

Change it to:

Quote
require('---MYFORUMPATH/Settings.php');
require('---MYFORUMPATH/SSI.php');

Advertisement: