Tip: script to automate posting in 1.1

Started by pctwo, September 15, 2006, 01:00:47 PM

Previous topic - Next topic

dakotaroar

#20
I've just started playing with this, but it seems like the member id is coming from the SMF cookie (if it gets included with SSI.php).  I tested this script while logged out, and it posted with the 'some.name
in the array; after I logged in I tested it again and it posted as 'root' (my name for my test install).

Curious, but a very very useful thing!

EDIT:  I was mistaken, it seems that the 'root' actually was user ID 5.  This makes sense now.
The only forum for students in North and South Dakota!  Campus Dakota.

Wookie

Would anyone be able to help me with running a query to insert the results into the msgBody?

I've tried echoing and includes and am getting nowhere fast, I want to post the results of a query as a new topic, and also insert the Date into the msgSubject.

Many thanks in advance!

Wookie

Wookie


dakotaroar

This example might get you started:

$query = mysql_query("SELECT * FROM smf.smf_messages WHERE ID_TOPIC = '$SMFTopic' ORDER BY ID_MSG") or die("Error: ".mysql_error());
while($smffetch=mysql_fetch_array($query))
{ //Loop through each message.
          $smfmessageID=$smffetch["ID_MSG"];
          $smftopicID=$smffetch["ID_TOPIC"];
$smftime=$smffetch['posterTime'];
$time = date("l, jS F Y ¥a¥¥t g:i a", $smftime);
          $smfsubject=$smffetch["subject"];
          $smfbody=parse_bbc($smffetch["body"]);
          $smfauthorID=$smffetch["ID_MEMBER"];
$smfpostername = $smffetch["posterName"];
}


Then take the variable you need and put it in msgSubject.  The query above is looking for individual messages, so it looks up the messages table (your database name will vary) and assigns variables values according to the column names in the messages table (ID_MSG, ID_TOPIC, etc.).  You can use this for doing any query you want, and coupled with the posting script above, will make a new topic in the board you designate.
The only forum for students in North and South Dakota!  Campus Dakota.

Wookie

Thanks mburtonk, will have a play with this code later tonight...looks good and gives me another avenue to expore. Will post back with results.

Wookie

PS> I wonder if anyone ever fixed the last part of this script? The part that is the topic starter area is all black and and has "?>" in it.

Wookie

dakotaroar

I'm posting this script here so people can understand what is going on--Some lines will need to be changed to get it to work on your system, but hopefully the comments are decent.  I threw it together quickly, so things might not be in exactly the right place for easiest reading.

<?php//These three files are needed for this script to work--adjust the path to suit your installation, a URL will not work.require_once('/Users/Matt/Sites/SMF1.1.4/SSI.php');require_once('/Users/Matt/Sites/SMF1.1.4/Sources/Post.php');require_once('/Users/Matt/Sites/SMF1.1.4/Sources/Subs-Post.php');//Example query--getting info about a member from the database.  You can replace this with your own query and variables.$name = 'Wookie';  //The value we are looking for in the query.$query = mysql_query("SELECT * FROM smf114.smf_members WHERE memberName = '$name'") or die("Error: ".mysql_error()); //Replace 'smf114' with your database name and 'smf_' with your table prefix.while($smffetch=mysql_fetch_array($query)) { //Loop through each record, even though there is only one. $memberid = $smffetch["ID_MEMBER"];  //Put the value in the ID_MEMBER column into a variable. $registerdate= $smffetch["dateRegistered"];  //Put the value in the dateRegistered column into a variable. }$query_results = $memberid . '<br>' . $registerdate; //This just builds your message body with the variables you created and a line break to separate them.$date = $context['current_time']; //Getting the current date and time.$board = '6';  //The board to which you want to post.$msgSubject = $date;  //The subject of the topic.  This is where the date will go. $msgBody = $query_results;  //Puts the query results into the message body.//Not much of this is changed from the original script. $msgOptions = array( 'id' => 0, // set id to modify 'subject' => $msgSubject, 'body' => $msgBody, 'icon' => 'xx', // look at smf_message_icons table to see all possible values 'smileys_enabled' => 1, 'attachments' => empty($attachIDs) ? array() : $attachIDs, ); $topicOptions = array( 'id' => empty($topic) ? 0 : $topic, 'board' => $board, 'poll' => null, 'lock_mode' => 0, 'sticky_mode' => null, 'mark_as_read' => true, ); $posterOptions = array( 'id' => $ID_MEMBER,  //User ID of the member whose name you want on the posts. 'name' => 'Mr. Roboto',  //The name you want associated if the user ID does not exist. 'email' => '[email protected]',  //Email associated with the above name. 'update_post_count' => 1 //This adds to the user's post count. ); $newTopic = empty($topic) || $topic == 0;  //Creates a new topic. createPost($msgOptions, $topicOptions, $posterOptions);  //Actually creates the post and topic.//The following is from the original script.  Makes sure the board stays up and knows what just happened to it. if (isset($topicOptions['id'])) $topic = $topicOptions['id']; if ($newTopic) notifyMembersBoard(); else sendNotifications($topic, 'reply'); ?>
The only forum for students in North and South Dakota!  Campus Dakota.

Wookie

I'm getting the following error on two different domains: a test board and a live one

Error: SELECT command denied to user 'user'@'localhost' for table 'smf_members'

Full privelidges exist for user via control panel at host on both domains, and script is being run from forum root. Minimal changes made to the above script, eg paths are changed, boardid etc.

Anyone anyideas why I would get this error as its normally associated with incorrect user permission to the database, and these have been checked numerous times at two different domains I've tried to run the script on.

Wookie

dakotaroar

I have gotten that error before when I have had the name of the database wrong, e.g.,

SELECT * FROM smf114.smf_members WHERE memberName = '$name'

The bolded bit is the name of your SMF database.
The only forum for students in North and South Dakota!  Campus Dakota.

Wookie

I've now made a fresh install of smf and I'm now getting a new error.

Warning: Invalid argument supplied for foreach() in /home/~/public_html/f/Sources/Subs-Post.php on line 2005

Which refers to this in subs-post php...

// Ignore any parents on the top child level.
foreach ($parents as $id => $parent)     // <-- Line 2005
{
if ($parent['level'] == 0)
unset($parent[$id]);
else


Tis strange to get so many errors on 3 installs now on two domains.

Wookie

dakotaroar

I *cough* ignored that warning, as the script seemed to work for me and posted the topic.  I should have warned you (ha. ha.) about that.

I'm wondering whether this might be the offending clause:

{
foreach ($setboards as $ID_BOARD)
$lastMsg[$ID_BOARD] = $ID_MSG;
}


I don't know how to fix it though.
The only forum for students in North and South Dakota!  Campus Dakota.

Wookie

Yup, indeed it does work. Just took a minute to go and see the post instead of just sitting here looking at an error page.

Would be good to fix the error, so if anyone else can shed any light on this subject I would be very gratful.

Thank you mburtonk, for sticking with me on this and offering the help above and beyond (and via email too!)

Wookie

Wookie

So this code now produces one result from a database table, how would one extend this script to include 40 or so results?

Would I need to use foreach to achieve that? Can anyone start me off?

Manyy thanks

Wookie

Wookie

Can anyone point me in the right direction?

Many thanks.

Wookie

dakotaroar

I'm not sure what you are asking.  Do you mean

-40 results from the same query in the same post,
-40 results from different queries in the same post, or
-40 posts with the same query?

This script will do the first, if the query returns multiple rows.
For the second, you have to code in each query manually.
For the third, you need to set up a for loop and pass the right variable each time (unless you want 40 of the same post).
The only forum for students in North and South Dakota!  Campus Dakota.

Wookie

The first option "-40 results from the same query in the same post"

I just have no idea how to implement itm although I have a feeling its to do with $rows or $rowsets ...and I also thought using foreach...but more than likely wrong in that.

Many thanks!

Wookie

dakotaroar

It's already in there:
while($smffetch=mysql_fetch_array($query)) { //Loop through each record, even though there is only one. $memberid = $smffetch["ID_MEMBER"];  //Put the value in the ID_MEMBER column into a variable. $registerdate= $smffetch["dateRegistered"];  //Put the value in the dateRegistered column into a variable. }

The example query searches by membername, so you only get one result.  If you run a query that gets multiple results (say, messages that match a topic ID), you would get it looping through each.  I think the code below will work (if you change your query and your variables).  It should take $query_results and keep tacking more on to the end of the variable (with a ':  ' between the two in the example and a '<br>' between different sets).

while($smffetch=mysql_fetch_array($query)) { //Loop through each record, even though there is only one. $memberid = $smffetch["ID_MEMBER"];  //Put the value in the ID_MEMBER column into a variable. $registerdate= $smffetch["dateRegistered"];  //Put the value in the dateRegistered column into a variable.                $query_results=$query_results . '<br>' . $memberid . ':  ' . $registerdate }$date = $context['current_time']; //Getting the current date and time.$board = '6';  //The board to which you want to post.$msgSubject = $date;  //The subject of the topic.  This is where the date will go.$msgBody = $query_results;  //Puts the query results into the message body.
The only forum for students in North and South Dakota!  Campus Dakota.

Wookie

Still struggling to get this working, here is the complete script complete with dakotaroar's addition..(many thanks for trying to help me!)


<?php//These three files are needed for this script to work--adjust the path to suit your installation, a URL will not work.require_once('/home/stev1965/public_html/foru/SSI.php');require_once('/home/stev1965/public_html/foru/Sources/Post.php');require_once('/home/stev1965/public_html/foru/Sources/Subs-Post.php');//Example query--getting info about a member from the database.  You can replace this with your own query and variables.//$name = '0';  //The value we are looking for in the query.$query = mysql_query("SELECT cpmsubtitle FROM stev1965_smf14.cpg14x_potdp WHERE position = 0") or die("Error: ".mysql_error());while($smffetch=mysql_fetch_array($query)) { //Loop through each record, even though there is only one. $memberid = $smffetch["cpmsubtitle"];  //Put the value in the ID_MEMBER column into a variable. $registerdate= $smffetch["title"];  //Put the value in the dateRegistered column into a variable.                $query_results=$query_results . '<br>' . $memberid . ':  ' . $registerdate; }$query_results = $memberid . '<br>' . $registerdate; //This just builds your message body with the variables you created and a line break to separate them.$date = date("F j, Y"); //Getting the current date and time.$board = '5';  //The board to which you want to post.$msgSubject = $date;  //The subject of the topic.  This is where the date will go. $msgBody =  $query_results;  //Puts the query results into the message body.//Not much of this is changed from the original script. $msgOptions = array( 'id' => 0, // set id to modify 'subject' => $msgSubject, 'body' => $msgBody, 'icon' => 'xx', // look at smf_message_icons table to see all possible values 'smileys_enabled' => 1, 'attachments' => empty($attachIDs) ? array() : $attachIDs, ); $topicOptions = array( 'id' => empty($topic) ? 0 : $topic, 'board' => $board, 'poll' => null, 'lock_mode' => 0, 'sticky_mode' => null, 'mark_as_read' => true, ); $posterOptions = array( 'id' => $ID_MEMBER,  //User ID of the member whose name you want on the posts. 'name' => 'Mr. Roboto',  //The name you want associated if the user ID does not exist. 'email' => '[email protected]',  //Email associated with the above name. 'update_post_count' => 1 //This adds to the user's post count. ); $newTopic = empty($topic) || $topic == 0;  //Creates a new topic. createPost($msgOptions, $topicOptions, $posterOptions);  //Actually creates the post and topic.//The following is from the original script.  Makes sure the board stays up and knows what just happened to it. if (isset($topicOptions['id'])) $topic = $topicOptions['id']; if ($newTopic) notifyMembersBoard(); else sendNotifications($topic, 'reply'); ?>


Its still just giving me one result, in the table there are around 40 results and nearly all are positon = 0, these are what I would like to show in the post. Can anyone help?

Many thanks!

Wookie

rockinaway

<?php//These three files are needed for this script to work--adjust the path to suit your installation, a URL will not work.require_once('/home/stev1965/public_html/foru/SSI.php');require_once('/home/stev1965/public_html/foru/Sources/Post.php');require_once('/home/stev1965/public_html/foru/Sources/Subs-Post.php');//Example query--getting info about a member from the database.  You can replace this with your own query and variables.//$name = '0';  //The value we are looking for in the query.$query = mysql_query("SELECT cpmsubtitle FROM stev1965_smf14.cpg14x_potdp WHERE position = 0") or die("Error: ".mysql_error());$query_results = '';while($smffetch=mysql_fetch_assoc($query)){//Loop through each record, even though there is only one.$memberid = $smffetch["cpmsubtitle"];  //Put the value in the ID_MEMBER column into a variable.$registerdate= $smffetch["title"];  //Put the value in the dateRegistered column into a variable.                $query_results .= '<br>' . $memberid . ':  ' . $registerdate; }$date = date("F j, Y"); //Getting the current date and time.$board = '5';  //The board to which you want to post.$msgSubject = $date;  //The subject of the topic.  This is where the date will go.$msgBody =  $query_results;  //Puts the query results into the message body.//Not much of this is changed from the original script.$msgOptions = array('id' => 0, // set id to modify'subject' => $msgSubject,'body' => $msgBody,'icon' => 'xx', // look at smf_message_icons table to see all possible values'smileys_enabled' => 1, 'attachments' => empty($attachIDs) ? array() : $attachIDs,);$topicOptions = array('id' => empty($topic) ? 0 : $topic,'board' => $board,'poll' => null,'lock_mode' => 0,'sticky_mode' => null,'mark_as_read' => true,);$posterOptions = array('id' => $ID_MEMBER,  //User ID of the member whose name you want on the posts.'name' => 'Mr. Roboto',  //The name you want associated if the user ID does not exist.'email' => '[email protected]',  //Email associated with the above name.'update_post_count' => 1 //This adds to the user's post count.);$newTopic = empty($topic) || $topic == 0;  //Creates a new topic.createPost($msgOptions, $topicOptions, $posterOptions);  //Actually creates the post and topic.//The following is from the original script.  Makes sure the board stays up and knows what just happened to it.if (isset($topicOptions['id']))$topic = $topicOptions['id'];if ($newTopic)notifyMembersBoard();elsesendNotifications($topic, 'reply');?>


Replace the old code with this, see if it helps :)
Have I helped you? Then please join AdminFuel - The forum for forum administrators.

If you need help managing your forum, or maybe launching it, then visit us and view the quality articles, downloads and posts.

gimmoroy

What should be done to script to use it with SMF 2.0 RC1.2 ?
Is it possible at all?

liamgibbins

I hope this is still active....

If ran by itself it posts the sample to the forum... great but I want to post:

$msgBody =  "Staging System: " .$_POST['system'] . "<br />"."<br />"."<br />"." FC: ".$_POST['fcname']. "<br />"."<br />" ."Op Type: ". $_POST['selecttype']."<br /> " . "Date: ".$_POST['date']. "<br />"."<br />" ." SRP: ".$_POST['srp']. "<br />"."<br />" ."  Cycle: ".$_POST['cycle']. "<br />"."<br />" ." Planet:: ".$_POST['planet']. "<br />"."<br />" ." Moon: ".$_POST['moon']. "<br />"."<br />" ." Description: ".$_POST['description'];

These values are taken straight from a form but when ran it gives me a white page.
all i altered as I require this to be passed to the board..

any ideas how to fix this?

Advertisement: