GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Apr-2004, 23:26
pepee's Avatar
pepee pepee is offline
New Member
 
Join Date: Apr 2004
Posts: 11
pepee is on a distinguished road
Question

Revising Script style ??????


Hey guys,
I tried to read over all the 2,3 hundred threads to see if there was such a topic, but i didn't find so..
I was wondering whether there is anybody willing to look at my php and tell me what he/she thinks of style arrangement etc.
I am new to php and i found out that is it very important how you structure your script. I know what that an experienced coder would very easily recognize useless lines, easier way to do sth and so on.
So, is it appropriate to paste my code and wait for comments? It is a simple upload/edit/delete form, however it is w/ ~400 lines (not as bad as it sounds).

Waitin for a permission first...
Tanx anyway....
PePee
  #2  
Old 05-Apr-2004, 04:08
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Hello Pepee,

You don't need permission to ask your questions here if it's related to the forum topic, so go ahead and post your code (and seek reviews) if that is what you want to do.

What we normally frown upon is a member posting Ads and Offers in forums that have nothing to do with them (all ads and offers belong in the Ads and Offers Forum, nowhere else).

If pasting a 400 line code scares you, then you may copy the code into a .txt file and attach it to your post, at least I will take a look; and if I recall, guests who view these threads don't get to see your codes if it's in an attachment.
  #3  
Old 05-Apr-2004, 08:44
pepee's Avatar
pepee pepee is offline
New Member
 
Join Date: Apr 2004
Posts: 11
pepee is on a distinguished road
No, I'm not scared, but it should be ok with you. I'll paste it. if it is too long, you can tell me, i will edit the thread and attach file. Okay...here we go...
As i said, this creates a MySQL table w/ ID/Address/Notes columns. You can upload, edit and delete info. One known problem is that when editing the adress, you leave it blank, it would not perform the update, however, if you put a space only, it would empty the data and then everything messes up. Obviously it considers " " to be the same as "". Also a question: is "" and NULL the same thing?
I looked at the library and liked Elmseeker's way of outputing things, so...the file is called noHTML0.php
tank you guys!
PHP Code:

<html>
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="default.css" />
</head>
<body text="#CCCCCC" bgcolor="e2e2e2" leftmargin="0" topmargin="0" bottommargin="0" marginwidth="0" marginheight="0" >
<?php

$obj = new do_form();

/////////////// GLOBALS/////////////////////////////
                                                ////
// defined in the function globals_defined()
$g = "get";
$p = "post";
$this_file_name = "noHTML0.php";
                                                ////
////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////
                                                                        ///// MYDQL //
                                                                            //////////
                                                                                //////
                                                                                    //
    # CONNECTION
    $host = "localhost";
    $user = "";
    $pass = "";
    $database_name = "buildings_database";
    $table_name = "table_name";
    
    $link = mysql_connect ($host, $user, $pass);
    #if($link ) 
    #echo "Establishing Connection... V<br>";
    #else 
    #echo "Establishing Connection... X<br>".mysql_error();
    
    # DB CREATION
    
    $creating_db_check = mysql_query("CREATE DATABASE $database_name");
    #if($creating_db_check)
    #echo "Creating Database...V<br>";
    #else
    #echo "Creating Database...X<br>".mysql_error();
    
    # DB SELECTION
    
    $selecting_db_check = mysql_select_db ($database_name);
    #if ( $selecting_db_check)
    #echo "Selecting Database... V<br>";
    #else
    #echo "Selecting Database... X<br>".mysql_error();

    # TABLE CREATION
    $create_table_check = mysql_query( "CREATE TABLE $table_name (
    id INT NOT NULL AUTO_INCREMENT,
    Address VARCHAR (40) UNIQUE NOT NULL,
    Notes VARCHAR (40),
    PRIMARY KEY (id)
    )");
    #if ($create_table_check)
    #echo "Creating Table... V<BR>";
    #else
    #echo "Creating Table... X<BR>".mysql_error()."<br>";
    
    # VARIABLES
    $address_upload = $_GET['address_upload_get'];
    echo "<br>address_upload: ".$address_upload;
    $notes_upload = $_GET['notes_upload_get'];
    echo "<br>notes_upload: ".$notes_upload;
    $upload_clicked = $_GET['upload_clicked_get'];
    echo "<br>upload_clicked:".$upload_clicked;
    $address_select = $_GET['address_select_get'];
    echo "<br>address_select: ".$address_select;
        $result = mysql_query ("SELECT * FROM $table_name WHERE Address = '$address_select'");
        $result = mysql_fetch_array ($result);
    $notes_select = $result['Notes'];
    echo "<br>notes_select: ".$notes_select;
        $result = mysql_query ("SELECT * FROM $table_name WHERE Address = '$address_select'");
        $result = mysql_fetch_array ($result);
    $id_select = $result['id'];
    echo "<br>id_select: ".$id_select;
    $address_edit = $_GET['address_edit_get'];
    echo "<br>address_edit: ".$address_edit;
    $notes_edit = $_GET['notes_edit_get'];
    echo "<br>notes_edit: ".$notes_edit;
    $hidden_transfer_id = $_GET['hidden_transfer_id_get'];
    echo "<br>hidden_transfer_id: ".$hidden_transfer_id;
    $select_edit_clicked = $_GET['select_edit_clicked_get'];
    echo "<br>select_edit_clicked: ".$select_edit_clicked;
    $update_edit_clicked = $_GET['update_edit_clicked_get'];
    echo "<br>update_edit_clicked: ".$update_edit_clicked;
    $address_delete = $_GET['address_delete_get'];
    echo "<BR>address_delete: ".$address_delete;
    
    #$select_all_query = mysql_query ("SELECT * FROM $table_name");
    

