Increment based on another incrementing variable - php

I have this odd situation and I can't think of a solution for it.
I have a variable $cat_count = 1; and I use it inside a loop and then do $cat_count++ somewhere below where I've used it.
Then I have another alphabetical counter which works the following way:
I have $alpha_string = 'abcdefghijklmnopqrstuvwxyz'; and $alpha_counter = 0;. I use this the following way - $alpha = $alpha_string{$alpha_counter}. I want my alphabetical counter to start counting from a, whenever $cat_count gets incremented by one.
So for example we would have this:
$cat_count = 1
$alpha = a
$alpha = b
$cat_count = 2
$alpha = a
$alpha = b
What I get momentarily is this:
$cat_count = 1
$alpha = a
$alpha = b
$cat_count = 2
$alpha = c
$alpha = d
Ideas?
Thank you.

following my answer in comments..
$counter = 0;
$cat_count = 1;
$alpha_count = 'abcdefghijklmnopqrstuvwxyz';
$rule_id = null;
$public_cats = array();
while ($row = $db->sql_fetchrow($result))
{
if ($rule_id != $row['rule_id'])
{
$group_ids = array_map('intval', explode(' ', $row['groups']));
$is_grouped = false;
// Check if user can see a specific category if he is not an admin or moderator
if (!$auth->acl_get('a_') && !$auth->acl_get('m_'))
{
$is_grouped = (group_memberships($group_ids, $user->data['user_id'], true)) ? true : false;
}
else
{
$is_grouped = true;
}
// Fill $public_cats with boolean values
if ($is_grouped !== false)
{
$public_cats[] = $is_grouped;
}
$rule_id = $row['rule_id'];
$template->assign_block_vars('rules', array(
'RULE_CATEGORY' => $row['rule_title'],
'ROW_COUNT' => $cat_count,
'CAN_SEE_CAT' => $is_grouped
));
$cat_count++;
$counter = 0;
}
$uid = $bitfield = $options = '';
generate_text_for_storage($row['rule_desc'], $uid, $bitfield, $options, $row['bbcode'], $row['links'], $row['smilies']);
$template->assign_block_vars('rules.rule', array(
'RULE_DESC' => generate_text_for_display($row['rule_desc'], $uid, $bitfield, $options),
'ALPHA_COUNT' => $alpha_count{$counter}
));
$counter++;
}

Related

how to explode comma seperated array in for loop?

