Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

HOw data can recognize which file data should be in

Posted on 2008-06-26
8
Medium Priority
?
305 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 111

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 111

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 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 111

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

916 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