I stored a Controller#Action in the database, but when I try to run it it don't has value.
I know that eval is very dangerous. If there is an alternative what I can use in this situation I'm open for it.
My code where I wan't to run it:
//$action = "ExchangeController::module_getAllValutaByBank";
//$params = "K&H";
$test = eval('\App\Http\Controllers\ModuleControllers' . "\\" . $action . "('" . $params . "');");
Debugbar::info($test);
The code what I want to eval:
It's an API request to the local ForexChange site. #param bank's name
#return stuctured string
public static function module_getAllValutaByBank($bankName){
$return = '';
$data['bankName'] = $bankName;
$response = self::getRequest($data);
if(is_array($response)){
$return .= $response[0]['bank'] . "\n";
foreach($response as $key => $item){
$return .= $item['valuta'] . " - " . round($item['buy'], 2) . " - " . round($item['sell'], 2) . "\n";
}
} else {
$return = $response;
}
Debugbar::info($return);
return $return;
}
Output of Debugbar::info($return);
K&H Bank GBP - 338.51 - 363.07 AUD - 189.9 - 207.8 DKK - 39.75 - 43.93
JPY - 2.26 - 2.48 CAD - 188.73 - 206.51 NOK - 30.92 - 34.18 CHF -
256.89 - 275.53 SEK - 29.18 - 32.26 USD - 246.14 - 260.32 CZK - 11.51 - 12.97 PLN - 69.87 - 78.79 EUR - 302.92 - 320.38 HRK - 39.39 - 44.41
Output of Debugbar::info($test);
null
Where it went wrong?
EDIT:
Found the solution:
$test = call_user_func('\App\Http\Controllers\ModuleControllers' . "\\" . $action, $params));
Found the solution:
$test = call_user_func('\App\Http\Controllers\ModuleControllers' . "\\" . $action, $params));
Related
I'm trying to upload an image file to the database. But, when it posted, it shows a full path of the image file (I use the getClientOriginalName() to get the filename).
DSController.php
public function createDS2(Request $request)
{
//dd($request->all());
$imgName1 = $request->file('opsi_a')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_a')->extension();
$imgName2 = $request->file('opsi_b')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_b')->extension();
$imgName3 = $request->file('opsi_c')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_c')->extension();
$imgName4 = $request->file('opsi_d')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_d')->extension();
$data = new DS2;
$data->pertanyaan = $request->input('pertanyaan');
$data->opsi_a = $request->file('opsi_a')->move(public_path('image'), $imgName1);
$data->opsi_d = $request->file('opsi_b')->move(public_path('image'), $imgName2);
$data->opsi_b = $request->file('opsi_c')->move(public_path('image'), $imgName3);
$data->opsi_c = $request->file('opsi_d')->move(public_path('image'), $imgName4);
$data->kunci = $request->input('kunci');
$data->save();
return redirect('ds2');
}
the image file in the database will show something like this :
C:\xampp\htdocs\admin_kuis\public\image\hiu_press.png-1614165665.png
how I can just save the hiu_press.png-1614165665.png in my database?
The same way as #zia-yamin but different syntax.
$imgName1 = $request->file('opsi_a')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_a')->extension();
$imgName2 = $request->file('opsi_b')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_b')->extension();
$imgName3 = $request->file('opsi_c')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_c')->extension();
$imgName4 = $request->file('opsi_d')->getClientOriginalName() . '-' . time() . '.' . $request->file('opsi_d')->extension();
$request->file('opsi_a')->move(public_path('image'), $imgName1);
$request->file('opsi_b')->move(public_path('image'), $imgName2);
$request->file('opsi_c')->move(public_path('image'), $imgName3);
$request->file('opsi_d')->move(public_path('image'), $imgName4);
$data = new DS2;
$data->pertanyaan = $request->input('pertanyaan');
$data->opsi_a = pathinfo($imgName1, PATHINFO_BASENAME);
$data->opsi_b = pathinfo($imgName2, PATHINFO_BASENAME);
$data->opsi_c = pathinfo($imgName3, PATHINFO_BASENAME);
$data->opsi_d = pathinfo($imgName4, PATHINFO_BASENAME);
$data->kunci = $request->input('kunci');
$data->save();
return redirect('ds2');
Use This Code:
if ($request->hasFile('file_name')) {
$file_name = $request->file('file_name');
$file_name = time() . $file_name->getClientOriginalName();
}
store your file full path in $stored_file variable and then use this one:
$data->opsi_d = $request->file('opsi_b')->move(public_path('image'), pathinfo($imgName2)['basename']);
Also, you can use:
$file = Input::file('image');
$fileName = $file->move(public_path('image'), $imgName1->getOriginalFileName());
$fileName->image = $fileName ->getRealPath();
I try to use a function which calculate distance between $user lat/lng and coordinates in my BDD with limited distance.
It's an SQL request and I try to use Doctrine to implement it.
Here is my code
$config = new \Doctrine\ORM\Configuration();
$config->addCustomNumericFunction('COS', 'DoctrineExtensions\Query\Mysql\Cos');
$config->addCustomNumericFunction('ACOS', 'DoctrineExtensions\Query\Mysql\Acos');
$config->addCustomNumericFunction('RADIANS', 'DoctrineExtensions\Query\Mysql\Radians');
$config->addCustomNumericFunction('SIN', 'DoctrineExtensions\Query\Mysql\Sin');
$maxLat = $form_citylat + rad2deg($rad / $R);
$minLat = $form_citylat - rad2deg($rad / $R);
$maxLng = $form_citylng + rad2deg(asin($rad / $R) / cos(deg2rad($form_citylat)));
$minLng = $form_citylng - rad2deg(asin($rad / $R) / cos(deg2rad($form_citylat)));
$qb = $this->createQueryBuilder('u')->select('u.lat, u.lng')
->addSelect('acos(sin(:lat)*sin(radians(u.Lat)) + cos(:lat)*cos(radians(u.Lat))*cos(radians(u.lng)-:lng)) * :R As D')
->where('lat Between :minlat And :maxlat And lng Between :minlng And :maxlng And acos(sin(:lat)*sin(radians(u.Lat)) + cos(:lat)*cos(radians(u.Lat))*cos(radians(u.Lng)-:lng)) * :R < :rad')
->setParameter('lat',deg2rad($form_citylat))
->setParameter('lng',deg2rad($form_citylng))
->setParameter('minlat',$minLat)
->setParameter('minlng',$minLng)
->setParameter('maxlat',$maxLat)
->setParameter('maxlng',$maxLng)
->setParameter('rad',$rad)
->setParameter('R',$R)
->orderBy('D');
return $qb->getQuery()->getResult();`
But I got this error message :
[Syntax Error] line 0, col 40: Error: Expected
Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got '.'
I tried different options but it doesn't work.
Anyone has an answer please ?
public function findByThemeAndDistance($theme,$distance,$user){
$latUser=$user->getAddress()->getLnt();
$lngUser = $user->getAddress()->getLgt();
return $this->createQueryBuilder('a')
//->select('a as activity,dist('.$distance.') as distance' )
->join('a.author','u')
->join('u.address','add')
->andWhere('a.theme=:val')
->andWhere( '(6378 * acos(cos(radians( add.lnt)) * cos(radians(' . $latUser . ')) * cos(radians(' . $lngUser . ') - radians(add.lgt)) + sin(radians(add.lnt )) * sin(radians(' . $latUser . '))))< :distance')
->setParameter('distance', $distance)
->setParameter('val',$theme)
->orderBy( 'a.author','ASC')
->getQuery()
->getResult();
}
Let me start by saying I am NOT a programmer. I'm a retail web manager that knows enough about HTML5 to understand what is going on. Ok now on to my issue. We recently upgraded our eCommerce platform from 3DCart to Magento. It's a completely different monster and I'm fairly lost. I'm trying to integrate Magento's simple pixel (just returns the total not the individual items) into our confirmation page but all of our tests are failing. I've tried bits and pieces of other codes that I've found around the web but I'm still missing the "amount" parameter. Can anyone help me? Below is what we have on our site now (please note this is part of the copy/paste code I've found):
//-------------------------------------------
// START CJ CONVERSION TRACKING PIXEL
//-------------------------------------------
$cjmerchID = '1521251';
$cjaid = '382643';
$cjorder = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$cjitems = $cjorder->getAllItems();
$cjorderID = $cjorder->getIncrementId();
//$cjsubtotal = round($cjorder->getSubtotal(), 2);
$i = 1;
foreach ($cjitems as $itemId => $item)
{
$unitPrice = round($item->getPrice(), 2);
$sku = $item->getSku();
$qty = $item->getQtyToInvoice();
//echo $qty . '<br>';
$itemsStr .= '&ITEM;' . $i . '=' . $sku . '&AMT;' . $i . '=' . $unitPrice . '&QTY;' . $i . '=' . $qty . '';
$i++;
}
?>
?<img src="https://www.emjcd.com/u?CID=<?php echo $cjmerchID; ?>&OID;=<?php echo $cjorderID; ?>&TYPE;=<?php echo $cjaid; ?><?php echo $itemsStr; ?>&CURRENCY;=USD&METHOD;=IMG" height="1" width="20">
<?php
//-------------------------------------------
// END CJ CONVERSION TRACKING PIXEL
//-------------------------------------------
According to CJ this is what I'm doing wrong:
Thank you for providing the results of your test. I am seeing the pixel calls on our server. However, both tests failed as the incorrect Action ID is being used and the 'AMOUNT' parameter has no associated value and is being passed back blank. I've attached the integration instructions for your convenience.
The Action ID for the simple action that should be integrated to replace the existing pixel is 382643.
Integration Test
Advertiser: 3448671
Ad: 12313358
Action Id: 346589
Action Type: item_sale
Query String: AMOUNT=&OID=100056687&CID=1521251&CURRENCY=USD&METHOD=IMG&TYPE=346589
Sid: TrackingTest
Surfer: 476602316150531682:VJXkXAhFHzU2 Click Ref:
Action Status: active
img src="https://www.emjcd.com/u?AMOUNT=&CID=1521251&OID=100056687&TYPE=346589&CURRENCY=USD&METHOD=IMG" height="1" width="20"
Please update the pixel to pull in the subtotal (pre-taxed amount of purchase) and to have 'TYPE' populated with 382643.
Any help you can give would me most appreciated!
Diana
See if this code works. what i did is get the subtotal of total checkout order and append a new parameter in img href with AMOUNT. Let me know how the results work out
//-------------------------------------------
// START CJ CONVERSION TRACKING PIXEL
//-------------------------------------------
$cjmerchID = '1521251';
$cjaid = '382643';
$cjorder = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$cjitems = $cjorder->getAllItems();
$cjorderID = $cjorder->getIncrementId();
//New Codee
$totals = Mage::getSingleton('checkout/cart')->getQuote()->getTotals();
$subtotal = $totals["subtotal"]->getValue();
//$cjsubtotal = round($cjorder->getSubtotal(), 2);
$i = 1;
foreach ($cjitems as $itemId => $item)
{
$unitPrice = round($item->getPrice(), 2);
$sku = $item->getSku();
$qty = $item->getQtyToInvoice();
//echo $qty . '<br>';
$itemsStr .= '&ITEM;' . $i . '=' . $sku . '&AMT;' . $i . '=' . $unitPrice . '&QTY;' . $i . '=' . $qty . '';
$i++;
}
?>
?<img src="https://www.emjcd.com/u?CID=<?php echo $cjmerchID; ?>&OID;=<?php echo $cjorderID; ?>&TYPE;=<?php echo $cjaid; ?>&AMOUNT;=<?php echo $subtotal; ?><?php echo $itemsStr; ?>&CURRENCY;=USD&METHOD;=IMG" height="1" width="20">
<?php
//-------------------------------------------
// END CJ CONVERSION TRACKING PIXEL
//-------------------------------------------
I am parsing a CSV file to add records to a table. this is how I do it.
public function parse_csv($file) {
$this->load->library('CSVReader');
$csvData = $this->csvreader->parse_file($file);
foreach($csvData as $key => $row) {
$data_n[$key] = array(
'mf_date' => $row['mf_date'],
'mf_student_id' => $row['mf_student_id'],
'mf_sender' => $row['mf_sender'],
'mf_amount' => $row['mf_amount'],
'mf_trx_id' => $row['mf_trx_id'],
);
$this->db->insert('monthly_fee', $data_n[$key]);
$this->student_model->update_monthly_due($row['mf_student_id'], $row['mf_amount']);
}
}
I wish to update another table where there is three columns
1)md_student_id //unique
2)md_due
3)md_paid
I want to update that table while uploading the csv where mf_student_id and md_student_id matches with the model function below.
public function update_monthly_due($mf_student_id, $mf_amount)
{
$this->db->select('mf_student_id');
$this->db->from('monthly_fee');
$this->db->join('monthly_due', 'monthly_due.md_student_id = monthly_fee.mf_student_id');
$sql = "UPDATE monthly_due set md_due = md_due - " . $mf_amount . ", md_paid = md_paid + " . $mf_amount . " WHERE md_student_id = " . $mf_student_id;
$this->db->query($sql);
}
But when I upload a csv it generates the following error.
Error Number: 1054
Unknown column 'MCS20145B41' in 'where clause'
UPDATE monthly_due set md_due = md_due - 5500, md_paid = md_paid + 5500 WHERE md_student_id = MCS20145B41
Filename: F:\xampp\htdocs\student\system\database\DB_driver.php
Line Number: 330
What I am doing wrong?!
Thanks in advance
you should replace this line as follow
$sql = "UPDATE monthly_due set md_due = md_due - " . $mf_amount . ", md_paid = md_paid + " . $mf_amount . " WHERE md_student_id = '" . $mf_student_id . "'";
take attention to the end of this line
Why not use CI's active record instead of raw SQL queries.When doing it do it the correct way. This below:
Change this,
$sql = "UPDATE monthly_due set md_due = md_due - " . $mf_amount . ", md_paid = md_paid + " . $mf_amount . " WHERE md_student_id = " . $mf_student_id;
$this->db->query($sql);
To,
$this->db->where('md_student_id', $mf_student_id);
$this->db->set('md_due', "md_due - $mf_amount", FALSE);
$this->db->set('md_paid', "md_paid + $mf_amount", FALSE);
$this->db->update('monthly_due');
Explanation:
The reason this works is because the third (optional) FALSE parameter tells CodeIgniter not to protect the generated query with backticks ('). This means that the generated SQL will be:
UPDATE monthly_due set md_due = md_due - 5500, md_paid = md_paid + 5500 WHERE md_student_id = "MCS20145B41"
I making a webadmin system and I want to monitor the CPU and RAM usage of a process in PHP
Can anyone help me?
"Counters and Logs to Monitor" => http://support.microsoft.com/kb/300504
Save as CSV and read it() by PHP
[OR]
http://pecl.php.net/package/win32ps
[OR]
Use WMI:
<?PHP
#error_reporting(1);
$wmi = new COM("WinMgmts:{impersonationLevel=impersonate}") ;
$cpus = $wmi->ExecQuery("SELECT LoadPercentage FROM Win32_Processor");
foreach ($cpus as $cpu) :
echo $cpu->LoadPercentage . '%';
endforeach;
?>
Use this it works perfectly. Combine this with AJAX and a JAVASCRIPT timer so you get every xy seconds the usage of your cpu&ram.
function get_server_cpu_usage(){
$load = sys_getloadavg();
return $load[0];
}
function get_server_memory_usage(){
$free = shell_exec('free');
$free = (string)trim($free);
$free_arr = explode("\n", $free);
$mem = explode(" ", $free_arr[1]);
$mem = array_filter($mem);
$mem = array_merge($mem);
$memory_usage = $mem[2]/$mem[1]*100;
return $memory_usage;
}
echo '<h4>Server Memory usage: ' . number_format(get_server_memory_usage(), 2) . '%</h4><span style="width:' . get_server_memory_usage() . '%"></span<br>
<h4>Server CPU usage: ' . get_server_cpu_usage() . '% </h4><span style="width:' . get_server_cpu_usage() . '%"></span>';