How to show the result of function day_s() in a view just_organize/administrador/index
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends MY_Controller {
public function index()
{
$this->load->view("layouts/header");
$this->load->view("layouts/navbar");
$this->load->view("just_organize/administrador/index");
$this->load->view("layouts/footer");
}
public function day_s(){
$time = date("H");
$timezone = date("e");
if ($time < "12") {
$greeting= "Good morning";
} else
if ($time >= "12" && $time < "17") {
$greeting= "Good afternoon";
} else
if ($time >= "17" && $time < "19") {
$greeting= "Good evening";
} else
if ($time >= "19") {
$greeting= "Good night";
}
}
}
JUst add these lines in your index :
use $greetings variable in view
public function index()
{
$data['greetings'] = $this->day_s();
$this->load->view("layouts/header");
$this->load->view("layouts/navbar");
$this->load->view("just_organize/administrador/index", $data);
$this->load->view("layouts/footer");
}
Your day_s function should return $greeting:
public function day_s(){
$time = date("H");
$timezone = date("e");
if ($time < "12") {
$greeting= "Good morning";
} else
if ($time >= "12" && $time < "17") {
$greeting= "Good afternoon";
} else
if ($time >= "17" && $time < "19") {
$greeting= "Good evening";
} else
if ($time >= "19") {
$greeting= "Good night";
}
return $greeting;
}
Related
I'm working on a project where I need to calculate user trading analytics and update it on the user dashboard. Everything went smooth until the number of each user trades grew in number (total of 10.000 trades and above) which increase the load time. Is there a way to speed up the process?
My current code:
AnalyticsIndex.php
public function updateChart()
{
$trades = $this->filterTrades();
$tradeAnalyticsService = app(TradeAnalyticsService::class, ['trades' => $trades, 'balance' => $this->portfolio->balance]);
$this->netProfitData = $tradeAnalyticsService->getRangeNetProfit();
$this->winLoseData = $tradeAnalyticsService->getWinLossPercentage();
$this->bestTradeReturn = $tradeAnalyticsService->getBestTradeReturn();
$this->worstTradeReturn = $tradeAnalyticsService->getWorstTradeReturn();
$this->essentialsData = $tradeAnalyticsService->getEssentialsData();
$this->emit('updateData');
}
public function filterTrades()
{
$trades = $this->portfolio->trades(['return', 'return_percentage', 'status', 'entry_date', 'id', 'instrument'])->latest('entry_date')->get();
// $trades = Trade::select(['return', 'return_percentage', 'status', 'entry_date', 'id'])->where('portfolio_id', $this->portfolio->id)->orderBy('entry_date')->get();
switch ($this->filter) {
case '7D':
return $trades->where('entry_date', '<=', now()->endOfWeek())->where('entry_date', '>=', now()->startOfWeek());
break;
case '1M':
return $trades->where('entry_date', '<=', now()->endOfMonth())->where('entry_date', '>=', now()->startOfMonth());
break;
case '3M':
return $trades->where('entry_date', '<=', now()->endOfMonth())->where('entry_date', '>=', now()->startOfMonth()->subMonths(2));
break;
case '6M':
return $trades->where('entry_date', '<=', now()->endOfMonth())->where('entry_date', '>=', now()->startOfMonth()->subMonth(5));
break;
case '1Y':
return $trades->where('entry_date', '<=', now()->endOfYear())->where('entry_date', '>=', now()->startOfYear());
break;
case '2Y':
return $trades->where('entry_date', '<=', now()->endOfYear())->where('entry_date', '>=', now()->startOfYear()->subYears(1));
break;
case '3Y':
return $trades->where('entry_date', '<=', now()->endOfYear())->where('entry_date', '>=', now()->startOfYear()->subYears(2));
break;
case 'All':
return $trades;
break;
default:
return $trades;
break;
}
}
TradeAnalyticsService.php
<?php
namespace App\Services;
use App\Models\Portfolio;
use stdClass;
class TradeAnalyticsService
{
private $trades;
private $balance = 0;
public function __construct($trades, int $balance)
{
$this->trades = $trades;
$this->balance = $balance;
}
public function getNetProfit()
{
return $this->netProfit($this->trades);
}
public function getProfitFactor()
{
$win = $this->filterTrade('win')->sum('return');
$lose = abs($this->filterTrade('lose')->sum('return'));
if ($win < 1 || $lose < 1) {
return 0;
}
return $win / $lose;
}
public function getPercentProfitable()
{
$winCount = $this->filterTrade('win')->count();
$winLoseCount = $this->trades->whereIn('status', ['win', 'lose'])->count();
if ($winCount < 1 || $winLoseCount < 1) {
return 0;
}
return $winCount / $winLoseCount * 100;
}
public function getAverageTradeNetProfit()
{
$winLoseCount = $this->trades->whereIn('status', ['win', 'lose'])->count();
if ($winLoseCount < 1) {
return 0;
}
return $this->getNetProfit() / $winLoseCount;
}
public function getAverageWinner()
{
$sum = $this->filterTrade('win')->sum('return');
$count = $this->filterTrade('win')->count();
// dd($sum, $count);
if ($count < 1) {
return 0;
}
return $sum / $count;
}
public function getAverageLoser()
{
$sum = $this->filterTrade('lose')->sum('return');
$count = $this->filterTrade('lose')->count();
// dd($sum, $count);
if ($count == 0) {
return 0;
}
return $sum / $count;
}
public function getBestTradeReturn()
{
return $this->trades->sortByDesc('return')->first();
}
public function getWorstTradeReturn()
{
return $this->trades->sortBy('return')->first();
}
public function getWinCount()
{
return $this->filterTrade('win')->count();
}
public function getLoseCount()
{
return $this->filterTrade('lose')->count();
}
public function getTradeCount()
{
return $this->trades->count();
}
public function getBalanceGrowth()
{
return $this->balanceGrowth($this->trades);
}
public function getBalanceGrowthPercentage()
{
return $this->balanceGrowthPercentage($this->balanceGrowth($this->trades));
}
public function getEssentialsData()
{
return [
'profit_factor' => $this->getProfitFactor(),
'percent_profitable' => $this->getPercentProfitable(),
'average_trade_net_profit' => $this->getAverageTradeNetProfit(),
'trade_count' => $this->getTradeCount(),
'net_profit' => $this->getNetProfit(),
'longest_win_streaks' => $this->getLongestWinStreaks(),
'longest_lose_streaks' => $this->getLongestLoseStreaks(),
'average_winner' => $this->getAverageWinner(),
'average_loser' => $this->getAverageLoser(),
];
}
public function getWinLossPercentage()
{
$winCount = $this->filterTrade('win')->count();
$loseCount = $this->filterTrade('lose')->count();
if ($winCount + $loseCount == 0) {
return 0;
}
$win = $winCount / ($winCount + $loseCount) * 100;
$data = [
[
'x' => 'Win',
'y' => $win
],
[
'x' => 'Lose',
'y' => 100 - $win
]
];
$data = [$win, 100 - $win];
return $data;
}
public function getRangeNetProfit()
{
$data = array();
$trades = $this->trades->whereIn('status', ['win', 'lose']);
$rawDatas = $trades->groupBy(function ($item) {
return format_string_date($item->entry_date, 'd/m/y');
});;
if ($rawDatas->count() > 30) {
$rawDatas = $trades->groupBy(function ($item) {
return format_string_date($item->entry_date, 'm/y');
});;
}
foreach ($rawDatas as $key => $rawData) {
$tempData = [
'x' => $key,
'y' => $this->netProfit($rawData)
];
array_push($data, $tempData);
}
return $data;
}
public function getLongestWinStreaks()
{
$currentStreak = 0;
$longestStreak = 0;
foreach ($this->trades->whereIn('status', ['win', 'lose']) as $trade) {
if (!($trade->status == "win")) {
$currentStreak = 0;
continue;
}
$currentStreak++;
if ($currentStreak > $longestStreak) {
$longestStreak = $currentStreak;
}
}
return $longestStreak;
}
public function getLongestLoseStreaks()
{
$currentStreak = 0;
$longestStreak = 0;
foreach ($this->trades->whereIn('status', ['win', 'lose']) as $trade) {
if (!($trade->status == "lose")) {
$currentStreak = 0;
continue;
}
$currentStreak++;
if ($currentStreak > $longestStreak) {
$longestStreak = $currentStreak;
}
}
return $longestStreak;
}
private function balanceGrowth($trades)
{
return $this->balance + $trades->sum('return');
}
private function balanceGrowthPercentage($balance)
{
$initial = $this->balance;
if ($initial <= 0) {
return 0;
}
return ($balance - $initial) / $initial * 100;
}
private function netProfit($trades)
{
return $trades->whereIn('status', ['win', 'lose'])->sum('return');
}
private function filterTrade(string $status)
{
return $this->trades->where('status', $status);
}
}
Note: The data is calculated once with wire:init and every time the user clicks on the filter button (7D, 1M, 3M, etc)
I would like to know what is the best place to put additional functions in order to use it in a controller. Here is an example:
public function store(CreateServiceRequest $request)
{
function getMonth($value)
{
if ($value == 1)
{
return 1;
} else
if ($value == 2)
{
return 3;
} else
if ($value == 3)
{
return 6;
} else
return 0;
}
function getYear($value)
{
if ($value == 4)
{
return 1;
} else
if ($value == 5)
{
return 2;
} else
if ($value == 6)
{
return 3;
} else
return 0;
}
function getTax($price, $vat)
{
$tax = ($vat/100*$price);
return $tax;
}
$input = Request::all();
$months = getMonth($input['period']);
$years = getYear($input['period']);
$tax = getTax($input['price_net'], $input['vat']);
$input['price_vat'] = $tax;
$input['price_gross'] = $input['price_net'] + $tax;
$input['period_end'] = Carbon::createFromFormat('Y-m-d', $input['period_start'])->addYears($years)->addMonths($months)->toDateString();
return $input;
}
As you can see inside the store method I wrote three functions:
getMonth, getYear and getTax. The method first fetch all request, then I need to create some new variables - for instance I get the $input['period'] and use it to make $input['period_end']. So I don't change $input['period'] value but create a new $input['period_end']. If I would only change existing variable I used mutator... So the question is where I should put the code of getMonth, getYear and getTax functions? It seems like a mess in store method...
make helpers.php (in root, where .env file is).
if (! function_exists('helper_function')) {
function helper_function()
{
return 'hello';
}
}
and in your composer.json autoload it:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"files": [
"helpers.php"
]
},
now you are able to call your helper function everywhere in code base
public function shirts($type = '') {
{
if ($type == 'glass') {
$shirt = Product::where('category_id', '1') - > get();
}
elseif($type == 'ic') {
$shirt = Product::where('category_id', '2') - > get();
}
elseif($type == 'cover') {
$shirt = Product::where('category_id', '3') - > get();
} else {
$shirt = Product::all();
}
$products = Category::find($shirt);
return view('front.shirt', compact('products', 'shirt'));
}
}
can some one help me in minimising this Controller I don't want to use if else statement
my function with advanced wheres is not giving any syntax error but its not even working. Its displaying. i have written this function following this example http://laravel.com/docs/queries#advanced-wheres
public function getData($wheres1 = array(),$wheres2 = array(),$wheres3 = array(),$wheres4 = array(),$wheres5 = array(),$wheres6 = array(),$wheres7 = array())
{
$query = Listing::query();
$result = array();
$query = $query->where(function($q){
if(!empty($wheres1)){
$count=0;
foreach($wheres1 as $where){
if($count==0)
{ $q = $q->where($where['field'], $where['operator'], $where['value']);
$count++;
}
else
$q = $q->orWhere($where['field'], $where['operator'], $where['value']);
}
}
})->where(function($q){
if(!empty($wheres2)){
$count=0;
foreach($wheres2 as $where){
if($count==0)
{ $q = $q->where($where['field'], $where['operator'], $where['value']);
$count++;
}
else
$q = $q->orWhere($where['field'], $where['operator'], $where['value']);
}
}
})->where(function($q){
if(!empty($wheres3)){
$count=0;
foreach($wheres3 as $where){
if($count==0)
{ $q = $q->where($where['field'], $where['operator'], $where['value']);
$count++;
}
else
$q = $q->orWhere($where['field'], $where['operator'], $where['value']);
}
}
});
$result = $query->orderBy('id', 'desc')->paginate(3);
return $result;
}
You need to add use for each where
$query = $query->where(function($q) use ($wheres1) {
....
}
You may try this approach:
$query = Listing::query();
if(is_array($wheres1) && count($wheres1)) {
$first = array_shift($wheres1);
$query->where($first['field'], $first['operator'], $first['value']);
if(count($wheres1)) {
foreach($wheres1 as $where) {
$query->orWhere($wheres['field'], $wheres['operator'], $wheres['value']);
}
}
}
Now repeat the same process for rest of the wheres, for example ($wheres2):
if(is_array($wheres2) && count($wheres2)) {
$first = array_shift($wheres2);
$query->where($first['field'], $first['operator'], $first['value']);
if(count($wheres2)) {
foreach($wheres2 as $where) {
$query->orWhere($wheres['field'], $wheres['operator'], $wheres['value']);
}
}
}
At last:
return $query->orderBy('id', 'desc')->paginate(3);
i've wrote this code,
<?php
class fizzbuzz{
function mod3($angka)
{
$a = $angka % 3;
if($a==0) return true;
else return false;
}
function mod5($angka)
{
$b = $angka % 5;
if($b==0) return true;
else return false;
}
function index(){
for ($i=1; $i < 101; $i++) {
if(mod3($i) == true && mod5($i) == true){
echo "fizzbuzz, ";
}else if(mod3($i) == true){
echo "fizz, ";
}else if(mod5($i) == true){
echo "buzz, ";
}else echo $i.", ";
}
}
}
$show = new fizzbuzz;
$show->index();
?>
and then it came up with this error
Fatal error: Call to undefined function mod3() in C:\xampp\htdocs\tes-bimasakti\fizzbuzz.php on line 19
please help me with this error..
You forgot $this->:
if($this->mod3($i) == true && $this->mod5($i) == true){
^^^^^^^--- here ^^^^^^^---here
without $this->, php is looking for a top-level global function. It will NOT look for a method in your object.
use this keyword
function index(){
for ($i=1; $i < 101; $i++) {
if($this->mod3($i) == true && $this->mod5($i) == true){
echo "fizzbuzz, ";
}else if($this->mod3($i) == true){
echo "fizz, ";
}else if($this->mod5($i) == true){
echo "buzz, ";
}else echo $i.", ";
}
}