dongyi1939 2015-12-02 07:23
浏览 70
已采纳

数组在函数调用时解析为String

The webpage I am writing will be used to display slides images, their names, as well as a description. This is entered in a different page. The problem I am facing though is that the array I have created for the slide names, is seen as a string. So it does not return the slide name but rather just a single digit. Any help would be appreciated

This is my code. I used an alert to get the length of the value and that is when I saw the array is parsed as a single string

<body >
        <?php
        $sql_pres_slide = new CGenRs("SELECT * FROM presentation where pres_code='" . pg_escape_string($_GET['show']) . "'", $cao);
        $sql_pres_slide->first();
        $sql_pres_select = new CGenRs("SELECT * FROM slide where presentation_id='" . $sql_pres_slide->valueof('pres_id') . "' order by slide_no ASC", $cao);
        $sql_pres_select->first();
        ?>
        <table border="1" align="center">
            <tr>
                <td></td>
                <td><p align="center" ><h1 align="center"><?php echo $sql_pres_slide->valueof('pres_name'); ?> </h1><p></td>
                <td></td>
            </tr>
            <tr>
                <td rowspan="2" ><button onclick="prevSlide(slide_no)" style="color: #999; font-size: 25px" class="glyphicon glyphicon-menu-left" ></button></td>
                <td rowspan="2"> 
                    <div class="backgrund_img" >
                        <img src="../administration/file_manager/files/samsungS4-portrait-white.png" alt="" class="background_img" style="height:786px; width:406px;" />
                        <div id="inside_img"><img src="<?php echo $sql_pres_select->valueof('file_location') ?>" id="display" style="width: 360px; height:640px;"/></div>
                    </div>  
                </td>
                <td rowspan="2"><button onclick="nextSlide(slide_no)" style="color: #999; font-size: 25px" class="glyphicon glyphicon-menu-right" ></button></td>
                <td rowspan="2" class="info"><div id="func_slide_name"><font color="red" > <?php echo $sql_pres_select->valueof('slide_name') ?></font></div><div id="func_slide_desc" class="slide_select"><?php echo $sql_pres_select->valueof('description') ?></div></td>
            </tr>
            <tr rowspan="2" >
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td><div ><?php
                        while (!$sql_pres_select->eof()) {
                            echo "<a href=''>" . $sql_pres_select->valueof('slide_no') . "</a>" . " ";
                            $sql_pres_select->next();
                        }
                        ?> </div></td>
                <td></td>
            </tr>
        </table>
        <?php $sql_pres_select->first(); ?>
    </body>   
    <?php
    $slide_img_array = array();
    $slide_desc_array = array();
    $slide_name_array = array();

    while (!$sql_pres_select->eof()) {
        array_push($slide_img_array, $sql_pres_select->valueof('file_location'));
        array_push($slide_desc_array, $sql_pres_select->valueof('description'));
        array_push($slide_name_array, $sql_pres_select->valueof('slide_name'));
        $sql_pres_select->next();
    }

    ?>
    <script>

        var slide_no = 0;
        var slides = <?php echo json_encode($slide_img_array); ?>;
        var desc = <?php echo json_encode($slide_desc_array); ?>;
        var name = <?php echo json_encode($slide_name_array); ?>;



        function nextSlide() {
            if (slide_no < slides.length) {
                slide_no++;
                document.getElementById("display").src = slides[slide_no];
                document.getElementById("func_slide_desc").innerHTML = desc[slide_no];
              document.getElementById("func_slide_name").innerHTML = name[slide_no];
              alert(name.length);
            }
        }
        function prevSlide() {
            if (slide_no > 0) {
                slide_no--;
                document.getElementById("display").src = slides[slide_no];
                document.getElementById("func_slide_desc").innerHTML = desc[slide_no];
                document.getElementById("func_slide_name").innerHTML = name[slide_no];
            }
        }
    </script>

The second attempt

<body >
    <?php
    $sql_pres_slide = new CGenRs("SELECT * FROM presentation where pres_code='" . pg_escape_string($_GET['show']) . "'", $cao);
    $sql_pres_slide->first();
    $sql_pres_select = new CGenRs("SELECT * FROM slide where presentation_id='" . $sql_pres_slide->valueof('pres_id') . "' order by slide_no ASC", $cao);
    $sql_pres_select->first();
    ?>
    <table border="1" align="center">
        <tr>
            <td></td>
            <td><p align="center" ><h1 align="center"><?php echo $sql_pres_slide->valueof('pres_name'); ?> </h1><p></td>
            <td></td>
        </tr>
        <tr>
            <td rowspan="2" ><button onclick="prevSlide(slide_no)" style="color: #999; font-size: 25px" class="glyphicon glyphicon-menu-left" ></button></td>
            <td rowspan="2"> 
                <div class="backgrund_img" >
                    <img src="../administration/file_manager/files/samsungS4-portrait-white.png" alt="" class="background_img" style="height:786px; width:406px;" />
                    <div id="inside_img"><img src="<?php echo $sql_pres_select->valueof('file_location') ?>" id="display" style="width: 360px; height:640px;"/></div>
                </div>  
            </td>
            <td rowspan="2"><button onclick="nextSlide(slide_no)" style="color: #999; font-size: 25px" class="glyphicon glyphicon-menu-right" ></button></td>
            <td rowspan="2" class="info"><div id="func_slide_name"><font color="red" > <?php echo $sql_pres_select->valueof('slide_name') ?></font></div><div id="func_slide_desc" class="slide_select"><?php echo $sql_pres_select->valueof('description') ?></div></td>
        </tr>
        <tr rowspan="2" >
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td><div ><?php
                    while (!$sql_pres_select->eof()) {
                        echo "<a href=''>" . $sql_pres_select->valueof('slide_no') . "</a>" . " ";
                        $sql_pres_select->next();
                    }
                    ?> </div></td>
            <td></td>
        </tr>
    </table>
    <?php $sql_pres_select->first(); ?>
