开发者

Populate menu list with multiple text files using php

I'm trying to build a kiosk on a local machine, so no online requirement. I am going to build a big menu which will show exhibition stores and products. Due to the stores and products will always change, the client has requested to use a simple text file - .txt to populate the menu.

I used jQuery last week but unfortunately it didn't work. So after searching I've decided to use php instead. I'm new to php, but I have given it a try today, I have only managed to populate one list with the external file kate.txt

Here is my code for the page

<body>
    <ul class="sf-menu">
        <li class="current">
            <a href="#a">Area1</a>
            <ul>
                <li class="current">
                    <a href="#ab">Kate</a>
                    <ul>
                        <?php
                            //variables
                            $dir = "C:\wamp\www\Menu";
                            $txtfile="Textfiles\kate.txt";
                            foreach (glob("$txtfile") as $filename) {   
                                $file = $filename;
                                $contents = file($file); 
                                $string = implode("<li>", $contents);
                            }
                        ?>                      
                        <li><a href="#"><?php echo $string ?></a></li>
                    </ul>
                </li>
                <li>    
                    <a href="#">Cathy</a>
                    <ul>
                        <li><a href="#">Banana</a></li>
                        <li><a href="#">Apple</a></li>      
                    </ul>
                </li>
            </ul>
    开发者_如何学运维    </li>
        <li>
            <a href="#">Area2</a>
            <ul>
                <li>
                    <a href="#">lily</a>
                    <ul>
                        <li><a href="#">Watermelon</a></li>
                        <li><a href="#">Grapefruit</a></li>
                    </ul>
                </li>
                <li>
                    <a href="#">John</a>
                    <ul>
                        <li><a href="#">Orange</a></li>
                        <li><a href="#">Pineapple</a></li>          
                    </ul>
                </li>
            </ul>
        </li> <!--current-->
    </ul> <!--sf-menu-->
</body>

Can someone please give me some direction on the php code? I don't mind how the text files are structured in a folder, as long as I can populate all the menu list items and links with a text file or multiple text files not in the html code.


Assuming that your text file holds all your list items

kate.txt:

<li><a href="#">stuff</a></li>
<li><a href="#">stuff</a></li>
...

the php file

<?php
  $txtfile="Textfiles\kate.txt";
  $content = file_get_contents($txtfile);

<li class="current">
   <a href="#ab">Kate</a>
   <ul>
   <?php echo $content ?>
   </ul>
</li>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