I need to generate a dynamic text in accordance with the number of data that comes from the database.
In a html List, I need to display the following text as example:
GROUP A
GROUP B
GROUP C
If the amount of records coming from the DB is only 1000, then only one item list is displayed. 1000 records equals 1 Item list.
The text must be dynamically changed as described above.
Each text in the list is displayed for 1000 records.
The code below statically displays 11 items
I've made a few tries on the code below but still to no avail.
This code is working, but it display only the static text.
$return .= "<div id='test_id'><ul>";
// $qty = 10424 / $limit = 1000
// $tabs = $qty / $limit = 10,424
$tabs = $qty/$limit;
for ($i=0; $i < $tabs ; $i++)
{
$start = $i * $limit + 1;
$end = ($i + 1) * $limit;
$color = 'classpA';
$message = '<small>GROUP A</small>';
if ($i < $tab_selected) {
$color = 'classB';
$message = 'RESERVED';
}
$active = $i == $tab_selected ? "active" : "";
$return .= "<li class='tab {$color} {$active}' tab-id='#tab-".$i."'>
<span class='available {$color}_text'><small>{$message}</small></span>
<a href='#' class='{$color}_text'>".$start." - ".min($end,$qty)."</a></li>";
}
$return .= "</ul></div>";
And this is that logic that i`m trying to apply
$qty = 18424;
$limit = 1000;
$tabs = ($qty / $limit);
$group = $tabs / $limit;
$alphabet = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
for($i = 0; $i < $group; $i++) {
echo "Group {$alphabet[$i]}".'<br>';
}
echo 'Qtd Group= '.$group.' Value Var Tabs= '.$tabs;
https://www.tehplayground.com/zyVzmaeYnTyyA4S9
How can I make the magic happen?
Your logic is fine, just the limit to the for loop seems to be miscalculated.
Right now, for the variable group = (qty/limit)/limit, you have an extra division by limit here.
Whatever your need maybe for that division, you can simply use the value of tabs in your for loop condition, or assign it to another temporary variable its up to you.
for($i = 0; $i < $tabs; $i++) {
echo "Group {$alphabet[$i]}".'<br>';
}
Cheers!
Maybe this could helps you
<?php
//$tabs = ($qty / $limit);
//$group = $tabs / $limit;
$alphabet = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$return = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$a = array_chunk($return, 3);
//$a = array_chunk($return, $limit);
$key = 0;
$group = array();
$r = count($a);
foreach($alphabet as $v)
{
if($key < $r)
{
if(!isset($group[$v]))
{
$group[$v] = array();
}
$group[$v] = $a[$key];
$key++;
}
}
print_r($group);
Here
https://www.tehplayground.com/bfmZOubIMdmBcjhu
Related
I am trying to make a function to display client's items in an array, however only the first item id is shown in the array. What's wrong with my code? I guess I'm doing something wrong with the array.
Example of sItem value: https://pastebin.pl/view/e492ffa1
$items = array();
$client_item = bin2hex($u->sItem);
$x = 0;
for ($i = 0; $i < 78; $i++) {
$item = hexdec(reverse(substr($client_item, $x, 8)));
if ($item != 0) {
$ii = $db->get_object("SELECT * FROM Clients.dbo.ITEM WHERE Num =" . $item);
if (is_object($ii)) {
$items[] = array("ItemID" => $ii->Num, "ItemSlot" => $i);
}
}
$x += 16;
}
The code above will show just the first item in the array. All others are not shown.
var items = [{"ItemID":310511133,"ItemSlot":0}]
I am not sure what I'm doing wrong. If I remove $x += 16; then it will simply add more entries in the array with the first item id. I want more entries but not with the same item id obviously. :)
var items = [{"ItemID":310511133,"ItemSlot":0},{"ItemID":310511133,"ItemSlot":1},{"ItemID":310511133,"ItemSlot":2},{"ItemID":310511133,"ItemSlot":3},{"ItemID":310511133,"ItemSlot":4}....
This is the reverse function:
function reverse($str)
{
$len = strlen($str);
$i = $len - 2;
$ret = NULL;
while (0 <= $i) {
$ret .= substr($str, $i, 2);
$i -= 2;
}
return $ret;
}
Database Query not returning any object SELECT * FROM Clients.dbo.ITEM WHERE Num = $item
can you confirm values exists in database
you can echo $item value and see manually in database are those values exists.
I have a problem: I can't make my script working with penalties, but I made it work with bonuses.
Start: two-dimensional array of users, each has id, name, start position and modificator (if modificator < 0 its bonus; if modificator > 0 its penalty).
How modificators work: for example, I have a queue of users Test1, Test2, Test3. If I give user Test3 bonus 1, he will switch places with user Test 2 and final queue will be Test1, Test3, Test2. If I give him bous 2, than he will be first at queue.
Penalties works the other way around. If I give user Test1 penalty 1, he will be second in a queue. If I give him penalty 3, he will be moved to the end of the queue.
This is my working code for bonuses:
<?
$new = array(); $orders = array();
$new[111]['position'] = 1; $new[111]['user'] = 'Test1'; $orders[1] = 111; $new[111]['shift'] = 0;
$new[222]['position'] = 2; $new[222]['user'] = 'Test2'; $orders[2] = 222; $new[222]['shift'] = 0;
$new[333]['position'] = 3; $new[333]['user'] = 'Test3'; $orders[3] = 333; $new[333]['shift'] = 0;
$new[444]['position'] = 4; $new[444]['user'] = 'Test4'; $orders[4] = 444; $new[444]['shift'] = 0;
$new[555]['position'] = 5; $new[555]['user'] = 'Test5'; $orders[5] = 555; $new[555]['shift'] = 0;
$new[666]['position'] = 6; $new[666]['user'] = 'Test6'; $orders[6] = 666; $new[666]['shift'] = 0;
$new[777]['position'] = 7; $new[777]['user'] = 'Test7'; $orders[7] = 777; $new[777]['shift'] = 0;
$new[888]['position'] = 8; $new[888]['user'] = 'Test8'; $orders[8] = 888; $new[888]['shift'] = 0;
//shift function
function bonus($start, $bonus)
{
global $new, $orders;
//for bonuses, shift to start, $bonus is negative number
if ($bonus < 0)
{
for ($i = $start; $i > ($start - abs($bonus)); $i--)
{
$order_id = intval($orders[$i]);
$prev = intval($orders[$i - 1]);
if ($prev != 0)
{
$temp = $new[$order_id]['position'];
$new[$order_id]['position'] = $new[$prev]['position'];
$new[$prev]['position'] = $temp;
$orders[$i] = $prev;
$orders[$i - 1] = $order_id;
}
}
}
//end for bonuses
}
// Now I set modificator (bonus negative, penalty positive)
$new[555]['shift'] = -2;
// sort by bonuses
$i = 1;
foreach($new as $value)
{
$order_id = $orders[$i];
bonus($new[$order_id]['position'], $new[$order_id]['shift']);
$i++;
}
// sort 2d array with field position
usort($new, function($a, $b){
return $a['position'] <=> $b['position'];
});
//DATA OUTPUT
$i = 1;
foreach($new as $value)
{
echo $i . '. ' . $value['user'] . ' ; shift: ' . $value['shift'];
echo '<br>';
$i++;
}
?>
At this code I set bonus with: $new[555]['shift'] = -2;
So, penalty will be like: $new[444]['shift'] = 3;
Code for penalties I think must be something like this:
//for penalty, shift to the end, $bonus positive number
if ($bonus > 0)
{
$total_new = count($new);
for ($i = $start; $i < ($start + $bonus); $i++)
{
$order_id = intval($orders[$i]);
$next = intval($orders[$i + 1]);
if ($next < $total_new)
{
$temp = $new[$order_id]['position'];
$new[$order_id]['position'] = $new[$next]['position'];
$new[$next]['position'] = $temp;
$orders[$i] = $next;
$orders[$i + 1] = $order_id;
}
}
}
//end code for penalty
Here is something wrong with $orders. I tried many different types of code all day long, including array enumeration in asc/desc order, but it didn't work correcty...
P.S. I know about array_multisort, but I dont need to sort, I need to switch elements with each other.
Again: bonus 2 means that user must switch places with previous number 2 times. Penalty 3 means that user must switch places with next user 3 times.
I am trying to display possibilities for additions of specific numbers but have not been getting the right results.
<?php
$num3 = 20;
$set = null;
$balance = $num3;
$dig = mt_rand(1,5);
for($i = $balance; $i > 0; $i -= $dig ){
echo $dig.'<br>';
if($i < 1){
$set .= $i;
$balance = 0;
}
else{
$set .= $dig.'+';
$dig = mt_rand(1,5);
}
}
echo $set.'='.$num3;
?>
Here are some of the outputs:
2+5+1+4+5+3+=20
1+4+3+5+3+=20
3+1+1+2+3+4+4+1+3+=20
Appreciate any pointers. Thank in advance...
Ok, even though the requirement isn't completely clear, here's an approach:
(edit: demonstrating prevention of endless loop)
$max_loops = 1000;
$balance = 20;
$found = [];
while($balance > 0 && $max_loops-- > 0) {
$r = mt_rand(1, 5);
if ($balance - $r >= 0) {
$found[] = $r;
$balance -= $r;
}
}
echo implode(' + ', $found) . ' = '.array_sum($found);
Note: This code has a small risk of getting caught in an endless loop... though it's doubtful that it'll ever happen :)
Edit: Now the loop contains a hard-limit of 1000 iterations, after which the loop will end for sure...
To provoke an endless loop, set $balance = 7 and modify mt_rand(4, 5).
You can use a recursive function for this:
function randomAddends($target, $maxAddend = 5, $sum = 0, $addends = [])
{
// Return the array of addends when the target is reached
if ($target <= $sum) {
return $addends;
}
// generate a new random addend and add it to the array
$randomAddend = mt_rand(1, min($maxAddend, $target - $sum));
$addends[] = $randomAddend;
// make the recursive call with the new sum
return randomAddends($target, $maxAddend, $sum + $randomAddend, $addends);
}
I found this Stack Overflow post explaining how you can generate random coupon codes.
I'm looking into using that code and generate multiple coupons at once (e.g. 50), while separate them by a comma.
The output would be: COUPON-HMECN, COUPON-UYSNC, etc.
Code below and codepad example available.
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$res = "COUPON-";
for ($i = 0; $i < 5; $i++) {
$res .= $chars[mt_rand(0, strlen($chars)-1)];
}
echo $res . ",";
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$numCodesToGenerate = 5;
for ($n = 0; $n < $numCodesToGenerate; $n++)
{
$res = "COUPON-";
for ($i = 0; $i < 5; $i++) {
$res .= $chars[mt_rand(0, strlen($chars)-1)];
}
echo $res . ",";
}
Why not use uniqid()?
$coupon_str = '';
$seperator = '';
for($i = 0; $i < 50; $i++) {
$coupon_str .= $seperator . uniqid('COUPON-');
$seperator = ',';
}
echo $coupon_str;
Output:
COUPON-502373ac95dd2,COUPON-502373ac95de8,COUPON-502373ac95ded,....
Here is a much neater version (and faster) that does what you need:
function MakeCouponCode() {
$res = "COUPON-";
for($i = 0; $i < 5; ++$i)
$res .= chr(mt_rand(0, 1) == 0 ? mt_rand(65, 90) : mt_rand(48, 57));
return $res;
}
$coupons = array();
for($i = 0; $i < 5; ++$i)
$coupons[] = MakeCouponCode();
echo implode(', ', $coupons);
Output:
COUPON-D707Y, COUPON-4B37E, COUPON-3O397, COUPON-M799X, COUPON-24Q36
You can use the coupon code generator PHP class file to generate N number of coupons and its customizable, with various options of adding own mask with own prefix and suffix. The coupon codes are separated by comma. Simple PHP coupon code generator
Example:
coupon::generate(8); // J5BST6NQ
I have a for loop and inside of it I have a if statement something like this:
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++) {
if($i < 10){
$output .= my_function($i*1);
}elseif($i < 20){
$output .= my_function($i*2);
}elseif($i < 30){
$output .= my_function($i*3);
}
//elseif 30 => 550
}
Problem is I find it very tedious to have to continue this elseif statement down to 550. Is there any way to do this without writing 55 elseif statements.
Your thinking is exactly right. Having to repeat that much code is a sure sign that something is off.
In this case, the solution is pretty simple. You just want to use integer division to knock off the ones digit, which you don't care about. You also need to adjust by one since you're checking less than and not less than or equal.
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++) {
$temp = (int) ($i / 10) + 1;
$output = my_function($i*$temp);
}
for ($i = 1;$i <= 550;$i++) {
$multiplier = (int) ($i / 10) + 1;
$output = my_function($i * $multiplier);
}
My first thought is that you could use:
$output = "";
$c = 0;
$limit = 550;
for ($i = 1; $i <= $limit; $i++) {
if (($i + 1) % 10 == 0) {
$c++;
$output = my_function($i*$c);
}
}
How about
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++) {
$num = ($i / 10) + 1
$output = my_function($i*$num);
}
Try this:
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++)
{
$output = my_function($i * (1 + (int)($i / 10)));
}
The solution should be as simple as the following:
$output = "";
$limit = 550;
for ($i = 1; $i <= $limit; $i++)
{
$num = ceil($i/10);
$output = my_function($num, $num-1);
}
hmm the original code seems to have changed since I started typing this. The my_function function now only has 1 input when it originally had 2.
for ($i = 1; $i <= $limit; $i++) {
$output = my_function(i*floor($i/10+1));
}
Point is not really about this question:
You reassign output on every loop iteration, but only last will be vailable after loop? Need you $output[]= or $output.= ?