Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Need a benchmark - someone rate this code!!
Old 07-19-2006, 02:00 PM Need a benchmark - someone rate this code!!
Skilled Talker

Posts: 82
Trades: 0
Hi guys,

Bit of a silly request, I suppose, but I would really like some feedback on the current standard of my coding... here's a sample:
PHP Code:
<?
// script to dynamically alter the <page> content of config.xml in Flipping Book,
// V.1.0 - create new page and return an updated list of pages
// INLINE CODE IS CLEARLY MARKED - THIS WILL NEED TO BE ADDRESSED ON INCEPTION OF AN *OO* VERSION OF THIS SCRIPT
session_start();
?>
 
<form action = "<? echo $_SERVER['PHP_SELF']; ?>" method = "post">
<input type = "text" name = "width" id = "width" value = "<? $width 100; echo $_POST['width']; ?>">
<input name="submit" type="submit" value="Submit">
</form>
 
<?
//-- FUNCTION -------------------------------------------------------------------------------------------------------------//
 
function get_Pages $file "config.xml" ) { // change this to your default file
 
$_SESSION['file'] = $file;
// open $file and extract items between the <pages> tags
$content file_get_contents($file);
 
$start_Page '<pages>';
$end_Page '</pages>';
$split1 split($start_Page,$content);
$data $split1[1]; // The second index of the array, everything after <pages>
$split2 split($end_Page,$data);
$data $split2[0]; // The first index of the array, everything before </pages>
$split3 explode("<page>",$data);
$_SESSION['data'] = array_slice($split3,1);
$_SESSION['val'] = $count count($_SESSION['data']);
$_SESSION['next'] = $next $count+1;
 
// Now we need to go through the array of pages and add the opening <page> tag back to the beginning of each line
// Easiest way is to convert the array to a string, perform a str_replace that puts a marker in front of pages (e.g, *<page>)
// that allows us to explode the string into an array again afterwards
$string implode($_SESSION['data']);
$strep str_replace("pages","*<page>pages",$string);
$_SESSION['data'] = $array array_slice(explode("*",$strep),1);
 
// END FUNCTION get_Pages ()
 
// INLINE CODE //////////////////////////////////////////////////////////////////////////////////////////////////////
// echo "<h4>Here's the array we're going to work with:</h4>"; //
get_Pages (); // 
//
echo "<h4>Create some info for the other functions to use</h4>"//
echo $_SESSION['val']." pages in ".$_SESSION['file']."<br />\r\n"//
echo "The next image number will be ".$_SESSION['next']."<br />\r\n"//
//__________________________________________________________________________________________________________________//
 
//-- FUNCTION ------------------------------------------------------------------------------------------------------//
 
function new_Image ( ) {
 
// Replicate the last key of the array from get_Pages
$array array_reverse($_SESSION['data']);
$key $array[0];
$new str_replace($_SESSION['val'],$_SESSION['next'],$key);
echo 
"Replacing "$_SESSION['val'] ." with "$_SESSION['next'] ."<br />\r\n";
$_SESSION['new_img'] = $new;
 
// END FUNCTION new_Image ()
 
 
// INLINE CODE ///////////////////////////////////////////////////////////////////////////////////////////////////////
echo "<h4>Create a filename for the new image</h4>"//
new_Image (); //
//___________________________________________________________________________________________________________________//
//--- FUNCTION ------------------------------------------------------------------------------------------------------//
 
 
function make_New_Array ( ) {
 
// Combine the elements of the $_SESSION['data'] array then add $_SESSION['new_img'] to the end of it
$_SESSION['new_Array'] = $new_Array implode($_SESSION['data']).$_SESSION['new_img'];
 
// END OF FUNCTION insert_New_Array ( )
 
 
//INLINE CODE////////////////////////////////////////////////////////////////////////////////////////////////////////
make_New_Array ( ); //
//__________________________________________________________________________________________________________________//
//--- FUNCTION ------------------------------------------------------------------------------------------------------// 
 
function insert_Array ( ) { // Function to re-insert the array from 
 
// Split the XML file into two parts with the pages section removed
$content file_get_contents($_SESSION['file']); // Get the file contents again
 
$start_Page '<pages>';
$end_Page '</pages>';
$split1 split($start_Page,$content); // Splits the page content on the value of $start_Page
$part1 $split1[0]; // The first index of the array, everything before <pages>
$split2 split($end_Page,$content);
$part2 $split2[1]; // The second index of the array, everything after </pages>
// Use resultant parts to create the "sandwich" that will be "filled" by $new_Array
$_SESSION['new_Content'] = $new_Content $part1.$start_Page."\r\n".str_replace(" ","",$_SESSION['new_Array']).$end_Page.$part2."\r\n";
//return $new_Content;
echo "<hr /><b>New content for XML page: </b><br />".$new_Content;
 
// END OF FUNCTION insert_Array ()
 
//--- FUNCTION -----------------------------------------------------------------------------------------------------//
 
function overwrite_XML_File ( ) {
// Overwrite the contents of $file
$handle fopen($_SESSION['file'],"w");
$content $_SESSION['new_Content'];
fwrite($handle,$content);
fclose($handle);
 
// END FUNCTION overwrite_XML_File
 
// INLINE CODE ///////////////////////////////////////////////////////////////////////////////////////////////////////
insert_Array ( ); //
overwrite_XML_File(); //
//___________________________________________________________________________________________________________________//
//--- FUNCTION -------------------------------------------------------------------------------------------------------//
 
function parse_XML( ) { // Output the file as XML
 
$file $_SESSION['file']; 
////
function contents($parser$data){ 
echo 
$data

////
function startTag($parser$data){ 
echo 
"<b>"

////
function endTag($parser$data){ 
echo 
"</b><br />"

////
$xml_parser xml_parser_create(); 
xml_set_element_handler($xml_parser"startTag""endTag"); 
xml_set_character_data_handler($xml_parser"contents"); 
$fp fopen($file"r"); 
$data fread($fp80000); 
if(!(
xml_parse($xml_parser$datafeof($fp)))){ 
die(
"Error on line " xml_get_current_line_number($xml_parser)); 

xml_parser_free($xml_parser); 
fclose($fp);
 
// END FUNCTION parse_XML ()
 
//INLINE CODE////////////////////////////////////////////////////////////////////////////////////////////////////
echo "<h4>Here's the XML, parsed for output:</h4>"//
parse_XML(); //
//_____________________________________________________________________________________________________________//
 
//--- FUNCTION -------------------------------------------------------------------------------------------------//
 
function get_Posts($array) { 
foreach(
$array as $key => $value) {
 
if (
$key!=="submit") {
// $key will be used to define the start and finish points of a split process
// $value will be inserted between the relevant tags 

}

 
//INLINE CODE///////////////////////////////////////////////////////////////////////////////////////////////////
get_Posts($_POST); //
session_destroy(); //
//_____________________________________________________________________________________________________________//
 
 
 
//+ END +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
 
?>
Please be thoroughly critical (although constructively!) and give me an idea of where I'm at!!! Reason I'm asking is because I'm considering looking for a job as a PHP developer and need to know what kind of level I aught to expect to be entering at...

Most thankfully,

Matt

Last edited by mattcooper; 07-19-2006 at 02:03 PM..
mattcooper is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-19-2006, 09:52 PM Re: Need a benchmark - someone rate this code!!
Extreme Talker

Posts: 217
Trades: 0
Im not really sure what you mean. Do you want us to evaluate you in terms of how neat your code is, documentation, efficiency etc.?

If yes, then id say you are doing all right (though im no professional or expert). Few things I would recommend are putting simmilar functions into a seperate file as a class. Another thing would be using the TAB key to organize the code. IE:

PHP Code:
class test{
    
    function 
test1(){
        echo 
"This is a test";
    }

    function 
test2(){
        echo 
"This is another test";
    }

In my opinion it looks alot better and is alot easier to read. Also I dont really understand why on a bunch of stuff right at the end after the ';' you have comment slashes...
like this:

PHP Code:
echo "<h4>Create some info for the other functions to use</h4>"//
echo $_SESSION['val']." pages in ".$_SESSION['file']."<br />\r\n"//
echo "The next image number will be ".$_SESSION['next']."<br />\r\n"// 
Slick Nick is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need a benchmark - someone rate this code!!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.42758 seconds with 12 queries