Row value count [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
id | type
---|---------------
1 | online
2 | online
3 | online
4 | offline
5 | offline
I want count total type with "Online" value.
and echo the result in page.
Please, Can any one help me to echo the "online" Percentage ?
like:
Online is (3) = 75% of type

Try below code with your database credentials and table name.
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table_name WHERE type='online'", $link);
$num_rows = mysql_num_rows($result);
echo "Online count: $num_rows\n";
Ref: http://www.php.net/mysql_num_rows

$sql=mysql_query("SELECT COUNT(id) FROM table WHERE type='online'");
$total=mysql_fetch_assoc($sql);
echo "Total Online - ".$total["id"];
This will print total number of ids which are ONLINE.

$data = mysql_query("SELECT * FROM t1 JOIN t2 ON t1.type=t2.type WHERE t1.type=t2.type AND t2.type='online' ORDER BY t1.type DESC,t2.type ASC UNION SELECT * FROM t1 JOIN t2 ON t1.type=t2.type WHERE t1.type!=t2.type ORDER BY t1.type ASC,t2.type DESC");
$dataa = mysql_numrows($data);

Related

how to sort mysql data and find position? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have mysql data of 3 rows (user_id, exam_id, marks).
I want to find each user ranking position based on marks and exam_id .
How will be the SQL query with php code for this. Please help me. Thanks in advance.
Here is partial code
$p = "SELECT * FROM IDTABLE WHERE EID = '$info[EID]'";
$rowp = mysqli_query($conn, $p);
while (($ret = mysqli_fetch_assoc($rowp)) > 0) {
$q = "SELECT * FROM INFOTABLE WHERE EID = '$ret[EID]'";
$rowq = mysqli_query($conn, $q);
while (($retq = mysqli_fetch_assoc($rowq)) > 0) {
$user[$ret['EID'] . $retq['UID']] = $retq['Marks'];
}
}
arsort($user);
var_dump($user);
First of all you should consider a few things:
You should always better search for an already answered question before posting a new one (it's part of stack overflow's code)
Bring more information of what you want, being as clear as possible
Giving more details (for example: you sql structure)
However, I will try to give you a hint - Hope it helps!
For example you want to order your user_id based on their marks:
select user_id from my_table order by "marks" asc;
For example you want to order your user_id based on their marks and exam_id :
select user_id from my_table order by "marks", "exam_id" asc;
For example you want to create a kind of ranking based on users's marks:
SET #rank := 0;
SELECT
*,
#rank := #rank + 1 AS rank
FROM my_table
order by marks ASC;
For example you want to get the ranking number from a specific
SET #rank := 0;
SELECT * from ( select
*,
#rank := #rank + 1
FROM my_table
order by marks ASC ) AS rank where user_id = 2;
Hope this helps! You could get more information about mysql's ranking here
Regards,

how to display the content from MySQL table as the row count equals to multiple of 3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a MySQL table having many rows and my question is,
How to
i) Display rows from MySQL table as the row length is equal to multiples of 3
ii) In other case if row count not equal to multiples of 3 then display the row equal to nearest multiples of 3
for example, if the row length is 5 then display only first 3 rows.
for more clarification,
If mysql_num_rows = 3, display all three rows.
If mysql_num_rows = 5, display any three rows.
If mysql_num_rows = 11, display any nine rows.
Use
$res=mysqli_query(
$con,"SELECT * FROM tbl_name");
$row=mysqli_num_rows($res);//calculating total row count
$lim=$row-$row%3;//deciding limit
$res1=mysqli_query(
$con,"SELECT * FROM tbl_name LIMIT $lim");
//echo '<br>'.$row1=mysqli_num_rows($res1);
while($show=mysqli_fetch_array($res1))
{
echo '<br>'.$show['id'];
}
Execute the query & show your data
MySQL Only
SELECT (COUNT(*)-(COUNT(*)%3)) FROM tbl_name INTO #lim;
SELECT #rownum := 0;
SELECT t.*, #rownum := #rownum + 1 AS rowno FROM tbl_name t WHERE rowno <= #lim;
PHP & MySQL
$res=mysqli_query( $conn, "SELECT COUNT(*) AS cnt FROM tbl_name");
$rec=mysqli_fetch_array($res);
$lim=$rec["cnt"]-($rec["cnt"]%3);
$res=mysqli_query( $conn, "SELECT * FROM tbl_name LIMIT $lim");
You'll need to validate cnt and lim