I have two comma separated arrays one for service ids and other for service quantities like this 1,2,3 for service ids and these are quantities 5,10,20 for service ids. Service id 1 have 5 quantity and id 2 have 10 and id 3 have 20 respectively. In this code i am getting services of ids but i have no idea how to set quantity against against those ids in loop and save. Hope you get my question.
I want to change $result['service_quantity'] when loop works first time 5 comes then 10 then 20. so that i can save that.
$checkRecord = Booking::where('id', $saveBooking->id)->get();
$allServicesArr = explode(",", $checkRecord[0]->services_ids);
$service = array();
for ($i = 0; $i < count($allServicesArr); $i++) {
$result = Service::find($allServicesArr[$i]);
if ($result['id'] != '') {
$service[$i]['id'] = $result['id'];
$service[$i]['category_id'] = $result['category_id'];
$service[$i]['event_id'] = $result['event_id'];
$service[$i]['service_name'] = $result['service_name'];
$service[$i]['service_kind'] = $result['service_kind'];
$service[$i]['service_price'] = $result['service_price'];
$service[$i]['service_quantity'] = $result['service_quantity'];
$service[$i]['service_detail'] = $result['service_detail'];
}
$insertDate = new BookedServices();
$insertDate->category_id = $result['category_id'];
$insertDate->event_id = $result['event_id'];
$insertDate->booking_id = $saveBooking->id;
$insertDate->user_id = $userID;
$insertDate->service_id = $result['id'];
$insertDate->bs_name = $result['service_name'];
$insertDate->bs_kind = $result['service_kind'];
$insertDate->bs_price = $result['service_price'];
$insertDate->bs_number = $result['service_number'];
$insertDate->bs_detail = $result['service_detail'];
$insertDate->save();
}
You can simply use built-in array_combine function for this purpose
$id=explode(",", "1,2,3");
$qty=explode(",", "5,10,15");
print_r(array_combine($id, $qty));
Result
Array
(
[1] => 5
[2] => 10
[3] => 15
)
Try this
$id=explode(",","1,2,3");
$qty=explode(",","5,10,15");
foreach($id as $index => $val){
echo "id:".$val." Qty:".$qty[$index]; // id:1 Qty:5
}
$checkRecord = Booking::where('id', $saveBooking->id)->get();
$allServicesArr = explode(",", $checkRecord[0]->services_ids);
$allServicesQuantitiesArr = explode(",", $checkRecord[0]->services_quantity);
$service = array();
for ($i = 0; $i < count($allServicesArr); $i++) {
$result = Service::find($allServicesArr[$i]);
if ($result['id'] != '') {
$service[$i]['id'] = $result['id'];
$service[$i]['category_id'] = $result['category_id'];
$service[$i]['event_id'] = $result['event_id'];
$service[$i]['service_name'] = $result['service_name'];
$service[$i]['service_kind'] = $result['service_kind'];
$service[$i]['service_price'] = $result['service_price'];
$service[$i]['service_number'] = $allServicesQuantitiesArr[$i];
$service[$i]['service_detail'] = $result['service_detail'];
}
$insertDate = new BookedServices();
$insertDate->category_id = $result['category_id'];
$insertDate->event_id = $result['event_id'];
$insertDate->booking_id = $saveBooking->id;
$insertDate->user_id = $userID;
$insertDate->service_id = $result['id'];
$insertDate->bs_name = $result['service_name'];
$insertDate->bs_kind = $result['service_kind'];
$insertDate->bs_price = $result['service_price'];
$insertDate->bs_number = $allServicesQuantitiesArr[$i];
$insertDate->bs_detail = $result['service_detail'];
$insertDate->save();
}

Why my Cakephp web service query takes lot of time to return data?

