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条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法