In mysql there are 1 to 10 numbers available in table colum, i am fetching number and want to show alert message when number range 1 to 10 or more there are show alert "numbers are enaught". You can alert message show through javascript but embedded with php.
<?php
$sql_query = "SELECT * FROM dndorder ORDER BY display_order";
$resultset = mysqli_query($conn, $sql_query) or die("database error:". mysqli_error($conn));
$data_records = array();
while( $row = mysqli_fetch_assoc($resultset)) {
echo $row['id'];
if($row==10){
foreach (range($row, 10) as $number) {
echo 'exceed num';
}
}
}
#Simone Rossaini
this range define 0 to 9 values
this range define 0 to 9 but disorder way
Actually the numbers keep changing and the numbers keep increasing.
For example if row still showing 10 but now i want to increase some more row with others colums data may be row get reach 2 more so number will show till 0 to 11 total count 12. I want show message, when last value increase with assending order like 1 2 3 4 5 6 7 8 9 10 11 last value increase 12 just show message "Last value has been increased is 12".
Related
i have a MYSQL database where i want to sort the totals of both paind and unpaid amounts. The Query i used was :
SELECT DISTINCT
YEAR( app_payments.created_at ) AS YEARS,
SUM( app_payments.amount ) AS Total,
app_users.serial,
app_payments.`status` AS payment_state
FROM
app_payments
INNER JOIN app_users ON app_payments.created_by = app_users.serial
WHERE
app_payments.created_by = 'd88faa'
GROUP BY
YEAR ( app_payments.created_at ),
app_payments.status
i got the results as:
2017 1995 d88faa 1
2018 1200 d88faa 1
2019 1250 d88faa 0
2019 4990 d88faa 1
Where 1 stands for PAID and 0 stand for UNPAID
in my php code, i tried to group the data into years
$Stats = array ();
while(!$this->EndofSeek()){
$result = $this->Row();
if($result->payment_state == 0 ){
if(in_array($result->YEARS,$Stats)){
array_replace($Stats,['y'=>$result->YEARS , 'b'=>$result->Total ]);
}else{ array_push($Stats,['y'=>$result->YEARS , 'a'=>0 , 'b'=>$result->Total ]);}
}else if($result->payment_state == 1){
array_push($Stats,['y'=>$result->YEARS , 'a'=>$result->Total , 'b'=>0 ]);
}
}
return json_encode($Stats)
This returns the output:
[{"y":"2017","a":"1995","b":0},
{"y":"2018","a":"1200","b":0},
{"y":"2019","a":"4990","b":"1450"},
{"y":"2019","a":"4990","b":0}]
Where y is YEARS , a is PAID and b is UNPAID
What i seek to achieve is to group all the data to a particular year where i would have
[{"y":"2017","a":"1995","b":0},
{"y":"2018","a":"1200","b":0},
{"y":"2019","a":"4990","b":"1450"}]
Without it duplicating the year but rather merging them into a single unit.
What do i need to do, and which code do i need to implement to achieve this.
Do you just want conditional aggregation?
SELECT YEAR(p.created_at) AS YEAR,
SUM( CASE WHEN p.status = 0 THEN p.amount END) AS Total_0,
SUM( CASE WHEN p.status = 1 THEN p.amount END) AS Total_1
FROM app_payments p INNER JOIN
app_users u
ON p.created_by = u.serial
WHERE p.created_by = 'd88faa'
GROUP BY YEAR(p.created_at);
I need to generate a matrix with n columns and m rows where each cell can be either 0 or 1, such that sum of numbers in each column equals c and sum of numbers in each row equals r. If it's not possible (most of the time probably), it should have columns sum to c+/-d and rows to r+/-d so that max(d over all rows and columns) is as small as possible. In other words, every sum of a row should be close to r and every sum of a column should be close to c.
To illustrate what I'm looking for when no perfect solution exists, this solution:
1.1.1.......
1.1.1.......
...1..1...1.
...1.1..1...
....1...1..1
is better than this solution:
1.1.1.......
1.1.1.......
...1..1...1.
...1.1..1...
....11..1...
Because the last row has a sum 0 (compared to 1) with the previous solution which is further away from the desired row sum of 2.
Creating a matrix where it holds just for rows is easy - take permutations of a vector with exactly r 1s.
How to satisfy the second condition though? Find a column with sum that's too high and another that's too low and swap some numbers? Is that going to help, how long will it take, is it even going to terminate, if it's impossible to get a perfect result, when do I stop? Is there a better way?
You can use pseudocode or your language of choice or just random blurbs.
I've found Finding if binary matrix exists given the row and column sums which talks about answering whether it's possible or not and even constructing a solution if it is possible. If it isn't possible such solution will have really large ds though.
There is also this paper https://www.sciencedirect.com/science/article/pii/S0012365X06003980 which I don't really understand but it seems to also be concerned with constructing the perfect solutions
This can be done via integer linear programming.
library(lpSolve)
# inputs
nr <- 3 # no of rows
nc <- 3 # no of cols
r <- 2 # sum of each row equals r
c <- 2 # sum of each col equals c
obj <- rep(1, nr * nc)
const.mat <- rbind(
t(rep(1, nc) %x% diag(nr)), # this matrix multiplied by c(m) is row sums
(diag(nc) %x% t(rep(1, nr))) # this matrix multiplied by c(m) is col sums
)
const.rhs <- c(rep(r, nr), rep(c, nc))
out <- lp(obj = obj, const.mat = const.mat, const.dir = "=", const.rhs = const.rhs,
all.bin = TRUE)
out
## Success: the objective function is 6
matrix(out$solution, nr, nc)
## [,1] [,2] [,3]
## [1,] 0 1 1
## [2,] 1 0 1
## [3,] 1 1 0
You could use the sample_degseq function from the igraph package for this, which creates a graph with given vertex degrees. The adjacency matrix of this graph will have your desired property.
library(igraph)
row_col_sum = 3
dim = 8
g = sample_degseq(rep(row_col_sum,dim), method = "simple.no.multiple")
as_adj(g)
Output:
8 x 8 sparse Matrix of class "dgCMatrix"
[1,] . . . . . 1 1 1
[2,] . . 1 1 1 . . .
[3,] . 1 . . 1 . 1 .
[4,] . 1 . . . 1 . 1
[5,] . 1 1 . . . 1 .
[6,] 1 . . 1 . . . 1
[7,] 1 . 1 . 1 . . .
[8,] 1 . . 1 . 1 . .
Hope this helps!
I am currently working on creating a Flot JavaScript Time Series Line Graph. The graph analyses the number of users who create an account with against the time that the account was created.
Here is a screenshot of the users table
I have so far been able to retrieve the created_date column in the users table x asis used for the graph. The y axis is then calculated using the COUNT() function in a separate query. My codes are found below.
PHP:
<?php
include "connection/connect2.php";
$datecreated = "SELECT created_date FROM users";
$rundatecreated = mysql_query($datecreated);
$foundnum = mysql_num_rows($rundatecreated);
while ($runrows = mysql_fetch_assoc($rundatecreated)) {
$dataset1[] = $runrows['created_date'];
}
if (is_array($dataset1)){
foreach($dataset1 as $x) {
$query = mysql_query("SELECT created_date, count(created_date) as date FROM users WHERE created_date='$x' ");
while ($runquery = mysql_fetch_assoc($query)){
$y[]= $runquery['created_date'] .",".$runquery['date']."<br>" ;
}
}
$graph=implode(" ", $y);
echo $graph;
} else {
echo "error";
}
?>
I then store the data retrieved using these two queries into an array, as shown below.
I am unsure of how to convert the date data currently retrieved using my PHP codes into JavaScript timestamp format. Is there any way I can do so using PHP codes?
1) You need only one SQL query to achieve this instead of one plus 1 per date. for this use grouping in the query.
2) To get timestamps from your dat values use the getTimestamp() method and multiply by 100 to convert from UNIX timestamps to JavaScript timestamps.
3) Flot need the data for a data series as array of array (data points). This can then be encoded as JSON and printed inside the JavaScript or fetched by AJAX.
Updated code (not tested but should be a good starting point):
<?php
include "connection/connect2.php";
$query = mysql_query("SELECT created_date, count(*) as date FROM users GROUP BY created_date ");
$foundnum = mysql_num_rows(mysql_query($query));
while ($runquery = mysql_fetch_assoc($query)){
$graph[] = array((new DateTime($runquery['created_date']))->getTimestamp() * 1000 , $runquery['date']);
}
if ($foundnum > 0) {
echo json_encode($graph);
} else {
echo "error";
}
?>
I written previously to calculate for client using javascript
This is my function
var check = {}, string = inputString, count = 0;
var occurrence;
$.each(string.split(''), function(){
if(!check[this]){
count++;
check[this]=true;
})
The function above I use it to calculate number occurrence in a string
For example
number 1234 will return 4, as there are number 1,2,3 and 4
number 1233 will return 3, as the unique number are 1,2 and 3
number 1212 will return 2, as the unique number are 1 and 2
I wonder how do I convert my function above into a working php function that return the same count.
The next thing is I written another check which is getMostOccurrence
The purpose is for example
number 1212, will return 2, as 1 appear 2 times, and 2 appear 2 times
However if number
1112, the result will be 3, as 1 appear 3 times
How do I do this count function in php
Thanks for helping!!
Use count_chars
Counts the number of occurrences of every byte-value (0..255) in string and returns it in various ways.
Example taken straight from PHP.net
<?php
$data = "Two Ts and one F.";
foreach (count_chars($data, 1) as $i => $val) {
echo "There were $val instance(s) of \"" , chr($i) , "\" in the string.\n";
}
?>
You can use it to fulfil your second purpose very easily as well.
I would like to seek some help with my query problem in php...I want to do is count all (atic) with the same number and if the specific (atic) is equal to 7 my Insert query will execute to that (atic).
The problem is my count query wont work as i wanted....and execute my insert query to all aic even the count is not = 7.
current code:
<?php
mysql_connect("localhost","root","") or die("cant connect!");
mysql_select_db("klayton") or die("cant find database!");
$total = NULL;
$count = "SELECT count(t.atic) as '$total', t2.name FROM app_interview as t, tb_applicants as t2 WHERE t.atic = t2.aic GROUP BY t.atic";
$query = mysql_query($count) or die (mysql_error());
while($rows =mysql_fetch_array($query)){
if($query = 7){
mysql_query("INSERT INTO afnup_worksheet (faic,fnl_name,interview,fregion,ftown,funiq_id,fposition,fsalary_grade,fsalary,dateinputed) SELECT DISTINCT atic, atname,(SELECT sum(inttotal) FROM app_interview t2 WHERE t2.atic = t.atic)/7, region, town, uniq_id, position, salary_grade, salary, CURRENT_TIMESTAMP FROM app_interview t GROUP BY atname HAVING COUNT(DISTINCT atic)");
}
}
?>
$query = 7 means you are assigning value 7 to variable $query
In order to compare you have to use double equal sign ==
or triple equal signs === for same data type.
if($query = 7)
means you assign $query is 7 and it will always true and always executed, you can try this
if( $query == 7 )
That means if $query value is equal to 7 or not