PHP - Php Array Group By Month
I have been struggling with this for awhile now, and I think it isn't as hard as I making it. Basically I want to "group" my array by month making a multidimensional array by months. Basically right now my print_r($my_array) outputs:
Code: [Select] Array ( [0] => stdClass Object ( [id] => 2 [did] => 2 [dates] => 2011-06-01 [enddates] => 2011-06-01 [shortdescription] => [max_attendance] => 1 [times] => 06:30:00 [endtimes] => 07:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 4 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 1 [avaliable] => 0 ) [1] => stdClass Object ( [id] => 57 [did] => 57 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 02:00:00 [endtimes] => 03:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [2] => stdClass Object ( [id] => 61 [did] => 61 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 03:00:00 [endtimes] => 04:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Waltz [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [3] => stdClass Object ( [id] => 1 [did] => 1 [dates] => 2011-06-06 [enddates] => 2011-06-06 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Foxtrot [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [4] => stdClass Object ( [id] => 11 [did] => 11 [dates] => 2011-06-08 [enddates] => 2011-06-08 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [5] => stdClass Object ( [id] => 15 [did] => 15 [dates] => 2011-07-04 [enddates] => 2011-07-04 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) ) I need to take that and make it look like this, grouping by the month of the 'dates' value: Code: [Select] Array ( [0] => Array ( //June [0] => stdClass Object ( [id] => 2 [did] => 2 [dates] => 2011-06-01 [enddates] => 2011-06-01 [shortdescription] => [max_attendance] => 1 [times] => 06:30:00 [endtimes] => 07:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 4 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 1 [avaliable] => 0 ) [1] => stdClass Object ( [id] => 57 [did] => 57 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 02:00:00 [endtimes] => 03:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [2] => stdClass Object ( [id] => 61 [did] => 61 [dates] => 2011-06-05 [enddates] => 2011-06-05 [shortdescription] => [max_attendance] => 15 [times] => 03:00:00 [endtimes] => 04:00:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Waltz [locid] => 1 [status] => 0 [shw_attendees] => 0 [club] => SW [url] => [street] => 8936 SW 17th Ave [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [3] => stdClass Object ( [id] => 1 [did] => 1 [dates] => 2011-06-06 [enddates] => 2011-06-06 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Foxtrot [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) [4] => stdClass Object ( [id] => 11 [did] => 11 [dates] => 2011-06-08 [enddates] => 2011-06-08 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Nightclub [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) ) [1] => Array ( //July [0] => stdClass Object ( [id] => 15 [did] => 15 [dates] => 2011-07-04 [enddates] => 2011-07-04 [shortdescription] => [max_attendance] => 15 [times] => 06:30:00 [endtimes] => 08:30:00 [registra] => 1 [unregistra] => 0 [titel] => Beginning Rumba [locid] => 2 [status] => 0 [shw_attendees] => 0 [club] => SE [url] => [street] => 615 SE Alder [please] => [city] => Portland, OR [country] => US [locdescription] => [catname] => Adult Classes [catid] => 1 [price] => Array ( ) [registered] => 0 [avaliable] => 15 ) ) ) Similar TutorialsHi All, I hope i'm posting in the right place but also hope someone can help! I've got the following code: <?php $variations = $product->get_available_variations(); if ($variations): ?> <div class="table-responsive"> <table class="table table-striped"> <tbody> <?php foreach ($variations as $key => $value) : ?> <tr> <td><?php echo $value['sku']; ?> <?php echo $value['variation_id']; ?></td> <?php foreach ($value['attributes'] as $attrKey => $attr) : $tax = str_replace('attribute_', '', $attrKey); $term_obj = get_term_by('slug', $attr, $tax); ?> <td><?php echo $term_obj->name; ?></td> <?php endforeach; ?> </tr> <?php endforeach; #$variations?> </tbody> </table> </div><!-- /table-responsive --> <?php endif; #$variations ?> This produces the following table: Product code System Pack Quantity Variation XT1ECWH 234 System 1 2 N/A XT2ECLWH 236 System 2 2 Left XT2ECRWH 237 System 2 2 Right XT3ECWH 238 System 3 2 N/AHowever, is it possible to alter the code to group the information? So it looks like the below? System 1 System 2 System 3 System 1XT1ECWH 234 Pack Quantity: 2 Variation: N/A System 2 XT2ECLWH 236 Pack Quantity: 2 Variation: Left XT2ECRWH 237 Pack Quantity: 2 Variation: Right System 3 Sorry, not sure why it's not formatted correctly but hopefully you can see what I'm trying to achieve. The array for $variations is as follows: Array ( [0] => Array ( [attributes] => Array ( [attribute_pa_system] => system-1 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => n-a ) [sku] => XT1ECWH [variation_description] => [variation_id] => 234 ) [1] => Array ( [attributes] => Array ( [attribute_pa_system] => system-2 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => left ) [sku] => XT2ECLWH [variation_description] => Left [variation_id] => 236 ) [2] => Array ( [attributes] => Array ( [attribute_pa_system] => system-2 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => right ) [sku] => XT2ECRWH [variation_description] => Right [variation_id] => 237 ) [3] => Array ( [attributes] => Array ( [attribute_pa_system] => system-3 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => n-a ) [price_html] => [sku] => XT3ECWH [variation_description] => ) ) If it helps, I need to always group by the first attribute, in this case : attribute_pa_system Any help on this would be very much appreciated! Thank you in advanced. hi, i'm trying to sort an array of associative arrays, based on one of the keys. if the value of the key is a primitive, normal sorting occurs - this works fine. if the value of the key is an array, i'd like to have it sorted by "groups" here's an example: Code: [Select] <?php $data[] = array('name' => 'h', 'list' => array(1,2)); $data[] = array('name' => 'g', 'list' => array(1)); $data[] = array('name' => 'a', 'list' => array(1,3)); $data[] = array('name' => 'f', 'list' => array(2)); $data[] = array('name' => 'e', 'list' => array(2,3)); $data[] = array('name' => 'b', 'list' => array(3)); $data[] = array('name' => 'c', 'list' => array(1,2,3,4)); $data[] = array('name' => 'd', 'list' => array(3,4)); function deep_sort($array, $sorton){ usort($array, function($a, $b) use($sorton) { $a = $a[$sorton]; $b = $b[$sorton]; if(is_array($a) && is_array($b)){ // this bit is obviously flawed - only included for illustrative purposes $a = implode('', $a); $b = implode('', $b); } return ($a == $b) ? 0 : ($a > $b) ? 1 : -1; }); return $array; } $sorted_by_name = deep_sort($data, 'name'); print '<pre>'; print_r($sorted_by_name); print '</pre>'; $sorted_by_list = deep_sort($data, 'list'); print '<pre>'; print_r($sorted_by_list); print '</pre>'; ?> the sorted_by_name bit is fine - but for sorted_by_list, what i want returned is all the array ordered as follows: all those with "1" (or the lowest if none have "1") in it's list value first, then all those remaining that have the next highest ("2"), etc. so right now, the returns for sort_by_list look like this: Code: [Select] Array ( [0] => Array ( [name] => g [list] => Array ( [0] => 1 ) ) [1] => Array ( [name] => f [list] => Array ( [0] => 2 ) ) [2] => Array ( [name] => b [list] => Array ( [0] => 3 ) ) [3] => Array ( [name] => h [list] => Array ( [0] => 1 [1] => 2 ) ) [4] => Array ( [name] => a [list] => Array ( [0] => 1 [1] => 3 ) ) [5] => Array ( [name] => e [list] => Array ( [0] => 2 [1] => 3 ) ) [6] => Array ( [name] => d [list] => Array ( [0] => 3 [1] => 4 ) ) [7] => Array ( [name] => c [list] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) ) )whereas i'd like to get back: Code: [Select] Array ( [0] => Array ( [name] => g [list] => Array ( [0] => 1 ) ) [1] => Array ( [name] => h [list] => Array ( [0] => 1 [1] => 2 ) ) [2] => Array ( [name] => c [list] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) ) [3] => Array ( [name] => a [list] => Array ( [0] => 1 [1] => 3 ) ) [4] => Array ( [name] => f [list] => Array ( [0] => 2 ) ) [5] => Array ( [name] => e [list] => Array ( [0] => 2 [1] => 3 ) ) [6] => Array ( [name] => b [list] => Array ( [0] => 3 ) ) [7] => Array ( [name] => d [list] => Array ( [0] => 3 [1] => 4 ) ) ) TYIA for any suggestions I have it to where it groups two parts of the Array randomly but I need to make it where it won't pair the same together, or if it has already been paired with a team then use another team. Each value of the array can only be used once. Here is my code shuffle($teamname); $loopCount = $games; for ($i = 0; $i < $loopCount; $i++) { $player1 = array_pop($teamname); $player2 = array_pop($teamname); if(is_null($player1)) { echo 'Not enough different teams to create '.$games.' games'; break; } // output to screen echo "Team: " . $player1 . " Vs Team: " . $player2 . "<br />"; } } Hi all, I would like to ask you if somebody could have a better idea to build this function. The current one is working, but I feel this built a bit in an amateur manner. Feel free to add your ideas so we can learn more about PHP and how to deal with arrays.
I have an array: $arrayVideoSpecs = Array( Array( 'aspect' => '4:3', 'density' => '442368', 'resolution' => '768x576' ), Array( 'aspect' => '4:3', 'density' => '307200', 'resolution' => '640x480' ), Array( 'aspect' => '16:9', 'density' => '2073600', 'resolution' => '1920x1080' ), Array( 'aspect' => '16:9', 'density' => '121600', 'resolution' => '1280x720' ) );
and I want an array as output grouped by video aspect ratio where the key is the pixel density and the value is the video resolution, namely like that for aspect ratio 16:9 ... Array ( [2073600] => 1920x1080 [121600] => 1280x720 )
Then I coded this function which is working but seems amateur ... function groupAndExtractByAspect($array, $groupBy, $aspect, $density, $resolution) { $groupByAspect = Array(); foreach ($array as $value) { $groupByAspect[$value[$aspect]][] = Array($value[$density], $value[$resolution]); } $arrayClean = Array(); foreach ($groupByAspect as $key => $value) { if ($key == $groupBy) { $arrayClean[$key] = $value; } } foreach ($arrayClean as $aspectGroup) { $arrayOutput = Array(); for ($i = 0; $i <= count($aspectGroup); $i++) { $densityIsValid = false; $resolutionIsValid = false; if (!empty($arrayClean[$groupBy][$i][0])) { $density = $arrayClean[$groupBy][$i][0]; $densityIsValid = true; } if (!empty($arrayClean[$groupBy][$i][1])) { $resolution = $arrayClean[$groupBy][$i][1]; $resolutionIsValid = true; } if (($densityIsValid === true) && ($resolutionIsValid === true)) { $arrayOutput[$density] = $resolution; } } } return $arrayOutput; }
The usage is as follow ... $showArray = groupAndExtractByAspect($arrayVideoSpecs, '16:9', 'aspect', 'density', 'resolution'); echo '<pre>'; print_r($showArray); echo '</pre>';
Thank you very much for your ideas! Mapg Hi All, I've a loop which creates an array as follows: $product_table[] = ['SKU' => $sku, 'Label' => $attribute_name, 'Value' => $term_obj->name ]; I'm then grouping the data by SKU code: #group the products by SKU $group_products = array(); foreach ($product_table as $element) : $group_products[$element['SKU']][] = $element; endforeach; Finally, I output the data: #output the data foreach ($group_products as $itemName => $rows) : echo '<tr>'; #echo '<td>', $element['SKU'], '</td>'; $i=0; foreach ($rows as $row) : $i++; #echo '<td>'. $row["SKU"]. '</td><td>'. $row["Label"]. '</td><td>'. $row["Value"]. '</td>'; if ($i == 1): echo '<td>'. $row["SKU"]. '</td><td>'. $row["Value"]. '</td>'; else: echo '<td>'. $row["Value"]. '</td>'; endif; #echo '<td>'. $row["Value"]. '</td>'; endforeach; echo '</tr>'; endforeach; ?> And looks like: Product code System Pack Quantity XT1CWH System 1 1 x 3m XT2CWH System 2 1 x 3m XT3CWH System 3 1 x 3m
This works perfectly fine. However, some products share the same SKU and therefore it causes an issue, like the below: Product code System Pack Quantity XT1CLWH System 1 8 x 3m System 2 8 x 3m System 3 8 x 3m Is there a way I can avoid this, so if perhaps creates the new row but shows the same SKU code? Many thanks Hi all, I have a multi-dimensional array looking somewhat like the following: Array ( [0] => Array ( [id] => 853 [title] => item 853 [price] => 17.99 [createdtime] => yyyy-mm-dd 00:00:00 ) [1] => Array ( [id] => 854 [title] => item 854 [price] => 11.99 [createdtime] => yyyy-mm-dd 00:00:00 ) ... I need to re-order this array so that any items with the 'createdtime' within a month from todays date are at the top, followed by all of the other items in the original order. I would usually amend the sql but I need to do this with the array. What would be the best way to do this? I found array_multisort but im not too sure how to adapt this to my needs. Thank you for any help with this! I have an array that looks like this:
array(5642) { [0]=> string(19) "2021-02-10 09:04:48" [1]=> string(19) "2021-02-10 09:04:54" [2]=> string(19) "2021-02-10 09:05:00" [3]=> string(19) "2021-02-10 09:05:06" [4]=> string(19) "2021-02-10 09:05:12" [5]=> string(19) "2021-02-10 09:05:18" [6]=> string(19) "2021-02-10 09:06:18" [7]=> string(19) "2021-02-10 09:06:24" }
I need to group the instances any time there is more than a 6 second gap between elements. So 0 =>5 would be one array and 6 and 7 would be a new array.
Ive been able to produce the following but its not ideal
array(5642) { [0]=> string(19) "2021-02-10 09:04:48" [1]=> string(19) "2021-02-10 09:04:54" [2]=> string(19) "2021-02-10 09:05:00" [3]=> string(19) "2021-02-10 09:05:06" [4]=> string(19) "2021-02-10 09:05:12" [5]=> string(19) "2021-02-10 09:05:18" [6]=> string(19) "end" [7]=> string(19) "2021-02-10 09:06:18" [8]=> string(19) "2021-02-10 09:06:24" }
$burner_time = array(); foreach($Burner_Control_Alm as $new_burner){ $burner_time[] = strtotime($new_burner); } $repl = 'end'; for ($i=1; $i<count($burner_time); $i++) { $value_second = $burner_time[$i]; $value_first = $burner_time[$i-1] === $repl ? $burner_time[$i-2] : $burner_time[$i-1]; if ($value_second > $value_first + 6) { array_splice($burner_time, $i++, 0, $repl); } print_r($burner_time); } Trying to get the below to generate the month in word form from a numeric entry in a database. Code: [Select] elseif($_GET['rma']=="calander"){ $sql101010="SELECT DISTINCT rma_year_issued FROM $tbl_name4 ORDER BY rma_year_issued"; $result101010=mysql_query($sql101010); while($row101010=mysql_fetch_array($result101010)){ extract($row101010); $content.='<a href="./acp_admincp.php?rma=calander&year='.$rma_year_issued.'">'.$rma_year_issued.'</a> <br />'; } if(isset($_GET['year'])){ $logout.=' | <a href="./acp_admincp.php?rma=calander">Back to RMA Calander</a>'; $rma_year_issued=$_GET['year']; $sql111010="SELECT DISTINCT rma_month_issued FROM $tbl_name4 WHERE rma_year_issued='$rma_year_issued' ORDER BY rma_month_issued"; $result111010=mysql_query($sql111010); while($row111010=mysql_fetch_array($result111010)){ extract($row111010); $months = array('Janurary', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); for($i=0; $i <=11; $i++){ if($rma_month_issued=$i){ $rma_month_issued2=$months[$i]; } } $content=""; $content.='<a href="./acp_admincp.php?rma=calander&year='.$rma_year_issued.'&month='.$rma_month_issued.'">'.$rma_month_issued2.'</a> <br />'; } } } Basically in the data base I may have 2,5,7 under the year 2011. I want to get the numeric months into words for use in the text of the link. The above is returning December even though the entry in the database is 5. Hi.. I need help in getting the 3 Months Name from my table field FromMonth and ToMonth. Here is the scenario. First, I select FromMonth and ToMonth. Ex. FromMonth = 5 ToMonth = 7 So it means FromMonth is May ToMonth is July. Now I save the MonthNumber to my database: table- so_month FromMonth = 5 ToMonth = 7 Now I need to get the between Months Name from FromMonth to ToMonth which are (May, June, July). How does it possible? Thank you so much. So I am creating a list of events. There will be a drop down menu to change between months. I only want the months to be listed if an event during that month exists. So if I have events listed for September and November, list only September and November, skipping October. Any idea on how to approach this or if there are any functions for such a thing? Hi, I have database, screenshot attached. I have to print all records of attendance sort by ID and have sum group by ID. How to know or figure out when the ID changes so that i can show the total for that ID. Bare with me please and try to show me where is my mistake! Code: <?Php include('session.php'); include('dbcon.php'); ?> <html> <head> <link rel="stylesheet" type="text/css" href="bootstrap.css"> </head> <body> <div class="container"> <div class="row"> <div class="col m-auto"> <div class="card mt-5"> <table class="table table-bordered"> <tr> <input type="button" onClick="window.print()" value="Print The Report"/> <td>OracleID</td> <td>Name</td> <td>Designation</td> <td>Clocking In Time</td> <td>Clocking Out Time</td> <td>Duration</td> </tr> <?php $isdone = -1; $query = "select * from attendance_records where isdone= '".$isdone."' order by OracleID"; $result = mysqli_query($con, $query); $query2 = "SELECT sec_to_time(SUM(timestampdiff(SECOND, ClockingInDate, ClockingOutDate))) as total from attendance_records group by OracleID"; $result2 = mysqli_query($con, $query2); while ($row = mysqli_fetch_assoc($result)) { $userid = $row['OracleID']; $name = $row['Name']; $des = $row['Des']; $clockingindate = $row['ClockingInDate']; $clockingoutdate = $row['ClockingOutDate']; $duration = $row['Duration']; $t=time(); $curdate = date("d-m-Y",$t); $curTime = date("g:i:s A",$t); ?> <tr> <td><?php echo $userid ?></td> <td><?php echo $name ?></td> <td><?php echo $des ?></td> <td><?php echo $clockingindate ?></td> <td><?php echo $clockingoutdate ?></td> <td><?php echo $duration ?></td> </tr> <?php } while($row2 = mysqli_fetch_assoc($result2)) { $totals= $row2['total']; echo '<td><h5>Total Attendance Time:</h5></td>'; echo '<td>'; echo $totals; echo '</td>'; } echo '<tr>'; echo 'Report created by ', $_SESSION['user_id2'], ' on ', $curdate, ' at ', $curTime; echo '</tr>'; ?> </table> <a href="home.php" width="100%">Click here to go back to Main Menu</a> </div> </div> </div> </div> </body> </html> and screenshot of result is attached. I have the totals but not at the end of each OracleID, it is at the end of the report. Thanks.
Hi all, does anyone know how to perform this, or if it's even possible? I need every 6th row to be a GROUP BY of the following 5 rows, they all share a common group_id so that's what the GROUP BY is performed on. 1. GROUP BY group_id 2. item #1 3. item #2 4. item #3 5. item #4 6. item #5 7. GROUP BY group_id 8. item #6 ... and so on I am trying to get a group by to work but for some reason it will only display one output. Not sure what the problem is. Code: [Select] mpid mpyear mpmonth mpday mptitle 4 2012 3 2 Text stuff here 3 2012 3 1 Day 1 stuff here 2 2012 2 28 Feb stuff here 1 2011 12 27 First post test here So what I am trying to do is sort out per year month ie: 2012 3 Text stuff here Day 1 stuff here 2 Feb stuff here 2011 12 First post test here That is what I eventually want it to look like, but first thing I need to do is get atleast the mptitle to group first. Code: [Select] $query = "SELECT * FROM monsterpost group BY 'mpmonth'"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo $row['mptitle']; echo "<br />"; } ?> I have a table that has pairs of matching records that are identified as matching on a specific column. In other words, 2 users submit data on the same record, and they are identified as matching and belonging to the same record data because the table column gameid is the same for the pair. How can I output the information for the pair of matching records for each of these into a table repeating it until Ive looped through all records? In other words I want to create 1 table with headers for each matching pair all the way down... thanks in advance. This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=353968.0 Hi
I am reading in data from a csv file
if (($handle = fopen("data.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { echo "User Name: $data[0]"; echo "Booking IDs: $data[1]"; } echo "<hr>";The problem is some of the usernames are duplicated so get outputted the same user name 20 times but with different IDs, obviously but I am unsure how to group them together. I can't seem to figure out how to write if the next username is the same as current then just add another ID not go through the entire loop. Any ideas? Thanks im trying to pull the information for a post which is to include 3 db tables(posts, users, attachments) Ive tried the following code: Code: [Select] $query = $link->query("SELECT p.*, u.u_username, u.u_posts, u.u_avatar, u.u_signature, u.u_avatar_cropped, group_concat(a.a_name, a.a_size, a.a_date_posted separator '<br />') attachments FROM ".TBL_PREFIX."posts as p JOIN ".TBL_PREFIX."users as u ON (u.u_username = p.p_poster) LEFT JOIN ".TBL_PREFIX."post_attachments as a USING (".TBL_PREFIX."posts.p_pid) WHERE p.p_tid = '$tid' GROUP BY p.p_pid ORDER BY p.p_time_posted ASC")or die(print_link_error()); but that generates the error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.p_pid) WHERE p.p_tid = '158' GROUP BY p.p_pid ORDER BY p.p_time_pos' at line 10" All i am trying to do is pull everything from posts, join users and select all attachments that belong to the post. Here is my database setup(shortened) asf_posts p_pid | p_name | p_poster 1 test doddsey65 asf_users u_uid | u_username 1 doddsey65 asf_attachments a_aid | a_pid | a_name 1 1 name can anyone help? Hi, Cut down code SELECT count(number) FROM table GROUP BY title echo $title .'<br>'. $number; That outputs Code: [Select] Title1 5 Title2 9 Title3 2 But what do i do if i wanted to retain and loop through the "$number" actual values, not JUST count the total? I still need access to what "$number" is while looping. But as GROUP BY returns one result.. im stuck. Ideally i want this as the resulting output Code: [Select] title1 // grouped title 3 // total number of $numbers per grouped title 1234 12345 123456 // The actual values of $number totalling to the above grouped number title2 // grouped title 2 // total number of $numbers 1234 123456 // The actual values of $number totalling to the above grouped number Hi Just a quick one. I want my output result to look like this. In its simplistic form I have 2 columns, NAME and NUMBER. Adam 1 2 3 Becky 1 2 3 Charlie 1 2 3 So the output results are grouped by the "name" field, then ordered by the "number" field. I thought it was GROUP BY, but that function is something else. Iv tried searching around but im unsure what you call this form of grouping results. Many Thanks! This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=306114.0 |