Jump to content


Photo

PHP Script not working properly


  • Please log in to reply
7 replies to this topic

#1 phpstarter

phpstarter
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 09 June 2006 - 03:14 PM

Hi All,
I'm a new starter with PHP. Need urgent help with the following script as it does not make sense to me. Your help will be much appreciated in advance.

<?

$connection = mysql_connect("host", "username", "pwd")
or die("Couldn't connect.");

$db = mysql_select_db("database", $connection)
or die("Couldn't select database.");

$filename = "info.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd,filesize ($filename));

fclose ($fd);
$delimiter = "\n";
$splitcontents = explode($delimiter, $contents);

$counter = "0";
?>

<br><br>

<?
foreach ( $splitcontents as $key => $value )
{

$sql = "SELECT * FROM table where field = \"$value\"";

$sql_result = mysql_query($sql,$connection) or die(mysql_error());

$row = mysql_fetch_array($sql_result);
$sku = $row["sku"];
$mpn = $row["mpn"];

echo "
<table border=1>
<tr>
<td>$counter</td>
<td>$sku</td>
<td>$mpn</td>
</tr>
<tr><td><b>Split $counter: </b> $value\n<br></td></tr>
</table> ";
$counter = $counter+1;
mysql_free_result($sql_result);

}

mysql_close($connection);

?>

The Info.txt file has following values:
n03-00032
n04-00033
n05-00034
.
.
.
.
n70-00069

The output comes as follows. I can not see any error in the script. Just don't know why it is not working. It is supposed to get values from Info.txt and with the help of mysql query display all values one by one but it only displays last value.


0
Split 0: n03-00032

1
Split 1: n04-00033

2
Split 2: n05-00034
.
.
.
.
70 n70-00069 A134ABU
Split 70: n70-00069

If anyone see any error or has any idea of solving this problem then pls let me know.



#2 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 03:20 PM

To separate a file into an array by lines you don't need to use fread and explode, just use $array = file('info.txt'); and each array value will be a new line. You can then work with that.
[table]



Don't worry, the printer fairies will sort it out.

#3 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 09 June 2006 - 03:24 PM

Try making this ammendment:
while($row = mysql_fetch_array($sql_result)) {
    $sku = $row["sku"];
    $mpn = $row["mpn"];

    echo "
    <table border=1>
    <tr>
    <td>$counter</td>
    <td>$sku</td>
    <td>$mpn</td>
    </tr>
    <tr><td><b>Split $counter: </b> $value\n<br></td></tr>
    </table> ";
    $counter = $counter+1;
}


#4 phpstarter

phpstarter
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 09 June 2006 - 03:29 PM

[!--quoteo(post=381883:date=Jun 9 2006, 04:20 PM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 9 2006, 04:20 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
To separate a file into an array by lines you don't need to use fread and explode, just use $array = file('info.txt'); and each array value will be a new line. You can then work with that.
[/quote]

Thanks for quick response but your given style comes up as blank page in browser. There are no values displayed at all. Any suggestions.......

#5 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 03:37 PM

That suggests there's something wrong with the file. You might not have read permissions.
$array = file('info.txt');
echo "<pre>";
var_dump($array); // see if it's worked
echo "</pre>";
foreach($array as $line)
{
// Do whatever you need to do to each line here using $line
}

[table]



Don't worry, the printer fairies will sort it out.

#6 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 09 June 2006 - 03:43 PM

^ sorry this post wasn't here when i was typing

remember you need to split the value of array[linenum]

foreach ($array as $key => $value)
{
function my_BossSaysI_CanGoHome_now () // or whatever you need to do with the line
}
z..z..z..z..z..z..z..z..

#7 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 03:51 PM

[!--quoteo(post=381901:date=Jun 9 2006, 10:43 AM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 9 2006, 10:43 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
^ sorry this post wasn't here when i was typing

remember you need to split the value of array[linenum]

foreach ($array as $key => $value)
{
function my_BossSaysI_CanGoHome_now () // or whatever you need to do with the line
}
[/quote]
foreach($array as $key => $value) is not needed in this instance as the $key is useless for this loop, all that's needed is the value which can be obtained by foreach($array as $value)
Also, if you're calling a function my_BossSaysI_CanGoHome_now(), it won't be able to do anything to the line because it doesn't have anything passed to it. If it's going to affect the line you need to pass it to the function like my_BossSaysI_CanGoHome_now($line);
[table]



Don't worry, the printer fairies will sort it out.

#8 phpstarter

phpstarter
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 12 June 2006 - 09:15 AM

[!--quoteo(post=381907:date=Jun 9 2006, 04:51 PM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 9 2006, 04:51 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
foreach($array as $key => $value) is not needed in this instance as the $key is useless for this loop, all that's needed is the value which can be obtained by foreach($array as $value)
Also, if you're calling a function my_BossSaysI_CanGoHome_now(), it won't be able to do anything to the line because it doesn't have anything passed to it. If it's going to affect the line you need to pass it to the function like my_BossSaysI_CanGoHome_now($line);
[/quote]


Dear Fyorl,
I have tried exactly the way you have mentioned in your post as below.

$array = file('info.txt');
echo "<pre>";
var_dump($array); // see if it's worked
echo "</pre>";
foreach($array as $line)
{
My code to check database for items through text file.
}

I have checked the info.txt file also and it is on file is ready for archiving which means it has full read and write permission.

This code bring out the same result i.e., it reads text file, displays each product part number from text file but only displays the values from database for last text file entry. I really do not understand what is the problem. Any more suggestions please.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users