I've got a web service which takes around 15 secs to return JSON response to front-end.My code looks like this:
Controller code:
public function getDetailReport($data){
$user_id = $data->user_id;
$test_id = $data->test_id;
$result_obj = new TestSetDetailResultTable();
$data_array = $result_obj->getDetailReportByUser($user_id, $test_id);
if($data_array['status'] == 1) {
echo $this->successMessage('successfully Executed',$data_array['record']);
} else {
echo $this->failMessage($data_array['error'],$data_array['message']);
}
exit;
}
Model code:
public function getDetailReportByUser($user_id,$test_id) {
$data_array = Array();
$subject_array = Array();
$answer_array = Array();
$topper_array = Array();
$percentile_array = Array();
$max_marks = 0;
$perc = 0;
$hrs = 0;
$mint = 0;
$sec = 0;
$query = new AppQuery();
$query->callProcedure('getSummaryResult',array($test_id,$user_id));
$row_list = $this->loadRowList($query);
if(count($row_list) > 0) {
$max_marks = $row_list[0]->maximum_marks;
$perc = $row_list[0]->percentage;
$query->callProcedure('getCompletedTestTime',array($user_id,$test_id));
$row_time = $this->loadRowList($query);
$query->callProcedure('getAllUserPerTest',array($test_id));
$row_user = $this->loadRowList($query);
if(count($row_time)> 0 && count($row_user) > 0) {
foreach ($row_list as $list) {
$item['test_name'] = $list->test_name;
$item['total_question'] = $list->total_question;
$item['right_answer'] = $list->right_answer;
$item['wrong_answer'] = $list->wrong_answer;
$item['question_attempted'] = $list->question_attempted;
$item['question_not_attempted'] = $list->question_not_attempted;
$item['positive_marks'] = $list->positive_marks;
$item['negative_marks'] = $list->negative_marks;
$item['obtaine'] = $list->obtaine;
$item['maximum_test_time'] = $list->maximum_test_time;
$item['maximum_marks'] = $list->maximum_marks;
$item['test_date'] = $list->test_date;
$number = floatval($list->obtaine)* 100 / intval($list->maximum_marks);
$item['percentage'] = number_format($number, 2, '.', ''); // upto 2 decimal places
$data_array['detail'] = $item;
}
$tmp = Array();
$hrs = $row_time[0]->spent_hours;
$mint = $row_time[0]->spent_minute;
$sec = $row_time[0]->spent_second;
$completed_minute = $row_time[0]->compeleted_minute;
$completed_second = $row_time[0]->compeleted_second;
if($completed_second < 0) {
$completed_second = -1 * $completed_second; // only removing - sign
}
$tmp = $this->calculateTime($hrs, $mint, $sec);
$tmp['compeleted_minute'] = $completed_minute;
$tmp['compeleted_second'] = $completed_second;
$data_array['time'] = $tmp;
foreach ($row_user as $list) {
$tem['total_user'] = $list->total_user;
$data_array['users'] = $tem;
}
// Now get The subject wise Result
$temp = Array();
$query->callProcedure('getTestResult',array($test_id,$user_id));
$subject_result = $this->loadRowList($query);
foreach ($subject_result as $res) {
$temp['subject_name']= $res->subject_name;
$temp['marks_obtained'] = $res->obtaine;
$temp['total_question'] = $res->total_question;
$temp['question_attempted'] = $res->question_attempted;
$temp['wrong_answer'] = $res->wrong_answer;
// $temp['total_spent_hours'] = $res->total_spent_hours;
// $temp['total_spent_minute'] = $res->total_spent_minute;
// $temp['total_spent_second'] = $res->total_spent_second;
$time_arr2 = $this->calculateTime($res->total_spent_hours, $res->total_spent_minute, $res->total_spent_second);
$temp['total_spent_hours'] = $time_arr2['hours'];
$temp['total_spent_minute'] = $time_arr2['minute'];;
$temp['total_spent_second'] = $time_arr2['second'];
$temp['max_marks'] = intval($res->total_question) * intval($res->positive_marks);
$subject_array[] = $temp;
}
$data_array['subject_result'] = $subject_array;
//>>>>>>>>>>> Now get Answer of Question with Time spent>>>>>>>>>>
$temp = Array();
$query->callProcedure('getAnswerwithTime',array($test_id,$user_id));
$answer_list = $this->loadRowList($query);
foreach ($answer_list as $res) {
$temp['question']= utf8_encode($res->question);
$temp['user_answer']= $res->user_answer;
$temp['correct_answer'] = $res->correct_answer;
$temp['spent_hours'] = $res->spent_hours;
$temp['spent_minute'] = $res->spent_minute;
$temp['spent_second'] = $res->spent_second;
$temp['obtaine'] = $res->obtaine;
$answer_array[] = $temp;
}
$data_array['answer_with_time'] = $answer_array;
/*>>>>>>>>End>>>>>>>>>>>>>*/
/*>>>>>>>>>>>>>>For Topper result for Comparing>>>>>>>>>>>>>>>>*/
$temp = Array();
$query->callProcedure('getTopperResult',array($test_id));
$top_arr = $this->loadRowList($query);
foreach ($top_arr as $top) {
$temp['user_name'] = $top->user_name;
$temp['test_name'] = $top->test_name;
$temp['total_question'] = $top->total_question;
$temp['right_answer'] = $top->right_answer;
$temp['wrong_answer'] = $top->wrong_answer;
$temp['question_attempted'] = $top->question_attempted;
$temp['question_not_attempted'] = $top->question_not_attempted;
$temp['positive_marks'] = $top->positive_marks;
$temp['negative_marks'] = $top->negative_marks;
$temp['maximum_marks'] = $top->maximum_marks;
$temp['obtaine'] = $top->obtaine;
$temp['percentage'] = $top->percentage;
$temp['maximum_test_time'] = $top->maximum_test_time;
$temp['test_date'] = $top->test_date;
$timer = $this->calculateTime( $top->spent_hours, $top->spent_minute, $top->spent_second);
$temp['spent_hours'] = $timer['hours'];
$temp['spent_minute'] = $timer['minute'];
$temp['spent_second'] = $timer['second'];
$temp['completed_minute'] = $top->completed_minute;
$sec_var = $top->completed_second;
if($sec_var < 0) {
$sec_var = -1 * $sec_var;
}
$temp['completed_second'] = $sec_var;
$percentile = $this->getPercentileRank($test_id,$top->percentage,$top->maximum_marks);
$temp['percentile'] = $percentile; // percentile
// $temp['rank'] = intval($percentile); // Rank
$topper_array[] = $temp;
}
//>>>>>>>>>>>>>>>>>> topper array contain Topper Percentile,now we need to get Rank according to Percentile
$topper_array = $this->rank($topper_array);
$data_array['toppers_result'] = $topper_array;
/*>>>>>>>>>>>>>>For Topper Result>>>>>>>>>>>>>>>>*/
/*>>>>>>>>>>>>>>For Login user Percentile>>>>>>>>>>>>>>>>*/
$percentile = $this->getPercentileRank($test_id, $perc, $max_marks);
$percentile_array = $this->loginUserRank($topper_array, $percentile);
$data_array['percentile'] = $percentile_array;
/*>>>>>>>>>>>>>>For Login user Percentile End>>>>>>>>>>>>>>>>*/
/*>>>>>>>>>Get subject Wise Time of Toppers >>>>>>>>>>>>>*/
$subject_wise_time = $this->getSubjectWiseTopperTime($test_id);
$data_array['subject_wise_topper_time'] = $subject_wise_time;
/*>>>>>>>>>Get subject Wise Time of Toppers End >>>>>>>>>>>>>*/
/*>>>>>>>>>Get Answer with Time of Toppers >>>>>>>>>>>>>*/
$topper_answer_with_time = $this->topperAnswerTime($test_id);
$data_array['topper_answer_with_time'] = $topper_answer_with_time;
/*>>>>>>>>>Get Answer with Time of Toppers Ends >>>>>>>>>>>>>*/
return $this->modelMessage(1,'non','success',$data_array); exit;
} else {
return $this->modelMessage($this->getStatus(),$this->getError(),$this->getMessage()); exit;
}
} else {
return $this->modelMessage($this->getStatus(),$this->getError(),$this->getMessage()); exit;
}
}
I'm trying to debug this code but I don't know what am I missing here.How can this take 15 secs of time to return response back? Have I done something wrong?
When you debug than calculate the needed times with microtime(true), my guess are the DB querys for instance:
$start=microtime(true);
$answer_list = $this->loadRowList($query);
$stop=microtime(true);
$neededTime=$stop-$start;
echo "Time for answer_list $neededTime s for query $query";
Than you see what need to longest time. Than look on your query an look on your database schema. In most cases you can solve that issue by adding some indices on your db table. You can "debug" the query with explain on sql level, this will show you if you use an index.

