Solved

HOw data can recognize which file data should be in

Posted on 2008-06-26
8
260 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Accepted Solution

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

8 Experts available now in Live!

Get 1:1 Help Now