I am using the Codesleeve Stapler and i have a small problem.
I did exactly what the last example described on this page:
https://github.com/CodeSleeve/stapler
The difference is that my new model is called Pictures instead of ProfilePictures
and my model is not User but Trip.
The <img src="<?= asset($picture->photo->url('thumbnail')) ?>">
on the view shows the last picture that has been uploaded.
I want to show the Picture that belongs to each Trip. How could i perform this?
Thanks.
So, you have two models: 'Trip', and 'Picture'. In your Trip model, you need to define a 'hasMany' relationship to the Picture model:
public function pictures(){
return $this->hasMany('Picture');
}
Then, in your Picture model, you define the Stapler attachment:
// Be sure and use the stapler trait, this will not work if you don't:
use Codesleeve\Stapler\Stapler;
// In your model's constructor function, define your attachment:
public function __construct(array $attributes = array()) {
// Pictures have an attached file (we'll call it image, but you can name it whatever you like).
$this->hasAttachedFile('image', [
'styles' => [
'thumbnail' => '100x100#',
'foo' => '75x75',
'bar' => '50x50'
]
]);
parent::__construct($attributes);
}
Now that you've defined the attachment on the Picture model, every time you access a Picture object, you'll also have access to the file attachment. Assuming you had a Trip record, you could do this:
<?php foreach ($trip->pictures as $picture): ?>
<img src="<?= asset($picture->image->url('thumbnail')) ?>">
<?php endforeach ?>
You could access the original image like this:
<img src="<?= asset($picture->image->url()) ?>">
// or
<img src="<?= asset($picture->image->url('original')) ?>">
In fact, you can access any of the style you defined:
<img src="<?= asset($picture->image->url('foo')) ?>">
<img src="<?= asset($picture->image->url('bar')) ?>">
Hope this helps.
Related
I am beginner in code igniter php framework. Today I have a task to modify existing code and somehow I am lost in the middle of the line on attempting to located a database table. This is the controller snippet that renders the view
..........
$data["all_types"] = $this->data_model->get_data("all_types",300,"Asc");
$this->load->view("preview_brands",$data);
...........
when the view is loaded I am iterating through the in the view to load data as shown
<div class="decoration"></div>
<?php foreach($all_types as $item): ?>
<div style="display:block;color:#000;">
<a style="font-size:17px;margin:15px 2px;font-family:Cgothic;"
href="<?php echo site_url()."...../..../".$item["id"]; ?>"
class="scale-hover"
title="<?php echo str_replace("_"," ",$item["name"]);?>">
<strong><?php echo str_replace("_"," ",$item["name"]);?></strong>
</a>
</div>
<?php endforeach; ?>
this is the model snippet code as shown
function get_data($db,$i,$order)
{
$this->db->order_by("name",$order);
$this->db->where('consent',"yes");
$query=$this->db->get($db);
return $query->result_array();
}
My challenge is how can I locate or identify the database table the above model is pointing to to fetch data. Hoping someone assists me
all_types is your table name.
$this->db->get($your_table name), get method take table name as a parameter.
Get Method Documentation
If more help needed I'm happy to help.
Happy Coding.
From your model code:
$query=$this->db->get($db);
This means $db is the table name. Looking at your controller code which calls the model function:
$data["all_types"] = $this->data_model->get_data("all_types",300,"Asc");
I can say that all_types is the name of the table.
It's first time when i use Codeigniter, so I tried to work with Bootstrap. I knew Bootstrap has a class for images: img-responsive, but you can only use a parameter in img() codeigniter function. So , i tried something like this:
$logo = array(
'src' => 'assets/images/logo.png',
'class' => 'img-responsive',
);
Then i included $logo into img($logo). If I check View Source of page, link looks like:
<img src="http://localhost/assets/images/logo.png" class="img-responsive" alt="" />
but image is not responsive. Any idea ?
Am creating a court management system whereby a person should be able to view images and videos pertaining to a certain case
I have two related tables that is case and evidence tables
RELATIONSHIPS
Evidence model code:
public function getCaseRef()
{
return $this->hasOne(Cases::className(), ['ref_no' => 'case_ref']);
}
public function rules()
{
return [
[['case_ref', 'saved_by', 'saved_on', 'evidence_type'], 'required'],
[['saved_on'], 'safe'],
[['path'], 'safe'],
[['case_ref', 'saved_by'], 'string', 'max' => 100],
[['evidence_type'], 'string', 'max' => 50],
[['path'], 'file']
];
}
Case model code:
public function getEvidences()
{
return $this->hasMany(Evidence::className(), ['case_ref' => 'ref_no']);
}
Uploading works well and I save the evidence with the case reference no. The path in the evidence model is where I save the path to where the image is uploaded to.
In the action view of the cases controller
public function actionView($id)
{
$evidence = new Evidence();
return $this->render('view', [
'model' => $this->findModel($id),
'id'=>$id,'evidence'=>$evidence,
]);
}
How do I put my view model so that it displays the images/videos/pdf files stored in a folder (corresponding to an id i.e $id passed during the click using the path stored in the database evidence table
if the folder is in the var myFolder and the filename in the var myFilename in your model you can easly use it your view for image and video adding the proer value to the related attribut
echo "<img src='". $model->myFolder . $model->myFilename . "' >";
echo " <video width='320' height='240' controls>
<source src='" . $model->myFolder . $model->myFilename ."'type='video/mp4'>
Your browser does not support the video tag.
</video> "
for pdf you should use a proper extension like mpdf or if you mime is correctly setted a simple link build with the same rules
If you have plenty of evidence connected to the case evidence, this evidence will be made available in an object with many instances or in related details with many istance. To handle this situation is sufficient that you make a foreach loop on these istances (submodels or sub object) and for each one produces the echo as suggested in the first part of the answer.
I have a projects table and an images' table. A project has many images and an image belongs to a project. In the images table I have a field called image_dimensions. What I want to achieve is to get foreach loops, but those loops need to be different for each image_dimensions.
I can display all the images by doing this:
<?php foreach ($project->images as $images): ?>
For example, there are 5 images in the database. 3 images that have the dimensions 2x2 and 2 images that have the dimensions 3x3. So in the code below it will give me 3 div's with an image and 2 empty divs. Those empty divs shouldn't be there.
What also needs to be different for the each image dimension is the class of the div. Sometimes I want to use col-lg-6, sometimes col-lg-12,...
<?php foreach ($project->images as $images): ?>
<div class="col-lg-6">
<?php
if($images->image_path && $images->image_dimensions == '2x2')
echo $this->Html->image($images->image_path, array(
'class' => 'img-responsive center-block project-images'));
?>
</div>
<?php endforeach; ?>
I also have read about the querybuilder in cakephp. I think I need to use it for my problem. But can't seem to get the query to work.
$project = $this->Projects->find();
$project->matching('Images', function($q){
return $q->where(['Images.image_dimensions' => '2x2']);
}
What I wanna achieve is to display all images, but images with 2x2 dimensions should be displayed differently then images with 3x3 dimensions. All the images in the same view ofcourse. So the class of the image and the class of the div around the image should be different for 2x2 and 3x3 image dimensions.
Is it possible to add conditions inside a foreach loop?
My view action inside my Projectscontroller
public function view($id = null)
{
$project = $this->Projects->get($id, [
'contain' => ['Categories', 'Users', 'Tags', 'Images'],
]);
$this->set('project', $project);
$this->set('_serialize', ['project']);
}
Solution
<?php if($images->image_dimensions == '2x2')
echo $this->Html->div(array('class' => 'col-lg-6')
);
elseif($images->image_dimensions == '3x3')
echo $this->Html->div(array('class' => 'col-lg-12')
);
?>
I am trying to learn Yii, and have looked at Yii documentation, but still do not really get it. I still have no idea how to use the CDataProvider on the Controller and View to display all the blog posts available on the view. Can anyone please advise or give an example based on the following:
The actionIndex in my PostController:
public function actionIndex()
{
$posts = Post::model()->findAll();
$this->render('index', array('posts' => $posts));
));
The View, Index.php:
<div>
<?php foreach ($post as $post): ?>
<h2><?php echo $post['title']; ?></h2>
<?php echo CHtml::decode($post['content']); ?>
<?php endforeach; ?>
</div>
Instead of doing the above, can anyone please advise how to use the CDataProvider to generate instead?
Many thanks.
The best that i can suggest is using a CListView in your view, and a CActiveDataProvider in your controller. So your code becomes somewhat like this :
Controller:
public function actionIndex()
{
$dataProvider = new CActiveDataProvider('Post');
$this->render('index', array('dataProvider' => $dataProvider));
}
index.php:
<?php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_post', // refers to the partial view named '_post'
// 'enablePagination'=>true
)
);
?>
_post.php: this file will display each post, and is passed as an attribute of the widget CListView(namely 'itemView'=>'_post') in your index.php view.
<div class="post_title">
<?php
// echo CHtml::encode($data->getAttributeLabel('title'));
echo CHtml::encode($data->title);
?>
</div>
<br/><hr/>
<div class="post_content">
<?php
// echo CHtml::encode($data->getAttributeLabel('content'));
echo CHtml::encode($data->content);
?>
</div>
Explanation
Basically in the index action of the controller we are creating a new CActiveDataProvider, that provides data of the Post model for our use, and we pass this dataprovider to the index view.In the index view we use a Zii widget CListView, which uses the dataProvider we passed as data to generate a list. Each data item will be rendered as coded in the itemView file we pass as an attribute to the widget. This itemView file will have access to an object of the Post model, in the $data variable.
Suggested Reading: Agile Web Application Development with Yii 1.1 and PHP 5
A very good book for Yii beginners, is listed in the Yii homepage.
Edit:As asked without CListView
index.php
<?php
$dataArray = $dataProvider->getData();
foreach ($dataArray as $data){
echo CHtml::encode($data->title);
echo CHtml::encode($data->content);
}
?>