send function's data to a variable - php

hello i have variable and a function. function get me current logged in user's full name and that works property with this code:
<?PHP
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("login.php");
exit;
}
?>
Welcome back <?= $fgmembersite->UserFullName(); ?>!
now if i want to put function to a variable what i can do?
<?php
$job1 =' ?><?= $fgmembersite->UserFullName();' ?>
<?php
echo "$job1";
?>
and my function is this:
function UserFullName()
{
return isset($_SESSION['name_of_user'])?$_SESSION['name_of_user']:'';
}
the result is just: ?>UserFullName();

<?php
$job1 = $fgmembersite->UserFullName();
?>
What did you tried is assign string value to a variable job

<?php
// <--- maybe lots of other code here --->
$job1 = $fgmembersite->UserFullName();
// <--- maybe lots of other code here --->
?>

Related

why when I type echo <?php ?> in inspect element auto command becomes <---?php ?--->?

I want to make a php tag but through echo, and why does the input I get instead become an automatic command?
which was originally :
<?php
public function section(string $name)
{
$this->nameSection = $name;
echo '<?php echo ' . $this->nameSection;
}
public function endSection()
{
echo "?>";
}
Becomes :
<!--?php ---?>
oh yes I call them like this :
<?php $kurumi->section("content") ?>
<?php $kurumi->endSection() ?>
my intention want to fill variable with html tag
and I want to get <?php ?> output in inspect element

Add code via session if a new user is registered on woocommerce

I am trying to add a script to my header once a new user is registered , here is my code
Function.php file :
add_action('user_register', 'registration_save');
function registration_save($user_id) {
if($user_id){
$_SESSION["user_registered"] = true;
}
}
Header.php file :
<?php if(isset($_SESSION["user_registered"])): ?>
<script>fbq('track', 'CompleteRegistration');</script>
<?php unset($_SESSION["user_registered"]); ?>
<?php endif ?>
The variable has to be global to check it on header.php
You can do it also with a session variable or a cookie.
Functions
<?php
add_action('user_register', 'registration_save');
function registration_save($user_id) {
if($user_id){
$_SESSION["user_registered"] = true;
}
}
?>
Header
<?php if(isset($_SESSION["user_registered"])): ?>
<script>fbq('track', 'CompleteRegistration');</script>
<?php session_destroy(); ?>
<?php endif ?>
Maybe you have to add session_start(); to the beginning of the header.php

How Can I use a function multiple times on the same page?

Say this piece of code:
<?php while($user=mysqli_fetch_array($resultuser)){ ?>
<?php
function my_function($variable) {
//do something here...
}
?>
<?php };?>
This will obviously return this error
Cannot redeclare my_function() previously declared
So what if someone needs to use the same function multiple times across the same page? Is there a way to generate random function() names? Any idea how to get around this? Thanks.
EDIT WITH ACTUAL CODE
<?php while($deposit26=mysqli_fetch_array($resultdeposit26)){ ?>
<td data-th="Hours">
<?php
$week1hours = $deposit26['hours_worked'];
$week2hours = $deposit26['hours_worked_wk2'];
function time_to_decimal($time) {
$timeArr = explode(':', $time);
$decTime = ($timeArr[0] + ($timeArr[1]/60) + ($timeArr[2]/3600));
return $decTime;
}
$groupd26hours = time_to_decimal($week1hours) + time_to_decimal($week2hours);
echo round($groupd26hours, 2);
?>
</td>
<?php };?>
<?php
// Earlier in the file or included with include or require
function time_to_decimal($time) {
$timeArr = explode(':', $time);
$decTime = ($timeArr[0] + ($timeArr[1]/60) + ($timeArr[2]/3600));
return $decTime;
} ?>
...
<?php while($deposit26=mysqli_fetch_array($resultdeposit26)) : ?>
<td data-th="Hours">
<?php
$week1hours = $deposit26['hours_worked'];
$week2hours = $deposit26['hours_worked_wk2'];
$groupd26hours = time_to_decimal($week1hours) + time_to_decimal($week2hours);
echo round($groupd26hours, 2); ?>
</td>
<?php endwhile ?>
you want to declare the function outside the loop and call it inside
<?php
function my_function($variable) {
//do something here...
}
while($user=mysqli_fetch_array($resultuser)){
my_function($variable);
}?>
Let me try and explain what I think could help. I think a good starting point would be to look into including a script or including a script once in your files that require your function logic. That way multiple files can take advantage of the same logic without it having to be repeated. For example:
<?php
// File functions.php
function my_function($variable) {
...
}
?>
<?php
// File one
include_once "functions.php"
...
// Use my_function() from file one
my_function($var);
?>
<?php
// File two
include_once "functions.php"
...
// Use my_function() from file two
my_function($var);
?>

How to pass a value of a variable from view to controller in codeigniter

using form post method i need to pass a value of numIDto a controller
for example assigning a value to numID inside view,
$numID= 25;
i need to use value of numID in a controller and assign that to temp variable. so i use following line of code,
$temp= $_POST[numID];
but its unable to get the exact value. Please help me on this. If have any other alternate way please let me know.
Here is an example of sending info from view to the controller:
View:
<?php echo form_open('invoice'); ?>
<?php echo validation_errors(); ?>
<?php echo form_input('order_num', $this->input->post('order_num')); ?>
<?php echo form_submit('submit','submit'); ?>
<?php echo form_close(); ?>
Controller:
public function invoice()
{
$order = $this->input->post('order_num');
$this->General_Model->get_customer_order($order);
}
Model:
function get_customer_order($order)
{
. . .
$this->db->where('client_orders.id', $order);
$result = $this->db->get('client_orders');
. . .
}
Note we are using the input name and id as = "order_num"
Then we ask the controller to find it as $this->input->post('order_num');

CakePHP Relation on link

The following link is used in a list of Favours! It links to a place where the user is from but is being used inside the favour list Hence the favour variable.
I have three models Users, Places and Favours. A user has many favours and one place, a favour belongs to a user.
<?php foreach($favours as $favour): ?>
<p><?php echo $this->Html->link($favour['User']['firstname'] . ' ' . $favour['User']['lastname'], array('controller'=>'users','action'=>'view','userName'=>$favour['User']['username'])); ?> in <?php echo $this->Html->link($favour['Place']['name'], array('controller'=>'places','action'=>'view',$favour['Place']['id'])); ?> asked a favour <?php echo $favour['Favour']['datetime']; ?></p>
<h3><?php echo $this->Html->link($favour['Favour']['title'], array('controller'=>'favours','action'=>'view',$favour['Favour']['id'])); ?></h3>
How do I display the link as at the moment I get an error saying that Place is undefined.
This is the controller action for that list:
function index()
{
$favours = $this->paginate();
if (isset($this->params['requested']))
{
return $favours;
}
else
{
$this->set('favours', $favours);
}
}
Make sure you contain Place when fetching data for Favour.
This is how it should look like in your favours_controller:
function index(){
$favours = $this->Favour->find('all');
$places = $this->Favour->Place->find('all');
$this->paginate();
$this->set(compact('users', 'places');
}
This is how it should look like in your index.ctp:
<?php foreach($favours as $favour): ?>
<?php echo $this->Html->link($favour['Favour']['username'], array('controller'=>'users','action'=>'view', $favour['Favour']['username'])); ?>
<?php endforeach; ?>
<?php foreach($places as $place): ?>
<?php echo $this->Html->link($place['Place']['place'], array('controller'=>'places','action'=>'view', $place['Place']['place'])); ?>
<?php endforeach; ?>
You may also need to define the uses variable:
var $uses = array('Place');

Categories