how to arrange below list any way in php,
Actually we will get item array list from MySQL Database(Select Output).
For example,
If We will get Array list like below
$item_arr = array("102","101","103","103","101","101","102","103","102");
The We have to arrange like below,
101 102 103 101 102 103 101 102 103
Again If Array list like below,
$item_arr = array("102","103","102","101","103","101","103","103","101");
The We have to arrange like below,
101 102 103 101 103 103 101 102 103
Again If Array list like below,
$item_arr = array("103","101","102","103","102","102","102","103","102");
The We have to arrange like below,
101 102 103 102 102 103 102 102 103
$rtr_item_arr = arrange_item($item_arr);
function arrange_item($results)
{
$last_val="";
$unique_only = array();
$additional = array();
foreach($results as $val):
if(in_array($val, $unique_only)):
$additional[] = $val;
endif;
if($val != $last_val && !in_array($val,$unique_only)):
echo $val." ";
$unique_only[] = $val;
endif;
$last_val = $val;
endforeach;
$additional = array_filter($additional);
if(!empty($additional)):
arrange_item($additional);
endif;
}
How to arrange it to keep similarity On the Other hand, how to arrange nicely.
Please any suggestion?
It looks like you are repeating the pattern of 101, 102, 103 no matter what order it comes out of the database.
I don't see your SQL posted here, but you could group by the 101, 102, 103 field and count the number of instances of each then order by the 101,102,103 field and create an associative array:
$item_arr = array(101=>count of 101,102=>count of 102,...)
Then in the PHP arrange them in another array using a loop from 0 to highest count. Each time through the loop add each key to the new array if the value is > 0 and subtract 1 from the value of each of the key value pairs.
Related
I have below values in formsubmit,
items[0][item_name]: Test
items[0][item_description]: testing1
items[0][item_price]: 11
items[0][item_group]: 18
items[0][item_code]: 11
items[0][item_is_active][]: 1
items[1][item_name]: test2
items[1][item_description]: test
items[1][item_price]: 12
items[1][item_group]: 18
items[1][item_code]: 12
items[1][item_is_active][]: 1
and here is serverside DB entry in laravel,
$items_data = $request->items;
foreach ($items_data as $i) {
$items = new Menuitems; //Must be reinit to reiterate all entries
$items->item_name = $i['item_name'];
$items->item_description = $i['item_description'];
$items->item_price = $i['item_price'];
$items->item_group = $i['item_group'];
$items->item_code = $i['item_code'];
$items->item_is_active = $i['item_is_active'];
$items->save();
}
It works fine all entries except for item_is_active because it has one more [ ] in form input,
How can I add item_is_active in foreach loop? should it have one more foreach loop inside?
Below is formsubmit values when some checkboxes are not selected,
items[0][item_name]: Capachno
items[0][item_description]: best
items[0][item_price]: 12
items[0][item_group]: 17
items[0][item_code]: 11
items[0][item_is_active][]: 1
items[1][item_name]: Lambc
items[1][item_description]: sdf
items[1][item_price]: 34
items[1][item_group]: 17
items[1][item_code]: 12
items[1][item_is_active][]: 1
items[2][item_name]: tasto
items[2][item_description]: ewr
items[2][item_price]: 12
items[2][item_group]: 17
items[2][item_code]: 13
items[3][item_name]: hingo
items[3][item_description]: 123
items[3][item_price]: 123
items[3][item_group]: 17
items[3][item_code]: 32
I would like to take the result from an SQL that looks like this
id name kg date
1 ABC 3 2017-09-14
1 ABC 2 2017-09-15
2 DEF 2 2017-09-14
2 DEF 5 2017-09-15
2 DEF 5 2017-09-16
2 DEF 3 2017-09-17
3 GHI 3 2017-09-15
3 GHI 6 2017-09-17
And put it into an array (or something else that I can loop through) so it looks like this
id name 2017-09-10 2017-09-11 2017-09-12 2017-09-13 2017-09-14 2017-09-15 2017-09-16 2017-09-17
1 ABC 3 kg 2 kg
2 DEF 2 kg 5 kg 5 kg 3 kg
3 GHI 3 kg 6 kg
It will only hold dates from last sunday until this sunday (Active week).
I haven't worked with arrays before, but of what I have read so far I think I have to create an array inside another array to achive this? I am thinking that from the SQL result I will go through each row and push the values into right row depending if the id already exists. One id could only have unique dates.
while ($row = odbc_fetch_row($resultSQL)){
Create an array with the columns above and push the result from the SQl
into the array on a new row or an existing column if the ID is
already in the array.
}
Anyone have any ideas how to achieve this? Thanks.
EDIT:
Solution: I have now done like this which achives the result I wanted.
//create two arrays
$items=array();
$dates=array();
//loop through the SQL result and put it into the arrays
while ($row = odbc_fetch_row($resultSQL)){
$ITEMNO=odbc_result($resultSQL,"id");
$ITEMNAME=odbc_result($resultSQL,"name");
$RecQtyDay=odbc_result($resultSQL,"kg");
$items[$ITEMNO] = $ITEMNAME;
$dates[$REGDATE][$ITEMNO] = $RecQtyDay;
}
//sort the dates array
ksort($dates);
//loop through and print out dates array within the item array
foreach ($items as $ITEMNO => $ITEMNAME) {
echo "$ITEMNO $ITEMNAME ";
foreach ($dates as $REGDATE => $values) {
if (isset($values[$ITEMNO])) echo "$REGDATE $values[$ITEMNO]";
}
echo"<BR>";
}
As you fetch your query results, build two arrays, one with all the items, and another that's grouped by date with values for any items that have records for each date.
while ($row = odbc_fetch_row($resultSQL)) {
$items[$row['id']] = $row['name'];
$dates[$row['date']][$row['id']] = $row['kg'];
}
Sort the dates/values array by key (date)
ksort($dates);
Then loop over your array of items to generate the rows. Within each row, loop over the array of dates and output the value for that id and date.
foreach ($items as $id => $name) {
echo "$id $name";
foreach ($dates as $date => $values) {
if (isset($values[$id])) echo "$value kg";
}
}
This output won't be neatly formatted, but I don't know exactly how you're planning to output this, so I'll leave that up to you.
I am building a leaderboard (scorebored) for a poker tournament. My aim is to echo data to a table to display scores for the season and scores all time. I want the table to run in order with the highest season points at the top.
I am getting an error message that reads:
Notice: Undefined index: season in C:\wamp\www\UPT Site\leaders.php on line 11
When I print_r an array from the $allplayers array, it shows that all the players arrays are going in correctly, including the [season] key and value declared on line 6 in the bit below...
Can anyone please tell me how to fix my code? (note, the real code doesn't have line numbers in it, I just added them here to make discussion easier).
1 foreach($allplayers as $player){
2 $i = $player[1];
3 if (${"seasonplayerid" . $i}){
4 $sum = array_sum(${"seasonplayerid" . $i});}
5 //$sum = points this season.
6 ${"playerid" . $i}['season'] = $sum;
7 }
8 function val_sort($array,$key) {
9 //Loop through and get the values of our specified key
10 foreach($array as $k=>$v) {
11 $b[] = strtolower($v[$key]);
12 }
13 asort($b);
14 /* foreach($b as $k=>$v) {
15 $c[] = $array[$k];
16 }return $c;
17 */
18 }
19 $sorted = val_sort($allplayers, '[season]');
20 foreach($allplayers as $player){
21 $i = $player[1];
22 echo ("<tr><td>" . $player[0] . $t . ${"playerid" . $i}[3] . $t . ${"playerid" . $i}[4] . $t. ${"playerid" . $i}['season'] . $t. count(${"seasonplayerid" . $i}). "</td><tr>");
23 }
Here is the print_r output for array $playerid1:
Array ( [0] => Jonathan Thompson [1] => 1 [2] => 2015-S 3 [3] => 944 [4] => 7 [season] => 470 )
Here is a key of the information in the array:
/*
$allplayers is a multidimentional array, containing many arrays of players called $playerid1, $playerid2, $playerid3 etc
playerid1[0] = Player name
playerid1[1] = Player ID
playerid1[2] = Current season
playerid1[3] = total points earned
playerid1[4] = total games played games
playerid1[season] = points earned in current season
*/
Looks like at line 19 you are trying to pass key in second parameter but defined it incorrectly.So, to call function val_sort($array,$key) you have to do something like that.
Therefore at line 19 change
$sorted = val_sort($allplayers, '[season]');
to
$sorted = val_sort($allplayers, 'season');
Also i suggest you to use data table it is good and fast
Instead of below line
$sorted = val_sort($allplayers, '[season]');
you should pass the key as below
$sorted = val_sort($allplayers, 'season');
Hope this helps.
I did a dodgy quick fix in this instance.
I wrote a loop to array_pop each array inside the main array, and created a new variable for each piece of data as the array_pop loop was going.
I also array_pushed the data I wanted to sort by and an ID number to a new array.
I used rsort to sort the new array, then looped through it using the ID number to reconstruct a 3rd array in the order I wanted to output data.
Next time I will take the advice of #rocky and do a data table. I may even go back to redo this page as a data table. To be honest, I'd never heard of a data table before yesterday. Once I've given this project over to the boss I'll be investing my time in learning about data tables and AJAX.
Thank you all for your tips, but the quick fix will have to do this time as I am time poor (This site needs to be live tomorrow morning).
Here is my $file1 structure (there are thousands of like that groups):
Group id_7653
{
type register
sub_name 155
1 3123 1 12
2 3124 1 8
3 3125 1 4
4 3126 1 12
5 3127 1 8
6 3128 1 4
.....
}
Group id_8731
{
type register
sub_name 155
1 4331 1 12
2 4332 1 8
3 4333 1 4
4 4334 1 12
5 4335 1 8
6 4336 1 4
.....
}
And here is my $file2 structure (again, there are thousands of defined values)
.....
3123 Spada+1
3124 Spada+2
3125 Spada+3
3126 Spada+4
3127 Spada+5
3128 Spada+6
3129 Spada+7
3130 Spada+8
.....
And here is my Worker script that makes, compares $file1 and $file2.
<?php
//read the first file in as a string
$file1 = file_get_contents("dataparser\names1.txt");
//read the second file in as an array
$file2 = file("dataparser\names2.txt");
//index from file2 that we are going to build
$file2Index = array();
foreach($file2 as $line){
//split the line
$line = explode("\t", $line, 2);
//validate the line, should be only 2 values after explode and first should be a number
if(count($line) == 2 && is_numeric($line[0])){
//add to index
$file2Index[$line[0]] = $line[1];
}
}
//now get all the values from file1 that we want (second column)
preg_match_all('/^\s*\d+\s*(\d+)\s*\d+\s*\d+\s*$/m', $file1, $matches);
$file1Values = array_unique($matches[1]);
//loop over the matches from column 2
foreach($file1Values as $value){
//check if the key doesn't exist
if(!isset($file2Index[$value])){
//echo error message
echo "Value {$value} does not exist in file2<br>";
}
}
?>
What makes that script:
Compares $file1 and $file2 and shows me which values are not defined in $file2
So far, everything works okay.
I want to extend that my script a little bit, so I want to replace that {$value} with my $file2 structure.
This time, I don't want to check that value, I want replace it directly from $file1 value. (Spada etc...)
Which paths I should follow...? Can I get some examples please...
I am to import a file, say june.txt that would have data such as the following data:
Sandy,820,384,133,18,408
Wanda,120,437,128,807,595
Jane,631,415,142,687,600
Andrea,179,339,349,594,986
Wanda,803,191,6,807,322
Jane,741,975,34,15,832
Jane,239,714,250,94,497
Andrea,219,188,411,584,713
And then the PHP would parse it into 2 difference ways:
The first way being all the names bundled together with totals, such as:
Sandy 820 384 133 18 408
Total 820 384 133 18 408
Jane 631 415 142 687 600
Jane 741 975 34 15 832
Jane 239 714 250 94 497
Total 1611 2104 426 796 497
Andrea 179 339 349 594 986
Andrea 219 188 411 584 713
Total 398 527 760 1178 1699
Wanda 120 437 128 807 595
Wanda 803 191 6 807 322
Total 923 628 134 1614 917
The second way would total and add the names together in a big list, such as
Sandy 820 384 133 18 408
Jane 1611 2104 426 796 497
Andrea 398 527 760 1178 1699
Wanda 923 628 134 1614 917
Any logic or suggestions would be helpful, I am new to PHP and not sure how this could even be done. My plan is to eventually display the results in HTML tables and have them sortable, but I can tackle that at a later date, Unless someone feels obligated to just add the and such for me in the parsing.
I think something useful for you would be the explode function.
As far as creating these views I'd start by loading all this data into an associative array of arrays based on the name, then iterate as necessary:
$datafile = file("filename.txt");
// reads lines into an associative array (key is the name) of arrays
// where each sub-array is a list of the records for each name
$arr = array();
foreach($datafile as $line){
$temp = explode(',', $line);
$arr[$temp[0]][] = $temp;
}
// iterate over each person
foreach($arr as $person_set){
// create an array to hold the sum of each column
// (and the name in the first column)
$totals = array();
$totals[0] = $person_set[0][0];
for($i = 1; $i < length($record); $i++){
$totals[$i] = 0;
}
// now iterate over each record for this person
foreach($person_set as $record){
// print a particular record
echo implode(' ', $record) . '<br>';
// add each column (1..end) to the totals array
for($i = 1; $i < length($record); $i++){
$totals[$i] += $record[$i];
}
}
// print out the totals line
echo implode(' ', $totals) . '<br><br>';
}
I'll leave formatting this data into a table as an exercise.
Well, to start, I'd real a file like that with PHP's fgetcsv(). Docs.
You could try a script like this:
<?php
echo "<table>";
$data = file("june.txt");
foreach($data as $month) {
$line = explode(',', $data);
echo '<tr><td>', implode('</td><td>', $line), '</td></tr>';
}
echo "</table>";
Edit:
My bad, didn't notice that you were sorting/grouping/totaling. This should set you on the right track, though. The key is to use $line as your source of information. Just compile it into an array and output later (instead of right in the loop).