I have a function
public function add($value1,$value2){
$a = $value1;
$b = $value2;
$c = $a + $b;
return $c;
}
In order to call this function i would use add(1,2)
The problem is :
if i want to be able to use this function on any view page in laravel, where do i place it and how do i call it ?
I have tried using it on page and it works fine, i just need a global solution.
public function add($value1,$value2){
$a = $value1;
$b = $value2;
$c = $a + $b;
return $c;
}
You need to create a helper file.
So create a helper.php file in app. app/helper.php
then in composer.json add the following under autoload:
"files": [
"app/helpers.php"
],
Then dump composer
composer dump-auto
Now you can call your function.
Related
I am using Codeigniter 4 with Oracle. Inside my controller, I have this code:
<?php
namespace App\Controllers;
class Home extends BaseController
{
public function index()
{
$this->db = \Config\Database::connect();
$query = "select * from test";
$this->db->_prepare($query);
$this->db->_execute();
echo "<table border='1'>\n";
$ncols =$this->db->getFieldCount();
echo "<tr>\n";
for ($i = 1; $i <= $ncols; ++$i) {
$colname = $this->db->getFieldNames($s, $i);
echo " <th><b>".htmlspecialchars($colname,ENT_QUOTES|ENT_SUBSTITUTE)."</b></th>\n";
}
echo "</tr>\n";
while (($row = $this->db->fetchAssoc()) != false) {
echo "<tr>\n";
foreach ($row as $item) {
echo "<td>";
echo $item!==null?htmlspecialchars($item, ENT_QUOTES|ENT_SUBSTITUTE):" ";
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
}
}
This code is returning an error: Call to undefined method CodeIgniter\Database\OCI8\Connection::_prepare()
the method CodeIgniter\Database\OCI8\Connection is returning the oci_connect result
How could I make this work?
Error:
Call to undefined method CodeIgniter\Database\OCI8\Connection::_prepare()
Explanation:
The error above is pretty clear. You're trying to call the _prepare(...) method which doesn't exist in the CodeIgniter\Database\OCI8\Connection class.
Solution:
<?php
namespace App\Controllers;
use CodeIgniter\Database\Query;
class Home extends BaseController
{
public function index()
{
$db = db_connect();
// Prepare the Query
$preparedQuery = $db->prepare(static function ($db) {
return (new Query($db))->setQuery('SELECT * FROM test WHERE 1=?');
});
$resultInterface = $preparedQuery->execute(1);
$preparedQuery->close();
$fieldNames = $resultInterface->getFieldNames();
$fieldCount = $resultInterface->getFieldCount();
$resultSet = $resultInterface->getResultArray();
echo "<table border='1'>\n";
echo "<tr>\n";
foreach ($fieldNames as $fieldName) {
echo " <th><b>"
. htmlspecialchars(
$fieldName,
ENT_QUOTES | ENT_SUBSTITUTE
)
. "</b></th>\n";
}
echo "</tr>\n";
foreach ($resultSet as $row) {
echo "<tr>\n";
foreach (range(0, $fieldCount - 1) as $fieldIndex) {
echo "<td>";
echo !empty($row[$fieldNames[$fieldIndex]])
? htmlspecialchars(
$row[$fieldNames[$fieldIndex]],
ENT_QUOTES | ENT_SUBSTITUTE
)
: " ";
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
}
}
Preparing the Query
This can be easily done with the prepare() method. This takes a
single parameter, which is a Closure that returns a query object.
Query objects are automatically generated by any of the “final” type
queries, including insert, update, delete, replace, and get.
This is handled the easiest by using the Query Builder to run a query.
The query is not actually run, and the values don’t matter since
they’re never applied, acting instead as placeholders. This returns a
PreparedQuery object:
If you don’t want to use the Query Builder you can create the Query
object manually using question marks for value placeholders:
Executing the Query
Once you have a prepared query you can use the execute() method to
actually run the query. You can pass in as many variables as you need
in the query parameters. The number of parameters you pass must match
the number of placeholders in the query. They must also be passed in
the same order as the placeholders appear in the original query:
getFieldCount()
The number of FIELDS (columns) returned by the query. Make sure to
call the method using your query result object:
getFieldNames()
Returns an array with the names of the FIELDS (columns) returned by
the query. Make sure to call the method using your query result
object:
i'm trying to store image to my DB using laravel with flutter
this is my flutter code:
and this is what i get in file.path:
here my laravel code:
public function store(Request $request){
dd(request->all);
$input =$request->json()->all();
$doctor = new AcceptDoctor();
echo "before if";
if ($request->hasFile('file')) {
echo "inside if";
$path = $request->file('file')->store('images');
$doctor-> signature_path = $path;
}
$doctor->save();
return $doctor;
}
this is the output:
dd output:
the if statement not working and i try to echo the signature_path is null
i try the same code using html it's work
what should i do?
if ($request->hasFile('file')) {
$path = $request->file('signature_path')->store('images');
$doctor-> signature_path = $path;
}
This piece of code seems conflicting, first you check if file is available, and then you try to access signature_path. My guess, according to your error message, it should be:
if ($request->hasFile('file')) {
$path = $request->file('file')->store('images');
$doctor-> signature_path = $path;
}
if this doesn't work, paste the code:
dd($request->all());
on the very first line of store method and let us know
I need a code sniffer for converting all the variables in all of my PHP file from snake_case to camelCase.
I don't need the functions which do it in php on a string, I want to convert the variables in my PHP files to camelCase.
I appreciate you in advanced.
I found a way to do it.
I integrate the code for cakePHP (There is the file with ctp extension and the variable passed to view by set function).
Save the below code as tocamelcase.php
php tocamelcase.php the/path/of/your/app
file content:
<?php
function snakeToCamel($val) {
preg_match('#^_*#', $val, $underscores);
$underscores = current($underscores);
$camel = str_replace('||||', '', ucwords(str_replace('_', '||||', $val), '||||'));
$camel = strtolower(substr($camel, 0, 1)).substr($camel, 1);
return $underscores.$camel;
}
function convert($str) {
global $j;
$name = '/(\$[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'.
'(->[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'.
'(::[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'.
'(\sfunction\s+[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'.
'(\$this->set\(\'[a-zA-Z0-9]+_[a-zA-Z0-9_]+\'\,)|'.
'(\$this->set\(\"[a-zA-Z0-9]+_[a-zA-Z0-9_]+\"\,)'.
'/i';
$str = preg_replace_callback($name, function($matches){
return snakeToCamel($matches[0]);
},$str);
return $str;
}
$path = $argv[1];
$Iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$i=1;
foreach($Iterator as $file){
if(substr($file,-4) !== '.php' && substr($file,-4) !== '.ctp')
continue;
echo($i.": ".$file."\n");
$i++;
$out = convert(file_get_contents($file));
file_put_contents($file, $out);
}
I need to concatenate variables come from model.I send the role_id from controller to model and get the role name according to its id.
controller:
function get_role_name(){
$data['rec']=$this->amodel->get_section();
foreach($data['rec'] as $i)
{
$name['x']=$this->amodel->get_name($i->role_id);
}
$this->load->view('sections',array_merge($data,$name));
}
I write $name['x'].=$this->amodel->get_name($i->role_id); but it shows the error which undefined index:x.How can I concatenate the role name in cotroller to send it to view?
you probably didnt define $name
$name = array();
// your foreach
foreach ()
btw, the concatenating was ok
$var = "foo";
$var .= "foo"
// will result in "foofoo"
If you want to append something using the .= syntax you need to make sure the variable or array exists first.
Try this:
function get_role_name(){
$data['rec']=$this->amodel->get_section();
$name = array();
foreach($data['rec'] as $i) {
if (isset($name['x'])) {
$name['x'] .= $this->amodel->get_name($i->role_id);
} else {
$name['x'] = $this->amodel->get_name($i->role_id);
}
}
$this->load->view('sections',array_merge($data,$name));
}
I have an method in Code Igniter, and want access using this URL "kuesioner/test/school_id/6/parent_id/7"
I using _remap function to dynamic order of params in URL. Code from this.
This is my code :
public function test($school_id, $parent_id)
{
echo "<pre>";
print_r($schoold_id);
print_r($parent_id);
echo "</pre>";
}
public function _remap($method, $params)
{
$map = array();
for( $i = 1; $i < count( $params ); $i = $i + 2 )
{
$map[$params[$i-1]] = $params[$i];
}
if( $method[0] != '_' && method_exists( $this, $method ))
return $this->$method($map);
}
Getting error on my controller that said Missing argument 2 for Kuesioner::test() ($parent_id is missing), it return array of map into single variable $school_id in test controller.
print_r($school_id) exactly show
Array
(
[school_id] => 6
[parent_id] => 7
)
How to solve this, so that value can be put into every right variable..
Always assign parameter with default values like 0 or anything else it will prevent from unnecessary warning and errors.
public function test($school_id = 0, $parent_id = 0)
{
echo "<pre>";
print_r($schoold_id);
print_r($parent_id);
echo "</pre>";
}
public function test($params = NULL)
{
print_r($this->uri->uri_to_assoc(3));
// #CHECK http://ellislab.com/codeigniter/user-guide/libraries/uri.html
}
Try to replace last line with this:
return call_user_func_array(array($this, $method), $map);
I think that would solve your issue,however if not, you can edit your "test" function to :
public function test($params)
{
echo "<pre>";
print_r($params['schoold_id']);
print_r($params['parent_id']);
echo "</pre>";
}
why you don't try with $this->uri->uri_to_assoc(n).
See detail : http://ellislab.com/codeigniter/user-guide/libraries/uri.html
don't need to use _remap