General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old February 9th, 2003, 04:41 PM
arthaus arthaus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Pasadena, CA
Posts: 11 arthaus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Writing to file --> Please critique my code...

Hi all,

I'm fairly new to PHP code. And I would like experts' help in writing efficient code.

Following is a form, once filled, it writes configuration file, which in turn I'll use to include in file(s).

PHP Code:
// file: config_write.php

<?php
    $title 
"Configuration Writer";
    
$writefile "data.txt";
?>

<html>
<head>
    <title>/ <? echo $title?> /</title>

    <style type="text/css">
    <!--
    html, body {
        background-color: #EFEFEF; 
        margin: 40px;
        }    
    .filecontent {
        font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; 
        font-size: 10px;
        line-height: 11px;
        color: #006699;
        width: 400px;
        }
    .mytext {
        font-family: "Verdana", "Arial", "Helvetica", "sans-serif"; 
        font-size: 12px; 
        color: #333333; 
        line-height: 18px;
        width: 400px;
        } 
    .mytext a:link {
        color: #006699; 
        text-decoration: underline;
        } 
    .mytext a:visited {
        color: #006699; 
        text-decoration: underline;
        } 
    .mytext a:hover {
        color: #006699; 
        text-decoration: none;
        } 
    .mytext a:active {
        color: #006699; 
        text-decoration: underline;
        }
    .btn {
        background: #006699;
        color: #FFFFFF;
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 11px;
        margin: 3px;
        }
    .btnUp {
        background: #CCCCCC;
        color: #006699;
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 11px;
        margin: 3px;
        }
    .btnOver {
        background: #006699;
        color: #FFFFFF;
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 11px;
        margin: 3px;
        }
    .mytextarea {
        background-color: #FFFFFF;
        width: 400px;
        padding: 6px;
        margin: 3px;
        border-left: solid 1px #333333;
        border-top: solid 1px #333333;
        border-right: solid 1px #CCCCCC;
        border-bottom: solid 1px #CCCCCC;
        color: #006699;
        }
    .mytextarea:focus {
        color: #006699;
        background: #FFFFCC;
        }
    -->
    </style>

</head>

<body>

    <form action="<? echo "$PHP_SELF"?>" method="post" class="mytext">
        File: config_write_2.php<br><br>
        Page title:<br>
        &nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="formTitle" class="mytextarea"><br>
        
        Company name:<br>
        &nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="formCompname" class="mytextarea"><br>

        Footer text:<br>
        &nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="formFooter" class="mytextarea"><br>

        &nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="Submit!" class="btnOver" onmouseover="this.className='btnUp'" onmouseout="this.className='btnOver'">&nbsp;&nbsp;
        <input type="reset" name="reset" value="Reset!" class="btnOver" onmouseover="this.className='btnUp'" onmouseout="this.className='btnOver'">
    </form>


    <?php
        $filePointer 
fopen($writefile"w+");

        
$title $formTitle;
        
$compname $formCompname;
        
$footer $formFooter;
        
        
fputs($filePointer"<?php\r\n"1024);
        
fputs($filePointer"/* copy configuration */\r\n");

        
fputs($filePointer"// file title\r\n");
        
fputs($filePointer"\t\$title = \"$title\";\r\n");

        
fputs($filePointer"// company name\r\n");
        
fputs($filePointer"\t\$compname = \"$compname\";\r\n");
        
        
fputs($filePointer"// footer DIV text\r\n");
        
fputs($filePointer"\t\$footer = \"$footer\";\r\n");
        
        
fputs($filePointer"/* body behaviour */\r\n");
        
fputs($filePointer"// onLoad\r\n");
        
fputs($filePointer"\t\$onload = \"P7_alignWD2('bottomLayer',6,0,0,'bottomLayer',1,14  1,0);P7_autoLayers(0,'bottomLayer');flevInitPersis  tentLayer('bottomLayer',1,'','','','','','',2);ini  tSwipeMenu()\"\r\n");
        
        
fputs($filePointer"?>\r\n");
                
        
fclose($filePointer);
    
?>

<?php
    
