This is a very simple query and I must be doing something wrong, but am not seeing it right now. I'm using codebird php library to connect and search Twitter. I am able to find all friends/followers, create and destroy friendships, but for some reason am not paginating through a search of users correctly.
$cb is already connected and logged into twitter.
The problem that I'm having is that $u has the same list of users for each page. In the end my $users array will have multiple copies of the first page for each loop and I cannot get more than 20 users from this search. What is wrong with the query?
$count = 20; //max for users_search
$type = 'q';
$total = 0;
for ($i = 1; $i <= 2; $i++) {
echo 'Page ' . $i . "\r\n";
$u = $cb->users_search(array($type => $search, 'page' => $i, 'count' => $count));
echo var_dump($u);
$users[] = (array)$u;
}
Try this:
$count = 20; //max for users_search
$nbPage = 2; //2 pages for eg.
$type = 'q';
$search = '...';
for ($i = 1; $i <= $nbPage; $i++) {
$params = array($type => $search, 'page' => $i, 'count' => $count);
$data = (array) $cb->users_search($params);
for ($j = 0; $j < $count; $j++) {
$status = $data[$j];
echo $status->name . " (#" . $status->screen_name . ")", '<br>';
}
}
Related
I am currently trying to create a team generator for a game that organizes teams based on player ratings. I am having a little issue when it comes to adding players in a nested array. I will eventually be adding database calls to the arrays. I can't figure out why I can't echo the players after I try and add them to the teams array. The randoms are for testing purposes.
$players = array();
$captains = array();
for ($i = 1; $i <= 40; $i++){
$players[] = array('name' => 'Player ' . $i, 'MMR' => rand(2800,4200));
}
for ($i = 1; $i <= 10; $i++){
$captains[] = array('name' => 'Captain ' . $i, 'MMR' => rand(3200,4200));
}
//sort the players by MMR
usort($players, function($a, $b) {
return $a['MMR'] - $b['MMR'];
});
//sort the captains by MMR
usort($captains, function($a, $b) {
return $a['MMR'] - $b['MMR'];
});
//put captains on teams
$teams = array();
for($i = 0;$i < count($captains); $i++){
$teams[] = array('name' => 'Team ' . ($i + 1), 'captain' => $captains[$i], 'players' => array(), 'totalMMR' => $captains[$i]['MMR']);
}
Here is where I think the problem may be:
function addPlayer($team,$newPlayer){
$teams[$team]['players'][] = $players[$newPlayer];
$teams[$team]['totalMMR'] += $players[$newPlayer]['MMR'];
}
addPlayer(0,0);
$output = '';
foreach($teams as $team){
$output .= '<div class="teams">' . $team['name'] . '<br />' . $team['captain']['name'] . ': ' . $team['captain']['MMR'] . '<br />';
for ($i = 0; $i < count($team['players']); $i++){
$output .= $team['players'][$i]['name'] . ': ' . $team['players'][$i]['MMR'] . '<br />';
}
$output .= '</div>';
}
echo $output;
Now the captains are echoing out, but the player that I added is not. Any help would be appreciated.
function addPlayer($team,$newPlayer){
$teams[$team]['players'][] = $players[$newPlayer];
$teams[$team]['totalMMR'] += $players[$newPlayer]['MMR'];
}
There's no variable named $teams in this function. If you mean to modify a global variable named $teams, then you can say global $teams; as the first line in your function.
Likewise for $players (although you should have gotten a notice about undefined indexes).
well I have a working script (see below) but it seems quite clunky and redundant; in my defense I wrote this code many moons ago, but that's not the point. I was curious if anyone has an idea on a more efficient way of writing this code, with less loops and conditionals and, well, noise in the code.
Code in question:
private function pageLinks($num, $page = 1, $search = false, $ne = false) {
$query = ($search) ? '&query='.$search : null;
$by = (is_numeric($ne)) ? '&by='.$ne : null;
$links = 'Page(s):1';
$count = 1;
$npp = $this->numPerPage;
$buttons = 9;
$half = 4;
for($i = 1; $i <= $num; $i++) {
if(($i%$npp) === 0) {
$count++;
}
}
if($count < $buttons) {
for($i = 2; $i <= $count; $i++) {
$links .= '' . $i . '';
}
} elseif($page <= ($half + 2)) {
for($i = 2; $i <= $buttons; $i++) {
$links .= '' . $i . '';
}
$links .= '...' . $count . '';
} elseif($page <= ($count - ($half + 2))) {
$links .= '...';
for($i = $half; $i > 0; $i--) {
$links .= '' . ($page - $i) . '';
}
$links .= '' . ($page - $i) . '';
for($i = 1; $i <= $half; $i++) {
$links .= '' . ($page + $i) . '';
}
$links .= '...' . $count . '';
} else {
$links .= '...';
for($i = $buttons - 1; $i >= 0; $i--) {
$links .= '' . ($count - $i) . '';
}
}
return($links);
}
The method is called like so:
$links = $this->pageLinks($count, $page, $url, $ne);
And the variables are as such:
$count = total number of clients in database (int)
$page = current page to build from (int)
$url = the name or email for the search (String)
$ne = is for the search string either by name (1) or email (2) (int)
And the output is something like (as links):
Page(s):1 2 3 4 5 6 7 8 9...33
Or if you're in the middle (page 20):
Page(s):1...16 17 18 19 20 21 22 23 24...33
Now this isn't always called through a search function, hence the default values for $url and $ne, but that's not very important. My question is there a cleaner way to handle building of these links? Or am I stuck with this cluster of loops?
With the help of the people at codereview.stackexchange.com I found exactly what I needed. You can find the answer here for a more in-depth approach, but here is the updated code if anyone comes across this and is curious:
private function pageLinks($num, $page = 1, $search = false, $ne = false) {
$query = ($search && is_numeric($ne)) ? "&query=" . $search . "&by=" . $ne : null;
$links = "Page(s):" . $this->page_link(1, $query);
$npp = $this->numPerPage;
$half = 4;
$count = floor($num / $npp) + 1;
$from = $page - $half;
if($from <= 2) {
$from = 2;
}
$to = $page + $half;
if($to >= $count - 1) {
$to = $count - 1;
}
if($from > 2) {
$links .= "...";
}
for($i = $from; $i <= $to; $i++) {
$links .= $this->page_link($i, $query);
}
if($i < $count) {
$links .= "...";
}
$links .= $this->page_link($count, $query);
return($links);
}
private function page_link($num, $query) {
return("" . $num . "");
}
I am doing a little function to generate random usernames, as the following:
public static function nicknames($data) {
if ($data['request'] == 'nickAvailable') {
foreach ($data as $value)
if (is_array($value))
$nick = $value['nickname'];
$random = rand(2, 2);
$nickname = $nick . '_' . $random;
$count = 3;
$nicknames = array();
for ($i = 1; $i <= $count; $i++) {
$select = self::$db->select('users', 'nickname', array('nickname' => $nickname));
if (count($select) == 0) {
$nicknames[] = $nickname;
} else {
$count = $count + 1;
}
}
$array = array("status" => 0,
"errors" => $nicknames,
"data" => array());
model::json($array);
}
}
The only problem I have is that $random is just executed one time and not on each loop. I need 3 different usernames to be in the array, and they must be different from each other.
How can I edit my code to achieve this?
Thanks for any suggestion
Place the random number generator and nickname building variable at the start of your loop. Also increase the range of numbers that rand is allowed to return, as it will always return 2 at the moment
$usernames = array();
do {
// inside generate random number,
// build nickname,
// query to see if its taken,
// and if NOT taken, add to the usernames array
} while(count($usernames) < 3);
As of now, you're not generating a random number.
$random = rand(2, 2);
This will always be 2.
Here's how I'd do it:
for ($i = 1; $i <= $count; $i++)
{
$random = rand(1, 3);
$nickname = $nick . '_' . $random;
$select = self::$db->select('users', 'nickname', array('nickname' => $nickname));
if (count($select) == 0) {
$nicknames[] = $nickname;
} else {
$count = $count + 1;
}
}
Hope this helps!
I found this Stack Overflow post explaining how you can generate random coupon codes.
I'm looking into using that code and generate multiple coupons at once (e.g. 50), while separate them by a comma.
The output would be: COUPON-HMECN, COUPON-UYSNC, etc.
Code below and codepad example available.
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$res = "COUPON-";
for ($i = 0; $i < 5; $i++) {
$res .= $chars[mt_rand(0, strlen($chars)-1)];
}
echo $res . ",";
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$numCodesToGenerate = 5;
for ($n = 0; $n < $numCodesToGenerate; $n++)
{
$res = "COUPON-";
for ($i = 0; $i < 5; $i++) {
$res .= $chars[mt_rand(0, strlen($chars)-1)];
}
echo $res . ",";
}
Why not use uniqid()?
$coupon_str = '';
$seperator = '';
for($i = 0; $i < 50; $i++) {
$coupon_str .= $seperator . uniqid('COUPON-');
$seperator = ',';
}
echo $coupon_str;
Output:
COUPON-502373ac95dd2,COUPON-502373ac95de8,COUPON-502373ac95ded,....
Here is a much neater version (and faster) that does what you need:
function MakeCouponCode() {
$res = "COUPON-";
for($i = 0; $i < 5; ++$i)
$res .= chr(mt_rand(0, 1) == 0 ? mt_rand(65, 90) : mt_rand(48, 57));
return $res;
}
$coupons = array();
for($i = 0; $i < 5; ++$i)
$coupons[] = MakeCouponCode();
echo implode(', ', $coupons);
Output:
COUPON-D707Y, COUPON-4B37E, COUPON-3O397, COUPON-M799X, COUPON-24Q36
You can use the coupon code generator PHP class file to generate N number of coupons and its customizable, with various options of adding own mask with own prefix and suffix. The coupon codes are separated by comma. Simple PHP coupon code generator
Example:
coupon::generate(8); // J5BST6NQ
In My last post I asked :
How to create dynamic incrementing variable using "for" loop in php? like wise: $track_1,$track_2,$track_3,$track_4..... so on....
whose answer I selected as
for($i = 0; $i < 10; $i++) {
$name = "track_$i";
$$name = 'hello';
}
and
for($i = 0; $i < 10; $i++) {
${'track_' . $i} = 'val'
}
Now, What If I need the Value of variable previous than the current variable?
for($i = 0; $i < 10; $i++) {
${'track_' . $i} = 'val'
if($i != 0){
$prev_val = ${'track_' . ($i - 1)}
}
}
But it's much better to use arrays for this, which are meant for this application.
$tracks = array();
for($i = 0; $i < 10; $i++){
$tracks[$i] = 'val';
if($i != 0){
$prev_val = $tracks[$i-1];
}
}
I guess the simples way would be to use two variables.
$name2 = "track_0";
for($i = 1; $i < 10; $i++) {
$name1 = $name2;
$name2 = "track_$i";
$$name1 = 'hello_previous';
$$name2 = 'hello_this';
}
Or if you explicitly use i = [0...10] to generate a variable name, you could simply write $name2 = "track_". $i; $name1 = "track_" . ($i - 1);
I know the others are saying just subtract by 1, but what if your list goes 1, 2, 4, 5, 8, 9? The previous of 8 is not 7, but 5, this following method (with a bit of modification to work as you require) will provide a way of getting the true previous value, and not a guessed one.
for($i = 0; $i < 10; $i++) {
${'track_' . $i} = 'val'
if(!empty($last_val))
// do what you want here
// set a var to store the last value
$last_val=$i;
}
if ($i != 0)
{
$prev = ${'track_' . ($i-1)} ;
}
?
${'track_' . ($i-1)}; won't suffice?