</body>   
<?php
$slide_img_array = array();
$slide_desc_array = array();
$slide_name_array = array();

while (!$sql_pres_select->eof()) {
    array_push($slide_img_array, $sql_pres_select->valueof('file_location'));
    array_push($slide_desc_array, $sql_pres_select->valueof('description'));
    array_push($slide_name_array, $sql_pres_select->valueof('slide_name'));
    $sql_pres_select->next();
}
?>
<script>

    var slide_no = 0;
    var slides = <?php echo json_encode($slide_img_array); ?>;
    var desc = <?php echo json_encode($slide_desc_array); ?>;
    var name = new Array("<?php echo implode('","', $slide_name_array); ?>");
    alert(name.length);


    function nextSlide() {
        if (slide_no < slides.length) {
            slide_no++;
            document.getElementById("display").src = slides[slide_no];
            document.getElementById("func_slide_desc").innerHTML = desc[slide_no];
            document.getElementById("func_slide_name").innerHTML = name[slide_no];

        }
    }
    function prevSlide() {
        if (slide_no > 0) {
            slide_no--;
            document.getElementById("display").src = slides[slide_no];
            document.getElementById("func_slide_desc").innerHTML = desc[slide_no];
            document.getElementById("func_slide_name").innerHTML = name[slide_no];
        }
    }
</script>

As you will be able to see, the var name array differs from the rest. This is just because I tried a different method to see if it solves the problem. It in fact does not.

If anyone can point me in the right direction here, it would be much appreciated I should add that the first result displays correctly, it is only as soon as the function is called that the error comes forth

The script I got from viewing the element in my browser.

<script>

        var slide_no = 0;
        var slides = ["..\/administration\/file_manager\/presentation\/Slides\/30_chair.jpg","..\/administration\/file_manager\/presentation\/Slides\/30_RTAC20150901_1.png","..\/administration\/file_manager\/presentation\/Slides\/30_RTAC20150901_2.png","..\/administration\/file_manager\/presentation\/Slides\/30_RTAC20150901_3.png","..\/administration\/file_manager\/presentation\/Slides\/30_RTAC20150901_4.png","..\/administration\/file_manager\/presentation\/Slides\/30_RTAC20150901_5.png"];
        var desc = ["This is the the new 1st slide","Slide one","Slide two","Slide the third","Slide the fourth","Slide the fifth"];

        var name1 = 'The new slide 1,Slide 1,Slide 2 ,Slide 3,slide 4,Slide 5';
        var name = name1.split(",");



        function nextSlide() {
            if (slide_no < slides.length) {
                slide_no++;
                document.getElementById("display").src = slides[slide_no];
                document.getElementById("func_slide_desc").innerHTML = desc[slide_no];
              document.getElementById("func_slide_name").innerHTML = name[slide_no];
              alert(name.length);
            }
        }
        function prevSlide() {
            if (slide_no > 0) {
                slide_no--;
                document.getElementById("display").src = slides[slide_no];
                document.getElementById("func_slide_desc").innerHTML = desc[slide_no];
                document.getElementById("func_slide_name").innerHTML = name[slide_no];
            }
        }
    </script>

Note that var name1 is equal to one long string, this should not happen.

When using json_encode I achieved the following result in my browser script

<script>

        var slide_no = 0;
        var slides = ["..\/administration\/file_manager\/presentation\/Slides\/30_chair.jpg","..\/administration\/file_manager\/presentation\/Slides\/30_RTAC20150901_1.png","..\/administration\/file_manager\/presentation\/Slides\/30_RTAC20150901_2.png","..\/administration\/file_manager\/presentation\/Slides\/30_RTAC20150901_3.png","..\/administration\/file_manager\/presentation\/Slides\/30_RTAC20150901_4.png","..\/administration\/file_manager\/presentation\/Slides\/30_RTAC20150901_5.png"];
        var desc = ["This is the the new 1st slide","Slide one","Slide two","Slide the third","Slide the fourth","Slide the fifth"];
        var name = ["The new slide 1","Slide 1","Slide 2 ","Slide 3","slide 4","Slide 5"];




        function nextSlide() {
            if (slide_no < slides.length) {
                slide_no++;
                document.getElementById("display").src = slides[slide_no];
                document.getElementById("func_slide_desc").innerHTML = desc[slide_no];
              document.getElementById("func_slide_name").innerHTML = name[slide_no];
              alert(name.length);
            }
        }
        function prevSlide() {
            if (slide_no > 0) {
                slide_no--;
                document.getElementById("display").src = slides[slide_no];
                document.getElementById("func_slide_desc").innerHTML = desc[slide_no];
                document.getElementById("func_slide_name").innerHTML = name[slide_no];
            }
        }
    </script>
  • 写回答

3条回答 默认 最新

  • duandie5707 2015-12-02 08:24
    关注

    You can try json_encode with your array:

    var name = <?php echo json_encode($slide_name_array); ?>;
    

    UPDATE

    I've changed name for names and it works :/

    http://jsbin.com/tojase/1/edit?js,console

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?