News:

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

Main Menu

Need help with two simple php items: grabbing userid

Started by DucMan, September 13, 2004, 09:31:06 PM

Previous topic - Next topic

DucMan

Hello,

I am a Visual Basic programmer that only knows very limited php.  I want to write a mod for a board that seems very simple but I am having a bit of trouble getting started.  I want to create a topic bookmark feature that will let users save topics of interest.

When in a topic I want to have a button that says bookmark.  Clicking this will insert a record in the database with their user id and the topic id.  Somewhere else they will be able to pull up all of their bookmarks to either click on them or to otherwise manage them. 

I need to grab the User ID, Topic ID, and Topic Name.  I know the user id is stored in the cookie and have tried unsuccessfully to grab it using the loadUserSettings.

If anyone could take the time to get me started with a function that will return the UserID (or tell me how to access it if it is a global variable) so I can store that along with the topic ID I'd appreciate it.  I know I can do the rest as far as using PHP to insert and retrieve the items from the database.

I'll need to grab the topic ID as well but I figured I take this one step at a time.

Thanks in advance.


[Unknown]

The current user's member ID is stored in the global variable $ID_MEMBER.  All you have to do is:

global $ID_MEMBER;

echo 'Hi, ', $ID_MEMBER, '!!';


The ID_TOPIC is also available in a global variable, but this time it's $topic.

The topic's name is not available, however, because it is generally retrieved with a query.  You might check SendTopic.php.

-[Unknown]

DucMan

Sorry to overcomplicate this but does this need to be wrapped in a function and called in a special way?  I created a php file called bookmark.php and inside of it I put the code sample you gave me

<?
"global $ID_MEMBER;

echo 'Hi, ', $ID_MEMBER, '!!';"

?>

I am logged into the board and when I try to access this file http://www.ourboardsurlnamehere/smf/Sources/bookmark.php 

I get this returned:
Hi, !!

Thanks again.
Tony



[Unknown]

Ah, yes... you do.  You probably want to add an action to SMF; this means editing index.php slightly.  There are instructions inside, but essentially you'll want to add a line like so:

'bookmark' => array('bookmark.php', 'Bookmark'),

Into the big array.  Then make bookmark.php look something like this:

<?php

function Bookmark()
{
  global
$ID_MEMBER, $topic, $db_prefix;

  echo
'Hi, ', $ID_MEMBER, '!';
}

?>


Now try going to:

http://www.yourdomain.tld/smf/index.php?action=bookmark

-[Unknown]

DucMan

I thought it might be something like that because I notice the frequent use of the action command.  I appreciate all your help and patience!

DucMan

#5
I am getting the following error (although my user_ID is being returned at the upper left hand corner of the screen):

An Error Has Occurred!
Unable to load the 'main' template. 

Does every new file we create, in this case, bookmark.php need to have a corresponding template in the themes directory?

[Unknown]

Well, it should.  Or you should specify what sub template to use.

In this case, the rawdata sub template might be useful.  So you just say:

$context['sub_template'] = 'rawdata';
$context['raw_data'] = 'Hi, ' . $ID_MEMBER . '!';

And you'll get rid of that error message.  Of course, using a specialized template or sub template is of course, ideal... but it seems you want this action to just take a topic and bookmark it.  It needn't show anything at all, right?  So no need for a template.... no need even for rawdata.  You just use redirectexit().

-[Unknown]

DucMan

[unknown],

You are correct that I won't need to show anything at all, I'll just do an insert into the bookmarks table with the user ID and the topic ID.  Right now I am just trying to get an idea of how the variables work and really how the board software works.  That is why I am currently trying to display the data.

When does the $topic global variable get set and is there a trick to calling it?  When I am in a topic and I call my http://myurlhere/smf/index.php?action=bookmark function I get this back:

UserID=, 1998!
Topic=,

My code looks like this:
<?php

function Bookmark()
{
   global $ID_MEMBER, $topic, $db_prefix, $context;

  $context['sub_template']='rawdata';
  $context['raw_data']='UserID=, '.$ID_MEMBER.'!<br> Topic=,'.$topic;
   
}

?>

Many thanks again.

[Unknown]

It gets set when you go to a topic; how is it to know what topic you want otherwise?

http://myurlhere/smf/index.php?action=bookmark;topic=1
(assuming there is a topic with the ID_TOPIC of 1...)

This will also populate the ever-useful $board global variable.

-[Unknown]

DucMan

Ok, I got you.  So I need to figure out where the buttons in a topic get built (like reply, send topic, etc), put my bookmark function in there and then the action of the bookmark button would be http://myurl/smf/index.php?action=bookmark;topic=1

I can't wait to get to the stuff I am familiar with like inserts and selects  ;D


Advertisement: