Can't display picture in CDetailView Yii 1.1 - php

I am learning PHP Yii 1.1
I'm trying to upload picture while creating a new user and display the picture in the view (in CDetailView).
In uploading picture process, I could save the name of the pic in the database and save the picture in a folder called banner which is included in protected folder.
I want now to display the pic in the view, I tried all the solutions I've found, but still doesn't work with me.
<h1>View User #<?php echo $model->id; ?></h1>
<?php
//die(Yii::app()->baseUrl . '/protected/banner/' . $model->picture);
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'username',
'address',
'email',
array(
'name'=>'picture',
'type'=>'raw',
'value'=>CHtml::image(Yii::app()->baseUrl . '/protected/banner/' . $model->picture),
)
),
)); ?>
I checked the image URL by executing this command, and it gave me the right path of the saved image:
die(Yii::app()->baseUrl . '/protected/banner/' . $model->picture);

Access to protected directory is disallowed to protect all internal files of your applications. You need to create this directory outside of protected directory if you want to link to files inside of it:
array(
'name'=>'picture',
'type'=>'raw',
'value'=>CHtml::image(Yii::app()->baseUrl . '/banner/' . $model->picture),
)

Related

Why the path of file image is not inserted into database MySQL in Laravel?

I'm learning Laravel right now on Laravel From Scratch 2022 by Traversy Media (Youtube). I want to create a file upload function for new listing form. When I tried to upload, the image was uploaded into the right path in public storage but the path is not in the database, instead the value inserted is 0.
Here is the code for ListingController.php
// Store Listing Data
public function store(Request $request) {
$formFields = $request->validate([
'title' => 'required',
'company' => ['required', Rule::unique('listings','company')],
'location' => 'required',
'website' => 'required',
'email' => ['required', 'email'],
'tags' => 'required',
'description' => 'required'
]);
$directory = "logos";
if($request->hasFile('logo')) {
$formFields['logo'] = $request->file('logo')->store('logos','public');
}
Listing::create($formFields);
return redirect('/')->with('message', 'Listing created successfully!');
}
Here is the screenshot of image that I successfully uploaded but the value in database is 0.
Screenshot of Laravel storage/app/public/logos directory
Screenshot of MySQL database, column logo is used to store image path
Thank you for your help!
In my case since he used protected $fillable in Models. I added logo this resolved my issue. See the code below.
protected $fillable = ['title', 'logo','company', 'location', 'website', 'email', 'description', 'tags'];
What Eric commented worked for me as well, in the Models file under Http, you have to add whatever name that you used for the logo ('logo', 'image', 'picture' etc.), since in the course he uses protected $fillable.
I tested it, and your code is fine. probably it might be related to the type of logo column in your database. make sure it's "Varchar" and not "Boolean".
you could use: var_dump($request->file('logo')->store('logos','public'));
to see if the path of image showed up or not. if not, try use storeAs()
instead of store() .
$request->file('logo')->storeAs('public' , "logo.jpg");
in this alternative code, you can set a proper name for your image as a second parameter and save it in the database.
It seems $request->file('logo')->store('logos','public') doesn't return the file name or path.
Considering that, it might be better to assign a custom file name. In the example below, we use the name of the original uploaded file.
$fileName = $request->file('logo')->getClientOriginalName();
$request->file('logo')->storeAs('logos', $fileName);
$formFields['logo'] = $fileName;
Source: https://blog.quickadminpanel.com/file-upload-in-laravel-the-ultimate-guide/
Are you using "protected $fillable =..." in your model file? If so, you need to add 'logo' to the array.
(I'm doing the course and got stuck at the same part.)

Yii - How To Display User's Own Uploaded Data In CGridView?

everyone I am working on a project that allow user do upload and download data. I want to show only a user's own uploaded data in CGridView.
Here's my relation table
I have tried to edit my page --> views/file/myUploads.php
<?php
$isMine=Yii::app()->user->id; // I have tried this
if($isMine){
$this->widget('zii.widgets.CGridView',array(
'id'=>'file-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
'name'
'description',
'category'
'uploader'
'upload_date',
array(
'header'=>'Actions',
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{delete}', //'visible'=> (Yii::app()->user->getLevel()==1),
'deleteConfirmation'=>"js: 'Are you want to delete '+$(this).parent().parent().children(':first-child').text()+ '?'",
'buttons'=>array(
'delete' => array(
'visible'=> '$data->pengunggah==Yii::app()->user->id',
),
)
),
),
)); }
?>
I have tried that codes above but the page still display all data, not user's own data.
I have tried to edit myUpload function in FileController.php too
public function actionMyUpload()
{
$isMine=Yii::app()->user->id; // I have tried this
if($isMine){
$model=new File('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['File']))
$model->attributes=$_GET['File'];
$this->render('myUpload',array(
'model'=>$model,
));
}
}
But still display all data.
Can anyone help me? Thanks a lot.
Revert all your changes back, Open Your Model Class File ie "File.php" and add this line in your search() function:
$criteria=new CDbCriteria;
// Simply add this line
$criteria->addCondition('id_user='.Yii::app()->user->id);

Zend Framework 2 custom style for each user

I am building an application for companies which sends anonymous-links to customers for filling out a questionnaire. The company should be able to change the colors and the logo of the questionnaire to reflect the affiliation to the company's CI.
My idea was to make a folder for every company (in my case, represented as doctrine entity Client) and load the layout's style.css and logo.png etc. dynamically from this folder.
The question: how do I implement this? How can I change a variable in the layout file from the controller? Or do I have to place the whole layout inside the view.phtml file for the ViewModel?
Thanks in advance!
If I had to have several layouts depending on some condition.
I would make the layouts for every company, set them in module.config.php
'view_manager' => array(
'template_path_stack' => array(
'module' => __DIR__ . '/../view/',
),
'template_map' => array(
'layout/company1' => __DIR__ . '/../view/layout/company1.phtml',
'layout/company2' => __DIR__ . '/../view/layout/company2.phtml',
)
),
Then in gloabal.php or in the same module.config.php would add some options:
'companies_layouts' => array(
'IDofComapny1' => 'layout/company1',
'IDofComapny2' => 'layout/company2',
)
And finally in the controller would do something like this:
public function indexAction()
{
$sm = $this->getServiceLocator();
// Getting company identifier
$companyId = $this->params()->fromRoute( 'companyId' );
// do something
...
$this->layout( $sm->get('Config')['companies_layouts'][$comanyId] );
return new ViewModel();
}
If you just need to set css depending on some conditions.
You can just do this in the view file:
switch( true ){
case some condition:
$css = 'file1.css';
break;
case some condition:
$css = 'file2.css';
break;
}
$this->headLink()->appendStylesheet( $css );
And in the layout file you should have next line:
<head>
...
<?= $this->headLink() ?>
...
</head>
You need to set style.css and logo file path according to company name in you action and then you can access this vairable in you layout also as same as you access in view file.
And set you css with headLink() function. and assign logo file in layout header.
You don't need to place layout code in view file.
Write below code on you controller. you can also access style variable in you layout.
return new ViewModel(array( 'style' => $style ,'logo' => $logo));

Display Multiple Images/Links In CDetailView Yii framework

Help
I have been surfing all day and can not find the topic, how to display multiple images in CDetailView.
My situation are as follows:
I have uploaded multiple images, the image jpg files stored in
/images/doc directory.
I have entried path to the images in a cell, means the cell contain
three filenames with comma separated: abc.jpg, xyz.jpg, abaca.jpg.
I wanna to display the link in CDetailView which clickable to open
the image in new tab browser.
I have tried with this script:
array(
'name'=>'File Link',
'type'=>'raw',
'value'=> Links of abc,
xyz,
and also this to display the images
$document= CHtml::encode($model->Document);
$file = str_getcsv($document ,",");
in CDetailView
array(
'name'=>'Image',
'type'=>'raw',
'value'=>link to $file[1]
),
array(
'name'=>'Image',
'type'=>'raw',
'value'=>link to $file[2]
),
array(
'name'=>'Image',
'type'=>'raw',
'value'=>link to $file[3]
),
but the result is not as I expected, when I click the link unrecognized opened by the browser.
I expect result like this: and it should be in Dynamic form may be using 'foreach' statement
how to use it i m not getting it
...
...
File Link : abc.jpg
xyz.jpg
abaca.jpg <== each must be clickable to the location of the image
...
...
Please Help
Regards
sandeep
If I understand your point, you need to have multiple images per row, right?
have a unanymouse function for value attribute and return the link of images:
'value' => function($data){ // here $data represents your model
$link = '';
if(!empty($data->images))
foreach($data->links as $l)
{
$link .= CHtml::link(CHtml::encode($file[$i]),Yii::app()->baseUrl . '/images/' . $file[$i]) . '<br />';
}
return $link;
}
Thanks for your help its simple and best way.......
here is my code document is an has multiple images in database and folder for one attribute
Function in Model
public function getDocument($model)
{
$link = '';
$attribute=CHtml::encode($model->documents);
$file = str_getcsv($attribute ,",");
if(!empty($model->documents))
foreach($file as $i=> $record)
{
$link .= CHtml::link(CHtml::encode($file[$i]),Yii::app()->baseURL . '/Documents/' . $file[$i]) . '<br />';
}
return $link;
}
In View widget CDetailView
array(
'name'=>"Uploaded Document ",
'value'=> $model->getDocument($model),
'type'=>'raw'
),

yii parent child Clistview paging issue

I am using Yii framework of php. My scenario is I've number of projects and all the projects have posts. For projects i used Clistview and in the 'itemView' of this control another page is specified named '_post'. In '_post' page again ClistView is specified for showing post details.
But the problem is when paging occur in post details Clistview of the projects and changing the page number of the one post detail to next page number, all the post detail clistview page number changes.
I have also specified post detail clistview 'id' but in no vain.
For Projects:
<?php
$this->pageTitle=Yii::app()->name . ' - Project Post Details';
echo '<div class="listViewBorder">';
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$dataProvider,
'id'=>'projectListView',
'itemView'=>'_post', // refers to the partial view
'enablePagination'=>true,
));
echo '</div><br />'
?>
For Post detail:
<?php
echo '<h2>Project: '. CHTML::encode($data->title).' </h2>';
echo '<div class="listViewBorder">';
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$this->CallProjectPosts($data->id),
'id'=>'postListView'.$data->id,
'itemView'=>'_postDetail',
'enablePagination'=>true,
));
echo '</div>'
?>
This is how i finally got it working, it will work until you find a better solution.
i'm assuming that project_id is a foreign key in your posts table.
Which will generate(using gii) the necessary HAS_MANY relation in your project model, which in turn enables us to access the posts of a project easily, without calling the CallProjectPosts method.
So modify your Post view(_post.php):
<?php
echo '<h2>Project: '. CHTML::encode($data->title).' </h2>';
echo '<div class="listViewBorder">';
$relatedPosts=new CArrayDataProvider($data->posts, // this is where the HAS_MANY relation comes into play
array(
'pagination'=>array(
'pageSize'=>1, // whatever your size was
)
)
);
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$relatedPosts,
'id'=>'postListView'.$data->id,
'itemView'=>'_postDetail',
'enablePagination'=>true,
));
echo '</div>'
?>
For Project list :
<?php
$this->pageTitle=Yii::app()->name . ' - Project Post Details';
echo '<div class="listViewBorder">';
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$dataProvider,
'id'=>'projectListView',
'itemView'=>'_post', // refers to the partial view
'enablePagination'=>true,
'ajaxUpdate'=>false
)
);
echo '</div><br />'
?>
As you will see, i have disabled ajaxUpdate for the project list view, if it is enabled, then the solution will not work, so if your requirement is to display/update the project list also through ajax then this will not work, currently only the project posts are updated through ajax.
Hope this helps.

Categories