reading contents in text files(php)

hi, i'm newbie in php. hopefully u can help me.
text file content about list of books-: series no.,title book,author,price
format text file:
a1;The King;Daniel David;$123.00
a2;The 7 Dwafs;Amy And;$25.00

on the web,i use table and it will displayed as below:
Title Book : The King
Author : Daniel David
Price : $123.00

Title Book : The 7 Dwafs
Author :Amy And
Price : $25.00

i have problem on displayed the 2nd content in text files. my coding as below:

<?php

// get contents of a file into a string
$strFileName = "ListBook.txt";
$fileArray=file("ListBook.txt");

$OpenFile = fopen ($strFileName, "r");
$strContent = fread ($OpenFile, filesize ($strFileName));
fclose ($OpenFile);


//split srting with explode
$split = explode(";",$strContent);


//this prints the array
//print_r($split);


echo "<TABLE>";
for($j=0;$j";
echo "
";

foreach($split as $strText)
{

switch($i)
{
case 0:
echo "Title Book =$strText
";
break;
case 1:
echo "Author =$strText
";
break;
case 2:
echo "Price =$strText
";
break;
}
$i++;
}

echo "";
echo "
";

}
echo "";
?>
hope somebody can guide me how to code correctly.
tq..

Comments

  • : hi, i'm newbie in php. hopefully u can help me.
    : text file content about list of books-: series no.,title book,author,price
    : format text file:
    : a1;The King;Daniel David;$123.00
    : a2;The 7 Dwafs;Amy And;$25.00
    :
    : on the web,i use table and it will displayed as below:
    : Title Book : The King
    : Author : Daniel David
    : Price : $123.00
    :
    : Title Book : The 7 Dwafs
    : Author :Amy And
    : Price : $25.00
    :
    : i have problem on displayed the 2nd content in text files. my coding as below:
    :
    : [code]<?php
    :
    : // get contents of a file into a string
    : $strFileName = "ListBook.txt";
    : $fileArray=file("ListBook.txt");
    :
    : $OpenFile = fopen ($strFileName, "r");
    : $strContent = fread ($OpenFile, filesize ($strFileName));
    : fclose ($OpenFile);
    :
    :
    : //split srting with explode
    : $split = explode(";",$strContent);
    :
    :
    : //this prints the array
    : //print_r($split);
    :
    :
    : echo "<TABLE>";
    : for($j=0;$j
    ";
    :
    : foreach($split as $strText)
    : {
    :
    : switch($i)
    : {
    : case 0:
    : echo "Title Book =$strText";
    : break;
    : case 1:
    : echo "Author =$strText";
    : break;
    : case 2:
    : echo "Price =$strText";
    : break;
    : }
    : $i++;
    : }
    :
    : echo "";
    : echo "
    ";
    :
    : }
    : echo "";
    : ?>[/code]
    : hope somebody can guide me how to code correctly.
    : tq..
    :
    :
    You read the file twice (the file() function and just after that the fopen() function) - choose what you want to use!
    This should work (not tested, may contain syntax errors):
    [code]
    // get contents of a file into a string
    $fileArray=file("ListBook.txt");

    echo "";
    for($j=0;$j
    ";

    $split = explode(";",$fileArray[$j]);
    echo "Title Book =$split[1]
    ";
    echo "Author =$split[2]
    ";
    echo "Price =$split[3]
    ";

    echo "
    ";
    echo "
    ";

    }
    echo "
    ";
    [/code]
    [hr][red][italic][b]N[/b][/red][blue]et[/blue][red][b]G[/b][/red][blue]ert[/italic][/blue][hr]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion