cakePHP 3.0 - displaying images differently in the same view - querybuilder - php

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')
);
?>

Related

Laravel 5 relationsheep between article and images

Hi i'm new in Laravel and i've this simple problem. I have app where user can create some article and also can to upload main image for that article and it works perfect. I also want to give possibility to user to upload more images for his article. I already create model (ArticleImage) and add relationship between Article and ArticleImage. And i created form field for uploading multiple file inside the article form. But i don't know now how to tell laravel to save all names of these paths into database and store image inside the public/images folder, than later in views i can use these images. Do i need now to create ArticleImage controller and inside controller to write that function or i should do that inside Article controller inside store function. Tnx in advance for every help.
Here is link to github-repo(https://github.com/Dabizlja/Laravel-blog)
If form sends input as array.
<input name="picures[]" />
In your ArticleController on save you can use a json type
$files=[];
$pictures = $request['pictures'];
if (count($pictures) > 0 && $pictures[0] != null) {
foreach ($pictures as $picture) {
$validator = Validator::make([$picture], ['mimes:jpeg,jpg,bmp,png']);
if ($validator->fails()) {
return $validator->messages()->all();
}
$picture->move(public_path() . '/images/', $picture->getClientOriginalName());
array_push($files, $picture->getClientOriginalName());
}
}
$article->images = json_encode($files);
To get it back in the view
<?php
foreach ( $images as $file) {
echo '<img src="'asset('/images/' . $file)'"/>';
}
?>
In your controller on edit remember to decode the json
$files = json_decode($article->images);
To delete a picture you would have to go through the array and remove it the same way.
Hope this works for you.
May be
$article->articleImages()->saveMany($images);
Hopefully this will solve your problem

Drupal 8 change image style in first row on views list

I have created view that displays on page 10 newest article. I have in row two fields: image and content. In settings of image field I chose image style (for example: medium). How can I change image style to another (example: large) only in first row?
I have tried it in preprocess but i don't know where is stored information about image style:
function theme_preprocess_views_view_unformatted__lista_depesz_default(&$variables) {
$view = $variables['view'];
$rows = $variables['rows'];
$style = $view->style_plugin;
$options = $style->options;
$variables['default_row_class'] = !empty($options['default_row_class']);
foreach ($rows as $id => $row) { $variables['rows'][$id] = array();
$variables['rows'][$id]['content'] = $row;
$variables['rows'][$id]['attributes'] = new Attribute();
if ($row_class = $view->style_plugin->getRowClass($id)) {
$variables['rows'][$id]['attributes']->addClass($row_class);
}
if ($id == 0 && $row['content']['#row']->_entity->field_image[0] != NULL) {
//some php code to change image style
}
}
}
Regards
You can create view with original images, and set style inside your twig files, using for example twig tweak:
https://www.drupal.org/project/twig_tweak
Inside twig file you can set any style with conditioning
{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}
Regarding your code and your explanations, I'm not sure to understand what you are trying to achieve.
1/ you try to add a CSS class to the 1st image of your view. Why not using the following CSS path .my_view .first img {}
2/ if you try to call another image style, can you create a view with only the 1st item, or a promoted? Then a second view with the rest of the items ?
3/ if you try to call another image style, you can do it without any code.
You install the module http://drupal.org/project/views_conditional then you add 2 images with 2 different image style, and you apply your condition inside the condition fieldset.
I really prefer the solution 3 because it's pure Drupal build.
I hope it helps.
Cheers

Going from, SELECT * FROM users WHERE url = "pretty url", to displaying 1 row from database with Lithium Framework

Hi I am new to php and mysql and jumped right into using the Lithium framework.....I know, bad idea.
BUT basically my question is, how do I go from;
$users = \app\models\Users::findByUrl($this->request->query['url']);
return array(
'users' => $users,
);
in my UsersController to actually echo'ing 1 specific row of data in my users/index.php.html page, according to the query string in my url?
My desired structure is localhost/users/"url", and "url" is a value in my mysql db labeled "url" in my Users table. e.g. localhost/users/Bob. "Bob" is "url" value.
My users/index.php.html page is using foreach ($users as $user) to echo the information like this;
<?=$user->firstname ?>
Snag and Confusion:
When I was using;
$users = \app\models\Users::find('all');
return array(
'users' => $users,
);
in my UsersController, the users/index.php.html page retrieved all the users in my database.
The content displays correctly, BUT I need the data from just the clicked-on user, so this is when I started using; $users = \app\models\Users::findByUrl($this->request->query['url']);
When linking to the users page this is how I do it;
<?php if( $user->avatar ):?>
<a href="/users/<?=$user->url?>">
<img class="img-responsive" src="/img/users/<?=$user->avatar?>" />
</a>
<?php else: ?>
Not sure if I need to add conditions to my controller, add something to my router, change my htaccess, or do something with params.... Pleease help, any direction would be greatly appreciated.
Finders return everything by default. If you want a single object, do:
Users::findFirstByUrl($this->request->query['url'])
...which is short-hand for:
Users::first(['conditions' => ['url' => $this->request->query['url']]]) or
Users::find('first', ['conditions' => ['url' => $this->request->query['url']]]).

CakePHP 2.0 - Pagination for an image gallery (with different item limits)

I want to build a small image gallery with CakePHP. Therefore I created an index view which displays a 3x3 table with image thumbnails. For pagination and sorting I added the following code:
ImagesController.php:
var $paginate = array(
'limit' => 9,
'order' => array('Image.id' => 'desc')
);
public function index() {
$data = $this->paginate('Image');
$this->set('images', $data);
}
index.ctp:
<?php echo $this->Paginator->sort('id', 'Id'); ?> |
<?php echo $this->Paginator->sort('name', 'Title'); ?>
...
<?php echo $this->Paginator->prev(' << Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->numbers(array('first' => 2, 'last' => 2)); ?>
<?php echo $this->Paginator->next('Next >> ', null, null, array('class' => 'disabled')); ?>
This works great. Now, when you click on a thumbnail a single image will be displayed using a view named 'view' (e.g. localhost/gallery/images/view/1). Like the index view, this view should have a pagination too, where the image with the id of 1 is currently selected and where you can navigate to the n previous and next images relative to the currently selected one. Moreover the images should be sorted by the same criteria as the pictures on the index view (e.g. by name). Is there an elegant way to solve this problem with CakePHP?
I'd approach this by passing the page number from the original pagination to the detail view.
If you were looking at Image ID 1 on page 2, your URL would look something like:
/gallery/images/view/1/2
That is:
/gallery/images/view/:id/:page
From this information, you can query the same paginated data from the index action on the specified page, and provide back and next navigation options from that data.

How do I set the width of a text box in CakePHP using the 'style' option?

I m doing an app in CakePHP, JQuery and Mysql. In my homepage I am listing all my form names. When I click on a form name, it will show the form. Within that view, I have code that looks like this
<?php echo $form->input($r['Attribute']['label'], array(
'type'=>'text',
'style"=width:' => $r['Attribute']['size']
));
?>
to generate a textbox with the label that I got it from $r['Attribute']['label']
and to keep the width of it using that style. However, the width is not working.
This should do it:
<?php
echo $form->input($r['Attribute']['label'], array('type'=>'text','style' => 'width:' . $r['Attribute']['size'] . 'px'));
?>
Two things: Your array key->value pairing was messed up, and you need to give units to the width. I assumed that want you wanted to give it was a width in pixels, but if you just want to set the size of the input field you can do this:
<?php
echo $form->input($r['Attribute']['label'], array('type'=>'text', 'size' => $r['Attribute']['size']));
?>

Categories