display a unique image relevant to the field's content

I am in the process of creating a new database table with the following structure:

CREATE TABLE IF NOT EXISTS `videothek9`.`movies` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(45) NOT NULL,
  `release_year` VARCHAR(45) NOT NULL,
  `available` TINYINT(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`))

and using the cakephp bake console to generate it.

This is an example of how the standard movies/index page will look like:

https://i.sstatic.net/olsK2.jpg

The "available" field can have values of 0 or 1. I want to display specific images instead, such as a checkmark tick or a red x-mark. Unfortunately, the cakephp cookbook (html helper section) couldn't provide much assistance...

Below is a snippet of the view code:

<?php foreach ($movies as $movie): ?>
    <tr>
        <td><?php echo h($movie['Movie']['id']); ?>&nbsp;</td>
<td><?php echo h($movie['Movie']['title']); ?>&nbsp;</td>
<td><?php echo h($movie['Movie']['release_year']); ?>&nbsp;</td>
<td><?php echo h($movie['Movie']['available']); ?>&nbsp;</td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $movie['Movie']['id'])); ?>
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $movie['Movie']['id'])); ?>
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $movie['Movie']['id']), array(), __('Are you sure you want to delete # %s?', $movie['Movie']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>

Is it possible to use a simple PHP if/else statement in the Movies Controller to achieve this?

Answer №1

Here's an easy solution: Rather than just echoing the "available" field value, utilize HtmlHelper::image() to display an image tag with the desired image.

Answer №2

It's really easy! Just use the if else statement to display an image or any content of your choice.

If (0)
  echo $this->Html->image('tick.png');
Else
  echo $this->Html->image('cross.png');

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Clean URL system inspired by Drupal

I've been tasked with revamping some older websites. These sites were built in a traditional manner, with each page having its own PHP file containing header, content, script, and footer. Now my client wants to refresh the look of all the pages, so I& ...

Choosing the Offspring: Deliberating on Multiple Children with Identical Names in Selenium

Just starting out with Selenium and web development in general, I've encountered a problem with locating an element using XPath. selenium.common.exceptions.NoSuchElementException I've been trying to troubleshoot for a while now, but haven' ...

v-for loop to populate dropdown menu with identical values

Recently, I started working on a points counter app using Vue.js and encountered some issues with the v-for loop and dropdown menu functionality. Within my Vue.js application, I have an array of players as shown below: var app = new Vue({ el: '#l ...

What is the best method for sending a document to a web API using Ajax, without relying on HTML user controls or forms?

I have successfully utilized the FormData api to upload documents asynchronously to a web api whenever there is a UI present for file uploading. However, I now face a situation where I need to upload a document based on a file path without relying on user ...

JQuery could not retrieve the property 'json' from an undefined or null reference

Currently, I am in the process of developing a Windows 8 app using HTML and accessing data from an API. Unfortunately, I keep encountering an error: 0x800a138f - JavaScript runtime error: Unable to get property 'json' of undefined or null refer ...

Using jQuery to find a specific element within a variable in an HTML template in order to dynamically load available choices

The issue at hand is this: I am attempting to utilize a dynamic jQuery modal form that changes the content of a table based on a template loaded within it. The template includes a select element that I intend to populate with data as soon as it is inserted ...

Oops! Looks like there was a hiccup - $ is not defined when trying to add the web

I keep encountering this issue: Uncaught ReferenceError: $ is not defined I've double-checked everything, yet this error persists. What am I missing? Below is the script I'm using, utilizing DOM elements. var head = document.head; jq = docum ...

What is the method for a mono social icon to function without requiring a specific class for each icon

Curious about the magic behind Mono Social Icons and how it effortlessly displays the correct icon. Unlike other font icon sets that require adding a class name to load a utf-8 character in :after, Mono Social Icons keeps it simple. Just add the icon as t ...

Looking to make the Bootstrap navigation bar fixed when scrolling down

Can someone help me fix this bootstrap code to stay fixed at the top of the webpage while scrolling down? Below is the bootstrap code that I currently have. I need assistance in making it change from a static position to a fixed one as users scroll down t ...

The parent navigation bar in HTML and CSS is hogging more space compared to its children

In the midst of my vuejs project, I have encountered an issue relating to the size of elements. Specifically, my header consists of a logo and a navbar. The navbar seems to be extending the size of the header unnecessarily. Upon inspection, I noticed tha ...

Organize relevant information in the table

I am facing difficulties in sorting the data results of a table within my CakePHP system's view. I can display all the data from the table, but I am unable to implement sorting functionality. Although I have managed to create Paginator links to sort ...

Printing a specified week number in PHP

I have a piece of code that I need help with. It is related to printing the selected day of the week from the server: $daysOfWeek = array( "Monday" => 1, "Tuesday" => 2, "Wednesday" => 3, ...

Unable to assign the second value of the <input type="datetime-local" /> element to zero

I am currently working on a class called ClosedPlatform that has an attribute called Start, which is of type DateTime. public class ClosedPlatform { public DateTime Start; } My goal is to bind the Start attribute of a ClosedPlatform instance to a d ...

Owl carousel experiencing issues with navigating to the previous and next slides

I've been struggling to get the owl carousel slider working properly, specifically the previous and next buttons. Here is a snippet of my code: <div class="magazine-carousel owl-theme"> <div class="item"></div> <div class="item" ...

Is it possible to create a special case for the beforeunload function?

Is there a way to exclude a specific URL or form button from the beforeunload event in JavaScript? I want to exempt them, not as error handling exceptions but to customize how they interact with this event. Here is the relevant JavaScript code: if(loggedi ...

There is a conflict between LightBox and jquery.animate-enhanced that needs to be

I recently added two plugins to my webpage - LightBox and animate-enhanced. While both are functioning correctly, I have encountered an issue. When clicking on images to display LightBox, the modal dialog appears fine; however, the lightboxOverlay is displ ...

Using Wordpress? Customize your sidebar with three widgets and don't forget to add the "last" class to the third one for a polished look

Successfully set up a custom sidebar on my WordPress website and configured it to display three widgets. The CSS for the sidebar includes a specific styling for each widget, with an additional class to remove margin-right from the last (third) widget. reg ...

Utilizing the CSS Flex property to position one element in the center and align the other element to either the left or right side

What is the best approach to center one element inside a container with CSS flex properties and align another element to the left or right? Even when I set ChatGPT's recommendations of margin-left and margin-right properties to auto for the right-ali ...

"Encountered an error while trying to access properties that

Struggling to create a practice menu with the functionality of making elements appear and disappear on click? If you are encountering issues with a class named "option" not working as expected, you are not alone. Clicking on nested objects like images or i ...

Ensure the initial div is positioned to the left while the following divs are aligned to the

I am looking to organize three distinct pieces of information in a horizontal line layout, shown below: | | | Chapter one Language: English | | ...