Paginate Nestoria Results using PHP - php

I'm using the Nestoria API to retrieve property results.
Everything is working quite well and one can return up to 50 properties using this method.
I would like to show 10 items at a time and allow for the user to paginate through them, but for some reason I'm having difficulty doing this.
The code snippet around the section that controls this is as follows:
$page = isset($_REQUEST["page"]) ? (int)$_REQUEST["page"] : 1;
$page = $page-1;
$pagination = new pagination;
$propertyResults = $pagination->generate($nestoria->decodedData->response->listings, 10);
foreach($propertyResults as $listing) {
//do stuff
}
A snippet of the data array would be:
Array
(
[0] => stdClass Object
(
[auction_date] =>
[property_type] => house
[summary] => Located in North Kingston a two double bedroom Victorian house presented in...
[title] => York Road, Kingston, KT2 - Reception
[updated_in_days] => 6.5
[updated_in_days_formatted] => this week
)
[1] => stdClass Object
(
[auction_date] =>
[property_type] => house
[summary] => Fine home was built about 50 years ago and enjoys one of the best locations...
[title] => Coombe Hill, KT2 - Conservatory
[updated_in_days] => 2.5
[updated_in_days_formatted] => this week
)
....
(sample cut down due to size of array elements)
Now I have been staring at this for way too long now and I've drawn a blank.
This code works correctly except if I try go to any other page other than 1, then the page doesn't finish loading, it just carries on until Firefox says: "The page isn't redirecting properly".
So basically, pagination is able to cut my data array up correctly, but is failing to "paginate" correctly.
Any help?

Turns out the problem with the redirect was actually an .htaccess issue which was using the $_GET["page"] variable and was therefore getting confused, so I renamed all references to the $_GET["page"] to $_GET["_page"] in this app.

Related

Codeingiter library multiple instances, not working

Iam developing a codeigniter POS system project.
I have separate sections to product sale and product GRN. both are using codeigniter cart library
in product sale section I created instance called : ocart
$this->load->library('cart','','ocart');
$ocart = $this->ocart->contents();
$this->ocart->destroy();
$this->ocart->insert($data);
product GRN section I created separate instance called : pcart
$this->load->library('cart','','pcart');
$pcart = $this->pcart->contents();
$this->pcart->destroy();
$this->pcart->insert($data);
When Im going to add product to the cart in sale section, same time same product added to the product GRN cart also. and wise versa.
then I checked array contents using print_r($pcart) and print_r($ocart);
both giving same output
Array
(
[6b913a2317d00f7bfa0abdaff1a1f67f] => Array
(
[rowid] => 6b913a2317d00f7bfa0abdaff1a1f67f
[id] => 22020
[pcode] => DS141
[note] => PLATE
[qty] => 20
[price] => 1
[name] => 97280
[subtotal] => 20
)
)
what is wrong with above code, please advice.
There is no built-in method to destroy loaded library object.
You can do simply like that by using unset or setting null.
unset($this->my_library);
OR
$this->my_library = null;

Opencart event data missing identifier

Using opencart 3 and trying to make use of the events but I'm unable to find the identifier.
E.g trying to extend the returns form added an event:
$this->model_setting_event->addEvent('mail_account_return_after', 'catalog/model/account/return/addReturn/after', 'extension/module/return/returnAdd');
Controller:
class ControllerExtensionModuleReturn extends Controller {
public function returnAdd(&$route, &$args, &$output) {
print_r($args);
exit;
$args is missing the main return_id identifier:
Array
(
[0] => Array
(
[firstname] => Foo
[lastname] => Bar
[email] => test#gmail.com
[telephone] => 01234556789
[order_id] => 29
[date_ordered] => 2017-06-29
[product] => Canon EOS 5D
[model] => Product 3
[quantity] => 1
[return_reason_id] => 4
[opened] => 0
[comment] => Test comment
)
)
Tried with $this->db->getLastId() but this returns 0. Tested with other events and appears to be missing the main identifiers.
Where does opencart set what data is passed to the before/after events?
If I'm not mistaken you should be able to access the auto increment return_id in $output, which holds the output of method addReturn(), i.e., $this->db->getLastId();.
As to why calling getLastId() a second time from within your event doesn't work, that's a good question. I would assume there may be some other query happening in between - though by default I don't think there should be. Is it possible there is another trigger running a query before your event gets triggered?
Look in system/engine/loader.php at for the strings before and after. There are methods that do view, controller, configuration and language.

Running thousands of MySQL queries, crashing server

I'm running a stock update on our company website, and the query is as follows;
$count = count($stock);
for($i = 0; $i < $count; $i++)
{
if(strtolower($stock[$i]['stockloc']) == 'retail')
{
if($stock[$i]['avail'] < 0)
{
$stock[$i]['avail'] = 0;
}
mysql_query("UPDATE `xcart_products`
SET `avail` = ".$stock[$i]['avail']."
WHERE `productid` IN
(SELECT `productid`
FROM `xcart_extra_field_values`
WHERE `productid` = ".$stock[$i]['productid']."
AND `fieldid` = 5
AND LOWER(value) = 'retail')");
}
}
Eessentially it runs in a for loop and iterates through thousands of products, updating stock levels. It runs fine for about 4 minutes, then things start to lag on our website, mysql seems to become backlogged with other website db queries that it can't run (they all appear locked), which then leads to the inevitable;
Internal Server Error The server encountered an internal error or
misconfiguration and was unable to complete your request.
Please contact the server administrator to inform of the time the
error occurred and of anything you might have done that may have
caused the error.
More information about this error may be available in the server error
log.
It doesn't seem to be a timeout I've upped the limit in PHP.ini to no avail. The query isn't particularly complex, however it does run several thousand times. Maybe as many as 5000+. I'm not sure what the problem is, as we ran a similar update system on our old website, and although it wasn't one I designed, it didn't have trouble uploading this many stock quantity changes.
I'll be happy to expand my question if further information is needed, but honestly, I'm at a bit of a loss even how to trace the problem.
The $stock array is an associative array which has the following format;
Array
(
[0] => Array
(
[avail] => 55
[productid] => 17761
[sku] => AER-TOUCH-2100
[stockloc] => retail
)
[1] => Array
(
[avail] => 166
[productid] => 22653
[sku] => CAS-AER-112
[stockloc] => retail
)
[2] => Array
(
[avail] => 272
[productid] => 22952
[sku] => CAS-ANT-001
[stockloc] => retail
)
[3] => Array
(
[avail] => 5
[productid] => 22956
[sku] => CAS-ANT-005
[stockloc] => retail
)
[4] => Array
(
[avail] => 12
[productid] => 22958
[sku] => CAS-ANT-007
[stockloc] => retail
I have tried running a delay every x iterations of the loop, but I'm still getting the 500 internal server error message. Clearly I need a better solution to updating my table.

vBulletin Getting new posts on a particular forum / replies on a thread

I need to get a new replies / forum posts out of a vbulletin forum .
What would be a best manner of fetching it.
Regards,
Azeem
Just figured this one out.
You have to use the API function search_getnew or search_getdaily (I'd recommend search_getdaily, since it is more likely to produce results).
So you make an API call (POST and signed) with api_m = "search_getdaily". If there are any results than you recieve an response array like that:
Array
(
[response] => Array
(
[errormessage] => search
)
[show] => Array
(
[canviewforums] => 1
[canviewcalendar] => 1
[foruminfo] =>
[threadinfo] =>
[searchid] => 62
[notices] =>
[notifications] =>
)
)
This means that the search was succssfull, and you can call 'search_showresults' to retrieve them actually.
To do this do an api_m = 'search_showresults' (POST and signed) with the 'searchid' you just recieved ('62' in the example).
You don't have to be logged in to do this API call, however then protected forums won't be listed.
Hope this was helpful.
Cheers
You can fetch new replies / forum posts from database or use the api:
https://www.vbulletin.com/forum/content.php/352-Method-List
https://www.vbulletin.com/forum/content.php/363-Thread-Post-Related-Methods
If this is possible with the api i would recommend using the api.

PHP Random Team Schedule Generator - Round Robin Scheduler

After getting negative feedback from asking this question in a new question... here is my revised question. Yes it is the same project I am working on, but I was unclear that I needed to basically have a Round Robin type of scheduler.
I'm working on a Round Robin Style Hockey League Scheduler, and need some help.
The overall goal is for the end admin user to be able to punch in 3 variables and have it perform a round Robin style schedule until the WEEKS counter has been hit. Below is an example of the amount of teams and the amount of weeks games are played.
$Teams = array('team1','team2','team3','team4','team5','team6','team7','team8');
$Weeks = 16;
The goal is to have it loop 16 times, making 4 games a week, having each team playing 1 time a week. The round robin algorithm should have teams playing different teams each week until all possibles combinations have been made, but not exceeding 16 weeks. In the event that we only have 4 teams or less teams than possible combinations, we would need to have the round robin start over again until the weeks number was hit.
EDIT:
I am about 90% into what I needed this script to do... but I am stuck on one thing. I need help with merging a multi-dimensional array.
First are the Tiers. Next are the Weeks (all are week 1). Then are the Games for the team match up.
Array
(
[1] => Array
(
[1] => Array
(
[1] => Array
(
[home] => Whalers
[visitor] => Lumberjacks
)
[2] => Array
(
[home] => Team America
[visitor] => Wolfpack
)
)
)
[2] => Array
(
[1] => Array
(
[1] => Array
(
[home] => Warriors
[visitor] => Litchfield Builders
)
[2] => Array
(
[home] => Icemen
[visitor] => Nighthawks
)
)
)
[3] => Array
(
[1] => Array
(
[1] => Array
(
[home] => The Freeze
[visitor] => Devils Rejects
)
[2] => Array
(
[home] => Cobras
[visitor] => New Haven Raiders
)
[3] => Array
(
[home] => Crusaders
[visitor] => Whalers
)
[4] => Array
(
[home] => Blizzard
[visitor] => CT Redlines
)
)
)
)
I want the end result to drop the tier and merge all same weeks games together to look like the following:
Array
(
[1] => Array
(
[1] => Array
(
[home] => Whalers
[visitor] => Lumberjacks
)
[2] => Array
(
[home] => Team America
[visitor] => Wolfpack
)
[3] => Array
(
[home] => Warriors
[visitor] => Litchfield Builders
)
[4] => Array
(
[home] => Icemen
[visitor] => Nighthawks
)
[5] => Array
(
[home] => The Freeze
[visitor] => Devils Rejects
)
[6] => Array
(
[home] => Cobras
[visitor] => New Haven Raiders
)
[6] => Array
(
[home] => Crusaders
[visitor] => Whalers
)
[8] => Array
(
[home] => Blizzard
[visitor] => CT Redlines
)
)
)
Maybe something like this?
<?php
$teams = array(
'Team 1',
'Team 2',
'Team 3',
'Team 4',
'Team 5',
'Team 6',
'Team 7',
'Team 8'
);
function getMatches($teams) {
shuffle($teams);
return call_user_func_array('array_combine', array_chunk($teams, sizeof($teams) / 2));
}
for ($i = 0; $i < 14; $i += 1) {
print_r(getMatches($teams));
}
I didn't really get how you define the schedule, so if you can explain this a bit, I'll try to help.
Pop one off, randomize, pop another. There's your game. If one is left over, some random team has to be a workhorse and play two games this week:
for ($week=1; $i<=$totalWeeksPlayed; $i++)
{
$games = 0;
$temp = $teams;
while (count($temp) > 1)
{
$team = array_shift($temp);
shuffle($temp);
$opponent = array_shift($temp);
$game[$week][$games] = $team . ' vs' . $opponent;
$games++;
}
if (count($temp) == 1)
{
$workhorses = $teams;
unset($workhorses[array_search($temp[0], $teams));
shuffle($workhorses);
$team = $temp[0];
$opponent = array_shift($workhorses);
$game[$week][$games] = $team . ' vs' . $opponent;
$games++;
}
}
Question below copied from above.
Correct me if I get this wrong, but if all teams have to play on the same regular basis, is it even possible to have all teams play the same amount of matches, if there is an odd number of teams? – Yoshi May 3 '11 at 15:05
Michelle,
The number of teams you are trying to pair-up (in this case 8 teams for 16 weeks) can be a daunting task, and is just the beginning of the "scheduling process". Once the correct, balanced team pairings have been determined, it's just the beginning of putting a schedule together for distribution. Next, a list of the 4 weekly time slots includes the; day of the week, start time and location name for each time slot for the whole 16 week season. Comment: What would be most helpful for you is to get an 8 team scheduling matrix that has balanced opponent, and home & away status. It makes a big difference in the quality of a schedule. It's important to evenly distribute early and late time slots, equal home & away status, and equal team distribution with opponents. Most of the balance is accomplished by using a balanced team pair matrix.
After 35 years of teaching, coaching and scheduling sports in the Boston area, I can offer the following information. Creating league or game schedules for sports organizations seems to be a never ending task shared by many and is repeated over and over as the ages of the participants grow and those running the leagues change, the learning curve associated with creating schedules is significant for those who take over.
With that said, I am amazed at how many highly educated mathematical wizards are involved with trying to come up with the perfect solution (algorithm) that will solve one's scheduling problem. Myself and a friend (who is a mathematical/programmer genius) over a period of 3 years created software that perfectly balances all the important components when creating schedules for 4 to 22 teams. What we learned is that there is no algorithm that can handle all the possible variables that are added to the normal variables to create balanced schedules. What I am saying is there are just as many "what ifs" as there are mathematical permutations and combinations that deal with just creating a team matrix listing opponents and home & visitor status of games.
For example: Let's create a perfectly balanced schedule for an 9 team division playing 4 games a week. After nine weeks all teams have played played 8 games, all have had 1 bye, all have played two times in each of the 4 time slots, and all have been scheduled as the home team 4 times and the visitor team 4 times.
What more could anybody want? Well now comes the fun. Because the 4 time slots you chose has 2 games each Saturday, and 2 games each Sunday, the first problem pops up (after the schedules are created, published and passed out, when 2 coaches from 2 different teams call and say they work Saturdays, can you move our games to Sundays? Sure, I can do that. I'll just make the changes, re-publish and re-distribute the schedules.
After the new schedules are distributed, several days later, a different coach calls up and says, "Hey, you moved some of my games to Saturday, I work Saturdays.., move them back". The phone rings again. This time a it's a coach from another team and says they are in a tournament the 5th week of the schedule and can't play that week. Finally the last call comes in from yet another coach. He says the parents of one of his players teaches CCD classes Sunday afternoons, and half of his team is in the CCD class and wants to move all our Sunday games to Saturday. Enough!
My point is no matter what you do or how perfect a schedule is, the best solution is to find out as many of the player/coach team limitations or restrictions before you assign the playing days and time slots to any schedule. These unpredictable variables makes a mess out of a perfect schedule. People do get angry and complain when undesirable schedules are distributed. When a schedule is bad enough, some parents won't sign their youngster to play the following year. This happens when you have a young family with two or three small children, and Dad's work limits his ability to be there. When there is an early morning game, I think you can see the difficulty for Mom's when it all falls on her shoulders.
For those that are new to scheduling, stay with it. It gets better over time after you gain
a little experience dealing with the problems. If you purchase a scheduling software program to calculate team pairs, be careful. Insist on seeing a full single round robin of the schedule they create. Check for the things described above (about balance and distribution).
Bob R
Given a table of teams, say
team (
teamname
);
And a table of fixtures
fixture (
date;
);
With the relationship decomposed by 'playing'
playing (
fixture_date.
teamname
);
Then it's would simply be a matter of iterating through each date, then team and selecting a team at random who does not already have a fixture for that date, and who have not played the selected team (or not played the selected team recently).
Although a simpler solution would be to have team[n] (where n is 0....number of teams -1) play team[(n+(number of teams)) % X] for varying values of X.

Categories