#print_r displays everything for a $, array, object;
# mysql_fetch_row returns an array with elements all the cells from this row
# mysql_fetch_array returns:1. assiciative array with key=number of cell value=cell content
#                            2. associative arrya with key=column name value=cell content
# mysql_fetch_object returns an object for which properties=comun names value = cell content
# mysql_fetch_field returns an object with all the properties of a column.

/////////////// INSERTION////////////////////////////
                                                ////
if (($idea_edit == null)&&($update_edit_clicked!=0)){
        $status_upload = "Pls specify address";
        $show_status_upload = 1;
    }
    if ($address_upload!=null){
        $check_insert = mysql_query( "INSERT INTO $table_name ( Address, Notes)    VALUES( '$address_upload', '$notes_upload')", $link );
        $show_status_upload = 1;
        if ( $check_insert){    
            $status_upload = "Inserting '$address_upload' Successful";
            $upload_successful = 1;
        }else{
            $status_upload = "You already have this one";
        }    
    }
                                                ////
////////////////////////////////////////////////////
        
/////////////// EDITING ////////////////////////////
                                                ////                                                
if (($address_edit == null)&&($update_edit_clicked!=0)){
    $update_edit_clicked = 1;
    $status_edit = "You cannot have EMPTY value for Adres! You can delete it from<br>|<BR>|<BR>|<BR>|<BR>\/";
}else{
    if (($hidden_transfer_id!=0)&&($address_edit != null)){
            $update_edit_clicked = 1;
            $result = mysql_query ("UPDATE $table_name SET Notes = '$notes_edit' WHERE id = '$hidden_transfer_id' ");
            if ($result) $status_edit_1 = 1;
            $result= mysql_query ("UPDATE $table_name SET Address = '$address_edit' WHERE id = '$hidden_transfer_id' ");
            if ($result) $status_edit_2 = 1;
            if (($status_edit_1==1)&&($status_edit_2==1)) $status_edit = "Updating \"".$address_edit."\" is successful.";
            else  $status_edit = "Updating \"".$address_edit."\" failed.";
    }elseif (($hidden_transfer_id!=0)&&($address_edit == null)){
            $update_edit_clicked =1;
            $status_edit = "You cant't leave the Address blank empty. Not updated.";
    }
}                                                ////
////////////////////////////////////////////////////

if ($address_delete!=null){
    $result = mysql_query ("DELETE FROM $table_name WHERE Address = '$address_delete' ");
    echo "<BR>ADDRESS_DELETE: ".$address_delete;
    if ($result)
        $status_delete = "Deleted successfully";
    else
        $status_delete = "Delete Failed";
    $show_status_delete = 1;
}                                                        
                                                                                    //
                                                                                //////
                                                                            //////////
                                                                        ///// MYSQL //
//////////////////////////////////////////////////////////////////////////////////////
$obj->ot();
$obj->orow();
$obj->odv("top");
//////////////////////////////////////////////////////////////////////////////////////
                                                                        /// UPLAODER /
                                                                            //////////
                                                                                //////
                                                                                    //

