Solved

HOw data can recognize which file data should be in

Posted on 2008-06-26
8
269 Views
Last Modified: 2008-07-05
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
Comment
Question by:firekiller15
  • 4
  • 4
8 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 21873199
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
 

Author Comment

by:firekiller15
ID: 21880393
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 21882557
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
 

Author Comment

by:firekiller15
ID: 21905160
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 21907182
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
 

Author Comment

by:firekiller15
ID: 21908938
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 21909105
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
 

Author Comment

by:firekiller15
ID: 21909255
>>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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now