Insert $i variable in object call in Propel & PHP - propel

I'm currently busy rebuilding our CMS in Propel. I currently have the functions to get several body's from our database but i'm struggling to get a desired one based on a $i variable.
The function is as follows:
for ($i = 0; $i < $item->getColumns(); $i++) {
if (strlen(strip_tags(stripslashes($item->getBody1()))) > 100) {
$body = strip_tags(stripslashes(substr(strip_tags($item->getBody$i()), 0, strpos(strip_tags($item->getBody.$i()), ' ', 100)))) . ' ...';
} else {
$body = stripslashes($item->getBody.$i());
}
}
In the function above you the see code $item->getBody1(). I want the function to use the $i variable to get the desired body, so for example if $i = 2, the getBody function should be $item->getBody2().
I've tried to use $item->getBody.$i() but that doesn't work. Is there a way I could create this?
Any help is appreciated!

This is not an issue with Propel, but with PHP.
Instead of using:
$item->getBody$i();
Use:
$getIthBodyMethod = 'getBody' . $i;
$item->$getIthBodyMethod();
PHP manual on variable methods

Related

-loop, is there easier way to do with carbon?

I'm using Laravel 5.7 and carbon 1.x.
I need to build dateLooper which interval is 5 days and follows calender dates.
I need to find the way build looper which add 5 days for startdate.
ex.
$startDate = "2014-01-01";
$interval = "5";
so $endDate = $startDate + 5 ;
so endDates 2014-01-05
2014-01-10
till 2014-01-30, so this is tricky because need to follow calender.
Next 2014-02-04.
I was reading Carbon but did not find any examples
which could have open solution for my problem.
And I realized that L5.7 is still using 1.x carbon.
I have tried to build double for-loop, but did not work
as it stops after reach end of inside loop.
$month ="13" ; // +1;
$day ="29" ; // +1;
for ($i = 1; $i < $month; $i++)
{
echo "Month: ".$i.'<br/>';
for ($i = 1; $i < $day; $i++)
{
echo "Day: ".$i.'<br/>';
}
}
1) So is there way to do with Carbon?
or is there some other library which I could use? Ideas..
Thanks MikroMike.
I found it from How to add CarbonInterval instance in Carbon instance
$carbon = Carbon::now();
$monthLater = clone $carbon;
$monthLater->addDay(15);
dd($carbon, $monthLater);
This has resolved my issue.

Laravel - Get data based on date range using eloquent

Heei, I want to show data according to daterange. Specifically data on this day and 6 days to go. Here's my code now.
Controller
$hari = [];
for ($i=0; $i < 6; $i++)
{
$hari[] = date("Y M d") + $i;
}
$booking_room = jadwal_meeting::whereBetween('tanggal', [$hari, $hari + 6])->get();
return view('homepage')->with($booking_room);
Note: 'tanggal' is a field on table.
But I just get error like this
Unsupported operand types
: $booking_room = jadwal_meeting::whereBetween('tanggal', [$hari, $hari + 6])->get();
What's wrong with my code, anyone can help me please :)
Since $hari is an array, you have to use something like this:
$booking_room = jadwal_meeting::whereBetween('tanggal', [$hari[0], $hari[5]])->get();
Or more general:
$booking_room = jadwal_meeting::whereBetween('tanggal', [$hari[0], end($hari)])->get();
Try this code it will help you.
$from = '2018-04-12';
$to = date('Y-m-d', strtotime($from. ' + 6 days'));
$reservations = Reservation::whereBetween('tanggal', [$from, $to])
->get();
Thanks,

Laravel chained jobs as array not working. Attempt to assign property of non-object