How to echo latest mysql row and some previous column? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new in php. I have a problem. I want to fetch data from Mysql db and print it on my PHP page. I want to print latest record and some previous data of selected column. For example i have a two tables
table1( id, student_name )
table2: ( id, student_id, English, Math, Physics......., Total Marks, Obtain Marks and grade)
PROBLEM: I want that i can fetch whole new record but i want that i can also fetch previous grade of the student.
Can it is possible? How i Can do it?
With
SELECT * FROM (
SELECT * FROM table2 WHERE (student_id=2) ORDER BY id DESC LIMIT 2
) sub
ORDER BY id ASC
You should be able to select the two last rows.
Edit syntax formatting:
$result = mysql_query($sql);
$rows = mysql_fetch_assoc($result);
echo $rows[0]["grade"]; // new grade
echo $rows[1]["grade"]; // previous grade
You can try with the following:
$query = SELECT t1.*,t2.* FROM table1 as t1
LEFT JOIN table2 as t2 ON t1.id = t2.student_id
where t1.id = '1' ORDER BY t2.student_id desc LIMIT 2
$result = mysql_query($query);
$fetch_record = mysql_fetch_assoc($result);
$grade = $fetch_record[0]['pass-your-column-name'];

MySQL Updating column based on other values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am building a simple gaming system and I was wondering how to get the users rank without calculating everytime the result in a query(maybe I should run a cron job?).
This is my points table structure at the moment:
+--username--+--rank--+--total_points--+
|######################################|
| Mark | 0 | 700 |
|------------+--------+----------------|
| Luke | 0 | 400 |
+------------+--------+----------------+
How do I update the rank column based on total_points?
Is there a better way to do this? Thank you for your time
You need only one query:
UPDATE
users,
(SELECT
#row:=#row+1 rownum,
username
FROM users,
(SELECT #row := 0) r
ORDER BY total_points DESC) as rank_ord
SET users.rank = rank_ord.row
WHERE users.username = rank.username;
If your table contains an index column (other then username), then change the condition
WHERE users.username = rank.username
to
WHERE users.id = rank.id
to make it faster.
Assuming that the rank depends on total_points you just need to sort query results by total_points DESC and row number is your position in rank. No need to calculate anything. Of course you may have two users with same total_points so if you want to have same rank this will not work, but you can do pretty simple the same approach (pseudo code):
$rows = query with order total_points DESC
$rank = 0;
$last_total_points = -1;
foreach( $rows as $row ) {
if( $row['total_points'] != $last_total_points ) {
$rank++;
$last_total_points = $row['total_points'];
}
update that row with $rank;
}
so at the end, you will get all users ranked with the same rank for equal total_points values too

Using UNIX_TIMESTAMP for a week [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I got the code to display a post by date from monday to friday but I thing the code display tha last post I publish
<?php
$query = mysqli_query($c, 'SELECT c.chapter, n.name, n.img FROM chapters AS c LEFT JOIN novels AS n ON c.novel = n.id WHERE c.time <= unix_timestamp()');
while($row = mysqli_fetch_row($query)):
?>
Can somebody tell me what code I have to use to display the publish post from monday to friday please?
I try with this, the server time is day 25 by I don't get a result and I least have display 1 post
$query = mysqli_query($c, 'SELECT c.chapter, n.name, n.img FROM chapters AS c LEFT JOIN novels AS n ON c.novel = n.id WHERE FROM_UNIXTIME(c.time,\'%N\')>=1 AND FROM_UNIXTIME(c.time,\'%N\')<=5');
Is fix now with this
$query = mysqli_query($c, 'SELECT c.chapter, n.name, n.img FROM chapters AS c LEFT JOIN novels AS n ON c.novel = n.id WHERE DAYOFWEEK(FROM_UNIXTIME(c.time))>=1 AND DAYOFWEEK(FROM_UNIXTIME(c.time))<=5');
while($row = mysqli_fetch_row($query)):
?>
You can get day name by this
SELECT DAYNAME(CURDATE()), DAYNAME(DATE_ADD(CURDATE(), INTERVAL 4 DAY));
OR
SELECT DAYNAME('2013-11-25'), DAYNAME(DATE_ADD('2013-11-25', INTERVAL 4 DAY));
Hope it should help for you.

Categories