0

I am having a small confusion here, i am creating a calender using a laravel plugin by Crinsane, the only problem is displaying multiple dates from my values delivered from the database using foreach loop. The basic structure is this

$data = array(
    3  => 'http://example.com/news/article/2006/03/',
    7  => 'http://example.com/news/article/2006/07/',
);

Calendar::generate(2006, 6, $data);

This will render a calender. But my data values are from database. This is my foreach loop

   @foreach ($events as $a)
       <?php  $string = $a->datebooked;
       $date = DateTime::createFromFormat("Y-m-d" ,$string);
       $date=$date->format("d");
       $date;?>
       <?php $data=array(
           $date=> $a->eventname,
         )?>
   @endforeach

 <?php echo Calendar::generate(date('Y'), date('n'), $data); ?>

When i run this code, it only displays the last event date, i know it is because it loops through the whole data variable, but how can i make it loop through $date=> $a->eventname only ?

3
  • 3
    It seem that your $data array is getting overwritten each time in your loop. Commented Nov 24, 2014 at 13:07
  • Thanks 4 helping, yeah i know that, how do i prevent it from being overwritten , that was my question... Commented Nov 24, 2014 at 13:19
  • I want an output like this $data = array( 3 => 'event 1', 7 => 'event 2', ); But so far only event 2 is showing... Commented Nov 24, 2014 at 13:36

1 Answer 1

1

Outside of the for-each loop, you don't want to overwrite $data, but add to it. The quick and dirty way to do this is to define a $data array outside the foreach-loop, then append to the array like such

<?php
$data = array();
?>
@foreach ($events as $a)
   <?php

   $string = $a->datebooked;
   $date = DateTime::createFromFormat("Y-m-d", $string);
   $date = $date->format("d");
   $data[] = array($date => $a->eventname);
   ?>
@endforeach

This isn't a great way to mix PHP into your view, though; as a suggestion, I'd put the logic to create $data into your controller, and only call Calendar::generate in the view.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @chris for helping :-) I made some changes in your code because i was getting some syntax errors, i change $data=[] to $data[]="" and $data[] = [$date => $a->eventname]; to $data=array($date => $a->eventname); , when i run it , still i get the last value from my events table and yes , i agree with you on view controller mixing, wil change that, thanks again 4 helpng me out and pliz c if u can find the way to solve this, when i print out inside the loop it returns all values but outside only the last value
@bobin56 - Well you did change my main change back to what you had, the $data[] ;) I had thought you were using Laravel 4.2 and PHP 5.4, so I had used short-hand array syntax. I just edited my post with using the long-form array syntax (array() instead of [])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.