0

Every day, you download a txt file that contains the url and then I have to manually update the index.html file of a pulldown menu. Is there a way to automatically update the url without having to edit the index file? This is an example of how and dialed the file.txt that drain each day and that below a portion of the index file code.

Text1, Text2, and Text3 to remain unchanged, while the url change daily

File_url.txt

<a href='http://example.com/update1/day1.txt'>text1</a><br/>
<a href='http://example.com/update2/day1.txt'>text2</a><br/> 
<a href='http://example.com/update3/day1.txt'>text3</a><br/>

in the index file, the url are inserted so

File index.html

    <!DOCTYPE html PUBLIC>



<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<title>CSS DropDown Menu</title>

<link rel="stylesheet" type="text/css" href="css/reset.css" media="screen" />

<link rel="stylesheet" type="text/css" href="css/simple.css" media="screen" />

</head>

<body>



<div id="drop-menu">

<ul id="menu">

  <li><a href="http://example.com/home.php"target="principale" >Home</a></li>

<ul>

</ul>

 </li>

 <li><a href="#"target="principale" >Extra</a>

<ul>
<li><a href='http://example.com/update1/day1.txt'target="principale">text1</a><br/></li>
<li><a href='http://example.com/update2/day1.txt'target="principale">text2</a><br/></li>
<li><a href='http://example.com/update3/day1.txt'target="principale">text3</a><br/></li>

</ul>

 </li>

 <li><a href="#">About</a></li>

 <li><a href="#">Contact</a></li>

</ul>

</div>

<iframe name="principale" src="http://example.com/home.php" marginheight="50" height="800" width="100%" allowfullscreen = "true" ></iframe>

</body>

</html>

For example, the next day I can find

File_url.txt

<a href='http://example.com/update1/day2.txt'>text1</a><br/>
<a href='http://example.com/update2/day2.txt'>text2</a><br/> 
<a href='http://example.com/update3/day2.txt'>text3</a><br/>

Is there a way to automatically update the reference Url perhaps using Text1, Text2, and Text3 that never change?

Boy
  • 15
  • 4
  • 1
    Where is the code that writes the index.html? Why not stick that inside an index.php and make that the index file (rather than static index.html). Read File_url.txt directly and dynamically generate your pulldown. – micah94 Apr 05 '17 at 16:05
  • I edited the post, now the index.html / index.php file is complete – Boy Apr 05 '17 at 16:40
  • Can you remove the tag php as this question no longer relates to php nor does the answer chosen reflect php. Thanks you – ctatro85 Apr 06 '17 at 19:30

2 Answers2

1

There are a couple of ways to solve this, One is to itterate over the file_url.txt in index.html( index.php? ) You could follow this example in the How to read a file line by line in php

Keep in mind 'file_url.txt' would be the path to where the file is located. Chances are if your structure is like this.

  • root dir

    • index.php
    • file_url.txt

You would want "./file_url.txt"

    ...  
    <li><a href="#"target="principale" >Extra</a>

      <ul>
          <?php if ($file = fopen("./file_url.txt", "r")) {
            while(!feof($file)) {
              $line = fgets($file);
              # do same stuff with the $line
                echo sprintf(
                  '<li>%s</li>',
                  $line
                );
            }
            fclose($file);
          } ?> 
     </ul>
 </li>

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

Otherwise, I believe javascript ( jQuery ) would have to be the way to go.

Community
  • 1
  • 1
ctatro85
  • 311
  • 3
  • 11
  • how should I edit the index.html file you can take an example, I'm currently running everything under localhost, are very novice with programming, cmq thank you for your answers – Boy Apr 05 '17 at 17:03
  • Added some basic structuring for your use. As you can see most people are suggesting to use client side scripting. Not sure that is the way you want to go as you lose SEO in some instances or if javcascript is turned off – ctatro85 Apr 05 '17 at 18:04
0

If you use server side script like PHP for example, you can use function like readfile() function to read the entire file then use foreach function to iterate over it's rows and render the items to your html.

But, if you don't use server side script at once, I think you can expose the txt file so it can be accessed via URL, for example localhost/file_url.txt then use javascript ajax function to get it's content from URL and render them into your HTML.

Here's the code using javascript to get the content of you file:

<!DOCTYPE html PUBLIC>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>CSS DropDown Menu</title>
    <link rel="stylesheet" type="text/css" href="css/reset.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="css/simple.css" media="screen" />
</head>
<body>
    <div id="drop-menu">
        <ul id="menu">
          <li><a href="http://example.com/home.php"target="principale" >Home</a></li>
          <li><a href="#"target="principale" >Extra</a></li>
        <ul>
        <ul id="dynamic-menu">

        </ul>
    </div>
    <iframe name="principale" src="http://example.com/home.php" marginheight="50" height="800" width="100%" allowfullscreen = "true" ></iframe>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            var menuRequest = $.ajax({
                url: 'http://localhost:30001/file_url.txt'
            });

            menuRequest.done(function(data, status, xhr) {
                var dynamicMenuWrapper = $('#dynamic-menu');
                dynamicMenuWrapper.html(data);
            });
        });

    </script>
</body>
</html>

*note: remember to make your txt file accessible by pointing it to a specific URL

  • Thanks for the help, I tried your edit, but it seems it does not work, in fact, under the extras menu, I can not find any link. – Boy Apr 05 '17 at 17:41
  • Have you placed id="dynamic-menu" to element where you wanna place your dynamic menus? here the logic of the script: 1. place an `id` to html element where you wanna place the dynamic menus, in my example I placed it inside `
      `, 2. get list of menu using ajax request from your file, 3. manipulate the html using javascript by appending list of menu you've got into html element which has id="dynamic-menu".
    – Surya Darma Putra Apr 05 '17 at 19:14
  • Sorry, how do you run your html script? By using web server such as XAMPP, etc or just open it directly from your file's directory? – Surya Darma Putra Apr 05 '17 at 19:18
  • Manually editing File_url.txt I knew it would work, but I have to change it by editing the file. `
  • Home
  • Extra
  • text2
    text3
    ` I find the links under the extras menu and it works, how I can directly add the changes before reading each string from the file – Boy Apr 05 '17 at 20:08
  • did you get your problem solved yet? Sorry for not responding for a while hehe – Surya Darma Putra Apr 06 '17 at 17:49