i want to output an array containing numbers.
I'm creating the array like this (it recieved the statistics for the last 7 days) :
<?php public function getStatisticsTeams()
{
$tab = array();
for($i=7;$i=0;$i--)
{
$q = Doctrine_Query::create()
->from('stJob j')
->where('j.created_at = ?', date('Y-m-d h:i:s' , time() - 86400 * $i ))
->execute()
->count();
$tab[] = $q;
}
return $tab;
}
action.class.php
$this->st_job = Doctrine::getTable('StJob')->getStatisticsTeams();
Use of the array in my template.php :
$chart->inlineGraph(array('hits' => $st_job), array('Monday', 'Tuesday', 'Wednesday' ....), 'div_id');
When i try to access to my array it fails because the function i use must have an array which is supposed to contain for example (43,5,87,3,29,8,10) , and when i var_dump($st_job) (my array)
object(sfOutputEscaperArrayDecorator)#363 (3) { ["count":"sfOutputEscaperArrayDecorator":private]=> int(0) ["value":protected]=> array(0) { } ["escapingMethod":protected]=> string(16) "esc_specialchars" }
Do you have any idea of what i'm doing wrong ?
Thank you
Yes, symfony is set to automatically apply escaping strategies to the data you pass from your controllers to your views. You can either remove the setting, which is not recommended, or use:
$original_array = $sf_data->getRaw('st_job');
Related
I created a form containing info about the user (doctors). Besides the default info, they now want to add opening hours from their office.
In the current form I added a form-group with 3 input elements:
select name="dayofweek[]"
input name="timeblock[]"
select name="type[]"
There is a button for the user to add an extra line with the same fields so he can create multiple opening hours (per day/timeblock).
All is stored with the submit-button from the form.
After saving my data looks like this:
"dayofweek" => ["monday", "monday", "tuesday", ...]
"timeblock" => ["10-12h", "14-18h", "10-16u", ...]
"type" => ["by appointment", "free", "free", ...]
Now I want to save these in my table with fields [ID, DayOfWeek, TimeBlock, Type].
To do so I have to rearrange the data received from the form-submit before I can save them row by row. This is how I do it now:
public static function prepareData($dayofweek = [], $timeblock = [], $type = []) {
$prepared = [];
$i = 0;
while($i < count($dayofweek)) {
$a = $dayofweek[$i];
$b = $timeblock[$i];
$c = $type[$i];
$record = ['dayofweek' => $a, 'timeblock' => $b, 'type' => $c];
array_push($prepared, $record);
$i++;
}
return $prepared;
}
To show the data on the form when editing, I have to do the inverse.
I was wondering if there is any other easier and cleaner way to do this?
Unfortunately, the native data type like array and string can have only one format and structure. If your use case uses the same data in different formats or different data structure, it is recommended to create a data object. A data object is an object that holds the data in its fields and has many inputs and output methods to allow to use the data in different formats and structures.
This is an OOP approach, in this case, it will make your code much cleaner and understandable and much more easy to expand or alter in the future. But note that this will not reduce the amount of code. The format conversion function is required anyway.
According to the description, we have a data object named Appointment with the data {DayOfWeek, TimeBlock, Type}. However, the input and output functions that are described referring to a list of Appointments, therefore, those functions do not belong to Appointment object. They refer to another data object, AppointmentList which contain an array of Appointments and input and output functions.
This object will look like this:
class Appointment
{
public $dayofweek;
public $timeblock;
public $type;
public function __construct($record)
{
$this->dayofweek = $record['dayofweek'];
$this->timeblock = $record['timeblock'];
$this->type = $record['type'];
}
}
class AppointmentList
{
public $appointmentArray = [];
function setData($data)
{
$prepared = [];
$i = 0;
while ($i < count($data['dayofweek'])) {
$a = $data['dayofweek'][$i];
$b = $data['timeblock'][$i];
$c = $data['type'][$i];
$record = ['dayofweek' => $a, 'timeblock' => $b, 'type' => $c];
$prepared[] = new Appointment($record);
$i++;
}
$this->appointmentArray = $prepared;
}
function getData() {
$data = ['dayofweek' => [],'timeblock' => [],'type' => []];
foreach ($this->appointmentArray as $appointment){
$data['dayofweek'][] = $appointment->dayofweek;
$data['timeblock'][] = $appointment->timeblock;
$data['type'][] = $appointment->type;
}
return $data;
}
}
Then when you receive $data from the form run:
$al = new AppointmentList();
$al->setData($data);
The you can use the array $al->appointmentArray to access the appointments one by one and store them in the table.
And afterward when you need to fill the form again simply use $al->getData()
Note that this is only an example. Usually different techniques are used to store the data object into the table automatically.
Hope this helps. Good luck.
I try to build custom search form and filter results in some range of dates:
$form = BootstrapForm::create (
$this,
'LetterSearchForm',
FieldList::create (
DateField::create('Sent_After','Sent After'),
DateField::create('Sent_Befor','Sent Before')
...
),
...
);
public function index (SS_HTTPRequest $request)
{
$letters = Letter::get()->sort('DateUpload');
if($search = $request->getVar('Sender')) {
$letters = $letters->filter(array(
'Sender:PartialMatch' => $search
));
}
if ( $search1 = $request->getVar('Sent_After') && $search2 = $request->getVar('Sent_Befor'))
{
What must be here?
}
}
}
Can I use here something like WithinRangeFilter?
I don't know of any range filter, but according to the documentation you could use something like:
$dateFilteredList = $letters->filter(array(
'DateUpload:LessThanOrEqual' => $search2, // Sent_Befor
'DateUpload:GreaterThanOrEqual' => $search1 // Sent_After
));
I made a few assumptions with the code above:
DateUpload is the date field you're trying to filter on.
The date format supplied by the form is already database friendly, ie yyyy-mm-dd (eg 2017-09-05)
You may need to edit it accordingly.
Hope that is what you're looking for :)
Hello Silverstripe Specialists!
I made the tutorial "extending a basic site"
(http://doc.silverstripe.org/en/tutorials/extending_a_basic_site)
That all worked very well so far.
I made this to show the latest news on the HomePage:
In HomePage.php:
// ...
public function LatestNews($num=5) {
$holder = ArticleHolder::get()->First();
return ($holder) ? ArticlePage::get()->filter('ParentID',
$holder->ID)->sort('Date DESC')->limit($num) : false;
}
And this in HomePage.ss:
// ...
public function LatestNews($num=5) {
$holder = ArticleHolder::get()->First();
return ($holder) ? ArticlePage::get()->filter('ParentID',
$holder->ID)->sort('Date DESC')->limit($num) : false;
}
That works very well!
Now my Question: All my News have a Date-Field. Is it possible to show only
the News of the current Date on the HomePage?
I tried this, but this wont work (Server Error) (Datum is my Date of the News):
public function LatestNews($num) {
$holder = ArticleHolder::get()->First();
return ($holder) ? ArticlePage::get()->filter('ParentID', "datum == CURDATE()",
$holder->ID)->sort('Date DESC')->limit($num) : false;
}
Thank you very much for your help!
filter() needs either two values (column and value) or an array of key-value pairs of what to filter. So if you want to filter for more than one thing you need an array as parameter:
$today = date('Y-m-d');
$todaysNews = ArticlePage::get()->filter(array(
'ParentID' => $holder->ID,
'datum' => $today
));
This will return a DataList you can sort and limit like you did in your example.
See also in docs:
Data Model and ORM general overview
Search filters how to filter "greater than" etc...
EDIT:
So a method in your controller could look like:
public function getTodaysNews($num=5) {
$holder = ArticleHolder::get()->First();
$today = date('Y-m-d');
return $holder
? ArticlePage::get()->filter(array(
'ParentID' => $holder->ID,
'datum' => $today
))->sort('Date DESC')->limit($num)
: false;
}
I am working in WordPress. I am using a plugin to get the admin options. The plugin takes an argument as an ID to get the value from the database like so.
$option = ot_get_option('email_address');
The above line of code would return
myEmail#example.com
I want to write a helper function that would get multiple values at once. Normally, I would get the options like this.
$option_1 = ot_get_option('option1');
$option_2 = ot_get_option('option2');
$option_3 = ot_get_option('option3');
I figured there could be a better way that would look a little nicer. I put together this little function that does work
function ritual_get_options($arg_list = array())
{
$options = array();
foreach( $arg_list as $key => $value){
$options[] = ot_get_option($value, array());
}
return $options;
}
using the function above, I can now pass the id's of the options like so
ritual_get_options('option1','option2','option3');
My issue is that the above will return an array with numeric keys. 0,1,2. Here is the exact array that is getting returned when I do a var_dump on the front end
[0]=>
string(16) "100 Main Street,"
[1]=>
string(18) "Hamilton, Ontario."
[2]=>
string(15) "+1 800 999 9898"
[3]=>
string(19) "mail#yourdomain.com"
I want to return the array keys with the value so that I can easily figure out what value is in what key. Here is the line that I used to get the above array
$options = ritual_get_options(array('number' => 'streetnumber','street' => 'street','phone' => 'phone_number','email' => 'email'));
When I go to use the returned values, I want to be able to use the key so that I could do
echo $options['number'];
instead of doing
echo $options[0];
I have not been able to figure out how to return the keys passed in to the function and preserve them into through the return.
Set the option name as key while building the array:
foreach( $arg_list as $option ){
$options[$option] = ot_get_option( $option );
}
Btw, using extract() on the $options array will allow you to use e.g. $phone_number instead of $options['phone_number']
I am trying to build a month report with Codeigniter.
i have problem in parse value to view,
when i enable profiler, i get 12 month query
Controller
$this->load->model('dash_model');
$data1= $this->dash_model->get_user_all();
$ind = $this->dash_model->monthreport();
$this->output->enable_profiler(TRUE);
$data = array(
'mont' => $ind,
'blok' => $data1
);
print_r($data);
$this->parser->parse('blank', $data);
the output print_r data
Array
(
[mont] => Array
(
[0] => stdClass Object
(
[trans_email] => 0
)
)
and dash_model
for($i=1; $i<=12;)
{
$month=array("","01","2","3","4","5","6","7","8","9","10","11","12");
$m = $month[$i];
$query2=$this->db->query("select count(*) as trans_email from trans_email where lup LIKE '2014-$m%' ");
$i++;
}
return $query2->result();
how i get output select count(*) as trans_email from trans_email where lup LIKE '2014-01%' and next month to view ?
like
month 1 = 356 data
month 2 = 2000 data and next
i'm trying this : Codeigniter - passing multiple values to view
but nothing happens
update
i'm trying to add this code into dash_model
$i++;
$resultarray[$i]=$query2->result();
}
return $resultarray;
and i got some error
* Object of class stdClass could not be converted to string*
okay dude let me try to guest :D
let's assume you use array on your view, i can assume that because you initialize $data with array.
First make sure you read this userguide
on result_array() section.
then change $query->result(); to $query->result_array();
then try to var_dump() it, hope it's work
just pass it as
$data['mont'] = $ind;
$data['blok'] = $data1;
$this->parser->parse('blank', $data);
in the view get the data as of $ind as $mont
and $data1 as $blok.
You could do
$data = array();
$data['mont'] = $ind;
$data['blok'] = $data1;
Instead of declaring it then initializing it at the same time. (It also allow you to add/change data in it whenever you want in your controller).
Then do debug($data);to see if you got everything you want in $data.