• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

HOw data can recognize which file data should be in

PHP
HOw data can recognize which file data should be in

My program is about getting data from database and write it to a text file with different file name.

I have config file use to initialize setting.
In my config file i have student description value
student description value use to set which student description i want to retrieved from database

say i want to retrieved
ASIAN and NORTHPOLE

in my config file i have
studentDescription1 = ASIAN
studentDescription2 = NORTHPOLE

and i my database i have

TABLE GETSTUDENTDESCRIPTION
DescriptionID studentDescription
1                 ASIAN
20                EUROPEAN
25                AMERICAN
27                NORTHPOLE


TABLE GETSTUEDENTID
studentID DescriptionID
1345687      1
1345678      20
1345692      1
1345798      25

with studentID i can get the data from other table in the database using join as in example
example:
$result = select c.course,c.age,c.address from COURSE c JOIN SCHOOL s ON c.courseid = s.courseid
          where studentID = $studenid;


After i get the value of $result , i want to insert $result into the text file.
 
i also had named my text file name in my config file as below
$filename1 = "ASIAN.txt";
$filename2 = "EROPEAN.txt";

THE PROBLEM I FACE IS THAT HOW CAN I IDENITIFY THAT WHICH $result DATA ARE GOING TO WHICH FILE

example
how can i make studentid 1345687,1345692 ASIAN go to ASIAN.txt
how can i make studentid 1345678 go to EROPEAN.txt


0
firekiller15
Asked:
firekiller15
  • 4
  • 4
1 Solution
 
Ray PaseurCommented:
Your best approach will be to do separate queries for the ASIAN and EUROPEAN and NORTHPOLE, etc.

You can use a WHERE clause like this" WHERE studentDescription = \"ASIAN\"

Hope that helps, ~Ray
0
 
firekiller15Author Commented:
i dont think it works like that.
my purpose is to say student data who is european put into european.txt
and you above query cant see any achieve of this purpose.
and also what hapen if i add more description in the future.
say i add southpole?/
how?
0
 
Ray PaseurCommented:
This is really very simple.  Let's apply a little creativity here!

You do SEPARATE queries - like this.  There are two query iterations, an outer one and an inner one.

First you query the geographic description table and get the list of geographic designations and their associated keys.  

Then you iterate over that list.  

With each iteration you do another query of the student table to get student Ids that match the key of the geographic designation.

With the results of the second query, you build the xxx.txt file, where xxx is the geographic designation.  You write that file, and the inner loop is now complete.  

Now you go back to fetch the next row from the first query and repeat the process until those rows are exhausted.  You will write out one file for each geographic area, eg: ASIAN.txt, SOUTHPOLE.txt, WHATEVER.txt -- this will be driven by the contents of the geographic designation table, so future additions are automatically included.

If you want to show us the code you are using now that is not getting you the results you desire, please post it in clear text via the "Attach Code Snippet" and we can take a look.

Best regards, ~Ray
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
firekiller15Author Commented:
below is my code if you have any question pls ask k
####config file#####
?php
$File1 = USA_TEXT;
$File2 = AUSTRALIA_TEXT;
$File3 = BRAZIL_TEXT;
 
$nation1=USA;
$nation2=AUSTRALIA;
$nation3=BRAZIL;
 
$Nationality = "$nation1:$File1,$nation2:$File2,$nation3:$File3";
 
?>
 
 
####Student.php#####
<?php
//why all data print to all file?
//how to put USA to USA_TEXT file,AUSTRALIA to AUSTRALIA_TEXT and BRAZIL to BRAZIL_TEXT
 