$obj->obt();
$obj->margin_up();
$obj->blh();
$obj->title("Uploader");
$obj->blh();
$obj->orow();
    $obj->bc();
    $obj->od();
    
        $obj->otwa(760,"left");    
            $obj->orow();
                $obj->oform($this_file_name,$g);
                $obj->odw(310);
                    $obj->otw(300);
                        $obj->orow();
                            $obj->odw(150);    
                            $obj->output("Adres of the Estate: ");
                            $obj->cd();
                            $obj->odw(150);
                            $obj->input("address_upload_get","");
                            $obj->cd();
                        $obj->crow();
                        $obj->orow();
                            $obj->odw(150);
                                $obj->output("Additional Notes: ");
                            $obj->cd();
                            $obj->odw(150);
                                $obj->input("notes_upload_get","");
                            $obj->cd();
                        $obj->crow();
                    $obj->ct();            
                $obj->cd();
                $obj->od();
                    $obj->submit("Upload Estate");
                $obj->cd();
                $obj->od();
                    $obj->otwa(250,"right");
                        $obj->orow();
                            $obj->odwa(90,"left");
                            $obj->output("View ");
                            $obj->cd();
                            $obj->odwav(90,"left", "middle");        
                                $obj->olink("view_all.php","");
                                $obj->output("- all the buildings and lots");
                                $obj->clink();
                            $obj->cd();
                        $obj->crow();
                            if ($upload_successful==1){
                            $obj->orow();
                                $obj->od();
                                $obj->cd();
                                $obj->oda("left");
                                    $obj->olink("view_one.php?", "");
                                    $obj->output("- only $address_upload");
                                    $obj->clink();
                                $obj->cd();
                            $obj->crow();
                            }
                    $obj->ct();    
                $obj->cd();                
                $obj->cform();
            $obj->crow();
        $obj->ct();    


    $obj->cd();
        $obj->bc();
    $obj->crow();
    $obj->blh();
    if (isset($show_status_upload)){
    $obj->orow();
        $obj->bc();
        $obj->od();
        $obj->ocss("orange");
        $obj->output("$status_upload");
        $obj->ocss("orange");
        $obj->cd();                                                                  
    $obj->bc();
    $obj->crow();
    $obj->blh();
    }
$obj->ct();
                                                                                    //
                                                                                //////
                                                                            //////////
                                                                        /// UPLOADER /
//////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////
                                                                        //// EDITOR //
                                                                            //////////
                                                                                //////
                                                                                    //
if (1==1){
$obj->obt();
$obj->margin_up();
$obj->blh();
$obj->title("Editor");
$obj->blh();
$obj->orow();
    $obj->bc();
    $obj->od();

        $obj->ota("left");
            $obj->orow();
                $obj->oform($this_file_name,$g);
                    $obj->oda("left");
                    $obj->oselect("address_select_get");
                        $select_all_query = mysql_query ("SELECT * FROM $table_name");
                        while ($temp = mysql_fetch_array($select_all_query)){
                            if ($address_select==$temp[Address]) $obj->options("$temp[Address]");
                            else $obj->option("$temp[Address]");
                            }            
                    $obj->cselect();                    
                    $obj->cd();
                    $obj->oda("left");
                    $obj->submit("Edit Selection");
                    $obj->hidden("select_edit_clicked_get","clicked");
                    $obj->cd();
                $obj->cform();
                    if ($address_select!=null){
                    $obj->oform($this_file_name,$g);
                        $obj->oda("left");
                            $obj->ot();
                                $obj->orow();
                                    $obj->od();
                                        $obj->input("address_edit_get","$address_select");
                                    $obj->cd();
                                $obj->crow();
                                $obj->orow();
                                    $obj->od();
                                        $obj->input("notes_edit_get","$notes_select");
                                        $obj->hidden("hidden_transfer_id_get", "$id_select");
                                        $obj->hidden("update_edit_clicked_get", "clicked");
                                    $obj->cd();
                                $obj->crow();
                            $obj->ct();
                        $obj->cd();
                        $obj->oda("left");
                        $obj->submit("Update Selection");
                        $obj->cd();
                    $obj->cform();
                    }
                $obj->crow();
        $obj->ct();    


    $obj->cd();
        $obj->bc();
    $obj->crow();
    $obj->blh();
    if ($update_edit_clicked==1){
    $obj->orow();
        $obj->bc();
        $obj->od();
            $obj->ocss("orange");
            $obj->output("$status_edit");
            $obj->ccss();
        $obj->cd();                                                                  
    $obj->bc();
    $obj->crow();
    $obj->blh();
    }
$obj->ct();
}                                                                                
                                                                                //
                                                                            //////
                                                                        //////////
                                                                    //// EDITOR //
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
                                                                    /// DELETER //
                                                                        //////////
                                                                            //////
                                                                                //
