I am trying to use a geoplugin (that code works) but I may wish to use the output in multiple locations. Because of this I've taken the code and added it to the header.php file and called the variable within my theme/inc/partials/header-contact.php. But the value is not displaying:
header-contact.php
<div class="head-contact">
<?php $details = get_field('options_company_details', 'options');
global $telephoneNumber; global $telephoneLink;?>
<span class="phone"><span class="phone-mob"><i class="fa fa-phone"></i></span><span class="tnumber"></span><?php echo $telephoneNumber;?></span>
<p class="tagline"><?php echo $details['options_tagline'];?></p>
</div>
header.php
<?php include_once(get_stylesheet_directory_uri().'/inc/geolocation.php');?>
var_dump in header-contact.php
NULL
var_dump in header.php
NULL
geolocation.php
function getUserIP() {
$client = #$_SERVER['HTTP_CLIENT_IP'];
$forward = #$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP)){$ip = $client;}
elseif(filter_var($forward, FILTER_VALIDATE_IP)){$ip = $forward; }
else { $ip = $remote; } return $ip;
} $user_ip = getUserIP();
$details = get_field('options_company_details', 'options');
$telephoneNumber = $details['options_telephone'];
$getGeoArray = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$user_ip.''));
if(in_array($getGeoArray['geoplugin_city'], $lincolnshire)){$telephoneNumber = 'NUMBER';}
elseif(in_array($getGeoArray['geoplugin_city'], $rutland)){$telephoneNumber = 'NUMBER';}
elseif(in_array($getGeoArray['geoplugin_city'], $northamptonshire)){$telephoneNumber = 'NUMBER';}
elseif(in_array($getGeoArray['geoplugin_city'], $norfolk)){$telephoneNumber = 'NUMBER';}
elseif(in_array($getGeoArray['geoplugin_city'], $cambridgeshire)){$telephoneNumber = 'NUMBER';}
elseif(in_array($getGeoArray['geoplugin_city'], $buckinghamshire)){$telephoneNumber = 'NUMBER';}
elseif(in_array($getGeoArray['geoplugin_city'], $bedfordshire)){$telephoneNumber = 'NUMBER';}
elseif(in_array($getGeoArray['geoplugin_city'], $hertfordshire)){$telephoneNumber = 'NUMBER';}
elseif(in_array($getGeoArray['geoplugin_city'], $essex)){$telephoneNumber = 'NUMBER';}
elseif(in_array($getGeoArray['geoplugin_city'], $leicestershire)){$telephoneNumber = $details['options_telephone'];}
$telephoneLink = str_replace(' ', '', $telephoneNumber);
I've removed the variables used in the in_array() just to save question length.
var_dump in geolaction.php
NUMBER
I ended up using $_SESSION[] not quite sure if this is the best way to do this but it works so I am happy:
$_SESSION['phonenumber'] = $telephoneNumber;
$_SESSION['phonenumberLink'] = $telephoneLink;
echo $_SESSION['phonenumber'];
echo $_SESSION['phonenumberLink'];
This is untested but this may work if called with wp_head or init. This might make the global keyword work in the template parts.
// functions.php
// load at wp_head
add_action('wp_head', function() {
include_once(get_stylesheet_directory_uri().'/inc/geolocation.php');
});
// or
add_action('init', function() {
include_once(get_stylesheet_directory_uri().'/inc/geolocation.php');
});
Then you don't need to include it in your header file or have to use sessions.
Related
How to count total numbers of images inside our folders and subfolders?
I put this in my view. Well, as MVC way, this may look sucks to be in view. I know how to put this into models, but don't know how to call it with controllers and views
<?php
$img = count(glob("./assets/images/*.*"));
$about = count(glob("./assets/images/aboutus/*.*"));
$blog1 = count(glob("./assets/images/blog/*.*"));
$mason = count(glob("./assets/images/blog/masonary/*.*"));
$tl = count(glob("./assets/images/blog/timeline/*.*"));
$blog2 = count(glob("./assets/images/blogdetails/*.*"));
$gallery = count(glob("./assets/images/gallery/*.*"));
$home = count(glob("./assets/images/home/*.*"));
$home2 = count(glob("./assets/images/home/slider/*.*"));
$ico = count(glob("./assets/images/ico/*.*"));
$lb = count(glob("./assets/images/lightbox/*.*"));
$keg = count(glob("./assets/images/kegiatan/*.*"));
$port1 = count(glob("./assets/images/portfolio/*.*"));
$port2 = count(glob("./assets/images/portfolio-details/*.*"));
$leader = count(glob("./assets/images/leaders/*.*"));
$srv = count(glob("./assets/images/services/*.*"));
$usr = count(glob("./assets/images/users/*.*"));
$count = $img+$about+$blog1+$mason+$tl+$blog2+$gallery+$home+$home2+$ico+$lb+$keg+$port1+$port2+$leader+$srv+$usr;
?>
Output:
<div class="col-sm-3 text-center wow bounceIn" data-wow-duration="1000ms" data-wow-delay="300ms">
<h1 class="timer bold" data-to="<?= $count;?>" data-speed="3000" data-from="0"></h1>
<h3>Total Images</h3>
</div>
Is there a way to make this simple?
Please take a look at
https://stackoverflow.com/a/10895775/7089527
You could do it like this using the [RecursiveDirectoryIterator][1]
<?php
function scan_dir($path){
$ite=new RecursiveDirectoryIterator($path);
$bytestotal=0;
$nbfiles=0;
foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) {
$filesize=$cur->getSize();
$bytestotal+=$filesize;
$nbfiles++;
$files[] = $filename;
}
$bytestotal=number_format($bytestotal);
return array('total_files'=>$nbfiles,
'total_size'=>$bytestotal,'files'=>$files);
}
$files = scan_dir('./');
echo "Total: {$files['total_files']} files, {$files['total_size']} >bytes\n";
//Total: 1195 files, 357,374,878 bytes
?>
[1]: http://php.net/manual/en/class.recursivedirectoryiterator.php
Hope it helps
Format to call a method in a model from a controller.
Model file:
application/models/Photo_shoot_model.php
class Photo_shoot_model extends CI_Model {
public function image_count() {
$img = count(glob("./assets/images/*.*"));
$about = count(glob("./assets/images/aboutus/*.*"));
$blog1 = count(glob("./assets/images/blog/*.*"));
$mason = count(glob("./assets/images/blog/masonary/*.*"));
$tl = count(glob("./assets/images/blog/timeline/*.*"));
$blog2 = count(glob("./assets/images/blogdetails/*.*"));
$gallery = count(glob("./assets/images/gallery/*.*"));
$home = count(glob("./assets/images/home/*.*"));
$home2 = count(glob("./assets/images/home/slider/*.*"));
$ico = count(glob("./assets/images/ico/*.*"));
$lb = count(glob("./assets/images/lightbox/*.*"));
$keg = count(glob("./assets/images/kegiatan/*.*"));
$port1 = count(glob("./assets/images/portfolio/*.*"));
$port2 = count(glob("./assets/images/portfolio-details/*.*"));
$leader = count(glob("./assets/images/leaders/*.*"));
$srv = count(glob("./assets/images/services/*.*"));
$usr = count(glob("./assets/images/users/*.*"));
$count = $img+$about+$blog1+$mason+$tl+$blog2+$gallery+$home+$home2+$ico+$lb+$keg+$port1+$port2+$leader+$srv+$usr;
return $count;
}
}
In controller:
public function index() {
// load model
$this->load->model('photo_shoot_model');
// call method
$howManyImages = $this->photo_shoot_model->image_count()
}
All about models: https://codeigniter.com/user_guide/general/models.html
But really, I'd use the RecursiveDirectoryIterator mentioned by #MayurVirkar.
I have a class function that calls JSON from a URL. That function then sets a list of variables from the results of the JSON.
What I am attempting to do is call back one of those strings from inside of another file (index.html). I do not receive any errors back, however the results are blank.
I'm sure it's not due to the json command because it works properly when not placed inside of a class/function. To be sure I attempted to add $somestring4 = 'this is string 4' into the ClassFile.php and call it - it was also blank.
Here is my code...
ClassFile.php
<?php
class newClass {
Function newFunction(){
$jsonFetched = file_get_contents('http://url.com/file.json'); //<== MISSING SINGLE QUOTES ADDED FOR CODE-READABILITY...
$jsonQuery = json_decode($jsonFetched);
$someString1 = $jsonQuery->level1->level2->string1 ;
$someString2 = $jsonQuery->level1->level2->string2 ;
$someString3 = $jsonQuery->level1->level2->string3 ;
}
}
$foo = new newClass;
?>
Call from Index.html
<?php
include($sitePath . '/classes/ClassFile.php') ;
$refClass = new newClass();
$someString3 = $refClass->newFunction();
echo $someString3;
?>
Thanks for the help and sorry for the ignorance.
<?php
class newClass {
public $jsonFetched = '';
public $jsonQuery = '';
public $someString1 = '';
public $someString2 = '';
public $someString3 = '';
function newFunction(){
$jsonFetched = file_get_contents('http://url.com/file.json');
$jsonQuery = json_decode($jsonFetched);
$this->someString1 = $jsonQuery->level1->level2->string1 ;
$this->someString2 = $jsonQuery->level1->level2->string2 ;
$this->someString3 = $jsonQuery->level1->level2->string3 ;
}
}
?>
<?php
include($sitePath . '/classes/ClassFile.php') ;
$refClass = new newClass();
$refClass->newFunction();
echo $refClass->someString3;
?>
I'm trying to create multiple .php files using php itself.
I want to put some code into a file; most of code is the same but only one or two variables that I wanted to be dynamic. I mean every file that I make are exactly like each other the only difference between theme is one variable.
My function is this:
function generate_corn_files()
{
$C = $GLOBALS['C'];
$db = $GLOBALS['db'];
//delete all contents of folder
RemoveDir($C->INCPATH.'cron/feed/', false);
$res = $db->query('SELECT id FROM category ');
while($cat = $db->fetch_object($res)) {
$id = $cat->id;
$open_output = <<<'PHP'
<?php
$outter_id = $id;
if($example = true){
echo 'test';
echo $C->INCPATH;
}
?>
PHP;
$fp=fopen($C->INCPATH.'cron/feed/filename_'.$id.'.php','w');
fwrite($fp, $open_output);
fclose($fp);
}
}
I tried to put content of file using heredoc but I want to $id in $outter_id = $id; be equal to $id = $cat->id;
it's a variable outside of heredoc I can't make it work inside of it !
Are there any other solutions to make it work ?
You aren't using HEREDOC syntax but rather NOWDOC syntax. If you use HEREDOC, all variables inside will be evaluated, so you will have to escape with \$ the variables you don't want evaluated.
$open_output = <<<PHP
<?php
\$outter_id = $id;
if(\$example = true){
echo 'test';
echo \$C->INCPATH;
}
?>
PHP;
Or, you can stick with NOWDOC, use a placeholder, and replace it afterwards.
$open_output = <<<'PHP'
<?php
$outter_id = %%%id%%%;
if($example = true){
echo 'test';
echo $C->INCPATH;
}
?>
PHP;
str_replace("%%%id%%%", $id, $open_output);
Maybe this could inspire you
function generate_corn_files()
{
$C = $GLOBALS['C'];
$db = $GLOBALS['db'];
//delete all contents of folder
RemoveDir($C->INCPATH.'cron/feed/', false);
$res = $db->query('SELECT id FROM category ');
while($cat = $db->fetch_object($res)) {
$id = $cat->id;
$open_output = <<<'PHP'
<?php
$outter_id = $id;
if($example = true){
echo 'test';
echo $C->INCPATH;
}
?>
PHP;
$php_var_name_pattern = '/\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/';
$open_output = preg_replace_callback(
$php_var_name_pattern,
function($matches) {
if(isset($GLOBALS[$matches[1]])) {
if(is_string($GLOBALS[$matches[1]])) {
return '\''.$GLOBALS[$matches[1]].'\'';
} else {
return $GLOBALS[$matches[1]];
}
} else {
return $matches[0];
}
},
$open_output);
$fp=fopen($C->INCPATH.'cron/feed/filename_'.$id.'.php','w');
fwrite($fp, $open_output);
fclose($fp);
}
}
I have the following problems !
I recieve the information from the form
public function crearcomentario ()
{
$blogId = \Request::input('identificador');
$autor = \Request::input('autor');
$correo = \Request::input('correo');
$content = \Request::input('content');
$find = User::where('username','=',$autor)->get();
$buscar = $find->toArray();
$comentarios = new Comentario();
//$comentarios->autor_id = $buscar[0]["id"];
$comentarios->correo = $correo;
$comentarios->articulo_id = $blogId;
$comentarios->contenido = $content;
echo $find;
echo $autor;
//$comentarios->save();
}
The variable $find return the information about user introduce in the form.
My question is how can I compare if the user find is empty [] ? I try to use
if(is_null($find))
but doesn't work.
$find would be a Laravel Collection. You can use the isEmpty() function to check whether it is empty:
if($find->isEmpty()) { ... }
try this,
$find = User::where('username','=',$autor)->get();
if($find) {...}
How can i pass the post variable to segment url? i can read it from url but i can't set it...
What i have so far : $route['shop/find/(:any)']= "shop/find/$1"; on routes,my find.php on views, and my function `
function find()
{
$this->load->model('shopFront/find');
$this->load->model('currency');
$this->load->model('shopFront/calculate');
$this->load->model('shop/proccess');
$data = $this->commondatashop->general();
$inputSlug = $this->input->post('findSlug');
$data['tofind'] = $inputSlug;
$data['cartInfo'] = $this->shopcart;
$data['Currency'] = $this->currency;
$data['Calculate'] = $this->calculate;
$data['Proccess'] = $this->proccess;
$data['title'] = "1.4.U Shop | Find";
$finder = $this->find;
$data['finderservice'] = $finder;
$data['user_id'] = $this->session->userdata('user_id');
$data['user_name'] = $this->session->userdata('user_name');
$this->load->view('shop/top/topNew',$data);
$this->load->view('shop/content/find',$data);
//footer
$curravailable = $this->calculate->getCurrencies();
if ( $curravailable !== false )
{
$data['currencies'] = $curravailable;
}
else
{
$data['currencies'] = 'none';
}
$this->load->view('shop/footer',$data);
}`
How can i post the variable to the find()function with url segment?
you need to have find accept an argument with a default
function find($i=0) {
//$i is whatever is in the URL or defaults to 0
In your view:
echo form_open('/shop/find');
echo form_input('findSlug','');
echo form_submit('submit', 'Submit');
echo form_close();
Controller:
$data[slug] = $this->input->post('findSlug');
You need to load form helper too