How Generate data to alphabet on MySQL table using PHP? [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 7 years ago.
Improve this question
Hello i have tables like this :
Employee
EmployeeID EmployeeName
1234 Sooyoung
1235 Yoona
1236 Tiffany
1237 Hyoyeon
1238 Taeyeon
1239 Seohyun
1240 Sunny
1241 Yuri
i want to generate the employee id sequentially like this :
1234 as A
1235 as B
1236 as C
1237 as D
1238 as C
1239 as E
1240 as D
1241 as F
may you know what mysql code and php what i have to use? thank you

This is how you would do it in PHP, I am assuming this is the method you want seeing as you tagged PHP.
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("yourdatabase") or die(mysql_error());
$employees = mysql_query("SELECT * FROM Employee ORDER BY EmployeeID")
or die(mysql_error());
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$position = 0;
$position2 = 0;
$toomany = '';
while($row = mysql_fetch_array( $employees )) {
echo "<DIV>" . $toomany.substr($letters, $position, 1) . " = " . $row['EmployeeName'] . " </div>";
$position ++;
if($position > 25) {
$position = 0;
$position2 ++;
if($position2 > 25) { echo "We need to rethink this idea."; break; }
$toomany = substr($letters, $position2, 1);
}
}
?>
EDITED: What if you run out of alphabets ? what did you want to happen? I took a guess, but you will have to explain more if I am not right.
EDITED: TurdPile has a simplified version of what I had. Accept his answer.

As stated in the comments, I don't think you can work with data directly and evaluate it to the alphabet as you are wishing (of course you can still assign results to variables).
If you must have A-Z for whatever reason, I think the best solution would be to use a PHP script to parse the results of the query. If this is the case, you won't really need the Employee ID number, as you can work with the occurrence of the results, but I left the ID number in there anyways.
<?php
// DB connection info
include("mysql_connection.php");
$query = mysql_query("SELECT EmployeeID, EmployeeName FROM Employees WHERE EmployeeID >= 1234");
$results = mysql_fetch_array($query);
$iteration = "A";
echo "ID - Name";
while(list($empNumber,$empName) = $results)){
echo $iteration." - ".$empName;
++$iteration;
}
?>
Note: mysql_query was used for the example, generally you should use MySQLi or PDO.
Note 2: The use of $iteration in this will automatically increment the letter output, when it reaches Z, it should go onto AA.

Related

Read the last data and generate alphanumeric employee number

I have a column in my database employee number and data which is ACZ2020000. When I add an employee, the generated employee number is ACZ2020002. How can I generate the ACZ2020001? Help me, please. Thanks so much, guys.
This what I did:
public function generate_employee_id() {
$query = $this->db->query("SELECT COUNT(*) AS num_rows FROM tbl_accounts");
$row = $query->fetch_object();
$number = $row->num_rows;
++$number;
$len = strlen($number);
for($i = $len; $i < 3; ++$i) {
$number = '0' . $number;
}
$employee_number = 'ACZ'. date("Y") . $number;
echo json_encode(['employee_number' => $employee_number]);
}
You want the highest number, something that doesn't have anything to do with the number of rows. If I understood the rules, you can do this to get the numbers already used:
SELECT employee_id, CAST(SUBSTR(employee_id, 5) AS signed) -- MySQL flavour
FROM tbl_accounts
WHERE employee_id LIKE 'ACZ20%'
... and good old MAX() to grab the latest:
SELECT MAX(CAST(SUBSTR(employee_id, 5) AS SIGNED)) + 1 AS next
FROM tbl_accounts
WHERE employee_id LIKE 'ACZ20%'
Said that, beware of potential issues caused by concurrent accesses!
Perhaps the best answer here would be to recommend not trying to manage your employee number sequence from your PHP application. Instead, let your database do that heavy lifting by way of an auto increment column or something similar. Note that if all employee numbers be prefaced by ACZ, then you really only need a way to generate a sequence, and this is something that databases are reasonably good at doing.

