I'm working right now to get a unique order number for the item being purchased.
The problem is such that I have payment gateway at quickpay and it comes up and says this: "Error in field: ordernumber"
I have tried to make it like this:
$idag = date("dmY");
$a = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz#!*_-?>/+,ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 4);
$total = $idag . $a;
$sql = "SELECT pakker.pris FROM pakker WHERE pakker.id = 3";
if ($stmt = $this->mysqli->prepare($sql)) {
$stmt->execute();
$stmt->bind_result($pris);
while ($stmt->fetch()) {
$protocol = '7';
$msgtype = 'authorize';
$merchant = '89898978';
$language = 'da';
$ordernumber = $total;// here must order number came up.
I have also tried to do it like this.
$idag = date("dmY");
$rand = strtoupper(substr(uniqid(sha1(time())), 0,8));
$a = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz#!*_-?>/+,ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 4);
$total = $idag . $a . $rand;
From the [API documentation for Quickpay]:
ordernumber
/^[a-zA-Z0-9]{4,20}$/
A value by merchant's own choice. Must be unique for each transaction. Usually an incrementing sequence. The value may be reflected in the your bank account list.
You need to alter your random order ID generator to exclude any non-alphanumeric characters.
i.e.
$idag = date("dmY");
$a = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 4);
$total = $idag . $a;
if I were you, I'd use the unix timestamp and then add a few extra characters to the end.
Related
I have attribute_description like in the image :
on table I have for cap_tabel values: d, lt, lu, amb, coada
I want to get them and make it variable and set the value equal to the attribute_id.
eg:
$d = 1;
$lt = 2;
$coada = 9;
I think i can create a function.php file where this variables are created and use them where is needed.
Is this possible or I have to create a script where I name the variable myself and set it the right value like:
$amb_ext = mysqli_fetch_array(mysqli_query($connect,"SELECT attribute_id FROM attribute_description WHERE cap_tabel='amb'"));
$amb = $amb_ext['attribute_id'];
What do you think is the best solution?
In the end I want to create a product table where in th will be value from cap_tabel and in td to get the right value with the correct th cell
You could use variable variables:
$res = mysqli_query($connect,"SELECT attribute_id, cap_tabel FROM attribute_description");
while ($row = $res->fetch_assoc()) {
$var = $row['cap_tabel'];
$$var = $row['attribute_id'];
}
// you now would have variables $d, $lt, etc
$result = mysqli_query($connect,"SELECT attribute_id, cap_tabel FROM attribute_description");
while ($aValue = mysqli_fetch_array($result)) {
${$aValue['cap_tabel']} = $aValue['attribute_id'];
}
echo ' $d = ' . $d . ', $lt = ' . $lt . ', $coada = ' . $coada;
To get result like this :
$d = 1, $lt = 2, $coada = 9
I have the code from the database, the data should appear automatically in sequence. the code is composed of text and number like this CO-00001 but when I generate the code it just adds one digit in the back but not add the value of example CO-00001 to CO-000001 how to solve it?
ex :
$id = 0902340
$query = "SELECT substr(code_id,3,5) as cd FROM tb_inv WHERE substr(code_id,3,5) = '".$id."' ORDER BY code_id DESC LIMIT 1";
$get_id = $this->db->query($query);
$show_id = $get_id->num_rows();
$new_code = $get_id->row();
if($show_id > 0){
$new = $new_code->cd + 1;
$fix_code = $new;
}else{
$fix_code = 'CO-00001';
}
you can't do algebra on alphabets, therefore you need to split the alphabets and the numerics like so:
$id = 0902340
$query = "SELECT substr(code_id,3,5) as cd FROM tb_inv WHERE substr(code_id,3,5) = '".$id."' ORDER BY code_id DESC LIMIT 1";
$get_id = $this->db->query($query);
$show_id = $get_id->num_rows();
$new_code = $get_id->row();
if($show_id > 0){
$prefix = substr($new_code->cd, 0, 3); // get the prefix 3 chars at front
$number = substr($new_code->cd, 3); // get the remaining chars
$new = $number + 1; // add it with 1, php automatically convert strings that can be converted to numbers
$fix_code = $prefix . substr("00000" . $number, -5); // re-assemble the code
}else{
$fix_code = 'CO-00001';
}
you can split the id into two parts, the string one, and the digit one. then increment the digit part then re concat the two parts again
list($strPrefix, $digitPart) = split('-', $oldId);
$digitPart++;
$newCode = $strPrefix . '-' . $digitPart;
I need to update tags column so each cell has the content like this:
2-5-1-14-5
or
3-9-14-19-23
or simmilar (five integers, in range from 1-25).
id column is not consecutive from 1-117, but anyway min id is 1 and max 117.
$arr = [];
$str = '';
$id = 1;
for ($x = 1; $x <= 25; $x++){
array_push($arr, $x);
}
while ($id < 117) {
shuffle($arr);
array_splice($arr, 5, 25);
foreach ($arr as $el){
$str .= $el . '-';
}
$str = rtrim($str,'-');
$db->query("update posts set tags = '" . $str . "' where id = " . $id);
$id += 1;
}
I'm not sure how to describe the final result, but it seems that the majority of cells are written multiple times.
Any help ?
To combine my comments into one piece of code:
$full = range(1, 25);
$id = 1;
while ($id < 117) {
shuffle($full);
$section = array_slice($full, 0, 5);
$str = implode('-',$section);
$db->query("update posts set tags = '" . $str . "' where id = " . $id);
$id += 1;
}
So the reset of $str is not needed anymore since I have inserted the implode() where it seems functional. The other bits of code could probably be improved.
Two warnings:
Using PHP variables directly in queries is not a good idea. Please use parameter binding. This particular piece of code might not be vulnerable to SQL-injection but if you do the same elsewhere it might be.
Your database doesn't seem to be normalized. This might cause trouble for you in the long run when you expand your application.
Good Day, I am working on some scoring system that would display all the scores by all of the users and would sum it up using PHP from MySQL. The result is working fine and the computation as well (the computation is done with php, not on MySQL). Now my problem is, how can I rank the total scores from highest to lowest.
Here's the code:
$sel_query="Select * from tbl_scores";
$result = mysql_query($sel_query);
while($row = mysql_fetch_array($result)) {
$crit_3 = $row["crit_3"];
$crit_3a = number_format($crit_3);
$crit2_3 = $row["crit2_3"];
$crit2_3a = number_format($crit2_3);
$crit3_3 = $row["crit3_3"];
$crit3_3a = number_format($crit3_3);
$user1 = ($crit_3) ;
$user2 = ($crit2_3);
$user3 = ($crit3_3);
$divide = ($user1 + $user2 + $user3);
$total = number_format($divide / 9 , 2, '.', '');
$average = number_format($total * 0.15 , 2, '.', '');
?>
Thanks in advance.
1.Please stop using (deprecated from php5.5 +removed from php7) mysql_*,use mysqli_* OR PDO.
2.Define an array variable outside of the loop, and inside in the loop assign total scores to that array.
3.Now you will get an array of scores and now you can use rshort() method to get data correctly.
So the code should be like below:-
$sel_query="Select * from tbl_scores";
$result = mysql_query($sel_query);
$scores_array = array();//create an array
while($row = mysql_fetch_array($result)) {
$crit_3 = $row["crit_3"];
$crit_3a = number_format($crit_3);
$crit2_3 = $row["crit2_3"];
$crit2_3a = number_format($crit2_3);
$crit3_3 = $row["crit3_3"];
$crit3_3a = number_format($crit3_3);
$user1 = ($crit_3) ;
$user2 = ($crit2_3);
$user3 = ($crit3_3);
$divide = ($user1 + $user2 + $user3);
$total = number_format($divide / 9 , 2, '.', '');
$average = number_format($total * 0.15 , 2, '.', '');
$scores_array[$row['user_name']] = $total; // assign total to the array and i assume that your table has one column name user_name for each user, change accordingly
}
rsort($scores_array);// sort the array in decending order of total scores
foreach ($scores_array as $key=>$value){ // iterate through array
echo $key.'has scored total score:-'.$value; //print the score along with username
}
?>
Note:- Please take the variable names in such a way that they will not produce any ambiguity.
I have a field (demo_field)(varchar) in mysql table. I want to increment this field value with a pre defined prefix.
For example my field first value is demo001. Now when ever new values inserted I want to increment the numeric numbers like demo002, demo003.
How can I do this with PHP.
try this -
//fetch data from table
$sql = $mysqli->query('select count(demo_field) as total,demo_field from tablename limit 1');
$res = $sql->fetch_assoc();
//generate string from existing data
$str = substr($res['demo_field'], 0, 4);
$dig = str_replace($str, '', $res['demo_field']);
$dig = $dig+$res['total'];
//add padding if needed
$dig = str_pad($dig, 3, '0', STR_PAD_LEFT);
//concatenate string & digits
$newStr = $str.$dig;
var_dump($newStr);
Another way without count
$sql = $mysqli->query('select max(demo_field) as demo_field from demo');
$res = $sql->fetch_assoc();
$str = substr($res['demo_field'], 0, 4);
$dig = str_replace($str, '', $res['demo_field']);
$dig += 1;
$dig = str_pad($dig, 3, '0', STR_PAD_LEFT);
$newStr = $str.$dig;
var_dump($newStr);
hope this might solve the problem with count.
another solution with max count for alphanumeric string and without padding -
$sql = $mysqli->query('select max(cast(substring(demo_field, 5) as unsigned)) as digit, demo_field from demo');
$res = $sql->fetch_assoc();
$str = substr($res['demo_field'], 0, 4);
$dig = $res['digit'] + 1;
$newStr = $str.$dig;
var_dump($newStr);
//use PHP not mysql
$pre = 'demo';
$num = 0;
define('MAX', 100);
for($num = 0; $num < MAX; $num++)
{
$pre_str = $pre . sprintf("%03d", $num);
//insert here
}
you have to use an INT field
and translate it to whatever format you want at "select" time.
In MySQL we cannot use AutoIncrement for Varchar.