Results 1 to 2 of 2

Thread: count in php

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    1

    count in php

    ive been following a tutorial for a few days now and i cant get this part to work.

    its ment to display the count but ive i use the code below it works but if i use the 2nd code it dosent work can anyone help please?.



    PHP Code:
     function getRealIpAddr()
    {
    if (!empty(
    $_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
    {
    $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty(
    $_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
    {
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
    $ip=$_SERVER['REMOTE_ADDR'];
    }
    return 
    $ip;
    }

    function 
    cart() {

    if(isset(
    $_GET['add_cart'])){

    global 
    $db;

    $p_id $_GET['add_cart'];

    $ip_add getRealIpAddr();

    $check_pro "select * from cart where ip_add='$ip_add' AND p_id='$p_id'";

    $run_check mysqli_query($db,$check_pro);

    if(
    mysqli_num_rows($run_check) > 0) {

    echo 
    "";

    }
    else {

    $q "insert into cart (p_id,ip_add) values ('$p_id','1')";

    $run_q mysqli_query($db,$q);

    echo 
    "<script>window.open('index.php','_self')</script>";
    }

    }

    }

    //getting the number of items from the cart

    function items() {

    if(isset(
    $_GET['add_cart'])){

    global 
    $db;

    $ip_add getRealIpAddr();


    $get_items "select * from cart where ip_add='1'";

    $run_items mysqli_query($db$get_items);

    $count_items mysqli_num_rows($run_items);

    }
    else {

    $ip_add getRealIpAddr();

    global 
    $db;

    $get_items "select * from cart where ip_add='1'";

    $run_items mysqli_query($db$get_items);

    $count_items mysqli_num_rows($run_items);

    }

    echo 
    $count_items;

    }

    //getting the total price of the items from the cart

    function total_price() {

    $ip_add getRealIpAddr();

    global 
    $db;

    $total 0;

    $sel_price "select * from cart where ip_add='1'";

    $run_price mysqli_query($db$sel_price);

    while (
    $record=mysqli_fetch_array($run_price)){

    $pro_id $record['p_id'];

    $pro_price "select * from products where product_id='$pro_id'";

    $run_pro_price mysqli_query($db,$pro_price);

    while(
    $p_price=mysqli_fetch_array($run_pro_price)){

    $product_price = array($p_price['product_price']);

    $values array_sum($product_price);

    $total +=$values;


    }

    }

    echo 
    "&pound;" $total;


    but it i change it to

    PHP Code:
    function getRealIpAddr()
    {
    if (!empty(
    $_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
    {
    $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty(
    $_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
    {
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
    $ip=$_SERVER['REMOTE_ADDR'];
    }
    return 
    $ip;
    }

    function 
    cart() {

    if(isset(
    $_GET['add_cart'])){

    global 
    $db;

    $p_id $_GET['add_cart'];

    $ip_add getRealIpAddr();

    $check_pro "select * from cart where ip_add='$ip_add' AND p_id='$p_id'";

    $run_check mysqli_query($db,$check_pro);

    if(
    mysqli_num_rows($run_check) > 0) {

    echo 
    "";

    }
    else {

    $q "insert into cart (p_id,ip_add) values ('$p_id','$ip_add')";

    $run_q mysqli_query($db,$q);

    echo 
    "<script>window.open('index.php','_self')</script>";
    }

    }

    }

    //getting the number of items from the cart

    function items() {

    if(isset(
    $_GET['add_cart'])){

    global 
    $db;

    $ip_add getRealIpAddr();


    $get_items "select * from cart where ip_add='$ip_add'";

    $run_items mysqli_query($db$get_items);

    $count_items mysqli_num_rows($run_items);

    }
    else {

    $ip_add getRealIpAddr();

    global 
    $db;

    $get_items "select * from cart where ip_add='$ip_add'";

    $run_items mysqli_query($db$get_items);

    $count_items mysqli_num_rows($run_items);

    }

    echo 
    $count_items;

    }

    //getting the total price of the items from the cart

    function total_price() {

    $ip_add getRealIpAddr();

    global 
    $db;

    $total 0;

    $sel_price "select * from cart where ip_add='$ip_add'";

    $run_price mysqli_query($db$sel_price);

    while (
    $record=mysqli_fetch_array($run_price)){

    $pro_id $record['p_id'];

    $pro_price "select * from products where product_id='$pro_id'";

    $run_pro_price mysqli_query($db,$pro_price);

    while(
    $p_price=mysqli_fetch_array($run_pro_price)){

    $product_price = array($p_price['product_price']);

    $values array_sum($product_price);

    $total +=$values;


    }

    }

    echo 
    "&pound;" $total;



  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: count in php

    Please format your code, it makes it extremely difficult to read how it is right now. This is your code formatted using PHP Beautifier:
    PHP Code:
    function getRealIpAddr()
    {
        if (!empty(
    $_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
        
    {
            
    $ip $_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty(
    $_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
        
    {
            
    $ip $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else {
            
    $ip $_SERVER['REMOTE_ADDR'];
        }

        return 
    $ip;
    }

    function 
    cart()
    {
        if (isset(
    $_GET['add_cart'])) {
            global 
    $db;
            
    $p_id $_GET['add_cart'];
            
    $ip_add getRealIpAddr();
            
    $check_pro "select * from cart where ip_add='$ip_add' AND p_id='$p_id'";
            
    $run_check mysqli_query($db$check_pro);
            if (
    mysqli_num_rows($run_check) > 0) {
                echo 
    "";
            }
            else {
                
    $q "insert into cart (p_id,ip_add) values ('$p_id','$ip_add')";
                
    $run_q mysqli_query($db$q);
                echo 
    "<script>window.open('index.php','_self')</script>";
            }
        }
    }

    // getting the number of items from the cart

    function items()
    {
        if (isset(
    $_GET['add_cart'])) {
            global 
    $db;
            
    $ip_add getRealIpAddr();
            
    $get_items "select * from cart where ip_add='$ip_add'";
            
    $run_items mysqli_query($db$get_items);
            
    $count_items mysqli_num_rows($run_items);
        }
        else {
            
    $ip_add getRealIpAddr();
            global 
    $db;
            
    $get_items "select * from cart where ip_add='$ip_add'";
            
    $run_items mysqli_query($db$get_items);
            
    $count_items mysqli_num_rows($run_items);
        }

        echo 
    $count_items;
    }

    // getting the total price of the items from the cart

    function total_price()
    {
        
    $ip_add getRealIpAddr();
        global 
    $db;
        
    $total 0;
        
    $sel_price "select * from cart where ip_add='$ip_add'";
        
    $run_price mysqli_query($db$sel_price);
        while (
    $record mysqli_fetch_array($run_price)) {
            
    $pro_id $record['p_id'];
            
    $pro_price "select * from products where product_id='$pro_id'";
            
    $run_pro_price mysqli_query($db$pro_price);
            while (
    $p_price mysqli_fetch_array($run_pro_price)) {
                
    $product_price = array(
                    
    $p_price['product_price']
                );
                
    $values array_sum($product_price);
                
    $total+= $values;
            }
        }

        echo 
    "&pound;" $total;

    Also, I would highly recommend that you use PDO instead of mysqli; PDO is definitely the future of PHP. On that note you should also be using prepared statements to prevent something as simple as a SQL injection. Again, on that same note, any time you get any $_GET values you need to sanitize the input by using htmlentities. I'd also recommend using single quotes and commas for your echo statements, not only does it execute quicker but it is easier to read as well. Finally... please comment your code; comments are not just for programmers with whom you share your code with, but they're for you as well.

    With all of that said, take a look at this example:
    PHP Code:
    <?php

    function getRealIpAddr() {
      if (!empty(
    $_SERVER['HTTP_CLIENT_IP'])) {
        
    // Check ip from share internet
        
    $ip $_SERVER['HTTP_CLIENT_IP'];
      } elseif (!empty(
    $_SERVER['HTTP_X_FORWARDED_FOR'])) {
        
    // To check ip is pass from proxy
        
    $ip $_SERVER['HTTP_X_FORWARDED_FOR'];
      } else {
        
    $ip $_SERVER['REMOTE_ADDR'];
      }

      return 
    $ip;
    }

    function 
    getPdoDb() {
      
    //TODO: Change any of the database configuration information here
      
    $config =  array(
        
    "host" => "127.0.0.1",
        
    "username" => "root",
        
    "password" => "",
        
    "name" => "my super cool DB name"
      
    );

      
    // Create the DSN
      
    $dsn "mysql:dbname=$config[name];host=$config[host];charset=utf8mb4";

      
    // Create a new PDO object
      
    $db = new PDO($dsn$config['username'], $config['password']);
      
    $db->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
      
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARESfalse);

      
    // Return the PDO object
      
    return $db;
    }

    function 
    cart() {
      
    // Check if the $_GET array contains an "add_cart" value
      
    if (isset($_GET['add_cart'])) {
        
    // Create a new PDO object
        
    $db getPdoDb();

        
    // Sanitize the input and get the IP address
        
    $p_id htmlentities($_GET['add_cart']);
        
    $ip_add getRealIpAddr();

        
    // Setup a prepared SQL statement to return the count of rows
        
    $stmt $db->prepare('SELECT COUNT(`p_id`) FROM `cart` WHERE `ip_add`=:ip AND `p_id`=:id;');

        
    // Execute the query passing the sanitized values
        
    $stmt->execute(array(
          
    ':ip' => $ip_add,
          
    ':id' => $p_id
        
    ));

        
    // Get the value returned by a scalar command
        
    $row_count $stmt->fetchColumn();
        if (
    $row_count 0) {
          
    // Print nothing out (don't know why you need this)
          
    echo '';
        } else {
          
    // Create a new item in the cart
          
    $stmt $db->prepare('INSERT INTO `cart` (`p_id`, `ip_add`) VALUES (:id, :ip);');

          
    // Execute the query passing the sanitized values
          
    $stmt->execute(array(
          
    ':id' => $p_id,
          
    ':ip' => $ip_add
          
    ));

          
    // Go to index.php using PHP and not JavaScript (what if JavaScript is disabled)
          
    header('Location: index.php');
        }
      }
    }

    // getting the number of items from the cart
    function items() {
      
    // Create a new PDO object
      
    $db getPdoDb();

      
    // Get the IP address
      
    $ip_add getRealIpAddr();

      
    // Setup a prepared SQL statement to return the count of rows
      
    $stmt $db->prepare('SELECT COUNT(`ip_add`) FROM `cart` WHERE `ip_add`=?;');

      
    // Execute the query passing just the IP address
      
    $stmt->execute(array($ip_add));

      
    // Return the value returned by a scalar command
      
    return $stmt->fetchColumn();
    }

    // getting the total price of the items from the cart
    function total_price() {
      
    // Create a new PDO object
      
    $db getPdoDb();

      
    // Get the IP address
      
    $ip_add getRealIpAddr();

      
    // Setup a prepared SQL statement to return the sum of the product_price field based on the cart's IP address
      
    $stmt $db->prepare('
        SELECT
          SUM(p.`product_price`)
        FROM
          `cart` AS c
          INNER JOIN `products` AS p ON c.`product_id` = p.`product_id`
        WHERE
          c.`ip_add`=?;'
    );

      
    // Execute the query passing just the ip
      
    $stmt->execute(array($ip_add));

      
    // Get the value returned by a scalar command
      
    $total $stmt->fetchColumn();

      echo 
    '&pound;'$total;
    }
    ?>
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

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



Click Here to Expand Forum to Full Width