Page 1 of 1

Reading the Bible from a TXT file

Posted: Tue Mar 23, 2010 11:57 am
by auvi1
Hello every one, I'm sure I'm on the right place. :D
Firs, I'm new to php but want to learn, so I'm getting to it, but for now here is my problem.

I'm building a website (HTML & CSS mostly) for a Christian group, they give me the bible on a text file...
so I suppose I have to put some link (when clicked) to open a PopUp with the correct row from the bible...
Inside that text file, data is separated by tabs, here are a few rows from that file to help me better explain myself.

---------------------------------------------------------------------------------------------
1 1 1 1 En el principio creó Dios los cielos y la tierra.
2 1 1 2 Y la tierra estaba desordenada y vacía, y las tinieblas estaban sobre la faz del abismo, y el espíritu de Dios se movía sobre la faz de las aguas.
3 1 1 3 Y dijo Dios: Sea la luz; y fue la luz.
4 1 1 4 Y vio Dios que la luz era buena; y apartó Dios a la luz de las tinieblas.
---------------------------------------------------------------------------------------------

The first tab is the order verse #
The second tab is the Book name
The third tab is the chapter
The forth tab is the actual verse #
the fifth tab is the actual text of that verse

So far I managed to do this:

<?php
$lines = file ("bible.txt");
$l_count = count($lines);
for($x = 0; $x< $l_count; $x++)
{
}
echo $lines[1];
?>

and I get as a resolute this:

1 1 1 1 En el principio creó Dios los cielos y la tierra.

What I need is to output the verse like this:
1. Ignore first tab
2. Convert the second tab into (a string) the books name (from an array I guess)
3. Output the third and forth tabs next to the outputted string from the second tab with a semicolon in the middle of the third and forth echo string ex. (Genesis 1:1 En el principio creó Dios los cielos y la tierra.)
4. Output the actual verse after the echoed tabs string (like in the example above)

This is possibly a ticket to heaven so there. Can some one help me with this?

Thank You for your help :wink:

Re: Reading from a TXT file

Posted: Tue Mar 23, 2010 12:15 pm
by pickle
Please post in the correct forum. Moved.

Re: Reading from a TXT file

Posted: Tue Mar 23, 2010 12:19 pm
by auvi1
pickle wrote:Please post in the correct forum. Moved.
Sorry :cry: I don't know where the correct forum is, please forgive my ignorance.

Re: Reading the Bible from a TXT file

Posted: Tue Mar 23, 2010 12:59 pm
by AbraCadaver
I would pull this into a database table and create a books table as well. Then it would be easy to search, display only one book, etc. However, to use the file if they are tabs, then something like this should work:

Code: Select all

$lines = file("bible.txt");
$books = array(1=>"Genesis"); //etc, etc.
 
foreach($lines as $line) {
    $parts = explode("\t", $line);
    $book  = $books[$parts[1]];
    $chap  = $parts[2];
    $verse = $parts[3];
    $text  = $parts[4];
 
    echo "{$book} {$chap}:{$verse} {$text}\n";
}

Re: Reading the Bible from a TXT file

Posted: Tue Mar 23, 2010 2:01 pm
by auvi1
OK, but here is the thing... how do I call a specific verse... the code you give me it's grate but it outputs the whole bible with Genesis as the name for all the books.

I'll attach the txt file so that you can see what I mean?

Thanks your great!

Re: Reading the Bible from a TXT file

Posted: Tue Mar 23, 2010 2:39 pm
by pickle
Ya, put this in a database - it'll make searching & working with the text MUCH easier.

Make the table the same structure as the text file, but convert the second "column" to book name. I think it's fine to store just the book name in the database, because no one says "Now let's read book 23, chapter 12, verse 1", they say "Let's read Ecclesiastes chapter 12, verse 1" (I don't think Ecclesiastes is the 23rd book - that's just an example)

Re: Reading the Bible from a TXT file

Posted: Tue Mar 23, 2010 2:47 pm
by AbraCadaver
That was just an example. You need to add the rest of the books to the $books array. This is one way to call a specific verse (example only):

Code: Select all

function getVerse($book, $chapter, $number) {
 
    $lines = file("bible.txt");
    $books = array(1=>"Genesis"); //etc, etc.
 
    foreach($lines as $line) {
        $parts = explode("\t", $line);
 
        if($parts[1] == $book && $parts[2] == $chapter && $parts[3] == $number) {
            $book  = $books[$parts[1]];
            $text  = $parts[4];                    
            return "{$book} {$chapter}:{$number} {$text}\n";
        }
    }
    return "NOT FOUND!";
}
 
echo getVerse(1, 1, 4);

Thank You abraCadaver

Posted: Tue Mar 23, 2010 10:34 pm
by auvi1
You are incredible :D , It's perfects!, this is how it ended up:

