So, i want to bring data from mysql, and organize by months. Im separating date by months and applying SUM, to SUM the value of all months, and organize on chart.js
My problem, is on Chart. Chart is apllying all the result on unique month.
January has so many data, and all of them is going to Feb, for example...
My controller:
`<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class start extends CI_Controller {
public function index(){
$this->load->model('Start_class');
$data['janeiro'] = $this->Start_class->janeiro();
$data['fevereiro'] = $this->Start_class->fevereiro();`
My model:
function janeiro(){
$minvalue = '2016-01-01';
$maxvalue = '2016-01-31';
$query1 = $this->db->select('SUM(valorcompra) as valorcompra')->from('compravista')->where("datavencimento BETWEEN '$minvalue' AND '$maxvalue' ")->get();
$retorno = $query1->row()->valorcompra;
return $retorno;
}
function fevereiro(){
$minvalue = '2016-02-01';
$maxvalue = '2016-02-31';
$query2 = $this->db->select('SUM(valorcompra) as valorcompra')->from('compravista')->where("datavencimento BETWEEN '$minvalue' AND '$maxvalue'")->get();
$retorno = $query2->row()->valorcompra;
return $retorno;
}
and my chart:
var data = {
labels: ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"],
datasets: [
{
label: "Dados primários",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: [<? echo $janeiro ?>, <? echo $fevereiro ?>, randomnb(), randomnb(), randomnb(), randomnb(), randomnb(), randomnb(), randomnb(), randomnb(), randomnb(), randomnb()]
},
In the model you have used the Where statement wrong.
Replace
where("datavencimento BETWEEN '$minvalue' AND '$maxvalue' ")
to
where("datavencimento BETWEEN '$minvalue' AND '$maxvalue' ",null,false)
in both query.
Related
I'm stuck on this from a while.Can't figured it out.I reed documantion, tried with several videos and tried like 10 different ways, nothing is working yet.So I have one view/model for one thing, in this example Destination and I have separate files for Offers.The controllers for both are in one file.I want tho the information that is in destination to go to Offers as well.Please help I can't figure out what I'm missing:
So here is the most important parts:
destination_model.php
<?php class Destination_model extends CI_Model
{
public function getDestinationDetails($slug) {
$this->db->select('
id,
title,
information
');
$this->db->where('slug', $slug);
$query = $this->db->get('destinations')->row();
if(count($query) > 0)
{
return $query;
}
else
{
// redirect(base_url());
}
}
public function getOffersByDestination($destination_id)
{
$this->db->select('
o.short_title,
o.price,
o.currency,
o.information,
o.long_title_slug,
oi.image,
c.slug as parent_category
');
$this->db->from('offers o');
$this->db->join('offers_images oi', 'oi.offer_id = o.id', 'left');
$this->db->join('categories c', 'c.id = o.category');
$this->db->group_by('o.id');
$this->db->where('o.destination', $destination_id);
$this->db->where('o.active', '1');
return $this->db->get();
} }
And then in the controller for offers I put this:
$this->load->model('frontend/destination_model');
$this->params['destination'] = $this->destination_model->getOffersByDestination($data->id);
All I need is the title and the information about the destination.
Here is the whole controller for the offers:
$data = $this->slugs_model->getOfferDetails(strtok($this->uri->segment(2), "."));
$this->load->model('frontend/offers_model');
$this->load->model('frontend/destination_model');
$this->params['main'] = 'frontend/pages/offer_details';
$this->params['title'] = $data->long_title;
$this->params['breadcumb'] = $this->slugs_model->getSlugName($this->uri->segment(1));
$this->params['data'] = $data;
$this->params['images'] = $this->slugs_model->getOfferImages($data->id);
$this->params['similar'] = $this->slugs_model->getSimilarOffers($data->category, $data->id);
$this->params['destination'] = $this->destination_model->getOffersByDestination($data->id);
$this->params['offers'] = $this->offers_model->getImportantOffers($data->offers, $data->category, $data->id);
You need to generate query results after you get it from model,
e.g: row_array(), this function returns a single result row.
here's the doc: Generating Query Results
try this:
$this->load->model('frontend/destination_model');
$data['destination'] = $this->destination_model->getOffersByDestination($data->id)->row_array();
$this->load->view('view_name', $data);
And in your view echo $destination['attribut_name'];,
or you can print the array, to see if it's work print_r($destination);
i need help, i'm new in laravel, i want to make query to get year only from table like this
public function index() {
$items = Items::distinct()->orderBy('date','desc')->select(DB::raw('YEAR(`date`) as date'))->get();
$itemDet = Items::where('date', $items->date);
}
then i want to show other value from table based on year above with this function
public function itemDetail($year) {
$itemDet = Items::where('YEAR(`date`)', $year);
return $itemDet;
}
But i don't know how to call it in view, i only know how to call the first function in a view, and I don't know how to pass value year from first function as parameters to second function.
2018
- item2018
- item2018
- item2018
2017
- item2017
2016
-item2016
-item2016
sorry for my bad grammar, thank you!
public function itemDetail($year) {
$itemDet = Items::where('YEAR(`date`)', $year);
//return $itemDet; change it to
return view('yourViewPath',compact('itemDet'));
}
in view you can access like this way
{{ $itemDet }}
You can get that output right away in your index function without passing it to second function, one way to do it.
public function index() {
$arr_container = []; //create array container
$items = Items::distinct()->orderBy('date','desc')->select(DB::raw('YEAR(`date`) as date'))->get();
foreach($items as $val){
$itemDet = Items::select(DB::raw('item'))->where('YEAR(`date`)', $val->year)->get();
if($itemDet->count()>0){
$arr_container[$val->year][] = $itemDet[0]->item;
}
}
return view('view')->with('years',$arr_container);
}
You should get this output when you call {{$years}}.
2018
- item2018
- item2018
- item2018
2017
- item2017
2016
-item2016
-item2016
To be more clear, none of these lines in default language's general_lang.php work:
$lang['general_welcome_message'] = 'Welcome, %s ( %s )';
or
$lang['general_welcome_message'] = 'Welcome, %1 ( %2 )';
I expect an output like Welcome, FirstName ( user_name ).
I followed the second (not accepted) answer at https://stackoverflow.com/a/10973668/315550.
The code I write in the view is:
<div id="welcome-box">
<?php echo lang('general_welcome_message',
$this->session->userdata('user_firstname'),
$this->session->userdata('username')
);
?>
</div>
I use codeigniter 2.
You will need to use php's sprintf function (http://php.net/manual/en/function.sprintf.php)
Example from http://ellislab.com/forums/viewthread/145634/#749634:
//in english
$lang['unread_messages'] = "You have %1$s unread messages, %2$s";
//in another language
$lang['unread_messages'] = "Hi %2$s, You have %1$s unread messages";
$message = sprintf($this->lang->line(‘unread_messages’), $number, $name);
I extended Code CI_Lang class like this..
class MY_Lang extends CI_Lang {
function line($line = '', $swap = null) {
$loaded_line = parent::line($line);
// If swap if not given, just return the line from the language file (default codeigniter functionality.)
if(!$swap) return $loaded_line;
// If an array is given
if (is_array($swap)) {
// Explode on '%s'
$exploded_line = explode('%s', $loaded_line);
// Loop through each exploded line
foreach ($exploded_line as $key => $value) {
// Check if the $swap is set
if(isset($swap[$key])) {
// Append the swap variables
$exploded_line[$key] .= $swap[$key];
}
}
// Return the implode of $exploded_line with appended swap variables
return implode('', $exploded_line);
}
// A string is given, just do a simple str_replace on the loaded line
else {
return str_replace('%s', $swap, $loaded_line);
}
}
}
ie. In your language file:
$lang['foo'] = 'Thanks, %s. Your %s has been changed.'
And where-ever you want to use it (controller / view etc.)
echo $this->lang->line('foo', array('Charlie', 'password'));
Will produce
Thanks, Charlie. Your password has been changed.
This handles single 'swaps' as well as multiple
Also it won't break any existing calls to $this->lang->line.
I have created a library in codeigniter. It is not working.
My Library file is;
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Image_pixelete{
function __construct()
{
$this->ci=&get_instance();
}
public function do_pixel()
{
$image = imagecreatefromjpeg(base_url().'photo/Penguins.jpg');
$imagex = imagesx($image);
$imagey = imagesy($image);
$pixelate_y=10;
$pixelate_x=10;
$height=$imagey;
$width=$imagex;
for($y = 0;$y < $height;$y += $pixelate_y+1)
{
for($x = 0;$x < $width;$x += $pixelate_x+1)
{
// get the color for current pixel
$rgb = imagecolorsforindex($image, imagecolorat($image, $x, $y));
// get the closest color from palette
$color = imagecolorclosest($image, $rgb['red'], $rgb['green'], $rgb['blue']);
imagefilledrectangle($image, $x, $y, $x+$pixelate_x, $y+$pixelate_y, $color);
}
}
}
Here is my controller call
public function pixel()
{
$this->load->library('Image_pixelete');
$this->Image_pixelete->do_pixel();
}
and here is my error;
I am confused about exact nature of the problem to cope it out. can you please review it?
Try by loading and using your library with lower case.
$this->load->library('image_pixelete');
$this->image_pixelete->do_pixel();
Check this similar question codeigniter cannot load library
I want to create some kind of sitemap in extbase/fluid (based on the pagetree). I have loaded the pages table into a model:
config.tx_extbase.persistence.classes.Tx_MyExt_Domain_Model_Page.mapping.tableName = pages
I have created a controller and repository, but get stuck on the part wich can load the subpages as relation into my model.
For example:
$page = $this->pageRepository->findByPid($rootPid);
Returns my rootpage. But how can I extend my model that I can use $page->getSubpages() or $page->getNestedPages()?
Do I have to create some kind of query inside my model? Or do I have to resolve this with existing functions (like the object storage) and how?
I tried a lot of things but can simply figure out how this should work.
you have to overwrite your findByPid repository-method and add
public function findByPid($pid) {
$querySettings = $this->objectManager->create('Tx_Extbase_Persistence_Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);
$this->setDefaultQuerySettings($querySettings);
$query = $this->createQuery();
$query->matching($query->equals('pid', $pid));
$pages = $query->execute();
return $pages;
}
to get all pages. Than you can write your own getSubpages-method like
function getSubpages($currentPid) {
$subpages = $this->pagesRepository->findByPid($currentPid);
if (count($subpages) > 0) {
$i = 0;
foreach($subpages as $subpage) {
$subpageUid = $subpage->getUid();
$subpageArray[$i]['page'] = $subpage;
$subpageArray[$i]['subpages'] = $this->getSubpages($subpageUid);
$i++;
}
} else {
$subpageArray = Array();
}
return $subpageArray;
}
i didn't test this method, but it looks like this to get alle subpages.
i wonder that i could´t find a typo3 method that return the complete Page-Tree :( So i write a little function (you can use in an extbase extension), for sure not the best or fastes way, but easy to extend or customize ;)
first you need an instance of the PageRepository
$this->t3pageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
this->t3pageRepository->init();
make the init, to set some basic confs, like "WHERE deletet = 0 AND hidden = 0..."
then with this function you get an array with the page data and subpages in. I implement yust up to three levels:
function getPageTree($pid,$deep=2){
$fields = '*';
$sortField = 'sorting';
$pages = $this->t3pageRepository->getMenu($pid,$fields,$sortField);
if($deep>=1){
foreach($pages as &$page) {
$subPages1 = $this->t3pageRepository->getMenu($page['uid'],$fields,$sortField);
if(count($subPages1)>0){
if($deep>=2){
foreach($subPages1 as &$subPage1){
$subPages2 = $this->t3pageRepository->getMenu($subPage1['uid'],$fields,$sortField);
if(count($subPages2>0)){
$subPage1['subpages'] = $subPages2;
}
}
}
$page['subpages'] = $subPages1;
}
}
}
return $pages;
}