if (1==1){
$obj->obt();
$obj->margin_up();
$obj->blh();
$obj->title("Deleter");
$obj->blh();
$obj->orow();
    $obj->bc();
    $obj->od();

        $obj->ota("left");
            $obj->orow();
                $obj->oform($this_file_name,$g);
                    $obj->oda("left");
                    $obj->oselect("address_delete_get");
                        $select_all_query = mysql_query ("SELECT * FROM $table_name");
                        while ($temp = mysql_fetch_array($select_all_query)){
                            $obj->option("$temp[Address]");
                        }            
                    $obj->cselect();                    
                    $obj->cd();
                    $obj->oda("left");
                    $obj->submit("Delete Selection");
                    $obj->cd();
                    //tazi kletka mozhe da se mahne...tq e za majtap. 
                    //Molq ne se smej. tq e samo kletka!
                $obj->cform();
            $obj->crow();
        $obj->ct();    


    $obj->cd();
        $obj->bc();
    $obj->crow();
    $obj->blh();
    if ($show_status_delete==1){
    $obj->orow();
        $obj->bc();
        $obj->od();
            $obj->ocss("orange");
            $obj->output("$status_delete");
            $obj->ccss();
        $obj->cd();                                                                  
    $obj->bc();
    $obj->crow();
    $obj->blh();
    }
$obj->ct();
}
                                                                                //
                                                                            //////
                                                                        //////////
                                                                    /// DELETER //
//////////////////////////////////////////////////////////////////////////////////
$obj->cd();
$obj->od();

 echo "\n<table border = \"1\">";
$result = mysql_query ("SELECT * FROM $table_name");
while ($temp2 = mysql_fetch_row($result)){
echo "\n\t<tr>";
foreach($temp2 as $temp3){
echo "\n\t\t<td>$temp3<br></td>";}
echo "\n\t</tr>";
}
echo "\n</table>";
$obj->cd();
$obj->crow();
$obj->ct();
class do_form {
//////////////////////////////////////////////////////////////////////////////////
                                                                    // TABLE FUNCTIONS
                                                                        //////////
                                                                            //////
                                                                                //
function ot(){ // Open Table 


echo <<<PEPE
<table bgcolor="#e2e2e2" border="0" cellpadding="0" cellspacing="0"  align="center">

PEPE;
}
function ota($align){ // Open Table w/ ALIGN
echo <<<PEPE
<table bgcolor="#e2e2e2" border="0" cellpadding="0" cellspacing="0"  align="$align">

PEPE;
}

function otw($width_table){ // Open Table w/ WIDTH
echo <<<PEPE
<table bgcolor="#e2e2e2" border="0" width="$width_table" cellpadding="0" cellspacing="0" marginleft="0" align="center">

PEPE;
}

function otwa($width, $align){ // Open Table w/ ALIGN
echo <<<PEPE
<table bgcolor="#e2e2e2" border="0" width="$width"  cellpadding="0" cellspacing="0"  align="$align">

PEPE;
}

function obt(){ //Open Big Table
echo <<<PEPE
<table bgcolor="#e2e2e2" border="0"  cellpadding="0" cellspacing="0" width="777" align="center">

PEPE;
}

function ct(){ // Close Table
echo <<<PEPE
</table>

PEPE;
}

function margin_up(){ //The one invisible row, that makes the table go down
echo <<<PEPE
    <tr>
            <td colspan="3" height="30"></td>
    </tr>

PEPE;
}

function blh(){ //blh = Black Line horizontal height=1
echo <<<PEPE
    <tr>
        <td bgcolor="#000000" width="1"></td>
        <td bgcolor="#000000" height="1"></td>
        <td bgcolor="#000000" width="1"></td>
    </tr>

PEPE;
}

function title($title_of_table){ // Row that contains the title
echo <<<PEPE
    <tr>
          <td bgcolor="#000000" width="1"></td>
        <td bgcolor="#CCCCCC" height="15" align="center" class="orangeThickSmallCaps">$title_of_table</td> 
          <td bgcolor="#000000" width="1"></td>
    </tr>

PEPE;
}

function orow(){ // Opening Row
echo <<<PEPE
    <tr>

PEPE;
}

function crow(){ // Closing Row
echo <<<PEPE
    </tr>

PEPE;
}

function bc(){ //Black Cell
echo <<<PEPE
        <td bgcolor="#000000" width="1"></td>

PEPE;
}

function od(){ // Open Cell
echo <<<PEPE
        <td align="center">

PEPE;
}

function oda($align){ // Open Cell
echo <<<PEPE
        <td align="$align">

PEPE;
}

function odw($width_cell){ //Open Cell w/ WIDTH
echo <<<PEPE
        <td align="center" width="$width_cell">

PEPE;
}

function odwa($width_cell, $align){ //Open Cell w/ WIDTH ALIGN
echo <<<PEPE
        <td align="$align" width="$width_cell">

PEPE;
}

function odwv($width_cell, $v_align){ //Open Cell w/ WIDTH VALIGN
echo <<<PEPE
        <td align="center" width="$width_cell" valign="$v_align">

PEPE;
}

function odwav($width_cell, $align, $v_align){ // Open Cell w/ WIDTH ALIGN VALIGN
echo <<<PEPE
        <td align="$align" width="width_cell" valign="$v_align">

PEPE;
}

function odv($v_align){
echo <<<PEPE
        <td valign="$v_align">

PEPE;
}

function cd(){ // Close Cell
echo <<<PEPE
        </td>

PEPE;
}
                                                                                //
                                                                            //////
                                                                        //////////
                                                                    /// TABLE DUNCTIONS
//////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////
                                                                ///// FORM //
                                                                    /////////
                                                                        /////
                                                                            
function oform($file, $method){ // Form Open 
echo <<<PEPE
    <form action="$file" method="$method">

PEPE;
}

function cform(){ // Form Close 
echo <<<PEPE
    </form>

PEPE;
}

function submit($value){ // Form Submit button
echo <<<PEPE
            <input type="submit" value="$value" >

PEPE;
}

function input($name,$value){ //Form Input Text
echo <<<PEPE
            <input name="$name" value="$value">

PEPE;
}                   

function oselect($name){ // Open Select Form
echo <<<PEPE
            <select name="$name">

PEPE;
}

function option($option){ // Put a Selection
echo <<<PEPE
                <option>$option

PEPE;
}

function options($option){
echo <<<PEPE
                <option selected>$option

PEPE;
}

function cselect(){ // Close Select Form
echo <<<PEPE
            </select>

PEPE;
}

function hidden($name,$value){
echo <<<PEPE
            <input type="hidden" name="$name" value="$value">

PEPE;
}
                                                                        ///
                                                                    ///////
                                                                /// FORM //
///////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////
                                                                ///// MISC //
                                                                    /////////
                                                                        /////

function globals_defined(){  // GLOBALS
global $this_file_name;
global $g;
global $p;
}

function olink($link, $class){ // OPEN LINK
echo <<<PEPE
                <a href="$link" class="$class">

PEPE;
}

function clink(){  // CLOSE LINK
echo <<<PEPE
                </a>

PEPE;
}

function brejk(){ // <BR>
echo<<<PEPE
<BR>

PEPE;
}


function output($the_put){ //Echo stuff
echo <<<PEPE
$the_put
PEPE;
}

function ocss($class){
echo <<<PEPE
                <div class="$class">

PEPE;
}

function ccss(){
echo <<<PEPE
                </div>

PEPE;
}


