在函数调用

时间:2015-12-02 07:23:01

标签: javascript php arrays string function

我正在撰写的网页将用于显示幻灯片图片,其名称以及说明。这是在不同的页面中输入的。我面临的问题是我为幻灯片名称创建的数组被视为一个字符串。因此它不会返回幻灯片名称,而只返回一个数字。任何帮助将不胜感激

这是我的代码。我使用alert来获取值的长度,这是当我看到数组被解析为单个字符串时

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

第二次尝试

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

正如您将看到的那样,var name数组与其他数组不同。这只是因为我尝试了一种不同的方法来查看它是否解决了问题。事实上它没有。

如果有人能指出我在这里正确的方向,我将不胜感激 我应该补充说,第一个结果显示正确,只有在调用函数时才会出现错误

我在浏览器中查看元素时获得的脚本。

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

请注意,var name1等于一个长字符串,这不应该发生。

使用json_encode时,我在浏览器脚本中获得了以下结果

<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 个答案:

答案 0 :(得分:1)

您可以使用您的阵列尝试json_encode

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

<强>更新

我已为name更改names并且有效:/

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

答案 1 :(得分:0)

试试这个:

var name1 = '<?php implode(",",$slide_name_array); ?>'; var name = name1.split(",");

答案 2 :(得分:0)

在不知不觉地尝试各种其他方法后,所有这些都归结为从

重命名变量
var name= <?php  echo json_encode($slide_name_array); ?>;

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

调整此变量的所有其他实例。让它成为一个教训。而是使用自己的变量!