PHP A* Pathfinding can't work for complex maze HackerRank

I'm trying to A* pathfinding with Pacman problem using PHP.
<?php
$_fp = fopen("php://stdin", "r");
// Node
class Node {
var $x;
var $y;
var $fCost;
var $hCost;
var $gCost = 0;
var $parent;
function __construct($x=0,$y=0){
$this->x = $x;
$this->y = $y;
}
function getNeighbours($depth = 1) {
$neighbours = array();
$operand = array(
array ('x' => -1, 'y' => 0),
array ('x' => 0, 'y' => -1),
array ('x' => 0, 'y' => 1),
array ('x' => 1, 'y' => 0)
);
foreach ($operand as $key => $value) {
$checkX = $this->x + $value['x'];
$checkY = $this->y + $value['y'];
if( $checkX >= 0 && $checkY >= 0 )
array_push( $neighbours, $node = new Node( $checkX, $checkY ) );
}
return $neighbours;
}
function fCost(){
global $food;
return $this->gCost() + $this->hCost($food);
}
function gCost(){
global $pacman;
return abs($pacman->x - $this->x) + abs($pacman->y - $this->y);
}
function hCost($destination){
return abs($destination->x - $this->x) + abs($destination->y - $this->y);
}
}
function retracePath($start,$end) {
$current = $end;
while ( $current != $start ) {
echo $current->x . " " . $current->y."<br>";
$current = $current->parent;
}
}
$pacman = new Node();
$food = new Node();
// Input data
fscanf($_fp, "%d %d", $pacman->x, $pacman->y); // Pacman's position
fscanf($_fp, "%d %d", $food->x, $food->y); // Food's position
fscanf($_fp, "%d %d", $row_size, $col_size); // For map size row and col
// Input for map by row
for($row=0; $row<$row_size; $row++) {
$map[$row] = trim(fgets(STDIN));
}
// Astar
$arr_open = array(); // set of nodes to be evaluated
$arr_close = array(); // set of nodes already evaluated
array_push($arr_open, $pacman); // add the start node to $arr_open
$key_arr_open = 0;
while( count($arr_open) > 0 ) { // loop
$current = new Node();
$current = $arr_open[$key_arr_open];
unset($arr_open[$key_arr_open]);
array_push($arr_close, $current);
if($current->x == $food->x && $current->y == $food->y) {
retracePath($pacman,$current);
echo "sukses<br>"
break;
}
$neighbours = $current->getNeighbours();
foreach ($neighbours as $key => $data) {
if($map[$data->x][$data->y] == "%" or in_array($data, $arr_close))
{
//echo "not traversable<br>";
continue;
}
$new_cost_to_neighbour = $current->gCost() + $current->hCost($data);
if( $new_cost_to_neighbour < $data->gCost() or !in_array( $data, $arr_open ) ) {
$data->gCost = $new_cost_to_neighbour;
$data->hCost = $data->hCost($food);
$data->fCost = $new_cost_to_neighbour + $data->hCost($food);
$data->parent = $current;
if( !in_array($data, $arr_open) )
{
array_push($arr_open, $data);
}
}
}
$key_arr_open ++;
}
?>
Input format : Position x and y pacman, position x and y food, count row and column of tile, then the grid. Grid format is "P" for pacman, "." for food, "-" for traversable path and "%" for wall.
The problem is when I give input with 6x6 tile like this :
%%%%%%
%-%%-%
%-%%-%
%-%%-%
%.--P%
%%%%%%
The code is work. But, if I give input with 37x37 tile with complex maze, the node in the array of open always looped. (From HackerRank)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-------%-%-%-----------%---%-----%-%
%-%%%%%%%-%-%%%-%-%%%-%%%-%%%%%%%-%-%
%-------%-------%-%-----%-----%-%---%
%%%%%-%%%%%-%%%-%-%-%-%%%-%%%%%-%-%%%
%---%-%-%-%---%-%-%-%---%-%---%-%---%
%-%%%-%-%-%-%%%-%%%%%-%%%-%-%%%-%%%-%
%-------%-----%---%---%-----%-%-%---%
%%%-%%%%%%%%%-%%%%%%%-%%%-%%%-%-%-%-%
%-------------%-------%-%---%-----%-%
%-%-%%%%%-%-%%%-%-%-%%%-%-%%%-%%%-%-%
%-%-%-----%-%-%-%-%-----%---%-%-%-%-%
%-%-%-%%%%%%%-%-%%%%%%%%%-%%%-%-%%%-%
%-%-%-%-----%---%-----%-----%---%---%
%%%-%%%-%-%%%%%-%%%%%-%%%-%%%-%%%%%-%
%-----%-%-%-----%-%-----%-%---%-%-%-%
%-%-%-%-%-%%%-%%%-%%%-%%%-%-%-%-%-%-%
%-%-%-%-%-----------------%-%-%-----%
%%%-%%%%%%%-%-%-%%%%%-%%%-%-%%%-%%%%%
%-------%-%-%-%-----%---%-----%-%---%
%%%%%-%-%-%%%%%%%%%-%%%%%%%%%%%-%-%%%
%---%-%-----------%-%-----%---%-%---%
%-%%%-%%%%%-%%%%%%%%%-%%%%%-%-%-%%%-%
%-%---%------%--------%-----%-------%
%-%-%-%%%%%-%%%-%-%-%-%-%%%%%%%%%%%%%
%-%-%---%-----%-%-%-%-------%---%-%-%
%-%-%%%-%%%-%-%-%-%%%%%%%%%-%%%-%-%-%
%-%---%-%---%-%-%---%-%---%-%-%-----%
%-%%%-%%%-%%%%%-%%%-%-%-%%%%%-%-%%%%%
%-------%---%-----%-%-----%---%-%---%
%%%-%-%%%%%-%%%%%-%%%-%%%-%-%%%-%-%%%
%-%-%-%-%-%-%-%-----%-%---%-%---%-%-%
%-%-%%%-%-%-%-%-%%%%%%%%%-%-%-%-%-%-%
%---%---%---%-----------------%-----%
%-%-%-%-%%%-%%%-%%%%%%%-%%%-%%%-%%%-%
%.%-%-%-------%---%-------%---%-%--P%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Your program is almost correct. The problem arises from using in_array() for looking up a node in $arr_close or $arr_open, because in_array() compares not only the position $x, $y, but also the other Node members $fCost, $hCost, $gCost ...; thus it doesn't recognize that a node is already in the closed set of nodes already evaluated if those other members differ, and gets to evaluate it repeatedly.
A quick fix is to use instead of in_array() a self-defined function that as needed only compares the $x, $y members:
function in($node, $arr)
{
foreach ($arr as &$member)
if ($member->x == $node->x && $member->y == $node->y) return TRUE;
return FALSE;
}

