Jump to content

drezzia

New Members
  • Posts

    4
  • Joined

  • Last visited

Posts posted by drezzia

  1. Thanks for your help...

    After spending several hours, I was able to figure where the error lies. All I posted was actually OK but the method for querying db that is inside load_comments() had lil issues. The line `$this->query_custom()` This is how the query_custom() method looks like...     

    function query_custom($sql, $params = "")
        {
    
            $stmt = $this->con->prepare($sql);
    
            if (isset($params)) {
                $stmt->bind_param($params[0], ...$params[1]);
            }
    
            $stmt->execute();
    
            $res = $stmt->get_result();
    
            $row = $res->fetch_assoc();
            
            $count = $res->fetch_row();
            
            return [
                "result" => $res,
                "execute" => $stmt,
                "row" => $row,
                "count" => $count,
            ];
        }
        

    Since I want to loop through the results with a while loop then I should have left out this line...  `$row = $res->fetch_assoc()` and therefore return *$res*. So when I changed it to what I have below, it works as expected.

     

    function query_custom($sql, $params = "")
        {
           
            $stmt = $this->con->prepare($sql);
    
            if (isset($params)) {
                $stmt->bind_param($params[0], ...$params[1]);
            }
    
            $stmt->execute();
    
            $res = $stmt->get_result();
            
            return [
                "result" => $res,
                "execute" => $stmt,
                
            ];
        }
        
    
     

     

  2. I have two methods in a class. The first is 

    save_comment()

    It basically just inserts comment into db and then after its successfully another method is called to load some comments and the method is named 

    load_comments()

     

    This is the code for better understanding. Unnecessary part had been commented out. 

     

     function save_comment()
        {
            //$this->Functions->debug();
    
            //request token
            //$token = $this->Functions->clean($_GET["token"]);
    
            //check for csrf
           // $check = $this->Functions->csrf_check($token);
    
            /*if ($check === false) {
                $this->Functions->api_response("Invalid Request!!!", 0, "csrf");
            }*/
    
            $pid = $this->Functions->clean($_GET["pid"]);
    
            $offset = $this->Functions->clean("", $_GET["offset"], "");
    
            //comment
            $comment = $this->Functions->clean($_GET["user_comment"]);
    
            //check login status
           /* if (!isset($_SESSION["user"])) {
                $this->Functions->api_response(
                    "You must login to make comments !!!.",
                    0,
                    "csrf"
                );
            }*/
    
            //check if comment is less than 5 charcaters.
           /* if (strlen($comment) < 5) {
                $this->Functions->api_response(
                    "Your Comment is too short !!!.",
                    0,
                    "csrf"
                );
            }*/
    
            try {
                $user = $_SESSION["user"];
    
                $params = ["sss", "?,?,?", [$pid, $user, $comment]];
    
                $save_comment = $this->CustomDB->insert(
                    "comments",
                    "pid,user,comment",
                    $params
                );
    
    
                if ($save_comment->affected_rows == 1) {
                    $offset = 0;
    
                    $data = $this->load_comments($pid, $offset);
             
    echo $data;//this returns empty string
    
                    $this->Functions->api_response(
                        "Thanks!! Your Comment has been submitted 🤗 ",
                        1,
                        "csrf",
                        $data
                    );
                } else {
                    throw new Exception("Something went wrong. Try again!!!");
                }
            } catch (Exception $e) {
                //try
    
                $msg = $e->getMessage();
    
                $this->Functions->api_response($msg, 0, "csrf");
            }
        } //comment()

    Now the second method is

     function load_comments($pid, $offset)
        {
            $this->Functions->debug();
    
            //get comments so we can update at frontend
            $params = ["si", [$pid, $offset]];
    
            //fetch comments
            $fetch = $this->CustomDB->query_custom(
                "SELECT * FROM comments WHERE pid=? ORDER BY id DESC LIMIT 10 OFFSET?",
                $params
            );
    
            $fetch = $fetch["result"];
    
    
            $html = "";
    
            //$html;
    
            while ($row = $fetch->fetch_assoc()) {
                $name = $row["user"];
    
                $comment = $row["comment"];
                $html .=
                    '<div class="columns is-mobile display"><div style="border-radius:30px 30px 30px 30px;" class="column is-full box"><p class="subtitle is-6 mb-3"><em>' .
                    $comment .
                    '</em></p><span class="bold ml-2">' .
                    $name .
                    "</span></div></div><br>";
                    
                 
            }
    
            return $html;
    
           
        }

    I can't really figure why it shouldn't work as expected

  3.  

    I am calling a method from another method in the same class but its showing undefined function.

        
         

    class Store {
    
    
        show_price() {
            
    
            alert("Well done , dude!!!");
    
    
    }
    
        //another method
        fast_selling() {
    
    
         //works here
        // this.show_price();
    
            
    
              let obj = {"0":"zero","1":"one"}
    
              let data;
    
              let index = 0;
    
              let html ="";
    
    $("#more").click(function () {
    
    
                let el = $(this);   
    
                const more = (index) => {
         
                    for (let i = 1; i <= 4; i++) {
                     
                        data = obj[index];
    
                        if (data === undefined && i > 1) {
                            continue;
                        } //condition 1
    
                      
                        if (data === undefined && i === 1) {
                            //change page number to 1
                            el.find(".pageNo").html(1);
                            //index of first element of page 1
                            index = 0;
    
                            //remove conteent of htmlm
                            html = "";
    
                            //re run loop
                            more(index);
    
                            return;
                        } //condition
    
                        
    
                        index++;
                    } //for loop
    
                    //display product
                    $("#fs-wrap").html(html);
    
                    return true;
    
    
                }; //end more()
    
                //run function to load more
                let load = more(index);
    
                if (load == true) {
                    //update page
                    el.find(".pageNo").text(next_page);
                }
    
                //doesnt work here
                this.show_price();
            });
        } 
    } //class store
    
    
    const store = new Store();
    
    
    store.fast_selling() 

        

    Now when I want to use the fast_selling, all works fine except that I can't access that show_price() method.  But when I call it at the first code after fast_selling() method it works.

        

    I get an error that states that the show_price method is undefined.

     

×
×
  • 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.