Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Resize Image from Query


Go to End


3 Posts   1757 Views

Avatar
fordy

Community Member, 46 Posts

24 June 2008 at 10:41am

Hey,

I am trying to resize an image that i am getting from a complex query. I have tried a few ways but not been successful. This is what i have at the moment. I cant get it to work. Is there any other way?

PHP:

function ShowHomeVacancies(){

$sqlQuery = new SQLQuery();
$sqlQuery->select = array(
'Vacancy.*',
'VacancyCategory.vCategory As vCategoryName',
'SiteTree.URLSegment As vURLSegment',
'(SELECT Member.Company FROM Member Where Member.ID= Vacancy.MyEmployerID) AS Employer',
'(SELECT Member.MemberLogoID FROM Member Where Member.ID= Vacancy.MyEmployerID) AS EmployerLogo',
'(SELECT Filename FROM File LEFT JOIN Member ON File.ID = Member.MemberLogoID WHERE Member.ID= Vacancy.MyEmployerID) AS Logo',
'Vacancy.hasLogo AS hasLogo'
);
$sqlQuery->from = array(
"Vacancy",
"LEFT JOIN VacancyCategory ON Vacancy.MyCategoryID = VacancyCategory.ID",
"LEFT JOIN VacancyCategoryPage ON Vacancy.MyCategoryID = VacancyCategoryPage.vCategoryID ",
"LEFT JOIN SiteTree ON VacancyCategoryPage.ID = SiteTree.ID"
);

$sqlQuery->where = array(
"vActive = true "
);

//$sqlQuery->orderby = "Vacancy.ID DESC";

$sqlQuery->limit = "0, 5";

$result = $sqlQuery->execute();

$pagedVacancies = singleton('Vacancy')->buildDataObjectSet($result);

return $pagedVacancies;

}

function eLogo($ID){

return ($img = DataObject::get_by_id("Image", $ID) ? $img : false);

}

SS Code:

<% if ShowHomeVacancies %>

<% control ShowHomeVacancies %>

<div class="VacancyListItem $EvenOdd">
<% if hasLogo = 1 %>

<% control eLogo($EmployerLogo).SetWidth(80) %>
<img src="$URL" alt="Company Logo" />
<% end_control %>

<% end_if %>
<h2><a href="/$vURLSegment/show/$ID">$vTitle</a></h2>
<p><span class="bold">Location: </span>$vLocation</p>
<p><span class="bold">Salary: </span>$vSalary</p>
<p>$vDescription.LimitWordCount(30)</p>
<p><span class="bold">Sector: </span><a href="/$vURLSegment/">$vCategoryName</a></p>
</div>

<% end_control %>

<% end_if %>

Avatar
Willr

Forum Moderator, 5523 Posts

24 June 2008 at 11:12am

I dont think you can call eLogo() and pass variables like

   <% control eLogo($EmployerLogo).SetWidth(80) %> 
   <img src="$URL" alt="Company Logo" /> 
   <% end_control %>

what I would do is in the ShowHomeVacancies method down the bottom is do something like this (Note doing this completely off the top of my head)

....
$result = $sqlQuery->execute();
$pagedVacancies = singleton('Vacancy')->buildDataObjectSet($result); 
foreach($pagedVacancies as $vacancie) {
           $vacancie->Logo = $this->eLogo($vacancie->EmployerLogo;
}          
return $pagedVacancies;
...

Then in the template just do a control Logo. Im not sure if the above works, if it saves the Logo field on the dataobject. You might just need to play around with that

Avatar
fordy

Community Member, 46 Posts

24 June 2008 at 6:56pm

IT WORKED!

Super, thanks willr. I had been scratching my head on this one for ages.

I played about with my code but it is basically the same. Here is my final code incase anyone else wants it.

PHP:

function ShowHomeVacancies(){

$sqlQuery = new SQLQuery();
$sqlQuery->select = array(
'Vacancy.*',
'VacancyCategory.vCategory As vCategoryName',
'SiteTree.URLSegment As vURLSegment',
'(SELECT Member.Company FROM Member Where Member.ID= Vacancy.MyEmployerID) AS Employer',
'(SELECT Member.MemberLogoID FROM Member Where Member.ID= Vacancy.MyEmployerID) AS EmployerLogo',
'Vacancy.hasLogo AS hasLogo'
);
$sqlQuery->from = array(
"Vacancy",
"LEFT JOIN VacancyCategory ON Vacancy.MyCategoryID = VacancyCategory.ID",
"LEFT JOIN VacancyCategoryPage ON Vacancy.MyCategoryID = VacancyCategoryPage.vCategoryID ",
"LEFT JOIN SiteTree ON VacancyCategoryPage.ID = SiteTree.ID"
);

$sqlQuery->where = array(
"vActive = true "
);

$sqlQuery->orderby = "Vacancy.ID DESC";

$sqlQuery->limit = "0, 5";

$result = $sqlQuery->execute();

$pagedVacancies = singleton('Vacancy')->buildDataObjectSet($result);

foreach($pagedVacancies as $vacancie) {
if ($vacancie->EmployerLogo != 0 && $vacancie->hasLogo == true){
$vacancie->Logo = $this->eLogo($vacancie->EmployerLogo);
}
}

return $pagedVacancies;

}

function eLogo($ID){

return ($LogoImg = DataObject::get_by_id("Image", $ID)) ? $LogoImg : false;

}

SS Code:

<% if ShowHomeVacancies %>

<% control ShowHomeVacancies %>

<div class="VacancyListItem $EvenOdd">
<% if hasLogo = 1 %>
<% control Logo.SetWidth(120) %>
<img src="$URL" alt="Company Logo" />
<% end_control %>
<% end_if %>
<h2><a href="/$vURLSegment/show/$ID">$vTitle</a></h2>
<p><span class="bold">Location: </span>$vLocation</p>
<p><span class="bold">Salary: </span>$vSalary</p>
<p>$vDescription.LimitWordCount(30)</p>
<p><span class="bold">Sector: </span><a href="/$vURLSegment/">$vCategoryName</a></p>
</div>

<% end_control %>

<% end_if %>