class student
{
 
 
//get studentid from config file
function GetNationalityFromConfigFile()
 {
  require("config.php");
  $this->GetNationality = $Nationality;
 
  $this->GetNationality_array = split(",",$this->GetNationality);
  foreach($this->GetNationality_array as $GetNationality_value)
	{
	  list($GetStudentNationality, $GetStudentFilename) = split(":", $GetNationality_value);
      $this->StudentNationalityReturn[] = $GetStudentNationality;
     
	  $this->StudentFileReturn[] = $GetStudentFilename.".txt";
	 
	}
 }
 
 
 function GetStudentID()
 {
  // Connect to database
 
  $dbLink = mysql_connect("localhost","root" ,"");
  $i = 0; 
  $j = 0; 
  
  foreach ($this->StudentNationalityReturn as $StudentNationalityReturn_value)
   {
   	$result =  mysql_query("SELECT STUDENTTYPE.STUDENTID.STUDENTID AS STUDENTID,STUDENTTYPE.STUDENTID.NATIONALITYID AS  NATIONALITYID
	FROM STUDENTTYPE.STUDENTID JOIN  STUDENTTYPE.STUDENTNATION ON   STUDENTTYPE.STUDENTID.NATIONALITYID = STUDENTTYPE.STUDENTNATION.NATIONALITYID
	WHERE NATIONALITY =\"$StudentNationalityReturn_value\"");
    
	
	while($row = mysql_fetch_array($result))
    {
	 $this->GetNationalityIDReturn[$i] = $row['NATIONALITYID']; 
     $i++;
	 $this->GetStudentIDReturn[$j] = $row['STUDENTID']; 
     $j++;
    } 
   }
  mysql_close($dbLink);
 }
 
 
function GetStudentInformation()
 {
  // Connect to database
  $dbLink = mysql_connect("localhost","root" ,"");
  if(!$dbLink) die("Could not connect to database. " . mysql_error());
    
  foreach ( $this->GetStudentIDReturn as $StudentIDReturn_value)
   {
   	$result =  mysql_query("SELECT STUDENTINFO.STUDENTADDRESS.STUDENTADDRESS AS STUDENTADDRESS
	FROM STUDENTINFO.STUDENTADDRESS
	WHERE STUDENTID =\"$StudentIDReturn_value\"");
    if(!$result) die("Query didn't work. " . mysql_error());
	
	while($row = mysql_fetch_array($result))
    {
	 $this->GetNationalityIDReturn = $row['STUDENTADDRESS']; 
    } 
   $output = "|{$this->GetNationalityIDReturn}|"; 
   
   foreach($this->StudentFileReturn as $writetofile)
	   {
	    file_put_contents($writetofile,$output, FILE_APPEND); 
	   }
   }
  mysql_close($dbLink);
 }
 
 
}
?>

Open in new window

0
 
Ray PaseurCommented:
Firekiller15, it might be more clear if I had your table definitions, but I'll try to attach at least pidgin code to show you the path...  I am sure you can follow the logic and adapt it to your specific case once you see the design.

Good luck! ~Ray
/** TABLE DEFINITIONS
 * Nations
 *  Nationality varchar Unique like CHINA, SAMOA, GERMANY, etc.
 *  NationalityID int like 1 2 3 corresponds to Students.NationalityID
 *
 * Students
 *  Student varchar ( a name ) like Joe Smith, Sadeo Watanabe, etc.
 *  NationalityID int like 1 2 3 - corresponds to Nations.NationalityID
 */
//
 
// NATIONALITY - OUTER LOOP
$nsql = "SELECT Nationality, NationalityID FROM Nations ORDER BY NationalityID";
if (!$n = mysql_query($nsql)) { die( mysql_error() ); }
$old_NationalityID = 'None';
while ($nrow = mysql_fetch_assoc($n)) {
	extract($nrow); 
		
// STUDENT - INNER LOOP
	$ssql = "SELECT Student FROM Students WHERE NationalityID = $NationalityID ORDER BY Student";
	if (!$s = mysql_query($ssql)) { die( mysql_error() ); }
	if (mysql_num_rows($s) == 0) continue; // NO STUDENTS IN THIS NATION
	if (!$nfp = fopen("$Nationality" . ".txt", "w")) { /* ERROR HANDLING */ }
	while ($srow = mysql_fetch_assoc($s)) { // WRITE STUDENTS TO NATIONALITY FILE
		extract($srow);
		fputs($nfp, "$Student");
	} // END WRITING STUDENTS
	fclose($nfp); // NATIONALITY FILE IS COMPLETE
 
} // END NATIONALITY LOOP

Open in new window

0
 
firekiller15Author Commented:
according to my understanding code above you show is use to create file name .
it doesnt insert studentaddress into the file.
below is the detail of my question


How to write data to file according to their filename


How to write data to file according to their filename
example is i have this table

Databasename: studentnation
Table: StudentNationality
nationalityID  nationality
1            USA
2            AUSTRALIA
3             BRAZIL

Databasename: studenttype
Table: StudentID
studentID   nationalityID
123            1      
124            3
125            1
126            2

Databasename: StudentInfo
Table: StudentAddress
studentAddress  StudentID
hillpark      123
downhill      124
cavestreet      125
burswood      126


output of my code:
#########################################
USA_TEXT.txt
|hillpark|downhill|cavestreet|burswood|

AUSTRALIA_TEXT.txt
|hillpark|downhill|cavestreet|burswood|      

BRAZIL_TEXT.txt
|hillpark|downhill|cavestreet|burswood|      
#########################################

it is not what i desired.
how to make it become as below