Code: Select all

 
function getVerse($book, $chapter, $number) {
 
    $lines = file("biblia/valera1602_1865.txt");
    $books = array(1=>"Génesis", 2=>"Exodo", 3=>"Leviticos", 4=>"Numeros", 5=>"Deutoronomios", 6=>"Josué", 7=>"Jueces", 8=>"Rut", 9=>"ISamuel", 10=>"II Samuel", 11=>"I Reyer", 12=>"II Reyes", 13=>"I Crónicas", 14=>"II Crónicas", 15=>"Esdras", 16=>"Nehemías", 17=>"Ester", 18=>"Job", 19=>"Salmos", 20=>"Proverbios", 21=>"Eclesiastés", 22=>"Cantares", 23=>"Isaías", 24=>"Jeremías", 25=>"Lamentaciones", 26=>"Ezequiel", 27=>"Daniel", 28=>"Oseas", 29=>"Joel", 30=>"Amos", 31=>"Abdías", 32=>"Jonás", 33=>"Miqueas", 34=>"Nahúm", 35=>"Habacuc", 36=>"Sofonías", 37=>"Hageo", 38=>"Zacarías", 39=>"Malaquías", 40=>"Mateo", 41=>"Marcos", 42=>"Lucas", 43=>"Juan", 44=>"Hechos", 45=>"Romanos", 46=>"1 Corintios", 47=>"2 Corintios", 48=>"Gálatas", 49=>"Efesios", 50=>"Filipenses", 51=>"Colosenses", 52=>"1 Tesalonicenses", 53=>"2 Tesalonicenses ", 54=>"1 Timoteo", 55=>"2 Timoteo ", 56=>"Tito", 57=>"Filemón", 58=>"Hebreos", 59=>"Santiago", 60=>"1 Pedro", 61=>"2 Pedro ", 62=>"1 Juan", 63=>"2 Juan", 64=>"3 Juan ", 65=>"Judas", 66=>"Apocalipsis");
 
    foreach($lines as $line) {
        $parts = explode("\t", $line);
 
        if($parts[1] == $book && $parts[2] == $chapter && $parts[3] == $number) {
            $book  = $books[$parts[1]];
            $text  = $parts[4];
            return "{$book} {$chapter}:{$number} {$text}\n";
        }
    }
    return "NOT FOUND!";
}
echo getVerse(1,1,1);
 
It works perfect.

Now... just one last thing: How do I select one book, one chapter and 8 verses? what if somebody whant to read Genesis 3:5-20? You don't have to help me anymore, my friend you've all ready done enough good.

But if you can, We'll love you forever. :wink:

Re: Reading the Bible from a TXT file

Posted: Wed Mar 24, 2010 12:24 pm
by AbraCadaver
Not the best way, but I just hacked the last one. This is a new function. You can use it to get single and multiple or have the first function for single and the second for multiple. This returns an array even if there is only one result. These are just simple examples to get you started. You need to learn and then refactor and cleanup.

Code: Select all

function getVerses($book, $chapter, $number, $last=false) {
 
    $lines = file("biblia/valera1602_1865.txt");
    $books = array(1=>"Génesis", 2=>"Exodo", 3=>"Leviticos", 4=>"Numeros", 5=>"Deutoronomios", 6=>"Josué", 7=>"Jueces", 8=>"Rut", 9=>"ISamuel", 10=>"II Samuel", 11=>"I Reyer", 12=>"II Reyes", 13=>"I Crónicas", 14=>"II Crónicas", 15=>"Esdras", 16=>"Nehemías", 17=>"Ester", 18=>"Job", 19=>"Salmos", 20=>"Proverbios", 21=>"Eclesiastés", 22=>"Cantares", 23=>"Isaías", 24=>"Jeremías", 25=>"Lamentaciones", 26=>"Ezequiel", 27=>"Daniel", 28=>"Oseas", 29=>"Joel", 30=>"Amos", 31=>"Abdías", 32=>"Jonás", 33=>"Miqueas", 34=>"Nahúm", 35=>"Habacuc", 36=>"Sofonías", 37=>"Hageo", 38=>"Zacarías", 39=>"Malaquías", 40=>"Mateo", 41=>"Marcos", 42=>"Lucas", 43=>"Juan", 44=>"Hechos", 45=>"Romanos", 46=>"1 Corintios", 47=>"2 Corintios", 48=>"Gálatas", 49=>"Efesios", 50=>"Filipenses", 51=>"Colosenses", 52=>"1 Tesalonicenses", 53=>"2 Tesalonicenses ", 54=>"1 Timoteo", 55=>"2 Timoteo ", 56=>"Tito", 57=>"Filemón", 58=>"Hebreos", 59=>"Santiago", 60=>"1 Pedro", 61=>"2 Pedro ", 62=>"1 Juan", 63=>"2 Juan", 64=>"3 Juan ", 65=>"Judas", 66=>"Apocalipsis");
    
    if(!$last) {
        $last  = $number;
    }
 
    foreach($lines as $line) {
        $parts = explode("\t", $line);
 
        if($parts[1] == $book && $parts[2] == $chapter && ($parts[3] >= $number && $parts[3] <= $last)) {
            $book  = $books[$parts[1]];
            $this_number = $parts[3];
            $text  = $parts[4];
            $results[] = "{$book} {$chapter}:{$this_number} {$text}\n";
        }
    }
    if(empty($results)) {
        $results[] = "NOT FOUND";
    }
    return $results;
}
 
$verses = getVerse(1,1,1,4);
 
foreach($verses as $verse) {
    echo $verse;
}