leveling up PHP algorithm

I want some algorithm to make a PHP level up system I already made a system to level up people but what if people gained a huge amount of exp so they need to level up multiple times example of my code:
if(isset($_POST['id']) && isset($_POST['amount'])) {
$amount = $_POST['amount'];
$user = $_POST['id'];
$exp;$etnl;$newExp;$level;
`//select exp and etnl from DB
$result = DB::getInstance() ->query("SELECT exp,etnl,level FROM exp WHERE id = $user");
$result->setFetchMode(PDO::FETCH_ASSOC);
$row = $result->fetch();
$exp = $row['exp'];
$etnl = $row['etnl'];
$level = $row['level']
//c where to level up user or to add exp to his ep bar
if($amount + $exp > $etnl) {
$etnl = $etnl * 2;
$leftOvers = ($amount + $exp) - $etnl;
$newExp = $leftOvers;
$level = $level + 1;`
$result = DB::getInstance()->prepare("UPDATE exp SET exp=?,level=?,etnl=? WHERE id=?");
$result->execute(array($amount,$level,$etnl,$user));
} else {
$result = DB::getInstance()->prepare("UPDATE exp SET exp=? WHERE id=?");
$result->execute(array($amount,$user));
}
}`
`
but this system fails if the exp could level up a person 2 times any help or any new code? BTW etnl stands for exp till next level thank you
If you don't want to manually type out a level table like the other answer suggets you can also try doing your process with a recursive function:
$current_exp = 0;
$current_level = 1;
$to_next_level = 1000;
function did_level( $amount = 0 ) {
global $current_exp;
global $curent_level;
global $to_next_level;
$tmp_amount = $amount + $current_exp;
$tmp_amount_remainder = $tmp_amount - $to_next_level;
if ($tmp_amount >= $to_next_level) {
$current_level++;
$to_next_level *= 2;
$current_exp = $tmp_amount;
if ($tmp_amount_remainder > 0) {
// Level us up again.
did_level( $tmp_amount_remainder );
} else {
// Store the new level, to next level, and current exp
}
} else {
// Store the new exp amount
}
}
You have to get a table of levels which is basically a list of [exp, level] pairs. For example,
$level_list = [
['exp' => 0, 'level' => 1],
['exp' => 100, 'level' => 2],
['exp' => 1000, 'level' => 3],
// etc.
];
Then, you iterate the table starting from the end:
$i = count($level_list);
while($i > 0) {
$i--;
if ($user_exp >= $level_list[i]['exp']) {
// this is the level we need
break;
}
}
$target_level = $level_list[$i]['level'];
$target_exp = $level_list[$i]['exp'];