USA_TEXT.txt
|cavestreet|hillpark|

AUSTRALIA_TEXT.txt
|burswood|

BRAZIL_TEXT.txt
|downhill|

in code snippet is my code
pls help
####config file#####
<?php
$File1 = USA_TEXT;
$File2 = AUSTRALIA_TEXT;
$File3 = BRAZIL_TEXT;
 
$nation1=USA;
$nation2=AUSTRALIA;
$nation3=BRAZIL;
 
$Nationality = "$nation1:$File1,$nation2:$File2,$nation3:$File3";
 
?>
 
 
####Student.php#####
<?php
//why all data print to all file?
//how to put USA to USA_TEXT file,AUSTRALIA to AUSTRALIA_TEXT and BRAZIL to BRAZIL_TEXT
 
class student
{
 
 
//get studentid from config file
function GetNationalityFromConfigFile()
 {
  require("config.php");
  $this->GetNationality = $Nationality;
 
  $this->GetNationality_array = split(",",$this->GetNationality);
  foreach($this->GetNationality_array as $GetNationality_value)
	{
	  list($GetStudentNationality, $GetStudentFilename) = split(":", $GetNationality_value);
      $this->StudentNationalityReturn[] = $GetStudentNationality;
     
	  $this->StudentFileReturn[] = $GetStudentFilename.".txt";
	 
	}
 }
 
 
 function GetStudentID()
 {
  // Connect to database
 
  $dbLink = mysql_connect("localhost","root" ,"");
  $i = 0; 
  $j = 0; 
  
  foreach ($this->StudentNationalityReturn as $StudentNationalityReturn_value)
   {
   	$result =  mysql_query("SELECT STUDENTTYPE.STUDENTID.STUDENTID AS STUDENTID,STUDENTTYPE.STUDENTID.NATIONALITYID AS  NATIONALITYID
	FROM STUDENTTYPE.STUDENTID JOIN  STUDENTTYPE.STUDENTNATION ON   STUDENTTYPE.STUDENTID.NATIONALITYID = STUDENTTYPE.STUDENTNATION.NATIONALITYID
	WHERE NATIONALITY =\"$StudentNationalityReturn_value\"");
    
	
	while($row = mysql_fetch_array($result))
    {
	 $this->GetNationalityIDReturn[$i] = $row['NATIONALITYID']; 
     $i++;
	 $this->GetStudentIDReturn[$j] = $row['STUDENTID']; 
     $j++;
    } 
   }
  mysql_close($dbLink);
 }
 
 
function GetStudentInformation()
 {
  // Connect to database
  $dbLink = mysql_connect("localhost","root" ,"");
  if(!$dbLink) die("Could not connect to database. " . mysql_error());
    
  foreach ( $this->GetStudentIDReturn as $StudentIDReturn_value)
   {
   	$result =  mysql_query("SELECT STUDENTINFO.STUDENTADDRESS.STUDENTADDRESS AS STUDENTADDRESS
	FROM STUDENTINFO.STUDENTADDRESS
	WHERE STUDENTID =\"$StudentIDReturn_value\"");
    if(!$result) die("Query didn't work. " . mysql_error());
	
	while($row = mysql_fetch_array($result))
    {
	 $this->GetNationalityIDReturn = $row['STUDENTADDRESS']; 
    } 
   $output = "|{$this->GetNationalityIDReturn}|"; 
   
   foreach($this->StudentFileReturn as $writetofile)
	   {
	    file_put_contents($writetofile,$output, FILE_APPEND); 
	   }
   }
  mysql_close($dbLink);
 }
 
 
}
?>

Open in new window

0
 
Ray PaseurCommented:
In the code snippet above, at line 20, just add the additional fields you want to select

   $ssql = "SELECT Student, StudentAddress, [etc] FROM Students WHERE NationalityID = $NationalityID ORDER BY Student";

And at line 26, add those fields to the output

   fputs($nfp, "$Student, $StudentAddress, [etc]");

The best EE can do for you is show you the way, we can't write your programs for you.  You will have to work with the examples and adapt them to your specific needs.  Please allow me to recommend a good book to you.  It will serve you well as a tutorial and reference.  Have a look here: http://www.sitepoint.com/books/phpmysql1/

Best regards, ~Ray

0
 
firekiller15Author Commented:
>>The best EE can do for you is show you the way
ya what i want is the way or the idea
now i have no idea how to seperate data and put it into specified file name as mention above

how to make it become as below

USA_TEXT.txt
|cavestreet|hillpark|

AUSTRALIA_TEXT.txt
|burswood|

BRAZIL_TEXT.txt
|downhill|
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now