Want to count entire table having value of "1" in MySql [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 3 years ago.
Improve this question
I have a table feedback having columns ans1,ans2,ans3 ...., each column has values from 0 to 2. I want to count all columns where value is 1
In frontend I have displayed 0 as Poor, 1 as Good and 2 as Excellent. I want to count total number of Poor Feedback, Good Feedback and Excellent Feedback. for example if the table have two 0s in ans1, three 0s in ans2 so total should be five 0s.
One option, using summation of boolean expressions:
SELECT
SUM((ans1 = 0) + (ans2 = 0) + ... + (ansN = 0)) AS poor_count,
SUM((ans1 = 1) + (ans2 = 1) + ... + (ansN = 1)) AS good_count,
SUM((ans1 = 2) + (ans2 = 2) + ... + (ansN = 2)) AS excellent_count
FROM feedback;
maybe double looping can solve it,
$feedback = $this->db->get('feedback'); //get your data table
$poor = 0; $good = 0; excel=0;
foreach($feedback as $data1){
foreach($data1 as $data2){
if($data2 == 0){
$poor++;
}
if($data2 == 1){
$good++;
}
if($data2 ==2){
$excel++;
}
}

PHP retrieve MySQL result based on nth letter(s) [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 am trying to retrieve some results from the database based on some letters known.
Here is my PHP script:
$sql = "SELECT * FROM lexem WHERE charLength = '$total'";
$doh = "__a_d";
$sql .= " AND formUtf8General LIKE '$doh'";
The length, letters, letters number and letters position are variable.
My script only returns results that contain some or one of my letters. How can I improve it?
I hope that i have understood correctly. I really want you to try the code below. It uses an array containing both letters and their position in the SQL parameter.
$filter_string = '';
$offset = 0;
$total = 10; //or any other number
$escaped_characters = array('_', '%'); //Any other MySQL special characters
$filters = array(
3=>'a', 5=>'d', 9=>'%', 11=>'r', 0=>'n', 2=>'k', 1=>'w', -1=>'t',
);
ksort($filters);
foreach($filters as $key => $value) {
if ($key < 1 || strlen($value) != 1) {
continue;
}
for($i = 1; $i < ($key - $offset); $i++) {
$filter_string .= '_';
}
if (in_array($value, $escaped_characters)) {
$filter_string .= '\\'.$value;
} else {
$filter_string .= $value;
}
$offset = $key;
}
//Code that escapes parameters in query
$sql = "SELECT * FROM lexem WHERE charLength = '$total' AND formUtf8General LIKE '$filter_string%';";
The $filters array is the one that has the positions of the characters in a key => value format (one character each time). We need to sort it by key so the offset variable will take the correct values each time in the foreach loop. In case you need characters that are also MySQL special characters then $escaped_characters array is useful.
Important: You have to escape all query parameters before you run queries like that because of security reasons (SQL Injection).
Additional info: How can I prevent SQL injection in PHP?

edit column in pdo select [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 have a column that is in seconds and I need to format it in Minutes:seconds, I am thinking that I will need to loop through the fetched array but not quite sure on how to go about that.
$query = 'SELECT calldate,recordingfile,uniqueid,clid,did,lastapp,dst,disposition,duration FROM cdr';
$sql = $remotedbh->prepare($query);
$sql->execute();
$dblist = $sql->fetchAll(PDO::FETCH_ASSOC);
while($row -)
if($sql->rowCount() > 0){
header('Content-type: application/json');
echo json_encode($dblist);
}
else {
echo 0;
}
?>
I would suggest changing the value in the query itself. Here is an example
SELECT calldate,
recordingfile,
uniqueid,
clid,
did,
lastapp,
dst,
disposition,
duration,
CONCAT(FLOOR(duration/60),':',LPAD((duration % 60), 2, '0')) AS duration_in_mins_and_secs
FROM cdr
Here I am assuming duration is the field you want to modify. I am simply dividing the duration by 60 to get minute component and then concatenating the remainder to it.
If you need this data regularly (i.e. you are going to perform this query a lot), it would probably be best to actually store this calculated field in the records themselves so you don't need to make a calculation at all when querying the data. Simply make this calculation upon each record insert.
I suggest fetching one row at a time rather than fetching them all into an array:
while ($dbrow = $sql->fetch(PDO::FETCH_ASSOC)) {
// process current row
}
If you'd rather fetch them all into one array, you can use PHP's foreach:
$dblist = $sql->fetchAll(PDO::FETCH_ASSOC);
foreach ($dblist as $dbrow) {
// process current row
}
The following code will format the duration in M:S format:
while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
$dur = $row["duration"];
$min = $sec = 0;
if (floor($dur / 60) > 0) {
$min = floor($dur / 60);
$sec = $dur % 60;
}
else {
$sec = $dur;
}
$str = $min . ":" . $sec;
}
However, I recommend having your database format the output for you, as Mike Brant specifies in his answer.

Calculate DateDiff in PHP in minutes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
<?php
$sql_apr = " SELECT SUM ( meter * minute ) FROM table";
$rs_apr = #mysql_query($sql_apr);
$total_apr = #mysql_fetch_array($rs_apr);
$try4 = $total_apr['SUM(meter * minute'];
while ($rs_t = #mysql_fetch_array($rs_t)) {
$minute = '';
$sql_t = "SELECT DATEDIFF(MINUTE,'e_date e_time','s_date s_time') AS minute";
$rs_t = #mysql_query($sql_t);
$minute = $rs_t['minute'];
}
?>
You are looking at the wrong result:
$rs_t=#mysql_query($sql_t);
$minute = $total_t['minute'];
should be
$rs_t=#mysql_query($sql_t);
$minute = $rs_t['minute'];
// ^ use the rs_t result, not the result from the first query
You are also result the variable that you are looping on. I highly doubt that this while loop will ever end. You are looping on the result from $rs_t and then you reassign $rs_t inside the loop.
why don't you do this in correct order
$query = "SELECT DATEDIFF(MINUTE,'e_date e_time','s_date s_time') AS minute";
$results = #mysql_query($query);
$row = #mysql_fetch_array($results);
$minute = $row['minute'];
print_r($minute);
$sql_apr = " SELECT SUM ( meter * " . $minute . " ) AS my_sum FROM table";
$rs_apr = #mysql_query($sql_apr);
$total_apr = #mysql_fetch_array($rs_apr);
$try4 = $total_apr['my_sum'];
You try to get result (in while) before you run query.
edit:
$query = "SELECT TIMESTAMPDIFF(MINUTE,'s_date s_time','e_date e_time') AS minute";

Categories