if (isset($title)) {
    echo 
"<span class=\"mytext\"><hr noshade size=\"1\">";
    echo 
"<font class=\"mytext\">
        Information was written!!
        <a href=\"$writefile\" onFocus=\"if(this.blur)this.blur()\" target=\"_blank\"> Preview file...</a> [new window]</font><br>"
;

    echo 
"<hr noshade size=\"1\">\n";
    echo 
"<span class=\"filecontent\">";
    echo 
"File: <strong>data.txt</strong><br>\n";
    
// Get a file into an array.
// This example we'll go through HTTP to get HTML source of a URL.
$lines file ($writefile);

// Loop through our array, show html source as html source; 
// and line numbers too.
foreach ($lines as $line_num => $line) {
    echo 
"<strong>Line #{$line_num}:</strong> " htmlspecialchars($line) . "<br>";
}

// Another example, let's get a web page into a string.  See also file_get_contents().
$html implode (''file ($writefile));

    echo 
"<hr noshade size=\"1\"></span>";
    }
?>

<?php
/*
$handle = fopen ("$writefile", "r");
while (!feof ($handle)) {
    $buffer = fgets($handle, 4096);
    echo $buffer;
}
fclose ($handle);
*/
?>

</body>
</html> 


I look forward and welcome comments, critiques and suggestions...

Thankx in advance,
Arthur

Reply With Quote
  #2  
Old February 10th, 2003, 05:23 AM
jpenn jpenn is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Washington, DC
Posts: 317 jpenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 3 sec
Reputation Power: 6
Bind all your data you want to write out the file pointer and only call the fputs() function once.

Another thing I would do is completely seperate the PHP code from the HTML code, making the data more readable...
__________________
~ Joe Penn

We work for free to help make this a valuable resource on the internet. Do you appreciate the help - did we provide help that will help you prosper and help that has contributed to sharpening your current skill set?

Show your appreciation and purchase something from our Amazon Wishlist's - it's simple and a great way to say thank you.




Reply With Quote
  #3  
Old February 10th, 2003, 07:52 AM
arthaus arthaus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Pasadena, CA
Posts: 11 arthaus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi jpenn,

Quote:
Bind all your data you want to write out the file pointer and only call the fputs() function once.


Q: How would I do it?

This tutorial is interesting. However, I couldn't use the function-method.

Thankx in advance,
Arthur

Last edited by arthaus : February 10th, 2003 at 07:57 AM.

Reply With Quote
  #4  
Old February 10th, 2003, 01:21 PM
jpenn jpenn is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Washington, DC
Posts: 317 jpenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 3 sec
Reputation Power: 6
PHP Code:
/* Instead of the following */

fputs($filePointer"<?php\r\n"1024);
fputs($filePointer"/* copy configuration */\r\n");

fputs($filePointer"// file title\r\n");
fputs($filePointer"\t\$title = \"$title\";\r\n");

fputs($filePointer"// company name\r\n");
fputs($filePointer"\t\$compname = \"$compname\";\r\n");
        
fputs($filePointer"// footer DIV text\r\n");
fputs($filePointer"\t\$footer = \"$footer\";\r\n");
        
fputs($filePointer"/* body behaviour */\r\n");
fputs($filePointer"// onLoad\r\n");
fputs($filePointer"\t\$onload = \" P7_alignWD2('bottomLayer',6,0,0,'bottomLayer',1,14   1,0);P7_autoLayers(0,'bottomLayer');flevInitPersis   tentLayer('bottomLayer',1,'','','','','','',2);ini  tSwipeMenu()\"\r\n");

/* You would do this */

$string = ( bool ) false;
$string .= "<?php\r\n";
$string .= "/* copy configuration */\r\n"
$string .= "// file title\r\n"
$string .= "\t\$title = \"$title\";\r\n"
$string .= "// company name\r\n"

/* And so on to get all your data in the one scalar - then */

$filePointer fopen$writefile'w+' );
fwrite$filePointer$string );
fclose$filePointer ); 

Reply With Quote
  #5  
Old February 10th, 2003, 03:41 PM
arthaus arthaus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Pasadena, CA
Posts: 11 arthaus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi Joe Penn,

Thankx. And I really appreciate your efforts.

I applied your suggestions. And It works perfect. Slowly I'm building/learning creating a dynamic web site, with browser-based editing management.

Regards,
Arthur

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Writing to file --> Please critique my code...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway