I'm trying to display first and second result from table, so that user can see both results on profil.php and the date from last result. I created a view vwresult that has these fields
id | ide | name | mark | date
---+-----+---------+------+-----------
1 | 1 | trickpd | 3 | 06.01.2018
1 | 2 | trickpd | 2 | 03.01.2018
5 | 3 | trickpd | 4 | 08.01.2018
5 | 4 | trickpd | 6 | 02.01.2018
That is my table with current result, insert new data in table will show more results in view table.
This is my code
$tst = mysqli_query($mysqli, "SELECT * FROM vwresult WHERE id=$ig AND name='trickpd' ORDER BY id, date desc");
$marks = [];
while($tstx = mysqli_fetch_assoc($tst)) {
$marks[] = $tstx['mark'];
}
After that I get stuck, and can't show the results.
The goal is:
to show mark 3 and 2 and date 06.01.2018 for id 1,
and when user opens details for id 5 to see results the will see 4, and 6 and also a 08.01.2018
Please help. Thank you all for your time.
Problem is with displaying the data.. I even create a two query one to show all the data and second with limit 1,1 but after that on script it's showing random data, not first and second result like I wont.. I'm trying to display last two results and the last date of inserting the data..
Related
i stuck at the problem that my code wont count total clicks in multiple rows.
This is how the Database looks
id | jb_clicks | created_time
--------------------------------------------------
1 | 14 | 1475420816
2 | 7 | 1475422200
3 | 9 | 1475422217
4 | 3 | 1475422239
I want the result to be 33 (14+7+9+3)
How is this possible?
The current code looks like this:
SELECT COUNT(*) AS jb_clicks FROM jb_urls
my function:
function sumdatabase($select, $statement){
$config = new mysql_config;
$link = mysqli_connect($config::MYSQL_HOST,$config::MYSQL_USER,$config::MYSQL_PASS);
mysqli_select_db($link, $config::MYSQL_DATABASE);
$result = mysqli_query($link,"SELECT SUM(".$config::MYSQL_PREFIX."$select) AS jb_clicks FROM $statement");
$row = mysqli_fetch_assoc($link,$result);
return $row[$config::MYSQL_PREFIX.$select];
}
Use the following:
SELECT SUM(jb_clicks) AS jb_clicks FROM jb_urls
COUNT() is used to count the row numbers and SUM() is to calculate the total sum of a column in a table. In your case, it would be SUM(jb_clicks).
Me again, guys. The titles is pretty self-explanatory, but for the details:
I want to display the timediff(in hours) results on a webpage but I can't wrap my head around how to do it.
I'm using PHP and Codeigniter for this website.
So far, I have a mySQL database with a table named 'server_log' which looks like:
+--------+-----------+--------+---------+------------+-------------+
| log_id | host_name | status | host_id | profile_id | event_date |
+--------+-----------+-+----------------+------------+-------------+
| 1 | http://...| Online | 1 | 1 | *timestamp* |
+--------+-----------+--------+---------+--------------------------+
*It shows also other entries with status "Offline" and "Maintenance".
I have this query to get the entries per user (based on profile_id) w/c are also sorted by status ('Online','Offline', or 'Maintenance'):
SELECT * FROM server_log
WHERE (profile_id=$id AND status='Online')
AND event_date BETWEEN $from_date AND $to_date
ORDER BY event_date,host_id;
*the from_date and to_date are results from the webpage which lets the user choose a 'From' and 'To' date to filter the results like a report page.
The problem I have is that after the above query I do not know what I else I need in order to calculate the TIMEDIFF results for each server(host_id) then display them on the webpage. (I'd like to get the TIMEDIFF between the results after getting the query results for each status)
How do I get the TIMEDIFF result (integer)?
Simply put, I want the flow to be like this:
[1] After 1st query: (SELECT ALL 'Online' for $id)
log_id | host_name | status | host_id | profile_id | event_date
1 ping.test1 Online 1 2 **
2 ping.test1 Online 1 2 **
3 ping.test2 Offline 2 2 **
4 ping.test2 Offline 2 2 **
[2] After 2nd query: (loop for each host_id)
log_id | host_name | status | host_id | profile_id | event_date
1 ping.test1 Online 1 2 **
2 ping.test1 Online 1 2 **
-> then calculate diff_date of event_dates
---> then return value to be assigned to $data['reports_ping']
-----> back to [1] but selecting all 'Offline', then 'Maintenance'
-------> if all values are assigned, display $data['reports_ping'] as row (foreach)
I would like the results to show something like:
+------------+------------+------------+-------------+-------------+
| profile_id | host_name | online_hrs | offline_hrs | maintenance |
+------------+------------+------------+-------------+-------------+
| 1 | http://... | 2 | 1 | 0 |
+------------+------------+------------+-------------+-------------+
I'm relatively a beginner and not really that good with CodeIgniter, so any help with this would be REALLY appreciated. Thanks in advance!
TIMEDIFF meaning different time between event_date and now? Try this :
SELECT *, (event_date - NOW()) as TIMEDIFF FROM server_log
WHERE (profile_id=$id AND status='Online')
AND event_date BETWEEN $from_date AND $to_date
ORDER BY 'event_date','host_id';
-- EDIT --
Is this the logic you want?
$temp = null;
foreach($rows){
$event_date = "select event_date from server_log order by log_id LIMIT 1"; // specify this query using your CI format
$timediff = $event_date - $temp;
$temp = $event_date;
}
I have 3 tables in my DB: ‘workouts’, ‘exercises’ and ‘exercise_list’.
workouts: | id | datetime | misc1 | misc2 |
exercises: | id | ex_id | wo_id | weight | reps | wo_order |
exercise_list: | id | title |
So far I have generated a view which grabs details of a specific workout (myurl.com/workouts/view/<datetime>)
I have built a query that grabs the fields from ‘workouts’ and also it grabs any ‘exercises’ entries that correspond to that workout (by get_where using wo_id).
I build a view which lists the exercises for that workout, but I can only get as far as foreach’ing out the ‘id’ of the exercise. I need to somehow have a further query that grabs the ‘title’ of each exercise that is associated with that workout ‘id’.
So I currently have a table (html):
| Exercise | Weight | Reps |
| 1 | 50 | 8 | ...
I need ‘1’ to become the title of the exercise in ‘exercise_list’ with an ‘id’ of ‘1’.
My solution
May not be perfect but it works:
public function get_exercises($wo_id)
{
$this->db->select('exercises.wo_id,
exercises.weight,
exercises.reps,
exercise_list.title');
$this->db->from('exercises');
$this->db->join('exercise_list','exercises.ex_id= exercise_list.id');
$this->db->where('exercises.wo_id',$wo_id);
$q = $this->db->get();
$query = $q->result_array();
return $query;
}
Not sure about the bestway to do the last few lines. This is in my model, so I needed to return the array. I am going tobet there is a way to do it better than the last 3 lines.
You can use joins and select title from your exercise_list table
$this->db->select('w.*,el.title')
->from('workouts w')
->join('exercises e','w.id = e.wo_id')
->join('exercise_list el','el.id = e.ex_id')
->where('e.wo_id',$yourid)
->get()
->result();
I have the following table and want to know how to set up a column total to calculate the sum of the row. I want the total column to calculate first_bid - second_bid.
id | First Bid | Second Bid | Total
0 | 7 | 1 | (first_bid)-(second_bid)
1 | 8 | 2 |
2 | 5 | 3 |
3 | 4 | 4 |
4 | 5 | 5 |
5 | 5 | 6 |
I need to display to the user all previous bids and total on a page. The total should be in descending order also.
SELECT id, First_Bid, Second_Bid, First_Bid - Second_Bid AS Total
For the grand total, you'd have to run a second query with SUM() aggregate functions. Which is somewhat redundant, since you can just do the summation in your client as you retrieve the data. e.g.
$total = 0;
while($row = fetch_from_db($result)) {
$total += $result['Total'];
... display row data ...
}
... display total ...
You can just calculate the total when you are querying the database.
SELECT first_bid, second_bid, (first_bid - second_bid) as total FROM table ORDER BY 3 DESC
Something along these lines
Try this query:
SELECT *, (First Bid-Second Bid) as total from TABLE
I have a log` that saves log records (amount earned, etc) of employees and a code that separates the data into tables grouped under each employee id:
Empid: 0001
---------------------------
| Logid | Hours | Pay |
---------------------------
| 1001 | 10 | 50 |
---------------------------
| 1002 | 2 | 10 |
---------------------------
Empid: 0003
---------------------------
| Logid | Hours | Pay |
---------------------------
| 1003 | 3 | 9 |
---------------------------
| 1004 | 6 | 18 |
---------------------------
I managed this with the following semi-pseudocode:
$query = mysql_query("SELECT * FROM `log` ORDER BY empid");
$id = 0;
while ($list = mysql_fetch_assoc($query)) {
if ($id != $list['logid']) {
create header (Logid, Hours, Pay)
$id = $list['logid'];
}
add each data row for the empid
}
But now I would like to add the total of the Pay column and put it at the bottom of each table for each empid.
By putting the code $total_pay = $total_pay + $list['pay'] in the while loop I can get the total pay but I can't figure out how I might be able to show the total at the bottom.
Would really appreciate any advice on this!
This should do it. You basically sum up until the id is changing.
$sum = 0;
while ($list = mysql_fetch_assoc($query)) {
if ($id != $list['logid']) {
//create the totals using $sum !!!
// after that re-set sum to 0
$sum = 0;
//create header (Logid, Hours, Pay)
$id = $list['logid'];
}
$sum += $list['Pay'];
//add each data row for the empid
}
Also...
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
There are two ways that you can do this.
PHP
Keep a running total of all of the "pay" values, and add it into your table at the bottom. For example:
$i=0;
while ($list = mysql_fetch_assoc($query)) { // for each row in your results
if ($id != $list['EmployeeId']) { // We only enter this loop if the EmployeeId doesn't equal $id. This can happen because either $id doesn't exist yet, or it doesn't match the previous EmployeeId
$i++; // increase $i by 1
if($i>1) { // Enter this loop only if $i is greater than or equal to 2 (if it is less than two, then this is our first time running this script, and adding a footer row wouldn't make any sense).
create footer (EmployeeId, Hours, Pay); // Log Id is irrelevant here
}
// reset your variables here
$id = $list['EmployeeId']; // set $id = the first or the new Employee ID
$total_pay = $list['pay']; // This is our first time for this Employee, so don't just add it to the running total
create header (EmployeeId, Hours, Pay) // Create the top half of your table
} else { // The EmployeeId has been established: we only need to change the running total
$total_pay = $total_pay + $list['pay'];
}
// add a data row for each LogId. This executes every time we go through the loop
create_normal_row(LogId, EmployeeId, Hours, Pay)
}
// At this point, both Employees have a header, and all data rows. However, we left the loop before we could add the last Employee's footer row
// Let's add one more footer row for the last user
create_footer (Logid, Hours, Pay);
SQL
MySQL has a function that does something very similar to what you are trying to do called ROLLUP. You can read more about it here:
http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html
Basically, you would change your query to work like this:
SELECT LogId, EmployeeId, SUM(Hours), SUM(Pay) FROM `log`
GROUP BY empid, logid WITH ROLLUP
This query will return a dataset that looks like this:
---------------------------------------
| Logid | EmployeeId| Hours | Pay |
---------------------------------------
| 1001 | 1 | 10 | 50 |
---------------------------------------
| 1002 | 1 | 2 | 10 |
---------------------------------------
| NULL | 1 | 12 | 60 |
---------------------------------------
| 1003 | 2 | 3 | 9 |
---------------------------------------
| 1004 | 2 | 6 | 18 |
---------------------------------------
| NULL | 2 | 9 | 27 |
---------------------------------------
| NULL | NULL | 21 | 87 |
---------------------------------------
Whenever $list['Logid'] is null, you know that you have a "total" row. Be careful though, this will add a "sum of all employees" row at the bottom of your dataset. If $list['EmployeeId'] is null, then you know you're in this "total" row.
On a related note (I'm not sure if this is what you're asking for), you can show this stuff in a table by using HTML <table> elements.
Each row would look like this:
<table> <!-- shown at the beginning of each table -->
<tr> <!-- shown at the beginning of each row -->
<td> <!-- shown at the beginning of each table cell -->
Your text goes here
</td> <!-- shown at the end of each table cell -->
<td>
More text can go here
</td>
</tr> <!-- shown at the end of each row -->
</table> <!-- shown at the end of each table -->
<tr>s can be repeated indefinitely within each <table>, and <td>s can be repeated within <tr>s.