www.webdeveloper.com
Results 1 to 3 of 3

Thread: Could someone please help me with better solution of this PHP processing file?

  1. #1
    Join Date
    Jan 2014
    Posts
    2

    Could someone please help me with better solution of this PHP processing file?

    <?php

    // This script interprets the data from the submitted form
    // Write what could you change in the code and why


    $operation = $_POST['option'];
    $id_record_used_later_in_class_for_getting_data_from_the_database = $_GET['id'];


    $adminIdList = array(1, 2, 3);
    $key = 'form_test'; //Debug

    echo '<table>
    <tr>
    <th>UserID</th>
    <th>User Name</th>
    </tr>';

    switch ($operation) {

    case 1:

    DbEngine::Instance()->Query("SELECT * FROM table_users WHERE id = $id_record_used_later_in_class_for_getting_data_from_the_database");
    $arrResult = DbEngine::Instance()
    ->FetchAll()
    ->Execute();

    for ($i = 1; $i < count($arrResult); $i++) {
    $row = &$arrResult[$i];
    if (in_array($row['id'], $adminIdList)) {

    //Skip admins

    }

    else{
    echo '<tr>
    <td>' . $row['id'] . '</td>
    <td>' . $row['username'] . '</td>
    </tr>';
    }
    }

    case "2":
    $arrToUpdate = array();
    foreach ($_POST as $val) {
    if (strpos($key, 'form_') !== false) {
    $fieldName = substr($key, 4);
    $arrToUpdate[$fieldName] = $val;
    }
    }

    $result = DbEngine::Instance()
    ->Table('tablu_users')
    ->Update()
    ->Where(array('id' => $id_record_used_later_in_class_for_getting_data_from_the_database))
    ->Set($arrToUpdate)
    ->Execute();

    echo 'Update executed successfully.';

    case "3":
    $arrToInsert = array();
    foreach ($_POST as $val) {
    if (strpos($key, 'form_') !== false) {
    $fieldName = substr($key, 4);
    $arrToInsert[$fieldName] = $val;
    }
    }

    echo 'Insert executed successfully.';

    }


    echo '</div>';
    ?>

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    517
    PHP Code:
    <?php
     
    // This script interprets the data from the submitted form
     // Write what could you change in the code and why
    $operation $_POST['option'];
    //  USE A SHORTER VARIABLE NAME!!!!!  what the heck are you thinking??
    $id_record_used_later_in_class_for_getting_data_from_the_database $_GET['id'];
    $adminIdList = array(123);
    $key 'form_test'//Debug
    echo '<table><tr><th>UserID</th><th>User Name</th></tr>';
    switch (
    $operation
    {
        case 
    1:
            
    DbEngine::Instance()->Query("SELECT * FROM table_users WHERE id = $id_record_used_later_in_class_for_getting_data_from_the_database");
            
    $arrResult DbEngine::Instance()->FetchAll()->Execute(); 
            for (
    $i 1$i count($arrResult); $i++) 
            {
                
    $row = &$arrResult[$i];
                if (
    in_array($row['id'], $adminIdList)) 
                {
                
    // WHAT ARE YOU DOING HERE?
                //Skip admins
                
    }
                else
                {
                    echo 
    '<tr><td>' $row['id'] . '</td><td>' $row['username'] . '</td></tr>';
                }
            }
            
    // ??? NO BREAK HERE ??
        
    case "2":
            
    $arrToUpdate = array();
            foreach (
    $_POST as $val
            {
                if (
    strpos($key'form_') !== false
                {
                    
    $fieldName substr($key4);
                    
    $arrToUpdate[$fieldName] = $val;
                }
            }
            
    //  I HAVE NO IDEA WHAT THIS CODE IS RELATED TO SO CAN'T SAY ANYTHING ABOUT IT, OTHER THEN
            //  YOU HAVE JOINED TOGETHER SO MANY FUNCTION (?) CALLS IT MAKES IT OBTUSE FOR A NEW READER
            //  WHO MAY HAVE TO DEBUG IT LATER.
            
    $result DbEngine::Instance()->Table('tablu_users')->Update()->Where(array('id' => $id_record_used_later_in_class_for_getting_data_from_the_database))->Set($arrToUpdate)->Execute();
            echo 
    'Update executed successfully.';
            
    // ??? NO BREAK HERE ??
        
    case "3":
            
    $arrToInsert = array();
            foreach (
    $_POST as $val
            {
                if (
    strpos($key'form_') !== false
                {
                    
    $fieldName substr($key4);
                    
    $arrToInsert[$fieldName] = $val;
                }
            }
            echo 
    'Insert executed successfully.';

    }
    echo 
    '</div>';
    ?>
    I tried to make it more readable. Now as for improving it I'll be brief.

    1 - what's with the 30+ character var names? are you a masochist who WANTS to type that name all the time?

    2 - you begin your code with a <table> tag but you end your code with a </div> tag. Kind of odd - you don't finish the table and you don't start the div. Did you look at this code before submitting for public perusal?

    3 - you attempt to garner both a POST and GET var from the input. AFAIK, that's not going to work. Make up your mind.

    4 - you don't have the correct usage of the switch in place - read up on it or you'll be getting unexpected output from the switch cases.

    5 - in case=1 process you are referencing $arrResult[$I] with an ampersand. Why? You don't do anything with that reference, so what's the point of the & ?

    6 - in case=2&3 - IMHO you should be doing:
    foreach ($_POST as $key=>$val)
    7 - in case=3 you echo that your INSERT was successful, but I don't see the query that accomplishes this.

    Now that you have had someone take the time to volunteer to go thru your (obviously unchecked) code, what was the point? You didn't say whether you had success or failure with it, so what were you expecting? Simply someone to be your syntax/logic checker?

    PS - I would make it a habit to use shorter var names and to stop using upper/lower case names. With PHP being very picky about mixing case in vars there is really no need to capitalize names like JS does and it will only cause frustration in the future.

  3. #3
    Join Date
    Jan 2014
    Posts
    2
    i'm new user here and had some trouble during posting this script, there was missing code , tag etc.. i'll delete it and repost it more clear
    THank you ginerjm for your reply and for spending time on it

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles