0

I am trying to wrap my head around this and i have searched google and im not sure how to go about it.

I have a table called Affiliates. In this table i have 3 fields

+---------+-----------------+-------+
| user_id | AffID           | total |
+---------+-----------------+-------+
|       1 | Aff1            |     10|
|       2 | Aff1            |     10|
|       3 | Aff2            |     5 |
|       4 | Aff2            |     5 |
+---------+-----------------+-------+

I want to echo out the following results :

Aff1 20
Aff2 10

As you can see it grouped the AffID and added the total value.

Now if i add another affiliate to the AffID for example AffID3 with a total of 10 , it must automatically just display it.

Aff1 20
Aff2 10
Aff3 10

I know i need to use groupby for the AffID , and Sum to calculate the total, but how would i echo out the results ? (in a loop?)

8
  • is there any code at all? it's not easy to help if there's nothing. Commented Jun 14, 2016 at 8:48
  • show what you tried so far ? Commented Jun 14, 2016 at 8:53
  • Yes, a loop will do just fine. There are hundreds of tutorials out there, even php manual lists some under the various MySQL APIs. So, pls do some independent digging yourself. Commented Jun 14, 2016 at 8:53
  • Yes, use a loop. While- or Foreach- loop are perfectly suited for this task as there are no further calculations of row count or incrementation of a counter variable necessary Commented Jun 14, 2016 at 8:53
  • select sum(total) as Total,AffID,user_id from Affiliates group by AffID order by user_id ASC foreach($result_array as $row) { echo $row['total']."<br/>"; echo $row['AffID']."<br/>"; echo $row['user_id ']."<br/>"; } Commented Jun 14, 2016 at 8:56

1 Answer 1

0

try something like this

    //db connection

     global $conn;

        $servername = "localhost";  //host name

        $username = "username"; //username

        $password = "password"; //password

        $mysql_database = "dbname"; //database name

    //mysqli prepared statement 

        $conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());

        mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");

        $stmt = $conn->prepare("select sum(total) as Total,AffID,user_id  from Affiliates  group by AffID order by user_id ASC");

                    $stmt->execute();
                    $get_result= $stmt->get_result();
                    $row_count= $get_result->num_rows;

                    if($row_count>0)
                    {
                       while($row=$get_result->fetch_assoc())
                       {

                          echo $row['user_id ']."     ".$row['AffID']."   ".$row['Total']."<br/>";


                       }


                    }
                    $stmt->close();
                     $conn->close();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.