I have 4 files. I am trying pass my fetch results through a function to another page. My code is below:
File actions.php
public function getAction($action, $page, Array $vars){
if(!empty($action)){
$action = strtolower($action);
$path = $page.'/'.$action.'.php';
$currentTemplate = $this->loadActionTemplate($path);
return Array($currentTemplate, $vars);
}
else{
$action = strtolower($action);
$path = $page.'/default.php';
$currentTemplate = $this->loadActionTemplate($path);
return ($currentTemplate);
}
}
File news.php(controller)
$file = VIEWS_PATH.strtolower($this->template) . '.php';
if (file_exists($file)){
$currentQuery = $newsModel->viewQuery($selectQuery,$returnAll);
include_once($file);
}
}
File news.php(Main view)
$factory = new Actions_Factory();
$factory->getAction($action, $page, $currentQuery);
File view.php(sub View)
print_r($currentQuery);
I can't get $currentQuery to print out the mysql dump on view.php but $currentQuery prints out fine on news.php. I am doing something wrong and can't figure out what it is.
Any help would be much appreciated.
Thanks in Advance
$factory->getAction($action, $page, $currentQuery);
print_r($currentQuery);
You set $currentQuery to getAction(), but this function return result if you want function affect on argument you have to use assign by reference!
Insert "&" in $var to assign variable by reference
public function getAction($action, $page, Array **&$vars**){
Related
I've retried solving this, by using a condition and a default attribute as recommended.
User-generated data is declared before to $Variable_1:
<?php
$Variable_1 = 'abc123!' //The user inputs the data
if ($booleanvalue == true) { // User selects if they've put data
name($user_data, $Variable_0 = $Variable_1 );
}
//Then the function will use the user's data from $Variable_1
function name($user_data, $Variable_0 = null) {
//Other code...
}
$Variable_2 = name($user_data);
$data['variable_2'] = $Variable_2;
?>
Is it possible to have $Variable_0 pre-declared and then put as an argument?
you have a few mistakes in your code. and I don't think that you can use a function named name.
you could do it this way for example:
<?php
$Variable_1 = 'abc123!';
function test($data) {
global $Variable_1;
//Other calculations...
return $Variable_1 . $data;
}
$testdata = "huhu";
$Variable_2 = test($testdata);
$data['variable_2'] = $Variable_2;
echo $data['variable_2'];
?>
I agree with the comment by El_Vanja, but you can access a global variable through the magic $GLOBALS array anywhere.
<?php
// what you might actually want
function name($variable = 'abc123!')
{
// if no value is passed into the function the default value 'abc123!' is used
}
$variable = 'abc123!';
// what you could do
function name2($variable)
{
// $variable can be any value
// $globalVariable is 'abc123!';
$globalVariable = $GLOBALS['variable'];
}
I'd also like to point out that currently you have no way of controlling what type of data is passed to the function. You might consider adding types.
<?php
<?php
// string means the variable passed to the function has to be a ... well string
function name(string $variable = 'abc123!'): void
{
// void means the function doesn't return any values
}
name(array()); // this throws a TypeError
I am working on a site where I have a map for each city I need e.g 'London.blade.php', 'Paris.blade.php'.
I a trying to implement a search bar on the site and I need it to return the views(blade.php pages) as the results, not contents of the views.
e.g where('views', 'LIKE', "%$search_query%").Is this actually possible as the views are not stored in the database, if so does anyone know how to go about this?
Here's a general-purpose function to list any file with the extension you specify, in this case, .blade.php. It starts searching at $path and then recursively searches any sub-directories it finds.
function findFileExtension_r($extension, $path, &$names = array()){
$files = array_diff(scandir($path), array('..', '.'));
foreach($files as $f){
$abs_path = $path.'/'.$f;
if(is_dir($abs_path)){ //directory, recurse
findFileExtension_r($extension, $abs_path, $names);
} else { //file, test if the name ends with $extension
$ext_length = strlen($extension);
if(substr($f, -$ext_length) === $extension){
$names[] = $f;
}
}
}
}
Usage:
//set up some values
$find = '.blade.php';
$path = 'path/to/laravel/resources/views';
//$blade_templates = array(); //can be initialized, or named in the function call below.
//call function
findFileExtension_r($find, $path, $blade_templates);
//output
echo '<h2>Results</h2><pre>' . print_r($blade_templates, true) . '</pre>';
You can actually store your blade templates in the database and query them via SQL: https://github.com/delatbabel/viewpages
If the views are not stored in the db, you'll have to point to the view in the routes and you can use a dynamic route.
Route::get('/{slug}', array('as' => 'page.show', 'uses' => 'PageController#show'));
The show function:
public function show()
{
$slug = input::get('location');
$page = page::whereSlug($slug)->get();
return View::make('pages')->with('page', $page);
}
I want to make html file that while be a ip adress and insert what client do via $_SERVER
My problem is that i cant make table in that file so code is this
FTP
public static function Write($Wfile, $Wtext)
{
$open = fopen($Wfile, "w+");
fwrite($open, $Wtext);
}
File to create log
public function __construct()
{
chdir("Log");
$this->_file = $_SERVER['REMOTE_ADDR'] .".html";
if(!is_file($this->_file)){
ftpFile::Write($this->_file,$this->Standards());
}
public function Standards()
{
$html = "<html>\r\n <body>\r\n <table cellpadding='10'>";
return $html;
}
AND WHAT I WANT TO INSERT NOW
public function Set()
{
$indicesServer = array(
'PHP_SELF',
'argv',
'argc',
'GATEWAY_INTERFACE',
'SERVER_ADDR',
'SERVER_NAME',
......
foreach ($indicesServer as $arg){
return '<tr><td>'.$arg.'</td><td>' . $_SERVER[$arg] . '</td></tr>';
}
so i try return, echo, print, file put content and i only get one result and that is last in my array.
ONCE Again i want to create log for user that come to my site and everthing inside $_SERVER write one time when sesion_id active and every where client go i want to insert and what $_POST insert in that file. Many of that i do only this is need to be fixed... TNX all
Your bottom code snippet doesn't work because you are attempting to return inside foreach loop: you can only return from a function once. Try this:
public function Set()
{
$indicesServer = array(
'PHP_SELF',
'argv',
'argc',
'GATEWAY_INTERFACE',
'SERVER_ADDR',
'SERVER_NAME',
......
$ret = "";
foreach ($indicesServer as $arg)
$ret .= '<tr><td>'.$arg.'</td><td>' . $_SERVER[$arg] . '</td></tr>';
return $ret;
}
I'm currently a beginner developer and have just started my first big project whilst I have spare time, What I'm trying to do is basically write variables to a html/tpl document, Which I have currently got working, Here is my code:
private function index(){
$username = 'MyUsername';
$onlineTime = 'MyOnlineTime';
$this->setParams('Username', $username); // $username Will be replaced by database queried results once completed.
}
And here is the setParams function.
function setParams($item1, $item2){
ob_start();
$theme = 'default';
include_once T . '/'.$theme.'/index.php'; // T . is defined at the beginning of the document.
if ((($html = ob_get_clean()) !== false) && (ob_start() === true))
{
echo preg_replace('~{(['.$item1.']*)}~i', ''.$item2.'', $html, 1);
}
}
And here is the coding inside the html/tpl document.
{username} has been online for {onlineTime} Hours
This is probably a very simple code for some of you but as this is my first attempt this is all I can do.
What I would like to do is have it so you can setParams as many times as you want without changing the $variable names like so:
private function index(){
$username = 'MyUsername';
$onlineTime = 'MyOnlineTime';
$this->setParams('Username',$username);
$this->setParams('OnlineTime', $onlineTime);
}
whilst keeping the setParams($item1, $item2)
But as you can imagine this just cuts the code completely. Does anyone know a solution to this problem? I've been searching all day without any real luck.
Thanks In Advance,
Ralph
I think what you need is a class with a static method;
<?php
class Params {
public static $params = array();
public static function setParam($key, $value) {
self::$params[$key] = $value;
}
public static function getParam($key) {
if (isset(self::$params[$key])) {
return self::$params[$key];
}
}
}
// Usage
// Set Username
Params::setParam("username", "JohnDoe");
Params::setParam("password", "12345");
echo Params::getParam("username");
echo Params::getParam("password");
I have created my own little PHP framework for fun, however, I am having trouble passing variables from bootstrap to the views....
if I put an echo,print_r,var_dump my target variable in the bootstrap, the output is displayed in the browser before the tag... yet the target var in bootstrap.php is not available in the view, it is coming up as "" even though at the top of the page it is being output correctly....
Somethings I noticed from similar questions:
- The target variable is not being over written
- The include target path is correct and the file exists
- The file is only being included one time (include_once is only fired once)
Any ideas are greatly appreciated, I am pulling my hair out over here lol...
Source Code
https://gist.github.com/jeffreyroberts/f330ad4a164adda221aa
If you just want to display your site name, I think you can use a constant like that :
define('SITE_NAME', "Jeff's Site");
And then display it in your index.tpl :
<?php echo SITE_NAME; ?>
Or, you can send your variables to the view by extending a little bit your JLR_Core_Views :
class JLR_Core_Views
{
private $data;
public function loadView($templatePath, $data = array())
{
$this->data = $data;
$templatePath = JLR_ROOT . '/webroot/' . $templateName . '.tpl';
if(file_exists($templatePath)) {
// Yes, I know about the vuln here, this is just an example;
ob_start();
include_once $templatePath;
return ob_get_clean();
}
}
function __get($name)
{
return (isset($this->data[$name]))
? $this->data[$name]
: null;
}
}
Then, you can call your template like that :
$view = new JLR_Core_Views();
$view->loadView("index", array("sitename" => "Jeff's Site"));
And here is your index.tpl :
<?php echo $this->siteName; ?>
Below is another example of what you can do.
First, you create this class in order to store all the variables you want :
<?php
class JLR_Repository {
private static $data = array();
public function set($name, $value) {
self::$data[$name] = $value;
}
public function get($name) {
return (isset(self::$data[$name]))
? self::$data[$name]
: null;
}
}
?>
Then, when you want to store something in it :
JLR_Repository::set("sitename", "Jeff's Site");
And in your index.tpl :
<?php echo JLR_Repository::get("sitename"); ?>
try using the 'global' keyword - http://php.net/manual/en/language.variables.scope.php