I'll try to describe this as best as possible, i haven't encountered this problem before but maybe i'm doing something wrong.
In my controller: i have:
public function indexAction() {
$this->view->projects = $this->projects->getProjects();
}
In the view file corresponding to this controller i have:
<?php echo $this->partial('partials/sidebar/_home.phtml', array($this->projects)); ?>
My _home.phtml contains the current code:
<div class="sidebar-content">
<p class="sidebar-title">Projects Portfolio</p>
<div id='coin-slider' style="margin:0 auto;">
<?php echo $this->partialLoop('partials/sidebar/_projects-slideshow.phtml', $this->projects);?>
</div>
</div>
And my _projects-slideshow.phtml has this code:
<a href="<?php echo $this->baseUrl($this->pimage); ?>">
<img src="<?php echo $this->baseUrl($this->pimage); ?>" alt="1" />
<span>
<?php echo $this->pname . ' by ' . $this->group . '. Client: ' . $this->client; ?>
</span>
</a>
The problem is that the variable is not passed to _home.phtml. I tried a Zend_Debug::dump($this->projects) and the result was NULL. I tried a Zend_Debug::dump($this) and I found the projects array. What am I doing wrong? The variable is not being passed, or maybe it is, to _home.phtml, not to mention that _projects-slideshow.phtml has no idea what $this->projects is.
If $this->projects in your _home.phtml is empty I think you should change
in index.phtml
<?php echo $this->partial('partials/sidebar/_home.phtml', array($this->projects)); ?>
into
<?php echo $this->partial('partials/sidebar/_home.phtml', array('projects' => $this->projects)); ?>
Here how it should has been:
$variables = array (
'records' => $result
);
$this->view->partial ("nodes/relations.php", $variables);
The variables in the array are named. What exactly is in your $this->projects->getProjects() ?
Related
I am creaiting my first Codeigniter application for a blog with news. In the main page there is only the title of the news which is also a link to a view with the detailed information and body of the news. Im having trouble on accessing the get through the URL that has to go over a function that receives the ID of the new as a parameter. I just can get that to work. Can someone help me?
The problem is not in the function itself because it works fine when i assig an static value to the URL, but for some reason i can send the $row->id object as a Get through the URL with the proper value for each of the news.
MODEL
class Post extends CI_Model{
public function getPost(){
$this->load->database('fintech_blog');
$data = $this->db->get('post');
return $data->$result();
}
CONTROLLER
public function getPost($id){
$query = $this->db->query("select * from post where id = '$id' ");
$rows = $query->result(); //method for putting into an array format
$data=array('result'=>$rows);
$this->load->view('view',$data);
}
VIEW
foreach ($result as $row):
$id = $row->id;
$post = site_url('welcome/getPost/$row->id');
?>
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="post-preview">
<a href="<?php echo $post; ?>">
<h2 class="post-title">
<?php echo $row->title; ?>
</h2>
<h3 class="post-subtitle">
<?php echo $row->calling; ?>
</h3>
</a>
<p class="post-meta">Posted on
<!-- Start Bootstrap -->
<?php echo time_elapsed_string($row->created); ?></p>
</div>
<hr>
</div>
</div>
</div>
<?php endforeach; ?>
The problem comes from the way you create the string for $post.
As one commenter suggests the problem is the use of single quotes and to use double quotes instead. The most important feature of double-quoted strings is the fact that variable names will be expanded. Meaning the variable symbol ($test in this case) is replaced with the value of the variable.
To illustrate:
$test = "blue";
//first, a string created with single quotes
echo 'The sky is $test today.'; //outputs: The sky is $test today.
But using double quotes to create the string...
echo "The sky is $test today."; //outputs: The sky is blue today.
The var $test get expanded to the value it holds.
So, the first two lines of the foreach loop in the view should be.
$id = $row->id;
$post = site_url("welcome/getPost/$row->id");
However, you never use $id and you only use $post once. That says to me that these two lines are not needed. Delete them both.
Change the line
<a href="<?php echo $post; ?>">
to
<a href="<?php echo site_url("welcome/getPost/$row->id"); ?>">
I have a view named home_view, where there are several lists, for example libri(books).The user can upload and delete the files.
This is a snippet of home_view.php
<?php
echo "<table>";
echo "<tbody>";
echo "</br>";
foreach ($libri as $row):
?>
<tr>
<td>
<div>
</br>
<img src="<?php echo base_url('Immagini/book.png'); ?>" />
<a class="pdf" data-fancybox-type="iframe" rel="group" href="<?php echo base_url($row['Url_suffix']) ?>"><?php echo $row['Nome']; ?> </a>
<a class="deleteUser" href="javascript:void(0)" rel="<?php echo site_url('libro/elimina/'.$row['ID']) ?>"><img src="<?php echo base_url('Immagini/button_close.png'); ?>"/></a>
</div>
</td>
</tr>
<?php
endforeach;
?>
libri is a table of Mysql db and have different columns, Url_suffix is a Varchar(255) column where there's the folder/filename.pdf. IN the second anchor I delete with success, the row from the DB, but not the file. I tried to do something like this
<a class="deleteUser" rel="<?php unlink($row['Url_suffix']); ?>"><img src="<?php echo base_url('Immagini/button_close.png'); ?>"/></a>
but without success. What I'm wrong?
Update:
controller libro.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Libro extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper("file");
}
public function elimina($libri_id) {
//$libri_ID = $this->libri->get_one($libri_id);
$result=$this->libri->did_delete_row($libri_id);
redirect(site_url('admin/dashboard'), 'refresh');
}
}
?>
You can't use "unlink" in that way, basically you need to process which you want to delete on your controller, with some help of a model, example:
public function delete_file($route, $file){
unlink($route . "/" . $file);
}
In a model so you can use it anywhere, the $route parameter will be the path to your directory where the file is stored, for example:
htdocs/website/static/books/user/
And the $file parameter will be the name with extension that you want to delete, like:
myfirstbook.pdf
then you will have the full route to the file and it will be deleted, you only have to call that funcion from the models in your controller where the link was given by that anchor, like
Delete file
And your controller will have something like
public function delete_file($user, $file){
$this->load->model('YourModel', 'yourmodel');
$path = FCPATH . "static/books/user/".$user;
$this->yourmodel->delete_file($path, $file);
}
And its done.
Hope it helps.
i am unable to concatenate i am not so good at it wondering how would i concatenate in this scenario
<?php $id = getfield('id'); ?>// this is a function to get fields from sql
<html>
<?php <a class="profile" href="profile.php?='$id' ">
<echo ucfirst ($firstname);</a> ?>//i cant seem to get this part
</html>
This is what i have tried so far i did try some other ways to do it but none of them seem to work
This is probably what you are looking for:
<?php $id = getfield('id'); ?> // this is a function to get fields from sql
<html>
<a class="profile" href="profile.php?id=<?php echo $id ?>">
<?php echo ucfirst ($firstname) ?>
</a>
</html>
Or, more compact if you have "short tags" enabled inside php:
<?php $id = getfield('id'); ?> // this is a function to get fields from sql
<html>
<a class="profile" href="profile.php?id=<?= $id ?>">
<?= ucfirst ($firstname) ?>
</a>
</html>
And finally you could inline the assignment, since the variable is used only once:
<html>
<a class="profile" href="profile.php?id=<?= getfield('id') ?>">
<?= ucfirst ($firstname) ?>
</a>
</html>
This should do the trick :
<?php
$id = getfield('id'); // this is a function to get fields from sql
echo '<html>
<a class="profile" href="profile.php?='.$id.' ">'.ucfirst ($firstname).'</a>
</html>';
?>
I'm trying to display an image in an html file. Up to now i used an url : http://placehold.it/400x300, it worked fine, now when i try to replace it with image from database it doesn't display anything. the image path is : D:/uwamp/www/project/upload/tcf_animal17.jpg i tried with different path but it doesn't work. i'm not sure where is the problem. i printed the data , this is the good path
html
<?php foreach ($data[0] as $film):?>
<div class = <?= $film['id_film'] ?> >
<div class= "col-lg-3 col-md-4 col-xs-6 thumb filmDiv" >
<a class="thumbnail " id = "filmShow" href= <?= "/project/admin/showFilm/" . $film['id_film']?>>
<p> <?= $film['title_film'] ?> </p>
<img class="img-responsive overlay" src= <?= $film['img'] ?> alt="">
</a>
<a class="" href =<?= "/project/admin/update/" . $film['id_film'] ?> >
<button name="upd" id="upd" type="button" class="btn btn-default">Update</button>
</a>
<a class="delete"name= <?= $film['id_film'] ?> >
<button id="delete" type="button" class="btn btn-default">Delete</button>
</a>
</div>
</div>
<?php endforeach; ?>
Controler :
public function GetLastFilms()
{
$films = $this->film->getFilms();
$lastFilms = $this->film ->getLastFilm();
$dict[0] = $films;
$dict[1] = $lastFilms;
return $dict;
}
public function index()
{
if(isset($_SESSION['login']))
{
$this->generateView($this->GetLastFilms());
}else
{
header('Location: /project/admin/login');
}
}
protected function generateView($data = array())
{
$classeControler = get_class($this);
$controler = str_replace("Controler", "", $classeControler);
$view = new View($this->action, $controler);
$view->generate($data);
}
View :
public function generate($data)
{
$contenu = $this->generateFile($this->viewFile, $data);
$racineWeb = Configuration::get("racineWeb", "/");
$view = $this->generateFile('View/Template/index.php',
array('title' => $this->viewTitle, 'contenu' => $contenu,
'racineWeb' => $racineWeb));
echo $view;
}
private function generateFile($viewFile, $data)
{
if (file_exists($viewFile))
{
extract($data);
ob_start();
require $viewFile;
return ob_get_clean();
}
else
{
throw new Exception("can't find '$viewFile'");
}
}
Your problem is that the path to the image in the database is stored as local path from your machine (D:/uwamp/www/project/upload/tcf_animal17.jpg), where for it to display correctly it needs to be a path relative to the root of your web server documents.
For example, if your web server document root is D:/uwamp/www, then the path of the image you need to store should be project/upload/tcf_animal17.jpg.
Of course if you can't get the image to be stored within the web root, then you always have an option to just dump the content directly, however it's really not a good idea, for performance reasons:
<?php
$mime = image_type_to_mime_type(exif_imagetype(string $image_path));
$data = "data:$mime;base64," . base64_encode(file_get_contents($image_path)); ?>
?>
<img src="<?= $data ?>">
This code is very crude and does no error checking - you'll need to add that for a production-level application.
I am looking for some help using the PHP function. At the moment my website is structured like this:
index.php:
<?php
require_once( 'page_elements.php' );
?>
<body>
<?php echo content();?>
</body>
page_elements.php:
<?php
function content() {
?>
<div class='main'>
<img class='main' src="<?=$ImgName?>"> </img>
</div>
<?php
} ?>
if statement:
if (isset($_SESSION['basket_total']))
{
$basket_total = $_SESSION['basket_total'];
if($basket_total !== '0')
{
$ImgName = 'img/basket.php';
}
else
{
$ImgName = 'img/basket_empty.php';
}
}
What I want to do is to be able to define $ImgName in an if statement that isn't involved in the function content() but if i include it in another file, include 'if_statement.php' or in another function then it doesn't recognise the variable.
Sorry, its my first time structuring a website like this and I am finding it a bit confusing.
Cheers in advance
First of all, you don't close an an "img" tag with another "img" tag ...
function content(){
echo'
<div class="main">
<img class="main" src="'.$imgname.'" alt="" title="">
</div>
';
}
is the proper way of doing things. Now as to your question, I'm having trouble understanding your goal, but do you perhaps mean something a.la ...
function content(){
$imgname = include "file.php";
echo'
<div class="main">
<img class="main" src="'.$imgname.'" alt="" title="">
</div>
';
}
and the if_statement.php would be something like ...
if(isset($_SESSION['basket_total'])){
return $_SESSION['basket_total'];
}else{
return "img/basket.php";
}
This will get around the current issue you are having, but I would do like Ionut Flavius Pogacian suggested above and look into an MVC
<?php
require_once( 'page_elements.php' );
$image_name = "batman.jpg";
?>
<body>
<?php echo content($image_name);?>
</body>
page_elements.php:
<?php
function content($image_name) {
?>
<div class='main'>
<img class='main' src="<?=$image_name?>" />
</div>
<?php
} ?>