Page 1 of 1
PLEASE HELP AM NEW TO PHP
Posted: Tue Oct 16, 2007 9:34 am
by IbnMuhammad
Hey every one,
I got this problem with my code and would love it if any one could help with this probelm.
Am creating a program that reconises commas in text file (comma delimited code) and i want to implement the same function and loop. This
program should get the file, store it ina array then enter a loop that jumps in to the
function.
here is the txt file:
Take That, Greatest Hits, 2007, 10.99, 35
50cent, Am Back, 2005, 11.88, 70
Mike Jone, Never Sing Alone, 2007, 12.99, 30
Terry Gamble, Alone, 1987, 13.99, 40
Michael Jackson, Am Bad, 1990, 15.99, 60
The problem is the code below displays this:
Take That
Am Back
2007
13.99
60
BUT IT SHOULD PRINT ALL THE INFORMATION IN THE TXT FILE ONE AFTER THE FILE UNDER EACH
OTHER.
PLEAAASEEE HELPPPPPPP ME WITH MY PROBLEM AM VERY NEW TO PROGRAMMING
Code: Select all
<?
$filename = "filename.txt";
$filepointer = fopen($filename, "r");
$myarray = file ($filename);
for ($mycount = 0; $mycount < count ($myarray);$mycount++)
{
$aline = $myarray[$mycount];
$cd = getvalue($myarray[0], $mycount);
print "$cd" . "<br> \n";
}
function getvalue($file, $mycount)
{
$intoarray = explode (",", $file);
return $intoarray[$mycount];
}
fclose($filepointer);
?>
Posted: Tue Oct 16, 2007 10:38 am
by arjan.top
Hmmmm... the problem is here:
Code: Select all
function getvalue($file, $mycount)
{
$intoarray = explode (",", $file);
return $intoarray[$mycount];
}
When you explode you get an array ($intoarray), but then you return just one value from the array ($intoarray[$mycount]), $myxount is defined in for loop so it is incremented every time function is called ...
You print something like this:
$intoarray[0]
$intoarray[1]
$intoarray[2]
etc...
I hope you see the problem now

HMMMM
Posted: Tue Oct 16, 2007 10:42 am
by IbnMuhammad
Thanks for the message:
Am still trying to figure it out and i am still lost if could please give me some more hints would be great.
Posted: Tue Oct 16, 2007 10:57 am
by John Cartwright
What is the desired output?
Posted: Tue Oct 16, 2007 11:08 am
by IbnMuhammad
The output should look like this:
Take That
Greatest Hits
2007
10.99
35
50cent
Am Back
2005
11.88
70
Mike Jone
Never Sing Alone
2007
12.99
30
Terry Gamble
Alone
1987
13.99
40
Michael Jackson
Am Bad
1990
15.99
60
Posted: Tue Oct 16, 2007 11:10 am
by IbnMuhammad
The array has this information froma txt file:
Array
(
[0] => Take That, Greatest Hits, 2007, 10.99, 35
[1] => 50cent, Am Back, 2005, 11.88, 70
[2] => Mike Jone, Never Sing Alone, 2007, 12.99, 30
[3] => Terry Gamble, Alone, 1987, 13.99, 40
[4] => Michael Jackson, Am Bad, 1990, 15.99, 60
)
Posted: Tue Oct 16, 2007 11:24 am
by arjan.top
Code: Select all
<?
$filename = "filename.txt";
$filepointer = fopen($filename, "r");
$myarray = file ($filename);
for ($mycount = 0; $mycount < count ($myarray);$mycount++)
{
$aline = $myarray[$mycount];
$cd = explode(",", $myarray[$mycount]);
print $cd[0] . "<br> \n";
print $cd[1] . "<br> \n";
print $cd[2] . "<br> \n";
print $cd[3] . "<br> \n";
print $cd[4] . "<br> \n";
}
fclose($filepointer);
?>
This will give you the output you want ...
Posted: Tue Oct 16, 2007 11:30 am
by patrikG
Hi IbnMuhammad,
welcome to the forum. Make sure you acquaint yourself with the
Forum Rules - especially
Forum Rules wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
- 2.1 When asking a question, be as specific as you can be, provide the relevant code and any other information that is helpful in a concise and coherent manner. If you are not sure how to do that, read these guidelines.
Also, you're probably familiar with the fact that ALL CAPS are considered as shouting. Don't. It's simply annoying.
Cheers & good luck with your project.
PatrikG
Posted: Tue Oct 16, 2007 11:31 am
by IbnMuhammad
Thats some excellent peace of help but is there any way of doing it with the function please help if you can and thank you for all your time.
Posted: Tue Oct 16, 2007 11:34 am
by IbnMuhammad
patrikG wrote:Hi IbnMuhammad,
welcome to the forum. Make sure you acquaint yourself with the
Forum Rules - especially
Forum Rules wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
- 2.1 When asking a question, be as specific as you can be, provide the relevant code and any other information that is helpful in a concise and coherent manner. If you are not sure how to do that, read these guidelines.
Also, you're probably familiar with the fact that ALL CAPS are considered as shouting. Don't. It's simply annoying.
Cheers & good luck with your project.
PatrikG
Yes sure sorry about that ill stick to the fourm rules.

Posted: Tue Oct 16, 2007 11:41 am
by RobertGonzalez
Have a look at
fgetcsv(). It might be of use here.
Posted: Wed Oct 17, 2007 12:44 am
by crystal ship
This may help you.
Code: Select all
<?
$filename = "filename.txt";
$filepointer = fopen($filename, "r");
$myarray = file ($filename);
$myarray=implode(',',$myarray);
$myarray=explode(',',$myarray);
foreach($myarray as $key=>$data){
echo $data."<br>";
}
?>
Posted: Wed Oct 17, 2007 10:38 am
by RobertGonzalez
This is straight out of the manual for
fgetcsv():
Code: Select all
<?php
$row = 1;
$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
?>
Have you tried this sample code yet?