1

I have a list of ads for my website. I would like to be able to visually tell if an ad is still active based on its row color in the index.ctp list. I have a form that controls each entry. In the form there is a $end_date. The color will based on that date. Not really knowing what I am doing I figured I'll need a sort of function in my AdsController to perform the logic. Please let me know if I'm on the right track.

The issue now is that no color change is showing up. Here is the code:

public function getStatusColor($status_color) {
    $this->loadModel('Advertisements');
    $date = $this->Advertisements->get($end_date);
    $now = new DateTime();
        if($date < $now) {
            echo '#ff0000' ;
        } else {
            echo '#00ff00' ;
        }
}

In my index.ctp, this is just a portion of the list code just so you can see how I might splice the color into the table

foreach($advertisements as $advertisement) {
        echo '<tr style="background-color: <?php echo $status_color; ?>;">';
        echo '<td>'.$advertisement->id.'</td>';
        echo '<td>'.$advertisement->title.'</td>';
halfer
  • 19,824
  • 17
  • 99
  • 186

3 Answers3

1

You probably don't need a separate function to do this and you were doing things in the function that really only needed to be done once (getting the current data for example). Without the function you created try this:

$this->loadModel('Advertisements');
$now = new DateTime();

foreach ($advertisements as $advertisement) {
    $date = $this->Advertisements->get($end_date);
    echo '<tr style="background-color: ' . ($date < $now ? '#ff0000' : '#00ff00') . '">';
    echo '<td>'.$advertisement->id.'</td>';
    echo '<td>'.$advertisement->title.'</td>';
}
Dave
  • 5,108
  • 16
  • 30
  • 40
  • could I use set() to pass the variable from Controller to View? – Corey Woods Jun 25 '19 at 20:20
  • Did you create the getStatusColor function? – Dave Jun 26 '19 at 11:09
  • Yes. I put it in my AdsController – Corey Woods Jun 26 '19 at 13:06
  • Take a look at the modified answer @user3083724. I think this can be a lot simpler than what you had. – Dave Jun 26 '19 at 13:11
  • I like how simple this solution is @Dave. I have deleted the previous function in my controller. Then changed the view to match your answer. I am getting an Error, Call to undefined method App\View\AppView::loadModel(), I think it might be because the solution is using a helper in the view. I usually see it used int he controllers. But not sure. – Corey Woods Jun 26 '19 at 14:53
  • Not sure what that is supposed to be doing so I'm afraid I won't be much help in figuring that out. Maybe just don't call `loadModel`? – Dave Jun 26 '19 at 15:09
  • Thanks anyways! Yea not sure exactly how it works myself. I am pretty sure that you need to call loadModel() to have access to the $end_date variable from the Ads model. In cake you need to set variables in the controller. At least from what I read. Then you call the variable in the view. I like the logic statement you are using though. What do you call the colon operator (:) ? Php is new to me, that is mainly why I struggle with cake in the first place. – Corey Woods Jun 26 '19 at 16:23
0

Well, I am not familiar with Cake PHP but a general observation is - I don't see where are you calling the function or returning that color code from the function.

I was expecting $advertisement->getStatusColor() in for loop in index.php

and in function

return ('#ff0000') or return ('#00ff00');

Excuse me if I am wrong in interpreting the code written by you.

0

OK, so because I am a newbie I was waaaay overthinking it. Got it done it done with just this.

 $now = new DateTime();
 echo '<tr style="background-color: '. ($advertisement->end_date < $now ? '#ff0000' : '#00ff00') .'">';

Thanks for everyone's input!