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');
Related
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);
?>
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');
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 --->
?>
I have a model that returns a list of artist's names from a database along with their ID. I want to loop through these artists in my view and create links to their pages with the following format:
http://www.example.com/the-artist-name/artist-portfolio/ID.html
What is the best way to do this?
Controller
$data['artists'] = $this->artists_model->get_all(); // should return array
$this->load->view('yourview', $data);
View
<?php foreach($artists as $artist): ?>
<a href="http://example.com/<?php echo $artist['name']; ?>/artist-portfolio/<?php echo $artist['id']; ?>.html">
<?php echo $artist['name']; ?>
</a>
<?php endforeach; ?>
Pass the data from the model into the view and loop through it like you normally would.
In your controller:
$view_data['artists'] = $this->artist_model->get_artists();
$this->load->view('view.php', $view_data);
In your view:
foreach ($artists as $artist) {
echo "{$artist['name']}";
}
I have a Profile model/controller in my cake app as well as an index.ctp view in /views/profiles. Now, when I go to add a column to my table that is already filled with data, and then add the corresponding code to the view to pick up this column's data, it just gives me an empty result.
My model:
<?php
class Profile extends AppModel
{
var $name = 'Profile';
}
?>
My controller:
<?php
class ProfilesController extends AppController
{
var $name = 'Profiles';
function index()
{
$this->set('profiles', $this->Profile->find('all'));
}
}
?>
My views printing (stripped down):
<?php foreach ($profiles as $profile): ?>
<?php echo $profile['Profile']['id']; ?>
<?php echo $profile['Profile']['username']; ?>
<?php echo $profile['Profile']['created']; ?>
<?php echo $profile['Profile']['thumbnail'];?>
<?php echo $profile['Profile']['account'];?>
<?php endforeach; ?>
Basically, the columns id, username, column, thumbnail always have been printing fine, but when I add a column called accountit returns no information (nothing prints, but no errors). Any suggestions?
I would delete the files in app/tmp/cache (but keep the directories).
For CakePHP 4, clearing tmp/cache/models worked for me.