【问题标题】:javascript array to php database and get it back as javascript arrayjavascript数组到php数据库并将其作为javascript数组取回
【发布时间】:2014-02-15 17:02:47
【问题描述】:

嗨,我有 javascript 数组,我想将其转换为 mysql 数据库,然后我想将其作为 javascript 数组取回

var bookmark = new Array();
bookmark[0] = new Array();
bookmark[1] = new Array();
bookmark[2] = new Array();

// set your bookmarks here: (If you do not fill 'thumb' for thumbnail will be used title)
// 1. BLOCK
bookmark[0][0] = {
    'title': 'gmail',
    'url': 'http://gmail.com',
    'thumb': 'gmail.png'
};
bookmark[0][1] = {
    'title': 'wikipedia',
    'url': 'http://wikipedia.org',
    'thumb': 'wikipedia.png'
};
bookmark[0][2] = {
    'title': 'YahooMail',
    'url': 'http://mail.yahoo.com',
    'thumb': 'yahoomail.png'
};
bookmark[0][3] = {
    'title': 'hotmail',
    'url': 'http://www.outlook.com/',
    'thumb': 'outlook.png'
};
bookmark[0][4] = {
    'title': 'youtube',
    'url': 'http://youtube.com',
    'thumb': 'youtube.png'
};
bookmark[0][5] = {
    'title': 'google',
    'url': 'http://google.com',
    'thumb': 'google.png'
};
bookmark[0][6] = {
    'title': 'twitter',
    'url': 'http://twitter.com/',
    'thumb': 'twitter.png'
};
bookmark[0][7] = {
    'title': 'MySpace',
    'url': 'http://www.myspace.com/',
    'thumb': 'myspace.png'
};
bookmark[0][8] = {
    'title': 'msn',
    'url': 'http://www.msn.com/',
    'thumb': 'msn.png'
};
bookmark[0][9] = {
    'title': 'facebook',
    'url': 'http://facebook.com',
    'thumb': 'facebook.png'
};
bookmark[0][10] = {
    'title': 'Yahoo',
    'url': 'http://yahoo.com/',
    'thumb': 'yahoo.png'
};
bookmark[0][11] = {
    'title': 'bing',
    'url': 'http://www.bing.com/',
    'thumb': 'bing.png'
};
// end of 1. BLOCK
// 2. BLOCK
bookmark[1][0] = {
    'title': 'linkedin',
    'url': 'http://www.linkedin.com/',
    'thumb': 'linkedin.png'
};
bookmark[1][1] = {
    'title': 'digg',
    'url': 'http://digg.com/',
    'thumb': 'digg.png'
};
bookmark[1][2] = {
    'title': 'flickr',
    'url': 'http://www.flickr.com/',
    'thumb': 'flickr.png'
};
bookmark[1][3] = {
    'title': 'pinterest',
    'url': 'http://pinterest.com',
    'thumb': 'pinterest.png'
};
bookmark[1][4] = {
    'title': 'reddit',
    'url': 'http://www.reddit.com/',
    'thumb': 'reddit.png'
};
bookmark[1][5] = {
    'title': 'technorati',
    'url': 'http://technorati.com/',
    'thumb': 'technorati.png'
};
bookmark[1][6] = {
    'title': 'eurosport',
    'url': 'http://espn.com',
    'thumb': 'espn.png'
};
bookmark[1][7] = {
    'title': 'delicious',
    'url': 'http://www.delicious.com/',
    'thumb': 'delicious.png'
};
bookmark[1][8] = {
    'title': 'ebay',
    'url': 'http://www.ebay.com/',
    'thumb': 'ebay.png'
};
bookmark[1][9] = {
    'title': 'amazon',
    'url': 'http://www.amazon.com/',
    'thumb': 'amazon.png'
};
bookmark[1][10] = {
    'title': 'tumblr',
    'url': 'http://www.tumblr.com/',
    'thumb': 'tumblr.png'
};
bookmark[1][11] = {
    'title': 'StumbleUpon',
    'url': 'http://www.stumbleupon.com/',
    'thumb': 'stumbleupon.png'
};
// end of 2. BLOCK
// 3. BLOCK
bookmark[2][0] = {
    'title': 'filenurse',
    'url': 'http://findgram.com/',
    'thumb': 'findgram.png'
};
bookmark[2][1] = {
    'title': 'upperpix',
    'url': 'http://upperpix.com/',
    'thumb': 'upperpix.png'
};
bookmark[2][2] = {
    'title': 'Videolikers',
    'url': 'http://clipstagram.com',
    'thumb': 'clip.png'
};
bookmark[2][3] = {
    'title': 'InfoVirus',
    'url': 'http://infovirus.org',
    'thumb': 'infovirus.png'
};
bookmark[2][4] = {
    'title': 'websyrup',
    'url': 'http://websyrup.net',
    'thumb': 'websyrup.png'
};
bookmark[2][5] = {
    'title': 'ArcadeOK',
    'url': 'http://arcadeok.com/',
    'thumb': 'arcadeok.png'
};
bookmark[2][6] = {
    'title': 'tvnations',
    'url': 'http://tvnations.com/',
    'thumb': 'tvnations.png'
};
bookmark[2][7] = {
    'title': 'Pixeditor',
    'url': 'http://pixeditor.net/',
    'thumb': 'pixeditor.png'
};
bookmark[2][8] = {
    'title': 'worldchat',
    'url': 'http://worldchat.tv/',
    'thumb': 'worldchat.png'
};
bookmark[2][9] = {
    'title': 'Discoverbit',
    'url': 'http://discoverbit.com/',
    'thumb': 'discoverbit.png'
};
bookmark[2][10] = {
    'title': 'virtualpiano',
    'url': 'http://virtualpiano.eu/',
    'thumb': 'virtualpiano.png'
};
bookmark[2][11] = {
    'title': 'MovieLikers',
    'url': 'http://movielikers.com',
    'thumb': 'movielikers.png'
};

我使用这个脚本从上面的数组中获取数据

var j=0;
for (j=0; j <= (num-1); j++) {      
    for(i=0;i<=11;i++){                             
        var title = bookmark[j][i]['title'];
        var url = bookmark[j][i]['url'];
        var thumb = bookmark[j][i]['thumb'];
        if(thumb==''){
            $('#thumb'+(j+1)+'-'+(i+1)).html('<img id="net" src="lib/net-back.png" /><a href="'+url+'"><div class="title">'+title+'</div></a>');
        }
        else{
            $('#thumb'+(j+1)+'-'+(i+1)).html('<a href="'+url+'"><img src="thumbs/'+thumb+'" /></a>');
        }
    };
};

不知道怎么转换
丢失了我的 javascript 脚本

【问题讨论】:

  • 我认为使用 JSON 对象会更容易,然后使用 json_encode、json_decode 更容易转换到 PHP 并返回
  • +1 使用 JSON,它会让你的生活更轻松。我不确定你在最后一行中“丢失我的 javascript”是什么意思?

标签: javascript php mysql arrays database


【解决方案1】:

请使用 JSON。带有“书签”变量的示例

javascript

var bookmarkJSON = JSON.stringify(bookmark); // => this is a string, save this to database

php

<?php 
   $bookmark;
   // get $bookmark value from database where you've saved it before
?>
<script>
   var bookmark = <?php echo $bookmark ?>; // => now you can use the "bookmark" variable in javascript
</script>

希望得到帮助:D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 2017-11-29
    • 2013-05-16
    • 2015-04-02
    • 2012-04-02
    • 1970-01-01
    • 2020-11-05
    相关资源
    最近更新 更多