Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to optimize my code to make it more flexible to add more item in the future

Posted on 2008-06-10
5
Medium Priority
?
253 Views
Last Modified: 2008-06-11
PHP

i have 2 php file
one is config.php and another is main.php

i want to insert data to diffrent file name based on UID given

i had done code for this but i think it is not flexible enough
code snippet is my code

Code i create is not user friendly or is not dynamic but static

expample
if i want to add more file like
$file4 = TEST4_TEXT;

at the moment my UID is 1,2,3

what happen if i want to add UID 4?

The SWITCH cannot read UID 4 and $file4
how to modified it?



--config.php--
$file1 = TEST1_TEXT;
$file2 = TEST2_TEXT;
$file3 = TEST3_TEXT;
 
 
--main.php--
switch($UID)
   {
 case 1: $fn = $file1; break;
 case 2: $fn = $file2; break;
 case 3: $fn = $file3; break;
   }
file_put_contents('c:\\'.$fn.'.txt',$data1,FILE_APPEND);
}

Open in new window

0
Comment
Question by:firekiller15
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
bansidhar earned 1000 total points
ID: 21756925
easiest method if you have a logic for the name of the text file:

--main.php--
$fn = "TEST{$UID}_TEXT";
file_put_contents('c:\\'.$fn.'.txt',$data1,FILE_APPEND);

You don't  need a config.php here. Here for any new $UID a new file is created.

Method 2:
If you want more  flexibility in naming file names and you are sure about the UIDs. In this you just add another UID in the config.php
--config.php--
$file[1] = "TEST1_TEXT";
$file[2] = "TEST2_TEXT";
$file[3] = "TEST3_TEXT";
$file[4] = "TEST4_TEXT";
 
-- main.php --
include ('config.php');
$fn=(isset($file[$UID]))?$file[$UID]:'unknownUID';
file_put_contents('c:\\'.$fn.'.txt',$data1,FILE_APPEND);

Open in new window

0
 

Author Comment

by:firekiller15
ID: 21757281
How about if UID is known?
i can be 1 or 10 o100 how>
0
 
LVL 11

Expert Comment

by:bansidhar
ID: 21757339
in Method 2: just add the file name in the config.php like if you want for UID=23 add the line no need to change the main.php

$file[23] = "TEST23_TEXT";

this will create TEXT23_TEXT.txt when it hits UID 23.

in Method 1: you don't have to do anything, it will automatically create TEXT23_TEXT.txt
0
 

Author Comment

by:firekiller15
ID: 21757365
that mean if i have data as follow in database table
UID  AMOUNT  TYPE  
23     5               A
26     6               A

is the data 23,5,A will go to TEST23_TEXT?
and
26,6,A should not got to TEST23_TEXT
0
 

Author Comment

by:firekiller15
ID: 21757439
why only test1 and test2 output on the drive test3 is not shown
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
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 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…

670 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