Jump to content

Mysql column to array with batches what am I doing wrong?


cobusbo
Go to solution Solved by cobusbo,

Recommended Posts

Hi I'm trying to recall a column from my table and insert it into an array. and then use the array and devide it into batches. Here is the code I got so far.

$query = "SELECT * FROM broadcast"; 
	 
$result1 = mysql_query($query) or die(mysql_error());

$users = array();
while($row = mysql_fetch_array($result1)){
	$users[] = $row['onorof'];
	}


$batchSize = 50;                          // set size of your batches

$batches = array_chunk($users, $batchSize);


require_once ('MxitAPI.php');
/* Instantiate the Mxit API */
$key = '50709335604c4feeaf9009b6e5f024a1';
$secret = '45a7b65b216d4f638a3cf4fc4f4e802f';
$app = 'guniverse';
$nick = urldecode($_SERVER['HTTP_X_MXIT_NICK']);
if(!isset($nick))
{
	$nick = "Debater";
}

$message = $_POST["message"];
$message1 = "*" . $nick . "*" . ": " . $message;

$api = new MxitAPI($key, $secret);
$api->get_app_token('message/send');

foreach ($batches as $batch) {
    $list = join(',', $batch);
    // process the batch list here
    $api->send_message($app, $batches, $message1, 'true');
}

Link to comment
Share on other sites

The first thing that crosses my mind when you say that you want to put a database column into an array is, "You are storing your data wrong".

That being said, $batch would never be an array, since it would be sent as a string from the database, so you cannot join it.

Link to comment
Share on other sites

The first thing that crosses my mind when you say that you want to put a database column into an array is, "You are storing your data wrong".

 

That being said, $batch would never be an array, since it would be sent as a string from the database, so you cannot join it.

Well here is my DB layout


-- phpMyAdmin SQL Dump
-- version 3.5.2.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Feb 28, 2015 at 09:13 PM
-- Server version: 10.0.11-MariaDB
-- PHP Version: 5.2.17

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `u342037492_chat`
--

-- --------------------------------------------------------

--
-- Table structure for table `broadcast`
--

CREATE TABLE IF NOT EXISTS `broadcast` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `mxitid` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `onorof` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ;

--
-- Dumping data for table `broadcast`
--

INSERT INTO `broadcast` (`ID`, `mxitid`, `onorof`) VALUES
(1, '27765238453', '27765238453');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

I basically want to make batches before making an array just like this code below but from my mysql database not text file

$visitor = 'http://guniverse.hol.es/unique.csv';

$f_pointer=fopen($visitor,"r"); // file pointer
$users = array();
while ($ar=fgetcsv($f_pointer)) {
    if ($ar[0] != '') $users[] = $ar[0]; //only store line in new array if it contains something
}
fclose ($f_pointer);

$batchSize = 50;                          // set size of your batches

$batches = array_chunk($users, $batchSize);


require_once ('MxitAPI.php');
/* Instantiate the Mxit API */
$key = '50709335604c4feeaf9009b6e5f024a1';
$secret = '45a7b65b216d4f638a3cf4fc4f4e802f';
$app = 'guniverse';
$nick = urldecode($_SERVER['HTTP_X_MXIT_NICK']);
if(!isset($nick))
{
	$nick = "Debater";
}

$message = $_POST["message"];
$message1 = "*" . $nick . "*" . ": " . $message;

$api = new MxitAPI($key, $secret);
$api->get_app_token('message/send');

foreach ($batches as $batch) {
    $list = join(',', $batch);
    // process the batch list here
    $api->send_message($app, $list, $message1, 'true');
}
 
Edited by cobusbo
Link to comment
Share on other sites

The first thing that crosses my mind when you say that you want to put a database column into an array is, "You are storing your data wrong".

 

That being said, $batch would never be an array, since it would be sent as a string from the database, so you cannot join it.

jcbones,

check out array_chunk

 

cobusbo,

Shouldn't you be sending $list in the send__message and not $batches

Link to comment
Share on other sites

jcbones,

check out array_chunk

 

cobusbo,

Shouldn't you be sending $list in the send__message and not $batches

Yes, I tried that but it didn't work, so I tried $batches and forgot to change it when I posted it here. The problem is still there.

 

I've been looking at an example script that was made to work with MongoDB, but its difficult to change it into php and mysql version. I'm going to post it below and maybe you guys can tell me what I missed in my attempt to create the Myqsl version.

<?php
/* Require the PHP wrapper for the Mxit API */
require_once ('MxitAPI.php');

/* Function to count the number of users in MongoDB */
function count_users() {
    $mongo = new Mongo('127.0.0.1');
    $collection = $mongo->sampleapp->users;
    $collection->ensureIndex(array('uid' => 1));

    return $collection->count();
}

