News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Help With some posting using Createpost

Started by evilopinions, June 07, 2014, 06:23:29 AM

Previous topic - Next topic

evilopinions

Hi Guys,

Just got this script that posts directly using createpost
However I cannot get to increment the id_board.
I need the id_board to increment each time it is posted.
I think it is just a small issue but am not able to figure it out.

        require_once (dirname(__FILE__) . '/SSI.php');
$x = $board;
for($board = 111; $board <= 113; $board++ ){
        post();
}

function post() {
                global $sourcedir;
$x++;
echo $x ;

                require_once($sourcedir . '/Subs-Post.php');

                $msgoptions = array(
                        'id' => 0,
                        'body' => 'Welcome',
                        'subject' => 'Welcome To The Boards',
                );
              $topicoptions = array(
                        'id' => 0,
                        'board' => $x,
                        'poll' => null,
                        'lock_mode' => 0,
                        'sticky_mode' => 0,
                        'mark_as_read' => false,
                );
               
                $posteroptions = array(
                        'update_post_count' => 1,
                );
               
                createPost($msgoptions, $topicoptions, $posteroptions);
        }

Shambles

I'm no php programmer, but to me it looks like the $x referenced within the post function needs to fix up to the $x outside that function.

I think.

        require_once (dirname(__FILE__) . '/SSI.php');
$x = $board;
for($board = 111; $board <= 113; $board++ ){
        post();
}

function post() {
                global $sourcedir, $x;     <============
$x++;
echo $x ;

                require_once($sourcedir . '/Subs-Post.php');

                $msgoptions = array(
                        'id' => 0,
                        'body' => 'Welcome',
                        'subject' => 'Welcome To The Boards',
                );
              $topicoptions = array(
                        'id' => 0,
                        'board' => $x,
                        'poll' => null,
                        'lock_mode' => 0,
                        'sticky_mode' => 0,
                        'mark_as_read' => false,
                );
               
                $posteroptions = array(
                        'update_post_count' => 1,
                );
               
                createPost($msgoptions, $topicoptions, $posteroptions);
        }

Arantor

What purpose does $x serve? Better question: do we need $x when we already have another variable?

Let's try a different tactic that makes more sense, and fix the indentation at the same time:

require_once (dirname(__FILE__) . '/SSI.php');
require_once($sourcedir . '/Subs-Post.php');

for ($boardnum = 111; $boardnum <= 113; $boardnum++ )
{
post($boardnum);
}

function post($boardnum)
{
$msgoptions = array(
'id' => 0,
'body' => 'Welcome',
'subject' => 'Welcome To The Boards',
);
$topicoptions = array(
'id' => 0,
'board' => $boardnum,
'poll' => null,
'lock_mode' => 0,
'sticky_mode' => 0,
'mark_as_read' => false,
);

$posteroptions = array(
'update_post_count' => 1,
);

createPost($msgoptions, $topicoptions, $posteroptions);
}


As you can see, instead of having it juggling back and forth, we follow a pattern called dependency injection - the post() routine needs to know what board to post in, and that's from outside, so when we call post() we can also tell it where to post at that point in time.

Advertisement: