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
Related
I am creating order numbers like this:
ACC4_11082018_1
ACC4_11082018_2
ACC4_11082018_3
ACC4_11082018_4
ACC4_11082018_5
.
.
.
.
etc.
I am getting an issue with incrementing the last digit.
I tried the code
date_default_timezone_set('Asia/Kolkata');
$myDate = date('dmY');
$get_id=4;
$i=1;
echo $order_no='ACC'.$get_id.'_'.$myDate.'_'.$i;
$i++;
Would you help me out with this?
I have no clue where you will be using this code/logic but this will give you infinity:
date_default_timezone_set('Asia/Kolkata');
$myDate = date('dmY');
$get_id=4;
$i=0;
while( ++$i )
{
echo $order_no='ACC'.$get_id.'_'.$myDate.'_'.$i;
// echo '<br>'; // uncomment this if you want it to look normal in your browser
}
Or if infinity is not desired then:
date_default_timezone_set('Asia/Kolkata');
$myDate = date('dmY');
$get_id=4;
$i=0;
// stop after 1,000
while( ++$i <= 1000 )
{
echo $order_no='ACC'.$get_id.'_'.$myDate.'_'.$i;
// echo '<br>'; // uncomment this if you want it to look normal in your browser
}
If you really want to generate a unique order number for anything:
you have to check that order number if already exists in database or note
/* --------------------------- New order number --------------------------- */
$check_for_order = $con->query("SELECT `order_no` FROM `table` ORDER BY `id` DESC LIMIT 1");
if($check_for_order->num_rows==1){
$row=mysqli_fetch_array($check_for_order);
$previous_order = trim($row['order_no']);
$new_order = $previous_order + 1;
$insert_order_number = $con->query("INSERT INTO `table`(`order_no`) VALUES ('{$new_order}')");
}else{
$previous_order = 1;
$on_date = date("Ymdhis"); // or you can use time() instead
$new_order = $on_date.$previous_order;
$insert_order_number = $con->query("INSERT INTO `table`(`order_no`) VALUES ('{$new_order}')");
}
/* ----------------------- New Order Number here -------------------------- */
$order_no = $new_order;
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'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.