Search code examples
javascriptphpmysqlphpmyadmintimeline

PHP in JavaScript integration code


I tried several options but are not working.... I have a code

<script type="text/javascript">
                $(document).ready(function() {
    <?php
    $sql = mysql_query("SELECT * FROM `info` WHERE `active` = '1' ORDER BY `id` DESC");
    $id = 'id';
    $title = 'title';
    $content = 'content';
    while ($rows = mysql_fetch_assoc($sql)){
    ?>
                    var timeline_data = 
                   [
                        { 
                            title:    '<?php echo $rows[$title];  ?>',
                            content:  '<?php echo $rows[$content];  ?>'
                        }
                   ];
    <?php
     }
     ?>
                    var timeline = new Timeline($('#timeline'), timeline_data);
                    timeline.setOptions({
                       animation:       true,
                        lightbox:        true,
                        first_separator: true,
                        max:             2,
                        loadmore:        5,
                        separator:       'year',
                        columnMode:      'dual',
                        order:           'desc'
                    });
                    timeline.display();
                });

</script>

without php code, timeline works correctly, when I added the code php, timeline does not work, nothing appears, Please help to resolve this error. Thank you


Solution

  •  <script type="text/javascript">
                        $(document).ready(function() {
            <?php
            $sql = mysql_query("SELECT * FROM `info` WHERE `active` = '1' ORDER BY `id` DESC");
            $id = 'id';
            $title = 'title';
            $content = 'content';
        ?>
          var timeline_data =[];
           <?php while ($rows = mysql_fetch_assoc($sql)){
            ?>
                             timeline_data[] = 
    
                                { 
                                    title:    '<?php echo $rows[$title];  ?>',
                                    content:  '<?php echo $rows[$content];  ?>'
                                }
                           ;
            <?php
             }
             ?>
                            var timeline = new Timeline($('#timeline'), timeline_data);
                            timeline.setOptions({
                               animation:       true,
                                lightbox:        true,
                                first_separator: true,
                                max:             2,
                                loadmore:        5,
                                separator:       'year',
                                columnMode:      'dual',
                                order:           'desc'
                            });
                            timeline.display();
                        });
    
        </script>