PLEASE HELP AM NEW TO PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
IbnMuhammad
Forum Newbie
Posts: 6
Joined: Tue Oct 16, 2007 9:29 am

PLEASE HELP AM NEW TO PHP

Post 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);
?>
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Post 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 :)
IbnMuhammad
Forum Newbie
Posts: 6
Joined: Tue Oct 16, 2007 9:29 am

HMMMM

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What is the desired output?
IbnMuhammad
Forum Newbie
Posts: 6
Joined: Tue Oct 16, 2007 9:29 am

Post 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
IbnMuhammad
Forum Newbie
Posts: 6
Joined: Tue Oct 16, 2007 9:29 am

Post 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
)
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Post 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 ...
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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
IbnMuhammad
Forum Newbie
Posts: 6
Joined: Tue Oct 16, 2007 9:29 am

Post 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.
IbnMuhammad
Forum Newbie
Posts: 6
Joined: Tue Oct 16, 2007 9:29 am

Post 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. :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Have a look at fgetcsv(). It might be of use here.
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post 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>";
}
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
Post Reply