I'm trying to loop over multiple servers that need to run 1 at a time using Laravel's withChain. The first job completes just fine but the data I'm passing within the chained jobs gives me the
Attempt to assign property of non-object
When I log out the initial dispatched data it looks just like the constructed data in my array so I'm not sure what I'm doing wrong.
$new_jobs_array = [];
foreach ($this->wasRequest->nodes->sortByDesc('pivot.node_type') as $node) {
if ($node->pivot->node_type != 'WAS_DMGR')
{
$snode = strtolower($node->hostname);
$shortname = strtok($snode, '.');
$fileName = strtolower($mnemonic).'_'.$shortname.'_'.$reqId.'.json';
$sourceJsonPath = base_path() . "/json/was/" . $fileName;
$new_job = 'new BootStrapWasNode('. $node .', '. $this->wasRequest .', '.$sourceJsonPath.')';
array_push($new_jobs_array, $new_job);
} else {
$dmgr_node = $node;
}
}
//Log::info($new_jobs_array);
$dmgr_node_sname = strtok($this->wasRequest->nodes->where('pivot.node_type', 'WAS_DMGR')->pluck('hostname')[0], '.');
$fileName = strtolower($mnemonic).'_'.$dmgr_node_sname.'_'.$reqId.'.json';
$sourceJsonPath = base_path() . "/json/was/" . $fileName;
$this->wasRequest->status = 'Bootstrapping Nodes';
$this->wasRequest->save();
//Log::info("DMGR-------------------".$dmgr_node.", ".$this->wasRequest.", ".$sourceJsonPath);
BootStrapWasNode::withChain($new_jobs_array)->dispatch($dmgr_node, $this->wasRequest, $sourceJsonPath);
I can attach the log view if needed but there is a lot of data for each node. The issue is with the $new_nodes_array, the initial dispatch($dmgr_node,$this->wasRequest,$sourceJsonPath) completes without issue.
Was able to figure out the issue.
This line was incorrect
$new_job = 'new BootStrapWasNode('. $node .', '. $this->wasRequest .','.$sourceJsonPath.')';
It should be
$new_job = new BootStrapWasNode($node, $this->wasRequest, $sourceJsonPath);

Php Generate random number without repeat

$C = $_POST['Cc'];
$X = $_POST['X'];
$CX = $_POST['Cc'] . $_POST['X'];
$NC = preg_replace_callback("/x/" ,function() {return rand(0,9);}, $CX);
$New = $NC ;
$NNew = str_repeat($New,10);
echo $NNew;
what's wrong when i output it , it gives me the same number How to make It Don't Give me the same Numbers ??
It's basically you are not changing the seed for the rand method. Each time it's getting same seed and generating same number.
Read this PHP manual : http://php.net/manual/en/function.srand.php
Check the code snippet below:
<?php
// seed with microseconds
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return $sec + $usec * 1000000;
}
srand(make_seed());
$randval = rand(0,9);
echo $randval;
?>
Or you can use mt_rand() which is seeded differently on each execution.

Laravel Saving data

In my Laravel project I use this code into controller method to save data:
$countCourseTeacher++;
$routine->p1=Input::get('course_teacher' . $countCourseTeacher);
$routine->p2=Input::get('course_teacher' . $countCourseTeacher);
$routine->p3=Input::get('course_teacher' . $countCourseTeacher);
But I want to use $routine->p$i into a for loop Like
for ($i=1; $i <=6; $i++) {
$countCourseTeacher++;
$routine->p$i=Input::get('course_teacher' . $countCourseTeacher);
}
When I use $routine->p$i
instead of using
$routine->p1 , $routine->p2, $routine->p3.....
It gives error. Is there any way to do this into a forloop using p$i???
That is the normal behaviour in PHP. You can use interpolation. If you want to use interpolation for object properties/methods, use brackets around the string. Here are a quick example:
for ($i=1; $i <=6; $i++) {
$countCourseTeacher++;
$routine->{"p$i"} = Input::get('course_teacher'.$countCourseTeacher);
}
Any Way I find My answer myself. I use thos code below
for ($j=1; $j <=6; $j++) {
$routine=new RoutineCreate;
$routine->configuration_id=$idcnfg;
$routine->day=Input::get('day' . $j);
for ($i=1; $i <=10; $i++) {
$pi="p" . $i;
$countCourseTeacher++;
$routine->$pi=Input::get('course_teacher' . $countCourseTeacher);
}
$routine->save();

Resources