Jump to content

benanamen

Members
  • Posts

    2,134
  • Joined

  • Last visited

  • Days Won

    42

Posts posted by benanamen

  1. The ORDER BY is pointless. You are only specifying a single ID in the WHERE clause.

    Update the method to return false if the query fails or if there are no results. You want to fail fast with a "guard clause". In your Home method check for false and redirect to the install if the condition matches.

    public function all_setting()
    {
        $query = $this->db->query("SELECT * FROM tbl_settings WHERE id=1");
        if ($query === false || $query->num_rows() === 0) {
            return false;
        }
        return $query->first_row('array');
    }


     

        if ($data['setting'] === false) {
            redirect('../install/');
        }
    
    // Other code... 

     

  2. For starters, the OP basically lied. The OP has ABSOLUTELY NOTHING to do with "Ratings in database ala Amazon". The OP admits it later on..

    "In reality, this is a quiz composed of 15 questions each of varying lengths."

    And no, I didn't know it was incorrect for what the op was really trying to do. Had it really been a rating system, the query does indeed work to show ratings. Nevertheless, I won't be using AI content in future posts.

  3. 4 hours ago, Barand said:

    To save time, let's just assume that sometimes you are.

    Sometimes yes, but in this case (and my other posts last night) no since it wasn't my response. Allow me to explain...

    I have been experimenting with the ChatGPT AI (Artificial Intelligence). "My" post was 100% generated by the ChatGPT AI based on the full text of the OP. Obviously AI still has a ways to go and can be absolutely wrong or even provide "dangerous" code solutions. Still, it is pretty cool what it can do at this stage. You can check it out here https://openai.com/blog/chatgpt/

  4. You can use a combination of the preg_grep() function and regular expressions to select the range of data that you want. The preg_grep() function returns all elements of an input array that match a certain regular expression pattern.

    Here's an example of how you can use it to select the range of data that you want:

    $start = preg_grep("/gpononu/", $array);
    $start_key = key($start);
    $end = preg_grep("/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/", $array);
    $end_key = key($end);
    $result = array_slice($array, $start_key, $end_key - $start_key + 1);
    print_r($result);

    This will give you an array containing the lines from the input array that match the pattern "gpononu" (line 9 and 13) until the first line that matches the pattern for IP addresses (line 11 and 15) You can use array_slice with 3rd parameter as $end_key - $start_key + 1 , this will give you desired result.

  5. The @ symbol in front of the variables in this code is called an error suppression operator. It is used to suppress error messages that would be generated when trying to access an undefined array key. However, this can make it difficult to identify and fix errors in your code.

    Instead of using the error suppression operator, you can check if the key exists in the array before trying to access it. You can use the isset() function to check if a variable is set, and the array_key_exists() function to check if a key exists in an array.

    Here is an example of how you can update the code to check for the existence of the key before accessing it:

    function CreateSignature()
    {
        $sig = "";
        if (isset($_SERVER["HTTP_USER_AGENT"])) {
            $sig .= $_SERVER["HTTP_USER_AGENT"];
        }
        if (isset($_SERVER["HTTP_ACCEPT"])) {
            $sig .= $_SERVER["HTTP_ACCEPT"];
        }
        if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
            $sig .= $_SERVER["HTTP_ACCEPT_LANGUAGE"];
        }
        if (isset($_SERVER["HTTP_ACCEPT_CHARSET"])) {
            $sig .= $_SERVER["HTTP_ACCEPT_CHARSET"];
        }
        $this->Signature = md5(Communication::GetIP() . $sig);
    }

    You can also use the ternary operator to shorten the code

    function CreateSignature()
    {
        $sig = isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : "";
        $sig .= isset($_SERVER["HTTP_ACCEPT"]) ? $_SERVER["HTTP_ACCEPT"] : "";
        $sig .= isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]) ? $_SERVER["HTTP_ACCEPT_LANGUAGE"] : "";
        $sig .= isset($_SERVER["HTTP_ACCEPT_CHARSET"]) ? $_SERVER["HTTP_ACCEPT_CHARSET"] : "";
        $this->Signature = md5(Communication::GetIP() . $sig);
    }

     

  6. You can use a subquery to get the list of all possible ratings and then left join it with your reviews table to get the count of each rating. Here's an example query that should work:

    SELECT ratings.rating, COALESCE(reviews.count, 0) as count FROM (
        SELECT DISTINCT rating FROM reviews
    ) as ratings
    LEFT JOIN (
        SELECT rating, COUNT(*) as count FROM reviews GROUP BY rating
    ) as reviews
    ON ratings.rating = reviews.rating

    This query first gets a list of all distinct ratings from the reviews table, and then left joins it with a subquery that gets the count of each rating from the reviews table. The COALESCE function is used to replace any NULL values in the count column with 0, in case a rating doesn't have any reviews.

  7. I am still not sure what you are doing with out more details, but I am sure that whatever you are attempting to do with the posted code is not the way to do it.

    5 hours ago, sen5241b said:

    I'm writing a lightweight  data agnostic processing engine

    This should be to focus of your post.

    5 hours ago, sen5241b said:

    It enables user to get items from a data catalog and add to it by simply adding new catalog items.

    More details or some third party example is in order at this point. This just sounds like basic database management.

  8. 1 hour ago, mac_gyver said:

    BTW - getCode() is the sqlstate, which groups together several similar errors, and is not the actual error number.

    I was just going to point that out. The if should be

    if ($e->getCode() == 23000) { // Duplicate user }

    1062 is held in the errorInfo array

  9. Small bit of code but several issues with it.

    1. Do not create variables for nothing. You already have the POST variables, just use them
    2. You need to check the REQUEST METHOD, not the name of a button. This can completely fail in certain cases. Also, in a properly coded form, ALL form elements save for checkboxes will be submitted (true)
    3. A blank space will get past your empty check. You need to trim the entire POST array all at once and THEN check for empty
    4. Never ever trust user supplied data. The code is vulnerable to an XSS Attack

     

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.