drkj41932 2014-02-04 19:44
浏览 17
已采纳

从数据库中抓取数组,然后使用每个数组值获取信息

totally stumped in this, basically I'm getting a comma seperated list from a user table, using it as an array and then using each value in the array to fetch data from a different table and output then.

$award_array = array($user_class->awards);
foreach($award_array as $award) {
    $getaward = mysql_query("SELECT `name`, `text`, `image` FROM `awards_av` WHERE `id` = '".$award."'");
    $awardstuff = mysql_fetch_array($getaward);
    echo "<img src='".$awardstuff['image']."' alt='".$awardstuff['name']."' title='".$awardstuff['text']."' />";
}

This is only giving out the first number in the array ($user_class->awards in this case is 1,2,3,4,5,6)

Any help is much appreciated!

  • 写回答

1条回答 默认 最新

  • drmq16019 2014-02-04 20:07
    关注

    I think I see the problem. You cannot simply take a string of a comma separated list, and throw it inside an array() tag and expect it to automatically convert it to an array. You must use the explode() function to do that.

    What you're trying to do:

    <?php
    $string = 'a,b,c,d,e';
    $myarray = array($string);
    foreach ($myarray as $k => $v) {
        print $v .'<br />';
    }
    ?>
    

    Except that doesn't work. You need to convert the comma-separated list of values (which is a string) into an array, but you don't use array() to do that. You use PHP's built-in function called explode() -> http://php.net/explode like this...

    <?php
    $string = 'a,b,c,d,e';
    $myarray = explode(',' $string);
    foreach ($myarray as $k => $v) {
        print $v .'<br />';
    }
    ?>
    

    I think that is the problem you're having

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记