检索数据库条目并将它们放入php数组

时间:2015-11-30 08:16:54

标签: javascript php sql arrays postgresql

我的数据库包含带有文件路径的记录。 我想检索这些记录并将它们放入php数组中,以便我可以在我的网页上显示这些图片。图片将显示在页面中央,左右按钮用于导航。

我希望实现的目标是,当按下左键时,它将显示上一条记录,当按下右键时,它将显示下一条记录

我告诉我们使用javascript和数组是最好的选择。因此,如果有人能告诉我如何这样做,那将非常感激

条目将由picture_id排序,包含路径的字段标记为file_location

数据库查询。表presentation被赋予唯一的codeid,而表slide包含file_location,并且还有一个唯一id以及它所属的演示文稿的id

$sql_pres_slide= new CGenRs("SELECT * FROM presentation", $cao);
        $sql_pres_slide->first();
        $sql_pres_select= new CGenRs("SELECT * FROM slide where presentation_id='".$sql_pres_slide->valueof('pres_id')."'", $cao);
        $sql_pres_select->first();

html

<table border="1" align="center">
        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>

    <tr>
        <td><span onclick="<?php echo "PREVIOUS DATABASE RECORD" ?>" style="color: #999; font-size: 25px" class="glyphicon glyphicon-menu-left" ></span></td>
        <td> <div class="backgrund_img" >

        <img src="../administration/file_manager/files/samsungS4-portrait-white.png" class="background_img" style="height:786px; width:406px; " />
        <div id="inside_img"><img src="<?php echo $sql_pres_select->valueof('file_location')  ?>" style="width: 360px; height:640px;"/></div>

    </div>  
</td>
<td><span onclick="<?php echo "NEXT DATABASE RECORD" ?>" style="color: #999; font-size: 25px" class="glyphicon glyphicon-menu-right" ></span></td>
    </tr>
       </table>

1 个答案:

答案 0 :(得分:0)

我不确定是否理解了你的问题,但在我看来有一点混乱...... 你想要实现一个轮播(网上有很多,准备好使用)但总的来说,当你用户按下左或右按钮时,你正在讨论使用javascript获取图像。

在我看来,这可以使用两种不同的方法: 1.将所有图像加载到javascript数组中 2.在javascript var中加载第一个图像,并使用ajax调用返回或转发。

让我们看几个例子

第一种方法:

<script  type="text/javascript">
var images=new Array(<?php implode($images);?>);
$().ready(function(){
  $('prev').click(function(){
    //go to previous image
  })
  $('next').click(function(){
    //go to next image
  })
})
</script>

第二种方法:

<script  type="text/javascript">
var images=<?php $images[0];?>;
$().ready(function(){
  $('prev').click(function(){
    $.ajax({
      url: "yourPhpImageScroller.php",
      data: {direction:'prev'},
      success: function(data){
        //go to previous image
      }
     });
    });
  }):
  $('next').click(function(){
    $.ajax({
      url: "yourPhpImageScroller.php",
      data: {direction:'next'},
      success: function(data){
        //go to next image
      }
     });
    });
  })
})
</script>

我假设$ images是包含所有图像的php数组