SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Resistance is Futile webgodjj's Avatar
    Join Date
    Nov 2002
    Location
    Madison, WI USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Script works! however needs help

    Ok... so I have finally finished my first admin script. I feel like a proud father. I think the script rocks (well, because it works). However, since this is my first script, I know that it probably isn't 100% good code.

    What could be improved on? what could be added/removed? specific examples would be great! I have learned so much designing this script, and would like to learn more...


    PHP Code:
    <?php

    $dB 
    mysql_connect("localhost""XXXX""XXXX");

    if (!
    $dB) {
          echo( 
    "<p>Unable to connect to the " .
                
    "database server at this time.</p>" );
          exit();
        }
    mysql_select_db("bandungr_bandung",$dB);

    if (! @
    mysql_select_db("bandungr_bandung") ) {
          echo( 
    "<p>Unable to locate the " .
                
    "database at this time.</p >" );
                 exit();
        }

    if (
    $t==1) {

    // Start File Upload
    if (!empty($_FILES['Menu_pict']['name'])) {
    //    if ($_FILES['Menu_pict']['type'] == "image/jpeg")
    //        {
            
    copy ($_FILES['Menu_pict']['tmp_name'], "../images/food/".$_FILES['Menu_pict']['name']) 
            or die (
    "Could not copy");

            echo 
    "<B>Name:</B> ".$_FILES['Menu_pict']['name']." ";
            echo 
    "<B>Size:</B> ".$_FILES['Menu_pict']['size']." ";
            echo 
    "<B>Type:</B> ".$_FILES['Menu_pict']['type']." ";
            echo 
    "<FONT COLOR=\"CC0000\">Copy Done....</FONT>";
    //        }
    //    else
    //        {
    //        echo "<br><br>";
    //        echo "Could Not Copy, Wrong Filetype (".$_FILES['Menu_pict']['name'].")<br>";
    //        echo ($_FILES['Menu_pict']['name']) ."<BR>";
    //        }
        
    }
    // End File Upload

    $Menu_pict $_FILES['Menu_pict']['name'];

    $SQL="INSERT INTO Main_Menu VALUES('','$Menu_item','$Menu_desc','$Menu_price','$Menu_pict','$Menu_cat')";
    $result=mysql_query($SQL,$dB);

    } elseif (
    $t==2) { 

    $SQL="SELECT * FROM Main_Menu WHERE Menu_id=$Menu_id";
    $result=mysql_query($SQL,$dB);

    $row mysql_fetch_array($result);

    $Menu_item $row["Menu_item"];
    $Menu_desc $row["Menu_desc"];
    $Menu_price $row["Menu_price"];
    $Menu_pict $row["Menu_pict"];
    $Menu_cat $row["Menu_cat"];


    } elseif (
    $t==4) {

    $SQL="DELETE FROM Main_Menu WHERE Menu_id=$Menu_id";
    $result=mysql_query($SQL,$dB);

    // Start delete file
    if (!empty($Menu_pict)) {
        if(
    trim($Menu_pict))

        {
            
    unlink("../images/food/".$Menu_pict);
        }
        else

        {
            echo 
    "$Menu_Pict";
            echo 
    "No File Exists!";
        }
    }
    // end delete file

    } else {



    // Start File Upload
    if (!empty($_FILES['Menu_pict']['name'])) {
        if (
    $_FILES['Menu_pict']['type'] == "image/jpeg")
            {
            
    copy ($_FILES['Menu_pict']['tmp_name'], "../images/food/".$_FILES['Menu_pict']['name']) 
            or die (
    "Could not copy");

            echo 
    "<B>Name:</B> ".$_FILES['Menu_pict']['name']." ";
            echo 
    "<B>Size:</B> ".$_FILES['Menu_pict']['size']." ";
            echo 
    "<B>Type:</B> ".$_FILES['Menu_pict']['type']." ";
            echo 
    "<FONT COLOR=\"CC0000\">Copy Done....</FONT>";
            }
        else
            {
            echo 
    "<br><br>";
            echo 
    "Could Not Copy, Wrong Filetype (".$_FILES['Menu_pict']['name'].")<br>";
            }
        }

    // End File Upload

    $Menu_pict $_FILES['Menu_pict']['name'];

    $SQL="UPDATE Main_Menu SET Menu_item='$Menu_item', Menu_desc='$Menu_desc', Menu_price='$Menu_price', Menu_pict='$Menu_pict', Menu_cat='$Menu_cat' WHERE Menu_id=$Menu_id";
    $result=mysql_query($SQL,$dB);

    }

    ?>

    <?php

    if ($t == 2) {
    $t_value 3;
    } elseif (
    $t == 3) {
    $t_value 3;
    } elseif (
    $t == 4) {
    $t_value 1
    } else {
    $t=1;
    $t_value 1;
    }

    ?> 
    <table width="600" border="0" cellspacing="0" cellpadding="2" class="content"><TR BGCOLOR="777777"><TD>
    <h2><?php

    $t_headers 
    = array('Insert Item','Edit Item','Item Edited','Item Deleted');
    if (
    $t == && (isset($Menu_item) && isset($Menu_desc) && isset($Menu_price) && isset($Menu_pict) && isset($Menu_cat))) {
    echo 
    "Record Inserted";
    $t_headers "";
    }

    echo 
    $t_headers[$t-1];
    ?></h2><TD Align="right"><H2><FONT COLOR="WHITE">Dinner Menu</FONT></H2></TD></TR>
    </TD></TR>
    </TABLE>
    <p>
    <form name="author_admin" method="post" action="<?php echo $PHP_SELF;?>" onSubmit="return verify()" enctype="multipart/form-data">
    <table width="600" border="0" cellspacing="2" cellpadding="2" class="content">
    <TR><TD>Menu Item: </TD><TD><input type="text" name="Menu_item" value="<?php echo $Menu_item;?>"></TD></TR>
    <TR><TD valign="top">Menu Description: </TD><TD><textarea name="Menu_desc" cols="40" rows="4"><?php echo $Menu_desc;?></textarea></TD></TR>
    <TR><TD>Menu Price: </TD><TD><input type="text" name="Menu_price" value="<?php echo $Menu_price;?>"></TD></TR>
    <TR><TD>Menu Picture: </TD><TD><?php if ($t != 4) { echo "<A HREF=\"../images/food/$Menu_pict\" onclick=\"NewWindow(this.href,'Photo','350','350','no');return false\">$Menu_pict</A>"; }?><BR><input type="file" name="Menu_pict"> (optional)</TD></TR>
    <TR><TD>Menu Category: </TD><TD>


    <?PHP

    if ($Menu_cat == 'Appetizers')
    {
        
    $sel1' SELECTED';
        }
    elseif (
    $Menu_cat == 'Soups')
    {

        
    $sel2' SELECTED';
    }
    elseif (
    $Menu_cat == 'House Specials')
    {
        
    $sel3' SELECTED';
    }
    elseif (
    $Menu_cat == 'Fried Rice/Noodles')
    {
        
    $sel4' SELECTED';
        }
    elseif (
    $Menu_cat == 'Desserts')
    {
        
    $sel5' SELECTED';
    }
    elseif (
    $Menu_cat == 'Main Entrees')
    {
        
    $sel6' SELECTED';
    }
    elseif (
    $Menu_cat == 'Sides')
    {
        
    $sel7' SELECTED';
    }
    ?>
    <SELECT name="Menu_cat">
    <option value="Appetizers" <?PHP echo $sel1?>>Appetizers</option>
    <option value="Soups" <?PHP echo $sel2?>>Soups</option>
    <option value="House Specials" <?PHP echo $sel3?>>House Specials</option>
    <option value="Main Entrees" <?PHP echo $sel6?>>Main Entrees</option>
    <option value="Fried Rice/Noodles" <?PHP echo $sel4?>>Fried Rice/Noodles</option>
    <option value="Desserts" <?PHP echo $sel5?>>Desserts</option>
    <option value="Sides" <?PHP echo $sel7?>>Sides</option>
    </SELECT>
    </TD></TR>

    <TR><TD></TD><TD><input type="submit" name="submit" value="Insert/Update"><P>
    <input type="hidden" name="t" value="<?php echo $t_value;?>">
    <input type="hidden" name="Menu_id" value="<?php echo $Menu_id;?>">
    <a href="<?php echo $PHP_SELF;?>">Insert New Records</a></TD></TR>
    </TABLE>
    </form>

    <p>
    <?php

    // COLUMN ORDER

    $validColumns = array('Menu_item','Menu_desc','Menu_price','Menu_pict','Menu_cat');

    if (isset(
    $_GET['orderby']) && in_array($_GET['orderby'],$validColumns))

    {
    $order $_GET['orderby'];
    }

    else
    {
    $order 'Menu_id';
    }

    // SORT ORDER

    $validDirection = array('DESC''ASC');

    if (isset(
    $_GET['orderdirection']) && in_array($_GET['orderdirection'], $validDirection))
    {
    $direction $_GET['orderdirection'];
    }

    else
    {
    $direction 'DESC';
    }

    // Set wich direction the sort is going to be (Need to be passed on to the link)

    $orderDirection reset(array_diff($validDirection, array($direction)));

    $SQL="SELECT * FROM Main_Menu ORDER BY $order $direction";

    $result=mysql_query($SQL,$dB);

    $num mysql_num_rows($result);
    $cur 1;

    echo 
    '<table width="600" border="0" cellspacing="2" cellpadding="2" class="content">
    <tr> 
    <td align="right">'
    ;
    echo 
    "Total number of items: $num";
    echo 
    '</td></tr></Table>';

    echo 
    '<table width="600" border="0" cellspacing="2" cellpadding="2" class="content">
    <tr BGCOLOR="777777"> 
    <td nowrap><a href="'
    .$_SERVER['PHP_SELF'].'?orderby=Menu_item&orderdirection=' $orderDirection '"><b>Item</b></a></td>
    <td nowrap><a href="'
    .$_SERVER['PHP_SELF'].'?orderby=Menu_price&orderdirection=' $orderDirection '"><b>Price</b></a></td>
    <td nowrap><b>Pict</b></td>
    <td nowrap><a href="'
    .$_SERVER['PHP_SELF'].'?orderby=Menu_cat&orderdirection=' $orderDirection '"><b>Cat</b></a></td>
    <td nowrap><b>action</b></td>
    </tr>'
    ;

    while (
    $num >= $cur) {
    $row mysql_fetch_array($result);

    $Menu_id $row["Menu_id"];
    $Menu_item $row["Menu_item"];
    $Menu_desc $row["Menu_desc"];
    $Menu_price $row["Menu_price"]; 
    $Menu_pict $row["Menu_pict"]; 
    $Menu_cat $row["Menu_cat"];

    if (
    $Menu_pict !="") {
        
    $Pict_list "<A HREF=\"../images/food/$Menu_pict\" onclick=\"NewWindow(this.href,'Photo','400','300','no');return false\"><IMG SRC=\"images/view.gif\" Border=\"0\"></A>";
    }
    else
    {
        
    $Pict_list ="";
    }

    echo 
    "<tr><td nowrap><B>$Menu_item</B></td><td><B>\$$Menu_price</B></td><td>$Pict_list</td><td><B>$Menu_cat</B></td><td nowrap><a href=\"$PHP_SELF?t=2&Menu_id=$Menu_id\"><IMG SRC=\"images/edit.gif\" Border=\"0\"></a> | <a href=\"$PHP_SELF?t=4&Menu_id=$Menu_id&Menu_pict=$Menu_pict\"><IMG SRC=\"images/trash.gif\" Border=\"0\"></a></td></tr>\n";
    echo 
    "<TR><TD COLSPAN=\"4\">$Menu_desc</TD></TR>";
    echo 
    "\n";
    echo 
    "<TR><TD COLSPAN=\"5\"><HR HEIGHT=\"1\" COLOR=\"000000\"></TD></TR>";
    echo 
    "\n";

    $cur++;
    }
    echo 
    "</table>";
    ?>

  2. #2
    SitePoint Guru GamerZ's Avatar
    Join Date
    Nov 2001
    Location
    Singapore
    Posts
    617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    erm u shld try to indent ur code, so that it can be read easily


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •