1

I have some data stored in a text file, with values separated like this: Name1 | Number1 | Name2 | Number2 |

I call some separate values with php like this:

<?php
$fp = fopen('db.txt','r');
if (!$fp) {echo 'ERROR'; exit;}
$loop = 0;
while (!feof($fp)) {
$loop++;
$line = fgets($fp, 1024); //use 2048 if very long lines
$field[$loop] = explode ('|', $line);
?>

<p>Some text.. <?php echo ''.$field[$loop][2].''; ?></p>
<p>Some more text. Text and value <?php echo ''.$field[$loop][4].''; ?></p>

<?php
$fp++; }
fclose($fp); 
?>

Now I need build the exact same site, loaded from the same database (the .txt-file) but I can't use any php or server side lang at all. Is there any way to do this without server side so that I can save my files as .html? Is it possible to include a text file (and call separated values) with javascript?


Found something like this, but I don't know how to separate the values and only call/get one value at a time? Right now the whole text file is displayed in the span. Would be nice if you could do something similar to the php code.

<script>
jQuery.get('db.txt', function(data) {
   $('#text').html(data.replace('|','<br />'));
});
</script>
 <span id="text"></text>

Can you help me out here? :S


UPDATE: My answer

This is what I've got, think it works pretty well, or do you have any suggestions how to improve it?

<script type="text/javascript">
jQuery.get('db.txt', function(data) {
    $('#db').html(data);
     var element = $("#db").text().split("|"); 
     $('#1').text(element[1]);
     $('#3').text(element[3]);
   });
  </script>

  <span id="db" style="display:none;"></span>

  <span id="1"></span>
  <span id="3"></span>

If my text file looks like this Name1 | Number1 | Name2 | Number2 | it will display:

Number 1 Number 2

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
jessifelt
  • 75
  • 3
  • 9
  • 1
    You could use jQuery to GET the text file, parse it, and render it into the page. Or you could use some program to statically render the HTML (refreshing it every time the text file changes) and upload the result. – Thilo Dec 22 '11 at 08:27
  • 1
    Something like this: http://stackoverflow.com/questions/1981815/jquery-read-a-text-file – matino Dec 22 '11 at 08:29

1 Answers1

1

Look at the instructions here:

http://api.jquery.com/load/

On how you can use client side language such as Javascript to load content dynamically.

There is also another question here:

loading contents of variable in div/modal box

Showing how you can load that data into a variable to do some processing and parsing.

Community
  • 1
  • 1
Raul Marengo
  • 2,287
  • 1
  • 15
  • 10