Page 1 of 1

Text file, Looping with no duplication

Posted: Tue Aug 08, 2006 2:45 pm
by babaloui
hi,
I did a php script to read from text file names.txt and to do loop inside it with mysql querying to output file sex.txt which contains the names with the sex infront of it.
but i had duplication inside the output file.
can you help me here plz.


SQL statment

Code: Select all

# phpMyAdmin MySQL-Dump
# version 2.2.3
# http://phpwizard.net/phpMyAdmin/
# http://phpmyadmin.sourceforge.net/ (download page)
#
# Host: localhost
# Generation Time: Aug 08, 2006 at 05:12 PM
# Server version: 3.23.47
# PHP Version: 4.1.1
# Database : `names`
# --------------------------------------------------------

#
# Table structure for table `list`
#

CREATE TABLE list (
  name varchar(10) NOT NULL default '',
  sex varchar(6) NOT NULL default ''
) TYPE=MyISAM;

#
# Dumping data for table `list`
#

INSERT INTO list VALUES ('john', 'male');
INSERT INTO list VALUES ('kate', 'female');
INSERT INTO list VALUES ('mary', 'female');
INSERT INTO list VALUES ('edward', 'male');
INSERT INTO list VALUES ('shanter', 'male');
INSERT INTO list VALUES ('tony', 'male');
INSERT INTO list VALUES ('meg', 'female');
INSERT INTO list VALUES ('mike', 'male');
INSERT INTO list VALUES ('sarah', 'female');
MySQL Connection File:

Code: Select all

<?php
$host = "localhost";          
// Database Host (usually localhost)
$user = "root";          
// Username
$pass = "";              
// Password
$database = "names";      
// Database name
?>

Code: Select all

<?php
		// calling the external file (has variables for the database connection)
		include("db.php");
		// Connect at the database using the config.inc.php 
		$connection = @mysql_connect($host,$user,$pass);
		// Select the database 
		$database = mysql_select_db($database,$connection);
		//open text file
		$file = file('names.txt');
        //doing loop in the text file lines
		foreach ($file as $line_num => $line) 
			{
		$who = $line;
		//DB SQL statment
		$query = "SELECT * FROM list WHERE name='$who'";		
		// Make the query
		$result = mysql_query($query);
		// Count the rows (total result)
		$row = mysql_num_rows($result);
		// For each result 
		while($row = mysql_fetch_array($result))
				{    
					$name = $row['name'];					
					$sex    = $row['sex'];
					
					$filedata = $name. " is ". $sex;
					echo $filedata;
				}//while
					$fp = fopen( "sex.txt" , "a" );
					fwrite( $fp, $filedata);
					echo $who. " is Unkown";
					echo "<br />\n";
			}//foreach
Input text file is:

Code: Select all

john
kate
mary
edward
shanter
tony
meg
kindey
george
sarah
mike
fed
the output text file should be like this

Code: Select all

john is male
kate is female
mary is female
edward is male
shanter is male
tony is male
meg is female
kindey is Unkown
george is Unkown
sarah is female
mike  is male
fed is Unkown
BUT i got this output file with many duplication:

Code: Select all

john is male
kate is femaleis Unkown
mary is femaleis Unkown
edward is maleis Unkown
shanter is maleis Unkown
tony is maleis Unkown
meg is femaleis Unkown
kindey is Unkown
george is Unkown
sarah is femaleis Unkown
mike  is maleis Unkown
fed is Unkown

Posted: Tue Aug 08, 2006 4:04 pm
by Ollie Saunders
Couple of things:
  • Your comments are superfluous and counter-intuitive. You shouldn't not comment every line just for the hell of it, if you do that I won't bother reading your comments at all and I'll miss anything useful that they might actually be saying. Comments need not explain what statements are doing, that much is obvious to anybody who knows the language, they should explain 'Why' you are doing what you are doing, think carefully.
  • Get a better indenting style, seriously yours is just plain silly
  • It may interest you to learn the difference between 'single quote' and "double quote" strings
Here's your code fixed from foreach down:

Code: Select all

foreach ($file as $line_num => $who) {
    $query = "SELECT * FROM list WHERE name='$who'";               
    $result = mysql_query($query);
    $row = mysql_num_rows($result);
    $filedata = '';
    
    while($row = mysql_fetch_array($result)) {   
        extract($row);        
        $filedata.= "$name is $sex\n";
    }
    
    $fp = fopen('sex.txt', 'a') or die('Couldn\'t open');
    fwrite($fp, $filedata);
    echo nl2br($filedata);
}
Have a nice day now! ;)

Posted: Wed Aug 09, 2006 2:55 am
by babaloui
hello Ole,
thanx for your useful comments.
regarding my script problem fixed.

Code: Select all

kindey is Unkown 
george is Unkown 
fed is Unkown
Kindey, George and Fed there are not in the DataBase so the script has ti write infront of their name "Uknown"

my script idea is looping inside the text file and do querying foreach recod and add suitable sex in the front of the name and "Unkown" if the name is not in the database.

help me here plz :)

Posted: Wed Aug 09, 2006 5:43 am
by Ollie Saunders

Code: Select all

define('NO_RECORDS_MSG', 'Unknown');
$filedata = '';
foreach ($file as $lineNum => $who) {
    $who = mysql_real_escape_string($who);
    $query = "SELECT sex FROM list WHERE name='$who'";               
    $result = mysql_query($query);
   
    $filedata.= $name . ' is ';
    while($row = mysql_fetch_array($result)) {   
        $filedata.= $row['sex'];
    }
    if (mysql_num_rows($result) == 0) {
        $filedata .= NO_RECORDS_MSG;
    }
    $filedata .= "\n";
}    
$fp = fopen('sex.txt', 'a') or die('Couldn\'t open');
fwrite($fp, $filedata);
echo nl2br($filedata);
fclose($fp);
There you go, that version is altogether better. Make sure you are trying it yourself for a good while before you come here asking for help. You learn more that way.

Posted: Thu Aug 10, 2006 12:31 am
by babaloui
Thanx Ole, I will try it and comming back to u with my feedback :)