PHP generate football schedule

I have 38 days and 20 clubs (EPL).
How can I generate not repeated matches for this clubs in this days (schedule)?
For example:
Day 1:
club1 - club2
club3 - club4
...
club19 - club 20
Day 2:
club1 - club3
club2 - club4
...
club20 - club18
Each club plays with other two games (home and away). Respectively do not play with himself.
My thinks:
$clubs1 = array();
$clubs2 = array();
$days = range(1, 38);
$calendar = array();
$pars = array();
$rows = Yii::app()->db->createCommand()
->select('id')
->from('clubs')
->queryAll();
foreach ($rows as $item) {
$clubs1[] = $item['id'];
$clubs2[] = $item['id'];
}
shuffle($clubs1);
shuffle($clubs2);
$total = (count($clubs1) * 2) - 2;
for ($j = 1; $j <= $total; $j ++) {
$day = $days[$j];
for ($i = 0; $i < count($clubs1); $i++) {
WHAT I SHOULD DO IN THIS BODY?
}
}
You need only one clubs array
1) remove $clubs2
2) rename $clubs1 to $clubs
3) remove whole for structure
//for testing: $clubs=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
$countofteams=count($clubs);
$c=1;
for($j=0;$j<2;$j++) //home/away
for($i=1;$i<$countofteams;$i++){ //move teams
echo '----DAY '.$c++.'----<br>';
for($a=0;$a<$countofteams;$a++) //all teams are playing
echo 'Match '.$clubs[$a].' vs '.$clubs[($a+$i)%$countofteams].'<br>';
}
$team = array();
$pars = array();
$rows = Yii::app()->db->createCommand()
->select('id')
->from('clubs')
->queryAll();
foreach ($rows as $k => $item) {
$team[$k+1] = $item['id'];
}
$all_team = count($team);
$k = $all_team/2;
$days = range(7, 100, 2); // first halh of season
$days2 = range(55, 100, 2); // second half
// 1 tour
for ($i=1; $i <= $k; $i++) {
$pars[] = $days[0].'|'.$team[$i].'|'.$team[($all_team-$i+1)];
$pars[] = $days2[0].'|'.$team[($all_team-$i+1)].'|'.$team[$i];
}
// Next tours
for($i=2; $i<$all_team; $i++)
{
$team2 = $team[2];
for($y=2;$y<$all_team;$y++)
{
$team[$y] = $team[$y+1];
}
$team[$all_team] = $team2;
for($j=1;$j<=$k;$j++)
{
$pars[] = $days[$i - 1].'|'.$team[$j].'|'.$team[($all_team-$j+1)];
$pars[] = $days2[$i - 1].'|'.$team[($all_team-$j+1)].'|'.$team[$j];
}
}
Here's my solution, replace the $clubs array with your result set of club IDs from database. The only slight inaccuracy with real-world EPL is that the second half of the season will mirror the first half exactly.
See if this adaption of http://board.phpbuilder.com/showthread.php?10300945-Round-Robin-Generator is any better :) - just for first half of the season.
$clubs = array(
'che', 'swa', 'ast', 'manc', 'liv', 'tot', 'ars', 'sot', 'hul', 'stok', 'wham', 'qpr', 'sun', 'mutd', 'lei', 'new', 'eve', 'wba', 'cry', 'bur',
);
shuffle($clubs);
$num_players = count($clubs) - 1;
// Set the return value
$ret = '';
// Generate the pairings for each round.
for ($round = 0; $round < $num_players; $round++) {
$ret .= '<h3>' . ($round + 1) . '</h3>';
$players_done = array();
// Pair each player except the last.
for ($player = 1; $player < $num_players; $player++) {
if (!in_array($player, $players_done)) {
// Select opponent.
$opponent = $round - $player;
$opponent += ($opponent < 0) ? $num_players : 1;
$playerName = $clubs[$player];
$opponentName = $clubs[$opponent];
// Ensure opponent is not the current player.
if ($opponent != $player) {
// Choose colours.
if (($player + $opponent) % 2 == 0 xor $player < $opponent) {
// Player plays white.
$ret .= "$playerName - $opponentName $br";
} else {
// Player plays black.
$ret .= "$opponentName - $playerName $br";
}
// This pair of players are done for this round.
$players_done[] = $player;
$players_done[] = $opponent;
}
}
}
// Pair the last player.
if ($round % 2 == 0) {
$playerName = $clubs[$num_players];
$opponent = ($round + $num_players) / 2;
$opponentName = $clubs[$opponent];
// Last player plays white.
$ret .= "$playerName - $opponentName $br";
} else {
$opponent = ($round + 1) / 2;
// Last player plays black.
$ret .= "$opponentName - $playerName $br";
}
}
echo $ret;

Categories