1
<?php include('db.php'); 

//it's connect the database to server

$sql = "select * from event";
$query = mysql_query ($sql);
?>


<script>
$(document).ready(function() {
    $('#calendar').fullCalendar({
        defaultDate: '2016-03-01',
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        events: [
        <?php while($rs = mysql_fetch_array ($query)){
        ?>
            {
                title: '<?php echo $rs['title']?>',
                start: '<?php echo $rs['date']?>'
            },
        <?php }?>
        ]
    });
});

I want to show title and date as long as there is record in my database table. But this code just doesn't work. Thanks

  • you can't do it like that..... but this can be achieve using `jquery ajax`... this might help you.. http://stackoverflow.com/questions/21898750/is-there-a-way-to-populate-other-textbox-when-onchange – Sam Teng Wong Mar 01 '16 at 09:56
  • 1
    You can not mix with PHP code and javascript code. – Roli Agrawal Mar 01 '16 at 09:56
  • 1
    What doesn't work? Is the data incorrect? does the query not execute? – Epodax Mar 01 '16 at 09:57
  • 1
    @Roli, Sam: This *could* theoretically work just fine here. – deceze Mar 01 '16 at 10:13
  • You could use AJAX and return an array in the `response` :) – Jaquarh Mar 01 '16 at 10:13
  • Is it your .php file? Then this should work. But better approach will be to call server for the JSON data from your HTML page through ajax. – dsk Mar 01 '16 at 10:16
  • @JayK yup this is my .php file. But it still not working – Sairam Salim Mar 01 '16 at 10:36
  • Where is your database connection? $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT emp_id, emp_name, emp_salary FROM employee'; mysql_select_db('test_db'); $retval = mysql_query( $sql, $conn ); – dsk Mar 01 '16 at 11:55

1 Answers1

0

Try this

<?php include('db.php'); 

    //it's connect the database to server

$sql = "select * from event";
$query = mysql_query ($sql);
$events = array();
while($rs = mysql_fetch_array ($query)){
    $events[] = array(
        'title' => $rs['title'],
        'start' => date('Y-m-d H:i:s', strtotime($rs['date']))
    );
}
?>


<script>
$(document).ready(function() {
    $('#calendar').fullCalendar({
        defaultDate: '2016-03-01',
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        events: <?php echo json_encode($events)?>
    });
});
Chintan Mirani
  • 1,464
  • 9
  • 18
  • Get rid of `$count`, you're not incrementing it anyway which leads to bugs here. `$events[] = array('title' => .., 'start' => ..);` is much saner. – deceze Mar 01 '16 at 10:14
  • i think you're not increment $count, and still it doesn't work after i tried increment it. @deceze still not working after i remove $count. – Sairam Salim Mar 01 '16 at 10:39
  • @Sairam You will have to be a bit more specific about what "not working" means! What's the generated Javascript output? Have you looked at your Javascript console for errors? Have you debugged with known good data instead of data from the database? – deceze Mar 01 '16 at 10:41
  • @chintanmirani yup it works thanks. i just know how json_encode works and the array shows `title: "title", start: "date"` until the last record. – Sairam Salim Mar 02 '16 at 03:19