简体   繁体   中英

Trying to display news from mysql using jquery newsticker

Hi I have created a table in my db

CREATE TABLE `newsticker` (
  `NID` bigint(20) NOT NULL AUTO_INCREMENT,
  `news` text CHARACTER SET utf8 NOT NULL,
  `status` char(1) CHARACTER SET utf8 NOT NULL DEFAULT '0',
  PRIMARY KEY (`NID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

I have these functions

function insert_get_newsticker($var)
{
    global $conn;
    $query="SELECT * FROM newsticker WHERE status = '1' ORDER BY NID DESC";
    $results = $conn->execute($query);
    $ns = $results->getrows();
    return $ns;
}

I am using this in my tpl file

{literal}
<script language="javascript" type="text/javascript" src="js/jquery.ticker.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('#js-news').ticker({
            speed: 0.10,
            pauseOnItems: 2000,
            controls: false,
            fadeInSpeed: 600,
            titleText: 'Our Latest News'
        });
    });
</script>
{/literal}

I have the following where I want the news to be displayed

<div id="tickerbg">
<div id="ticker-wrapper" class="has-js">
      <ul id="js-news" class="js-hidden">
<li></li>
<ul>
</div>
</div>

I think I need some kind of for loop to be able to display the contents of the news table just having a mind blank any ideas

thanks

So, you have the $ns variable passed to template engine.
It depends on what template engine you are using.
In smarty:

<ul>
{foreach from=$ns item=news_item}
    <li>{$news_item.news}</li>
{/foreach}  
</ul>  

In pure php:

<ul>  
<? foreach($ns as $news_item)?>  
<li><?= $news_item['news'] ?></li>
<? } ?>
</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM