2D array doesnt show data - php

I have a module in my projects its about the finding free places in the classroom timetable. I am taking variables without problem from database. I need an array which is returns for all classroom which is how many classroom i have in DB. This classrooms also has variable time period 1 till 12 and each period has duration for example if i have in 4th period 3 duration it should write 1 2 3 x x x 7 8 9 10 11 12 and if i have another course in 9th period 2 duration it should be 1 2 3 x x x 7 8 x x 11 12. I did it in 1D array if i give the class_no in query. but it should do it more than a classroom. it shows just numbers 1..12 in 1 line.
$dayy = $_GET['src_day0'];
$drt = $_GET['src_duration0'];
$tm = $_GET['src_time0'];
$faculty_id = $_SESSION['faculty_id'];
$scale = "select DISTINCT t.class_no,t.time,t.duration from ttable t,class c where
day='$dayy' AND (t.faculty='$faculty_id' OR c.faculty='$faculty_id')";
$result = $conn->query($scale);
$x = 1;
while ($rows = $result->fetch_assoc()) {
$class = $rows['class_no'];
$arr = array(
array($x => "$class"),
array(
1 => " 1 ", 2 => " 2 ", 3 => " 3 ", 4 => " 4 ", 5 => " 5 ", 6 => " 6 ",
7 => " 7 ", 8 => " 8 ", 9 => " 9 ", 10 => " 10 ", 11 => " 11 ", 12 => " 12 ",
));
$x++;
}
while ($rows = $result->fetch_assoc()) {
$time = $rows['time'];
$duration = $rows['duration'];
$result1 = ($time + $duration);
for($j=1;$j<$x;$j++)
for ($i = $time; $i < $result1; $i++)
$arr[$j][$i] = "x1";
}
}
for ($i = 1; $i < $x; $i++) {
for ($j = 1; $j < 13; $j++)
echo $arr[$i][$j];
echo "</br>";

In fact you missbuilded your array :)
If your looking your code:
$class = $rows['class_no'];
$arr = array(
array($x => "$class"),
array(
1 => " 1 ", 2 => " 2 ", 3 => " 3 ", 4 => " 4 ", 5 => " 5 ", 6 => " 6 ",
7 => " 7 ", 8 => " 8 ", 9 => " 9 ", 10 => " 10 ", 11 => " 11 ", 12 => " 12 ",
));
$x++;
For each iteration of the loop, you overwrite the array with a new value.
In fact, you need to create a new data and append it on the array.
A way to do it, is the following process:
$arr = [];
while ($rows = $result->fetch_assoc()) {
$arr[ $rows['class_no'] ] = [1 => ' 1 ', 2 => ' 2 ', /*...*/];
}
// free memory if needed
while ($rows = $result->fetch_assoc()) {
for( $i = $rows['time'] ; $i < $rows['time'] + $rows['duration'] ; $i++ ) {
$arr[ $rows['class_no'] ][ $i ] = 'x';
}
}
Here, you create an empty variable $arr of array type. Then, for each data on the loop, you created a new line (for each class_no) with the default values.
On the second loop, you iterate again on the results, but here, you take each array data by class_no and change the correct time value to x

Related

How to get and generate repetitive numbers (subset) arrays in PHP

I check the code and realized that it can't show repetitive numbers
MY CODE
<?php
/* Designated level for each exp
Level 2 - 23 exp
Level 3 - 34 exp
Level 4 - 45 exp
Level 5 - 56 exp
Level 6 - 68 exp
Level 7 - 79 exp
Level 8 - 90 exp
Level 9 - 101 exp
Level 10 - 112 exp
Level 11 - 123 exp
Level 12 - 134 exp
Level 13 - 145 exp
Level 14 - 156 exp
Level 15 - 168 exp
Level 16 - 179 exp
*/
$limit = 100000-99370;
// Level
$arrlevel = array ('Level 2','Level 3','Level 4','Level 5','Level 6','Level 7','Level 8','Level 9','Level 10','Level 11','Level 12','Level 13','Level 14','Level 15','Level 16');
// Exp
$array = array (23,34,45,56,68,79,90,101,112,123,134,145,156,168,179);
$array = array_filter($array, function($var) use ($limit) {
return ($var <= $limit);
});
$num = count($array);
$total = pow(2, $num);
$out = array();
for ($i = 0; $i < $total; $i++) {
$comb = array();
for ($j = 0; $j < $num; $j++) {
// is bit $j set in $i?
if (pow(2, $j) & $i){
$comb[] = $array[$j];
}
}
if (array_sum($comb) == $limit)
{
$out[] = $comb;
}
}
array_multisort(array_map('count', $out), SORT_ASC, $out);
$out = array_unique($out, SORT_REGULAR);
$m = 1;
$mapper = [
23 => "Level 2",
34 => "Level 3",
45 => "Level 4",
56 => "Level 5",
68 => "Level 6",
79 => "Level 7",
90 => "Level 8",
101 => "Level 9",
112 => "Level 10",
123 => "Level 11",
134 => "Level 12",
145 => "Level 13",
156 => "Level 14",
168 => "Level 15",
179 => "Level 16",
];
foreach($out as $result)
echo "<b>Possible Answer ". $m++. " : </b><br> " .implode(' , ', array_map(function($x) use ($mapper) {
return $mapper[$x] . " - " . $x;
}, $result))."
<br><br>";
My Input and Ouput
If i input 99318
the output is like this
Possible Answer 1 :
Level 10 - 112 , Level 11 - 123 , Level 12 - 134 , Level 13 - 145 , Level 15 - 168
I want to generate also the repetitive numbers too
But it cannot show some repetitive numbers answer like this
Possible Answer :
Level 4 - 45 , Level 10 - 112 , Level 11 - 123 , Level 11 - 123 , Level 12 - 134 , Level 13 - 145
You can see there's two Level 11 - 123
I want the ouput like this
Possible Answer :
Level 4 - 45 , Level 10 - 112 , Level 11 (x2) - 246 , Level 12 - 134 , Level 13 - 145
I want to group all repititive numbers and sum up them all
One option to get your result is to pass another value to array_map with the result of array_count_values.
Then inside the mapping you can determine to show the count for the number based on the index like $countValues[$x] just as for the mapper.
For example
foreach($out as $result) {
$countValues = array_count_values($result);
echo "<b>Possible Answer " . $m++ . " : </b><br> " . implode(' , ',
array_map(function ($x) use ($mapper, $countValues) {
$strCount = $countValues[$x] > 1 ? " (" . $countValues[$x] . ")" : "";
return $mapper[$x] . $strCount . " - " . $x;
}, array_unique($result))) . "
<br><br>";
}
That will give you a result like
Possible Answer 1 :
Level 2 - 23 , Level 6 - 68 , Level 7 (x2) - 79 , Level 9 - 101 , Level 10 - 112 , Level 15 - 168
Php demo with as a test a duplicate value 79 for $array

PHP Loop with While Loop confusion

I currently have a simple WHILE loop running as follows:
while($los = $result->fetch_row())
{
echo"<tr><td>".$los[0]."</td>";
echo"<td>".$los[1]."</td>";
echo"<td>".$los[2]."</td></tr>";
}
The contents of $los is as follows:
London => 15 => 32
Glasgow => 23 => 45
Leeds => 1 => 12
Truro => 5 => 23
All working fine but what I am struggling with is that I have a second array $outs, the contents of which are:
London => 3
Glasgow => 5
Liverpool => 2
Poole => 1
Within the first while loop I am trying to subtract the value for $outs from $los when the location name matches. I have tried adding the following inside the while loop but no joy:
if($los[0] == $outs[0]){
$los[1] = $los[1]-$outs[1];
}
But no joy, also when I have tried print_r($outs) from within the while loop, all it returns is:
Poole => 1
Poole => 1
Poole => 1
Poole => 1
I cannot fathom where I am going wrong or even if this is possible. Is the $outs array being modified as it's within the first loop? Any ideas, suggestion or pointers welcomed on how I might achieve this.
VAR DUMPS
So, as requested the contents of the two arrays are:
$los
London => 15 => 32
Glasgow => 23 => 45
Leeds => 1 => 12
Truro => 5 => 23
$outs
London => 3
Glasgow => 5
Liverpool => 2
Poole => 1
However, when I vardump or print_r for $outs when it is in the while($los = $result->fetch_row()) the contents are:
Poole => 1
Poole => 1
Poole => 1
Poole => 1
FURTHER CODE
The code to obtain the array for $outs is:
$query19 = "SELECT Country, COUNT(Country), Resp FROM `tresults_` WHERE q39 = 'Complete' GROUP BY Country;";
$result19 = $mysqli->query($query19);
while($row19 = $result19->fetch_assoc()){
$outs = $row19;
}
Please consider to post the RAW Content that was produced by var_dump and include a full but minimal script to produce the error.
What is unclear here seem to be if $outs is a (1) Key-Value Array or a if it's (2) containing sub-arrays.
Option 1:
$outs = [
"London" => 3,
"Glasgow" => 5,
"Liverpool" => 2,
"Poole" => 1
];
Option 2:
$outs = [
["London", 3],
["Glasgow", 5],
["Liverpool", 2],
["Poole", 1]
];
I did for testing assume your input rows look like this:
$citys = [
["London", 15, 32],
["Glasgow", 23, 45],
["Leeds", 1, 12],
["Truro", 5, 23]
];
Depending on this you sould adjust the if inside your while loop.
For option 1:
foreach ($citys as $los) {
if (array_key_exists($los[0], $outs)) {
$los[1] = $los[1] - $outs[$los[0]];
}
echo " <tr><td > " . $los[0] . "</td > ";
echo "<td > " . $los[1] . "</td > ";
echo "<td > " . $los[2] . "</td ></tr > ";
}
For option 2 it's a little bit more code, as we need to search for the cityname index inside $outs first. In php > 5.5 it could be done using array_column which would be easier.
foreach ($citys as $los) {
$indexInOuts = null;
foreach($outs as $index => $out){
if($los[0] === $out[0]){
$indexInOuts = $index;
break;
}
}
if($indexInOuts !== null){
$los[1] = $los[1] - $outs[$indexInOuts][1];
}
echo " <tr><td > " . $los[0] . "</td > ";
echo "<td > " . $los[1] . "</td > ";
echo "<td > " . $los[2] . "</td ></tr > ";
}
The output for both options would be like:
London 12 32
Glasgow 18 45
Leeds 1 12
Truro 5 23
And as stated in the comments you are replacing the $outs every time. So edit it to look like this:
$outs = [];
while ($row19 = $result19->fetch_assoc()) {
$outs[] = $row19;
}

Add value in specific index array

i have array data like this.
[0] => Array (
[id] => 1
[id_requestor] => 1
[jam_input] => 2015-06-20 06:00:00
[jam_pakai] => 2015-06-28 08:00:00
[total_poin] => 30
)
[1] => Array (
[id] => 2
[id_requestor] => 2
[jam_input] => 2015-06-20 07:00:00
[jam_pakai] => 2015-06-28 08:00:00
[total_poin] => 10
)
[2] => Array (
[id] => 3
[id_requestor] => 3
[jam_input] => 2015-06-20 06:30:00
[jam_pakai] => 2015-06-28 08:00:00
[total_poin] => 5
)
In above data, there is total_poin. This total_poin that i use later.
I want to sort total_poin array descending.
But i wont to use php built in array function.
Cause i have to use method from research paper like this.
for i=0 to i<main queue.size
if jobi+1 length > jobi length then
add jobi+1 in front of job i in the queue
end if
if main queue.size = 0 then
add job i last in the main queue
end if
And here is my implementation :
function LJFAlgorithm($time_str) {
$batchData = getBatch($time_str);
$ljf = [];
for ($i=0; $i < count($batchData)-1; $i++) {
echo $batchData[$i+1]['total_poin'] ." >= ". $batchData[$i]['total_poin'] . " = " . ($batchData[$i+1]['total_poin'] >= $batchData[$i]['total_poin']);
if ($batchData[$i+1]['total_poin'] >= $batchData[$i]['total_poin']) {
echo ", Add " . $batchData[$i+1]['total_poin'] . " in front of " . $batchData[$i]['total_poin'];
} else {
echo ", Add " . $batchData[$i]['total_poin'] . " in front of " . $batchData[$i+1]['total_poin'];
}
echo '<br/>';
}
print_r($ljf);
}
But it not work perfectly, i get one data missing.
Here is my output code :
10 >= 30 = , Add 30 in front of 10
5 >= 10 = , Add 10 in front of 5
5 value not added in array.
How to fix that?
Thank you very much.
Try this
for ($i=0; $i <= count($batchData)-1; $i++)

php array arrangements

Hi i convert an collections of string comma separated 0 1 5, 2 3 15, 4 18 20 into an array using this functions as follow:
$openHrs = explode(",", $openHrs['open_hours']);
The end result are as follow:
Array ( [0] => 0 1 5 [1] => 2 3 15 [2] => 4 18 20 )
In this array 0 1 5 means Mon 1 am 5 am and 4 18 20 means Thur 6 pm 8 pm, so first digit represent weekday rest 2 digits represent hours in 24hrs format, now how can i output the existing array into this format?
Array ( [0] => Mon 1 am 5 am [1] => Tue 3 am 3 pm [2] => Thur 6 pm 8 pm )
Thanks
I would use array_map to get a filtered version. You can create dates using mktime() and format them using date(). I believe this should be the solution:
$filtered = array_map(function($incoming) {
$parts = explode(' ', $incoming);
return
date('D', mktime(0,0,0,1, $parts[0])) . ' ' .
date('g a', mktime($parts[1])) . ' ' .
date('g a', mktime($parts[2]));
}, $openHrs);
For the weekday, I suggest:
$weekday=array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
foreach ($openHours As &$openHour) {
$foo = explode(' ',$openHour);
$openHour=$weekday[$foo].' '. // <-- write yourself a nice function to convert 24h to 12h, maybe there's something like this in PHP already?
}
Try this:
// This will be used for the week days
$weekdays = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
// Loop through each date
foreach($openHrs as &$temp) {
// Separate the numbers
$temp = explode(" ", $temp);
// Change from a 24 hrs clock to a 12 hrs clock
$temp[1] = $temp[1] > 12 ? $temp[1] - 12 . 'pm' : $temp[1] . 'am';
$temp[2] = $temp[2] > 12 ? $temp[2] - 12 . 'pm' : $temp[2] . 'am';
// Update the element
$temp = $weekdays[$temp[0]] . ' ' . $temp[1] . ' ' . $temp[2];
}
I would explode each Arrayelement again with a whitespace as Delimiter so you have a better array structure:
Array ( [0] => Array(0, 1, 5), [1] => Array(1, 3, 3) [2] => Array(4, 18, 18) )
If you have this structure there are probably several approaches. There might be a solution with date() and timestamps but Iam honestly not to sure about that. An other solution would be. Define another array with the Weekdays.
$weekdays = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
Then you can use the numbers that you have as index for that array to output the correct weekday.
The hours can you output with a little selfmade function ala:
function twentyFourHourToAmPM($number) {
if ($number > 12) {
return ($number - 12)." pm";
} else {
return $number." am";
}
}
Output everything should then work like this:
foreach ($openHrs as $key => $value) {
echo $weekdays[$value[0]]." ".twentyFourHourToAmPM($value[1])." - ".twentyFourHourToAmPM($value[2]);
}

php round/ceil/floor how to make a number to Multiples of 7

Firstly poor of my mathematica. I tried to use php to make a number to Multiples of 7. the rules as below:
$num >=0;
$num = '0'; => output '7'
$num = '1'; => output '7'
$num = '6'; => output '7'
$num = '8'; => output '14'
$num = '14'; => output '14'
$num = '16'; => output '21'
$num = '20'; => output '21'
$num = '40'; => output '42'
$num = '84'; => output '84'
...
I cost many times, but I am not smart to solve this by myself. such as
echo round(($num*7-1)/7); // not a right answer.
any one is kindly help me? Thanks.
Your requirements are not consistent if the output for 0 is 7; 0 is already a multiple of 7, since 0 * 7 = 0.
echo ceil($num / 7) * 7;
Alternatively, use the modulus operator:
$m = $num % 7;
echo $m == 0 ? $num : $num - $m + 7;
Try ceil($num / 7) * 7. The code:
<?php
for ($i = 0; $i < 50; $i++) {
echo get_number($i) . "\n";
}
function get_number($num) {
return ceil($num / 7) * 7;
}
?>
Produces:
0 => 0
1 => 7
2 => 7
3 => 7
4 => 7
5 => 7
6 => 7
7 => 7
8 => 14
9 => 14
10 => 14
11 => 14
12 => 14
13 => 14
14 => 14
15 => 21
...
46 => 49
47 => 49
48 => 49
49 => 49
One possibility is to use modulo arithmetic, e.g.:
$x = $whatever;
while($x % 7 != 0){
$x++
}
echo $x; // will now be next multipe of seven >= $whatever

Categories