Web Analytics Made Easy -
StatCounter Cleaning Up PHP Code - CodingForum

Announcement

Collapse
No announcement yet.

Cleaning Up PHP Code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cleaning Up PHP Code

    Greetings All,

    Im new to the forum and to PHP. The script i have written works just fine for now but i would like to try to "clean it up" a bit. Im sure since im new to this i will be writing bloated code for a while.

    This script pulls information from a mysql DB and populates to a <select> dropdown menu.

    Here is a snip of the code and some of the html.

    <?php
    $db = mysql_connect("");
    mysql_select_db("admin", $db);
    $StatusSql ="SELECT Status FROM ticket_status";
    $StatusResult=mysql_query($StatusSql);
    $options="";
    while ($StatusRow=mysql_fetch_array($StatusResult)) {
    $StatusThing=$StatusRow["Status"];
    $StatusOptions.="<OPTION VALUE=\"$id\">".$StatusThing;
    }
    ?>

    <?php
    mysql_select_db("admin", $db);
    $CatSql ="SELECT Category FROM ticket_category";
    $CatResult=mysql_query($CatSql);
    $Catoptions="";
    while ($CatRow=mysql_fetch_array($CatResult)) {
    $CatThing=$CatRow["Category"];
    $CatOptions.="<OPTION VALUE=\"$id\">".$CatThing;
    }
    ?>
    <?php
    mysql_select_db("admin", $db);
    $PriSql ="SELECT Priority FROM ticket_priority";
    $PriResult=mysql_query($PriSql);
    $Prioptions="";
    while ($PriRow=mysql_fetch_array($PriResult)) {
    $PriThing=$PriRow["Priority"];
    $PriOptions.="<OPTION VALUE=\"$id\">".$PriThing;
    }
    ?>
    <?php
    mysql_select_db("admin", $db);
    $MethSql ="SELECT ContactMethod FROM ticket_contact";
    $MethResult=mysql_query($MethSql);
    $Methoptions="";
    while ($MethRow=mysql_fetch_array($MethResult)) {
    $MethThing=$MethRow["ContactMethod"];
    $MethOptions.="<OPTION VALUE=\"$id\">".$MethThing;
    }
    ?>

    HTML

    <SELECT NAME=Status>
    <OPTION VALUE=0>Status
    <?=$StatusOptions?>
    </SELECT>

    <SELECT NAME=Priority>
    <OPTION VALUE=0>Priority
    <?=$PriOptions?>
    </SELECT>

    <SELECT NAME=Method>
    <OPTION VALUE=0>Method
    <?=$MethOptions?>
    </SELECT>

  • #2
    Well you might want to create a function...

    Something like...
    PHP Code:
    function getDBData($sql$dbName="dbName"$dbUser="userName"$dbPass="Password") {
        
    //include generic varables for access to the database;
        //define result set to be returned from function
        
    $linkID mysql_connect("localhost","$dbUser""$dbPass");
        
    //connect to database
        
    if ($linkID != FALSE) {
            
    //if good connection, select the database
            
    mysql_select_db("$dbName"$linkID);
            
    $resultID mysql_query("$sql"$linkID);
            
    //execute query
            
    if($resultID) {
                
    //if results, assign to result set
                
    $rs=$resultID;
            }
            else {
                die (
    "An error occured");
                
    //kill the program no results
            
    }
        }
        
    //close the database connection
        
    return $rs;
        
    mysql_close($linkID);

    Then just call it like this...

    PHP Code:
    $sql "SELECT * FROM TABLE";
    $rs getDBData($sql);
    while(
    $r mysql_fetch_assoc($rs)) {
        
    //do something
    }
    mysql_free_result($rs); 
    Create accessible online surveys -::- Koobten.com - compare netbook prices and reviews -::- Affordable, reliable hosting for less than $20 per year
    Zend Certified Engineer

    Comment


    • #3
      Ok ive done something way wrong here.

      I am using Nusphere PhpED to write this in. when i run it in the local environment all works perfectly when uploaded to the webserver i get several errors.

      the page can be viewed at
      http://nynex.cyberspeed.us/Tickets/addticket.php

      here is the code ive included all the code. here are the errors:

      <ERRORS>
      Notice: Undefined variable: StatusOptions in C:\Inetpub\nynex_cyberspeed_us\Tickets\addticket.php on line 9

      Notice: Undefined variable: id in C:\Inetpub\nynex_cyberspeed_us\Tickets\addticket.php on line 20

      Notice: Undefined variable: CatOptions in C:\Inetpub\nynex_cyberspeed_us\Tickets\addticket.php on line 20

      Notice: Undefined variable: id in C:\Inetpub\nynex_cyberspeed_us\Tickets\addticket.php on line 20

      Notice: Undefined variable: id in C:\Inetpub\nynex_cyberspeed_us\Tickets\addticket.php on line 20

      Notice: Undefined variable: PriOptions in C:\Inetpub\nynex_cyberspeed_us\Tickets\addticket.php on line 30

      Notice: Undefined variable: id in C:\Inetpub\nynex_cyberspeed_us\Tickets\addticket.php on line 40

      Notice: Undefined variable: MethOptions in C:\Inetpub\nynex_cyberspeed_us\Tickets\addticket.php on line 40

      Notice: Undefined variable: id in C:\Inetpub\nynex_cyberspeed_us\Tickets\addticket.php on line 40

      Notice: Undefined variable: id in C:\Inetpub\nynex_cyberspeed_us\Tickets\addticket.php on line 40
      </Errors>

      <?php
      $db = mysql_connect("", "", "");
      mysql_select_db("admin", $db);
      $StatusSql ="SELECT Status FROM ticket_status";
      $StatusResult=mysql_query($StatusSql);
      $options="";
      while ($StatusRow==mysql_fetch_array($StatusResult)) {
      $StatusThing=$StatusRow["Status"];
      $StatusOptions.="<OPTION VALUE=\"\">".$StatusThing;
      }
      ?>

      <?php
      mysql_select_db("admin", $db);
      $CatSql ="SELECT Category FROM ticket_category";
      $CatResult=mysql_query($CatSql);
      $Catoptions="";
      while ($CatRow==mysql_fetch_array($CatResult)) {
      $CatThing=$CatRow["Category"];
      $CatOptions.="<OPTION VALUE=\"$id\">".$CatThing;
      }
      ?>
      <?php
      mysql_select_db("admin", $db);
      $PriSql ="SELECT Priority FROM ticket_priority";
      $PriResult=mysql_query($PriSql);
      $Prioptions="";
      while ($PriRow==mysql_fetch_array($PriResult)) {
      $PriThing=$PriRow["Priority"];
      $PriOptions.="<OPTION VALUE=\"\">".$PriThing;
      }
      ?>
      <?php
      mysql_select_db("admin", $db);
      $MethSql ="SELECT ContactMethod FROM ticket_contact";
      $MethResult=mysql_query($MethSql);
      $Methoptions="";
      while ($MethRow==mysql_fetch_array($MethResult)) {
      $MethThing=$MethRow["ContactMethod"];
      $MethOptions.="<OPTION VALUE=\"$id\">".$MethThing;
      }
      ?>
      <html>
      <head>
      </head>
      <body>
      <strong><font size="2">Ticket Entry</strong></font>
      <br>
      <form method="post" name="addticket" action="addticket.php">
      <form type="hidden" name="CustomerID" value="<!-- need to insert php code here !-->">
      <table width="600" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="cccccc">
      <tr bgcolor="#F3F3F3">
      <td width="800" colspan="2">
      <table width="725" height="312" border="0" align="center" cellpadding="0" cellspacing="1">
      <tr>
      <td width="11%" height="18">
      <div align="right">User: </div>
      </td>
      <td width="18%" valign="middle">&nbsp;<b><!-- crete a script that will auto insert a cust id into field from previous page !--></b></td>
      <td width="15%">
      <div align="right">Quoted Hours:&nbsp;</div>
      </td>
      <td width="16%">
      <input type="text" name="Quoted" class="txtbox">
      </td>
      <td width="19%">
      <div align="right">Tickets This Month:&nbsp;</div>
      </td>
      <td width="21%"> <!-- create a script to count total tickets for user this month !--></td>
      </tr>
      <% end if %>
      <tr>
      <td width="11%" height="18">
      <div align="right">Assigned To:&nbsp;</div>
      </td>
      <td width="18%" valign="middle">
      <select name="AssignedTo">
      <!-- Need to write a script to select from admin users table !-->
      </select>
      &nbsp; </td>
      <td width="15%">
      <div align="right">Time Spent:&nbsp;</div>
      </td>
      <td width="16%">
      <input type="text" name="TimeSpent" class="txtbox">
      </td>
      <td width="19%">
      <div align="right">Total Tickets:&nbsp;</div>
      </td>
      <td width="21%"> <!-- create a script yo display current total tickets !--></td>
      </tr>
      <tr>
      <td height="17" width="11%">
      <div align="right">Status:&nbsp; </div>
      </td>
      <td width="18%">
      <SELECT NAME=Status>
      <OPTION VALUE=0>Status
      <?=$StatusOptions?>
      </SELECT>
      </td>
      <td height="16" width="15%">
      <div align="right">Billable:&nbsp;</div>
      </td>
      <td width="16%">
      <input type="checkbox" name="Billable">
      </td>
      <td width="19%">
      <div align="right">Created:&nbsp;</div>
      </td>
      <td width="21%"><!-- create script to insert date and time !--></td>
      </tr>
      <tr>
      <td width="11%">
      <div align="right">Category:&nbsp;</div>
      </td>
      <td width="18%">
      <SELECT NAME=Category>
      <OPTION VALUE=0>Category
      <?=$CatOptions?>
      </SELECT>
      </td>
      <td height="16" width="15%">
      <div align="right">Billed:&nbsp;</div>
      </td>
      <td width="16%">
      <input type="checkbox" name="Billed">
      </td>
      <td width="19%">
      <div align="right">Opened By:&nbsp;</div>
      </td>
      <td width="21%"><!-- create a script to auto insert creator of ticket from the login id !--></td>
      </tr>
      <tr>
      <td height="17" width="11%">
      <div align="right">Priority:&nbsp;</div>
      </td>
      <td width="18%">
      <SELECT NAME=Priority>
      <OPTION VALUE=0>Priority
      <?=$PriOptions?>
      </SELECT>
      </td>
      <td height="4" width="15%">
      <div align="right">E-mail User:&nbsp;</div>
      </td>
      <td height="4" width="16%">
      <input type="checkbox" name="E-mailUser" value="checkbox">
      </td>
      <td width="19%">
      <div align="right">Customer Name: </div>
      </td>
      <td width="21%">&nbsp;<!-- create script to select from customer table field comapny name so company name is display here !--></td>
      </tr>
      <tr>
      <td height="17" width="11%">
      <div align="right">Contact By:&nbsp;</div>
      </td>
      <td width="18%">
      <SELECT NAME=Method>
      <OPTION VALUE=0>Method
      <?=$MethOptions?>
      </SELECT>
      </td>
      <td height="2" width="15%">
      <div align="right">E-mail Assigned To: </div>
      </td>
      <td height="2" width="16%">
      <input type="checkbox" name="emailadmin" checked><!-- if checked will send email to assigned to !-->
      </td>
      <td width="19%">
      <div align="right">Phone Number: </div>
      </td>
      <td width="21%">&nbsp;<!-- create a script to get phone number from customer table !--></td>
      </tr>
      <tr>
      <td width="11%">
      <div align="right">Due Date:&nbsp;</div>
      </td>
      <td width="18%">
      <input type="text" name="CompleteBy" value="" class="txtbox">
      </td>
      <td height="2" width="15%">
      <div align="right">Show To Customer:&nbsp;</div>
      </td>
      <td height="2" width="16%">
      <input type="checkbox" name="showtocustomer" ><!-- if true set "ShowToCustomer" to 1 !-->
      </td>
      <td width="19%">
      <div align="right">Add to Knowledge Base:&nbsp;</div>
      </td>
      <td width="21%">
      <input type="checkbox" name="KnowledgeBase"><!-- if true set "PubKB" to 1 !-->
      </td>
      </tr>
      <tr>
      <td colspan="6"> Subject:<br>
      <input type="text" name="Subject" value="" size="100" >
      </td>
      </tr>
      <tr>
      <td colspan="6"> Description:<br>
      <textarea name="Description" rows="5" cols="100" ></textarea>
      </td>
      </tr>
      <tr>
      <td height="10" colspan="6"> Resolution:<br>
      <textarea name="Resolution" rows="5" width=5 cols="100"></textarea>
      </td>
      </tr>
      <tr>
      <td height="10" colspan="6"> Internal Notes:<br>
      <textarea name="HiddenResolution" rows="5" width=5 cols="100"></textarea>
      </td>
      </tr>
      </table>
      <div align="right">

      <input type="image" src="../images/save.jpg" width="50" height="17" border="0">
      <a href="#" onClick="if (confirm('Nynex Admin:\nAre you sure you want to reset the form\?')) {document.forms[0].reset()};"><img src="../images/reset.jpg" width="50" height="17" border="0"></a>&nbsp;&nbsp;&nbsp;&nbsp;
      </div></td>
      </tr>
      </table>
      </form>
      </body>
      </html>

      Comment


      • #4
        Your local environment and the server may have different configuration settings. Check the values with phpinfo() and look what's different. In your case, the server has error_reporting set at a more verbose level. It's reporting the use of undefined variables, and that's a good thing - it's showing where something fuzzy is going on in your code so you can take a closer look and fix it.

        You need to initialize a variable first before you use the assignment operator on it. This will throw an error:

        $foo .= "bar";

        but this is ok

        $foo = "";
        $foo .= "bar";

        Further, the variable $id seems to be nowhere defined. I can't see where it should come from, you need to tell us.

        P.S: You need to read the Posting Guidelines, especially point 4.
        De gustibus non est disputandum.

        Comment

        Working...
        X