Writing a Dynamic Data php function

I wrote a php function wasnt getting to much success from it.

function get_by_category($cat){
$base_img_url = get_stylesheet_directory_uri();
$img =‘’;

if($cat == "Announcements & Misc. Leads"){
	$img = "Announcements & Misc Leads.webp";
}
if($cat == "Blessed Event"){
	$img = "Blessed Event.webp";
}
if($cat == "Business Promotions"){
	$img = "Business Promotions.webp";
}
if($cat == "Goods and Services: For Sale (new/used)" || $cat == "Goods and Services: Free (new/used)" || $cat == "Goods and Services: For Sale (new/used)"){
	$img = "Goods and Services For Sale.webp";
}
if($cat == "Housing Construction or Remodeling" || $cat == "Housing Purchased: Single or Multiple Family" || $cat == "Housing Wanted: Buy or Rent" || $cat == "Housing for Rent" || $cat == "Housing for Sale" || $cat == "Real Estate: Sale, Lease, Rent, & Purchase of Land and Buildings" || $cat == "Sub-Divisions and Other Residential Developments"){
	$img = "Housing Construction or Remodeling.webp";
}
if($cat == "Information or Advice (ask or offer)"){
	$img = "Information or Advice.webp";
}
if($cat == "Investment Opportunity"){
	$img = "Investment Opportunity.webp";
}
if($cat == "Job or Position Openings"){
	$img = "Job or Position Openings.webp";
}
if($cat == "Local Area Commercial Leads" ||  $cat == "Out-of-Area Commercial Leads"){
	$img = "mind-info.webp";
}
if($cat == "Parting Thought for the Week"){
	$img = "thoughts.webp";
}
if($cat == "People on the Move / New People in the Area"){
	$img = "Job or Position Openings.webp";
}
if($cat == "Testimonials/Online Reviews"){
	$img = "Testimonials Online Reviews.webp";
}
return $base_img_url.''.$img;

}

The simplicity is, check if the value equals X, if it does use image, which it should return. But, while it was evaluating the conditions, they never provided true or false.

Here is the code.

{echo:get_by_category('{je_cct_leads_category})}

I am using Bricks Builder (recent release)
PHP 8.2.1
the je is Jetengine.

Any suggestions. I have checked also in logs to see if there are errors, also utilized error checking to see if the result is returned.

Did you paste in that code? If so, you’re missing a single quote.

Yah. I found that. I found a solution. just did a shortcode… Not really happy to have to but oh well.

Hey @pixelvolution,

not sure if this is the only issue in your scenario but you’re missing an important / in your URL. The get_stylesheet_directory_uri function returns a URL without a trailing slash. That’s why you need to add one between the directory and your filename:

return $base_img_url . '/' . $img;

Also: Did you register the function to be used with the dynamic echo tag? See academy article.

And finally you could make your function a bit more readable by using a switch statement instead of multiple if statements:

switch ( $cat ) {
  case 'Announcements & Misc. Leads':
    $img = 'Announcements & Misc Leads.webp';
    break;
  case 'Blessed Event':
    $img = 'Blessed Event.webp';
    break;
    ...
    ...
    ...
}

Best,

André

1 Like