/* Function to get batches of users from MongoDB */
function get_users($skip=0, $limit=50) {
    $mongo = new Mongo('127.0.0.1');
    $collection = $mongo->sampleapp->users;

    $collection->ensureIndex(array('mxitid'     => 1,
                                   'created_at' => 1));

    $users = $collection->find()->sort(array('created_at' => 1))->skip($skip)->limit($limit);

    return iterator_to_array($users);
}

/* Instantiate the Mxit API */
$api = new MxitAPI($key, $secret);

/* Set up the message */
$message = "(\*) Congratulations to our winners (\*)\n\n";
$message .= "1st place - Michael with 100 points\n";
$message .= "2nd place - Sipho with 50 points\n";
$message .= "3nd place - Carla with 25 points\n\n";
$message .= 'Good Luck!  $Click here$';

/* Mxit Markup is included in the message, so set ContainsMarkup to true */
$contains_markup = 'true';

/* Count the number of users in the database */
$count = count_users();

/* Initialise the variable that counts how many messages have been sent */
$sent = 0;

/* Keep looping through the user list, until the number of messages sent equals the number of users */
while ($sent < $count) {
    /* Get users in batches of 50 */
    $users = get_users($sent, 50);

    /* The list where the user MxitIDs will be stored */
    $list = array();

    foreach ($users as $user) {
        $list[] = $user['mxitid'];
        $sent++;
    }

    /* If there is a problem getting an access token, retry */
    $access_token = NULL;
    while (is_null($access_token)) {
        /* We are sending a message so request access to the message/send scope */
        $api->get_app_token('message/send');
        $token = $api->get_token();
        $access_token = $token['access_token'];

        // Only attempt to send a message if we have a valid auth token
        if (!is_null($access_token)) {
            $users = implode(',', $list);
            echo "\n$sent: $users\n";

            $api->send_message($app, $users, $message, $contains_markup);
        }
    }
}

echo "\n\nBroadcast to $sent users\n\n";
Link to comment
Share on other sites

  • Solution

 

Yes, I tried that but it didn't work, so I tried $batches and forgot to change it when I posted it here. The problem is still there.

 

I've been looking at an example script that was made to work with MongoDB, but its difficult to change it into php and mysql version. I'm going to post it below and maybe you guys can tell me what I missed in my attempt to create the Myqsl version.

<?php
/* Require the PHP wrapper for the Mxit API */
require_once ('MxitAPI.php');

/* Function to count the number of users in MongoDB */
function count_users() {
    $mongo = new Mongo('127.0.0.1');
    $collection = $mongo->sampleapp->users;
    $collection->ensureIndex(array('uid' => 1));

    return $collection->count();
}

/* Function to get batches of users from MongoDB */
function get_users($skip=0, $limit=50) {
    $mongo = new Mongo('127.0.0.1');
    $collection = $mongo->sampleapp->users;

    $collection->ensureIndex(array('mxitid'     => 1,
                                   'created_at' => 1));

    $users = $collection->find()->sort(array('created_at' => 1))->skip($skip)->limit($limit);

    return iterator_to_array($users);
}

/* Instantiate the Mxit API */
$api = new MxitAPI($key, $secret);

/* Set up the message */
$message = "(\*) Congratulations to our winners (\*)\n\n";
$message .= "1st place - Michael with 100 points\n";
$message .= "2nd place - Sipho with 50 points\n";
$message .= "3nd place - Carla with 25 points\n\n";
$message .= 'Good Luck!  $Click here$';

/* Mxit Markup is included in the message, so set ContainsMarkup to true */
$contains_markup = 'true';

/* Count the number of users in the database */
$count = count_users();

/* Initialise the variable that counts how many messages have been sent */
$sent = 0;

/* Keep looping through the user list, until the number of messages sent equals the number of users */
while ($sent < $count) {
    /* Get users in batches of 50 */
    $users = get_users($sent, 50);

    /* The list where the user MxitIDs will be stored */
    $list = array();

    foreach ($users as $user) {
        $list[] = $user['mxitid'];
        $sent++;
    }

    /* If there is a problem getting an access token, retry */
    $access_token = NULL;
    while (is_null($access_token)) {
        /* We are sending a message so request access to the message/send scope */
        $api->get_app_token('message/send');
        $token = $api->get_token();
        $access_token = $token['access_token'];

        // Only attempt to send a message if we have a valid auth token
        if (!is_null($access_token)) {
            $users = implode(',', $list);
            echo "\n$sent: $users\n";

            $api->send_message($app, $users, $message, $contains_markup);
        }
    }
}

echo "\n\nBroadcast to $sent users\n\n";

Well I found the solution

foreach ($batches as $batch) {
    $list = join(',', $batch);
    // process the batch list here
    $api->send_message($app, $list, $message1, 'true');
}

Should be implode

foreach ($batches as $batch) {
    $list = implode(',', $batch);
    // process the batch list here
    $api->send_message($app, $list, $message1, 'true');
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.