                                                                        ///
                                                                    ///////
                                                                /// MISC //
///////////////////////////////////////////////////////////////////////////

}
?>
</body>
</html> 

  #4  
Old 12-Apr-2004, 20:50
pepee's Avatar
pepee pepee is offline
New Member
 
Join Date: Apr 2004
Posts: 11
pepee is on a distinguished road
I don't wanna be pain in the neck, but can you take a quick look at my script... i would be very thankful!
Regards,
PePee
  #5  
Old 14-Apr-2004, 04:59
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Pepee,

I am sorry no one got to this thread of yours in time.

I did take a look at your code as soon as you posted it but I must admit that I am not too eager to go through it line by line (it's simply too much to digest and I have my own codes to figure out too).

However, I can say this about what you've pasted here:

I usually write PHP codes in small chunks. Every function and/or class is written in as few lines as possible, doing very specific things. I didn't ALWAYS code like that, it took months and many experiments to finally find my own style of breaking everything into little (reusable) pieces - and so lines upon lines of codes like this puts me off very quickly.

That's my only suggestion, if you can call it that.
 

Recent GIDBlogPrepping for deployment by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Call a C program through Linux shell script nuwandee C Programming Language 3 29-Mar-2004 21:54
JavaScript Tutorial Part 1 pcxgamer Web Design Forum 2 01-Dec-2003 09:16
sms web sender script jrobbio MySQL / PHP Forum 1 11-Apr-2003 12:19
A very good sitemap script jrobbio MySQL / PHP Forum 3 03-Mar-2003 01:07
VALIDATING file types on an PHP upload script? JdS MySQL / PHP Forum 0 02-Jan-2003 07:58

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:23.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.