Jump to content

jarvis

Members
  • Content Count

    459
  • Joined

  • Last visited

Everything posted by jarvis

  1. Hi All, I have a Bootstrap (v4) carousel. It has numerous slides but one slide has a video. For example: <div class="carousel-item" data-interval="10000"> <video id="player" loop> <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" type="video/mp4"><br> Your browser does not support the video tag. </video> <div class="carousel-caption d-md-block"> <h3 data-animation="animated flipInX"> This is the caption for slide 4 </h3> </div> </div> What I'm trying to do, is set the video to play only when you're on that slide (active) and pause when you either scroll away (via a click on the carousel arrows/indicators or the carousel interval). Therefore, I will use the event functionality (https://getbootstrap.com/docs/4.0/components/carousel/#events). As I'm using HTML 5 video tags, this is my solution: $('#carousel').carousel().on('slide.bs.carousel',function(){ $('.carousel-item').find('video').each(function(){ this.pause(); }); }); $('#carousel').carousel().on('slide.bs.carousel',function(){ $('.carousel-item active').find('video').each(function(){ this.play(); }); }); Unfortunately, it doesn't seem to work but I can't see why? Am I missing something very obvious?? Many thanks in advanced
  2. 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
  3. HI @Barand Is it possible to ask one more thing? In your very kind example, you have: $headings = [ 'attribute_pa_colour' => 'Colour', 'attribute_pa_description' => 'Description', 'attribute_pa_material' => 'Material', 'attribute_pa_pack-quantity' => 'Pack Quantity', 'attribute_pa_size' => 'Size', 'attribute_pa_system' => 'System', 'attribute_pa_variation' => 'Variation', 'variation_description' => 'Description', 'sku' => 'SKU', 'variation_id' => 'ID', 'price_html' => 'Price' ]; echo '<pre>', print_r($headings), '</pre>'; And if you print his out, you would see: Array ( [attribute_pa_colour] => Colour [attribute_pa_description] => Description [attribute_pa_material] => Material [attribute_pa_pack-quantity] => Pack Quantity [attribute_pa_size] => Size [attribute_pa_system] => System [attribute_pa_variation] => Variation [variation_description] => Description [sku] => SKU [variation_id] => ID [price_html] => Price ) I thought I'd try and automate this, so have the following code: $attribute_taxonomies = wc_get_attribute_taxonomies(); if ( $attribute_taxonomies ) : $taxonomyOfInterest = array(); foreach ($attribute_taxonomies as $tax=>$tax_value) : $attribute = 'attribute_pa_'.$tax_value->attribute_name; $name = 'pa_'.$tax_value->attribute_name; $label = wc_attribute_label( $name ); $taxonomyOfInterest[] = $attribute.' => '.$label.','; endforeach; endif; array_push($taxonomyOfInterest,"'variation_description' => 'Description',", "'sku' => 'SKU',", "'variation_id' => 'ID',", "'price_html' => 'Price'"); echo '<pre>', print_r($taxonomyOfInterest), '</pre>'; But when I print this out, I get: Array ( [0] => attribute_pa_colour => Colour, [1] => attribute_pa_description => Description, [2] => attribute_pa_material => Material, [3] => attribute_pa_pack-quantity => Pack Quantity, [4] => attribute_pa_size => Size, [5] => attribute_pa_system => System, [6] => attribute_pa_variation => Variation, [7] => 'variation_description' => 'Description', [8] => 'sku' => 'SKU', [9] => 'variation_id' => 'ID', [10] => 'price_html' => 'Price' ) Apologies for the terminology but how do I output this as you have it I guess without an index but arrays always need an index, right?
  4. Well that sounds jolly interesting and not something i've done before. I'll go and look into it Thanks again
  5. Hi @Barand Sorry for the delay replying. Thank you so, so much! That's absolutely bloody brilliant I think I can work out how to remove the bits and bobs I don't need to display (image_id, is_in_stock etc.) Thank you once again!
  6. Hi @Barand Mine looks like this: Array ( [attribute_pa_pack-quantity] => [dimensions] => [dimensions_html] => [image] => [image_id] => [is_in_stock] => [is_purchasable] => [is_sold_individually] => [min_qty] => [sku] => [variation_id] => [variation_is_active] => [variation_is_visible] => [weight_html] => ) I did remove a couple of items from the outset to tidy the initial array output (very first post). I'm sorry, I thought it would make life easier but wonder if I've now muddled the situation in doing so!? Otherwise it was rather lengthy: Array ( [0] => Array ( [attributes] => Array ( [attribute_pa_system] => system-1 [attribute_pa_pack-quantity] => 1-x-3m ) [availability_html] => [backorders_allowed] => [dimensions] => Array ( [length] => [width] => [height] => ) [dimensions_html] => N/A [display_price] => 0 [display_regular_price] => 0 [image] => Array ( [title] => xt1cwh_web_600 [caption] => [url] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png [alt] => [src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-600x600.png [srcset] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png 600w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-400x400.png 400w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-150x150.png 150w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-250x250.png 250w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-120x120.png 120w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-100x100.png 100w [sizes] => (max-width: 600px) 100vw, 600px [full_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png [full_src_w] => 600 [full_src_h] => 600 [gallery_thumbnail_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-100x100.png [gallery_thumbnail_src_w] => 100 [gallery_thumbnail_src_h] => 100 [thumb_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-400x400.png [thumb_src_w] => 400 [thumb_src_h] => 400 [src_w] => 600 [src_h] => 600 ) [image_id] => 719 [is_downloadable] => [is_in_stock] => 1 [is_purchasable] => 1 [is_sold_individually] => no [is_virtual] => [max_qty] => [min_qty] => 1 [price_html] => [sku] => XT1CWH [variation_description] => [variation_id] => 199 [variation_is_active] => 1 [variation_is_visible] => 1 [weight] => [weight_html] => N/A ) [1] => Array ( [attributes] => Array ( [attribute_pa_system] => system-2 [attribute_pa_pack-quantity] => 1-x-3m ) [availability_html] => [backorders_allowed] => [dimensions] => Array ( [length] => [width] => [height] => ) [dimensions_html] => N/A [display_price] => 0 [display_regular_price] => 0 [image] => Array ( [title] => xt1cwh_web_600 [caption] => [url] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png [alt] => [src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-600x600.png [srcset] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png 600w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-400x400.png 400w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-150x150.png 150w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-250x250.png 250w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-120x120.png 120w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-100x100.png 100w [sizes] => (max-width: 600px) 100vw, 600px [full_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png [full_src_w] => 600 [full_src_h] => 600 [gallery_thumbnail_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-100x100.png [gallery_thumbnail_src_w] => 100 [gallery_thumbnail_src_h] => 100 [thumb_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-400x400.png [thumb_src_w] => 400 [thumb_src_h] => 400 [src_w] => 600 [src_h] => 600 ) [image_id] => 719 [is_downloadable] => [is_in_stock] => 1 [is_purchasable] => 1 [is_sold_individually] => no [is_virtual] => [max_qty] => [min_qty] => 1 [price_html] => [sku] => XT2CWH [variation_description] => [variation_id] => 200 [variation_is_active] => 1 [variation_is_visible] => 1 [weight] => [weight_html] => N/A ) [2] => Array ( [attributes] => Array ( [attribute_pa_system] => system-3 [attribute_pa_pack-quantity] => 1-x-3m ) [availability_html] => [backorders_allowed] => [dimensions] => Array ( [length] => [width] => [height] => ) [dimensions_html] => N/A [display_price] => 0 [display_regular_price] => 0 [image] => Array ( [title] => xt1cwh_web_600 [caption] => [url] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png [alt] => [src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-600x600.png [srcset] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png 600w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-400x400.png 400w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-150x150.png 150w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-250x250.png 250w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-120x120.png 120w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-100x100.png 100w [sizes] => (max-width: 600px) 100vw, 600px [full_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png [full_src_w] => 600 [full_src_h] => 600 [gallery_thumbnail_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-100x100.png [gallery_thumbnail_src_w] => 100 [gallery_thumbnail_src_h] => 100 [thumb_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-400x400.png [thumb_src_w] => 400 [thumb_src_h] => 400 [src_w] => 600 [src_h] => 600 ) [image_id] => 719 [is_downloadable] => [is_in_stock] => 1 [is_purchasable] => 1 [is_sold_individually] => no [is_virtual] => [max_qty] => [min_qty] => 1 [price_html] => [sku] => XT3CWH [variation_description] => [variation_id] => 201 [variation_is_active] => 1 [variation_is_visible] => 1 [weight] => [weight_html] => N/A ) ) Apologies if this completely screws things!?
  7. What would cause: Notice: Array to string conversion in... line 93 This refers to echo "<div class='hdg'>{$headings[$h]}</div>$v<br>\n";
  8. Thank you so much @Barand for all of your help!
  9. Thanks @Barand I could do that, then use str replace to tidy it up - not ideal but a workable solution, what do you think? As I say, I could do with a pointer as to how to display the table though if possible? Sorry for asking! As a thought, the heading would always be the first attribute, so in theory would be 'attribute_pa_system'
  10. Thanks @Barand Hmm, I hadn't given that a thought! If i'm honest, i'm struggling a little as to how to output this anyway I hate arrays or they hate me, haven't decided which
  11. HI @Barand Sorry to trouble you further. If you have the array coming in with: ( [sku] => XT1ECWH [var_id] => 234 [variation] => n-a [pack_qty] => 2 ) Within your code, you currently have: $systems[$var['attributes'][0]][] = [ 'sku' => $var['sku'], 'var_id' => $var['variation_id'] ?? '', 'pack_qty' => $var['attributes'][1], 'variation' => $var['attributes'][2] ]; Which is utterly brill, however, from product to product, the attributes may differ (some have more, some have less, some are different). For example: ( [sku] => XT1ECWH [var_id] => 234 [variation] => n-a [colour] => black [pack_qty] => 2 [material] => plastic ) Is there anyway to dynamically create the internal part? $systems[$var['attributes'][0]][] = [ 'sku' => $var['sku'], 'var_id' => $var['variation_id'] ?? '', //some sort of loop here to gather the below? // 'pack_qty' => $var['attributes'][1], 'variation' => $var['attributes'][2] ]; Is that even possible?
  12. Ah it's because not every product has that specific attribute Is there a way to add a conditional check within the array?
  13. Thank you so much! I get a notice of Notice: Undefined offset: 2 which relates to this line: 'variation' => $var['attributes'][2], so now trying to work out what may be causing this Thank you again for all your help
  14. Thanks @Barand The variations (attributes) are created within a CMS. So there are various ones: System Pack Quantity Variation Colour Then each attribute has related terms, for example: System - System 1, System 2 etc. Pack Quantity - 1,2,3,10 etc. Variation: Left, Right Colour - Black, White, Grey When a product's created, you specify which Attributes you want and select the necessary variations So $variations then contains the necessary product variations As products differ with which option is first, it'd be ideal not to use a name you see, hence wondering whether you can use the [0] to denote the first attribute instead - I hope that makes sense!?
  15. Hi @Barand Thank you for your reply (as always!). May I ask, what does the double question mark mean ('var_id' => $var['variation_id'] ?? '',) Also, as the name is unknown from product to product, rather than a name like $systems[$var['attributes']['attribute_pa_system']] is it possible use a number like: $systems[$var['attributes'][0]] Thanks again
  16. Hi 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/A However, is it possible to alter the code to group the information? So it looks like the below? System 1 System 2 System 3 System 1 XT1ECWH 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.
  17. @Barand Once again, thank you so much!
  18. Hi @Barand That worked a charm! $results = []; foreach ($result as $year => $value) { if (!isset($results[$year])) { $results[$year] = []; } foreach ($value as $value) { if (!isset($results[$year][$value['team']])) { $results[$year][$value['team']] = 0; } $results[$year][$value['team']] += $value['value']; } } echo '<pre>', print_r($results, 1), '</pre>'; May I ask, how would I best convert that to a table output? - Sorry, cheeky I know
  19. Hi All, I'm at a loss on this and think it's something glaringly obvious. Basically, I've got the following array: rray ( [2016] => Array ( [0] => Array ( [team] => McLaren [value] => 0.4 ) [1] => Array ( [team] => McLaren [value] => 0.4 ) [2] => Array ( [team] => McLaren [value] => 0.4 ) [3] => Array ( [team] => McLaren [value] => 0.4 ) [4] => Array ( [team] => McLaren [value] => 0.4 ) [5] => Array ( [team] => McLaren [value] => 0.4 ) [6] => Array ( [team] => McLaren [value] => 0.4 ) [7] => Array ( [team] => McLaren [value] => 0.4 ) [8] => Array ( [team] => McLaren [value] => 0.4 ) [9] => Array ( [team] => McLaren [value] => 0.4 ) [10] => Array ( [team] => McLaren [value] => 1 ) [11] => Array ( [team] => McLaren [value] => 1 ) [12] => Array ( [team] => McLaren [value] => 1 ) [13] => Array ( [team] => McLaren [value] => 1 ) [14] => Array ( [team] => McLaren [value] => 1 ) [15] => Array ( [team] => McLaren [value] => 2 ) [16] => Array ( [team] => McLaren [value] => 2 ) [17] => Array ( [team] => McLaren [value] => 2 ) [18] => Array ( [team] => McLaren [value] => 2 ) [19] => Array ( [team] => McLaren [value] => 2 ) [20] => Array ( [team] => McLaren [value] => 2 ) [21] => Array ( [team] => McLaren [value] => 2 ) [22] => Array ( [team] => McLaren [value] => 3 ) [23] => Array ( [team] => McLaren [value] => 3 ) [24] => Array ( [team] => McLaren [value] => 10 ) [25] => Array ( [team] => McLaren [value] => 15 ) [26] => Array ( [team] => McLaren [value] => 25 ) [27] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [28] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [29] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [30] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [31] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [32] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [33] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [34] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [35] => Array ( [team] => Red Bull Racing [value] => 0.75 ) [36] => Array ( [team] => Red Bull Racing [value] => 0.75 ) [37] => Array ( [team] => Red Bull Racing [value] => 0.75 ) [38] => Array ( [team] => Red Bull Racing [value] => 1 ) [39] => Array ( [team] => Red Bull Racing [value] => 1 ) [40] => Array ( [team] => Red Bull Racing [value] => 1 ) [41] => Array ( [team] => Red Bull Racing [value] => 1 ) [42] => Array ( [team] => Red Bull Racing [value] => 3 ) [43] => Array ( [team] => Red Bull Racing [value] => 3 ) [44] => Array ( [team] => Red Bull Racing [value] => 5 ) [45] => Array ( [team] => Red Bull Racing [value] => 5 ) [46] => Array ( [team] => Red Bull Racing [value] => 5 ) [47] => Array ( [team] => Red Bull Racing [value] => 7 ) [48] => Array ( [team] => Red Bull Racing [value] => 8 ) [49] => Array ( [team] => Red Bull Racing [value] => 20 ) [50] => Array ( [team] => Red Bull Racing [value] => 30 ) ) [2017] => Array ( [0] => Array ( [team] => McLaren [value] => 0.2 ) [1] => Array ( [team] => McLaren [value] => 0.4 ) [2] => Array ( [team] => McLaren [value] => 0.4 ) [3] => Array ( [team] => McLaren [value] => 0.4 ) [4] => Array ( [team] => McLaren [value] => 0.4 ) [5] => Array ( [team] => McLaren [value] => 0.4 ) [6] => Array ( [team] => McLaren [value] => 0.4 ) [7] => Array ( [team] => McLaren [value] => 0.4 ) [8] => Array ( [team] => McLaren [value] => 0.4 ) [9] => Array ( [team] => McLaren [value] => 0.4 ) [10] => Array ( [team] => McLaren [value] => 0.5 ) [11] => Array ( [team] => McLaren [value] => 1 ) [12] => Array ( [team] => McLaren [value] => 1 ) [13] => Array ( [team] => McLaren [value] => 1 ) [14] => Array ( [team] => McLaren [value] => 1 ) [15] => Array ( [team] => McLaren [value] => 1 ) [16] => Array ( [team] => McLaren [value] => 1 ) [17] => Array ( [team] => McLaren [value] => 1 ) [18] => Array ( [team] => McLaren [value] => 2 ) [19] => Array ( [team] => McLaren [value] => 2 ) [20] => Array ( [team] => McLaren [value] => 3 ) [21] => Array ( [team] => McLaren [value] => 3 ) [22] => Array ( [team] => McLaren [value] => 3 ) [23] => Array ( [team] => McLaren [value] => 3 ) [24] => Array ( [team] => McLaren [value] => 5 ) [25] => Array ( [team] => McLaren [value] => 10 ) [26] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [27] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [28] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [29] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [30] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [31] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [32] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [33] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [34] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [35] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [36] => Array ( [team] => Red Bull Racing [value] => 1 ) [37] => Array ( [team] => Red Bull Racing [value] => 1 ) [38] => Array ( [team] => Red Bull Racing [value] => 1 ) [39] => Array ( [team] => Red Bull Racing [value] => 1 ) [40] => Array ( [team] => Red Bull Racing [value] => 3 ) [41] => Array ( [team] => Red Bull Racing [value] => 3 ) [42] => Array ( [team] => Red Bull Racing [value] => 4 ) [43] => Array ( [team] => Red Bull Racing [value] => 5 ) [44] => Array ( [team] => Red Bull Racing [value] => 7 ) [45] => Array ( [team] => Red Bull Racing [value] => 8 ) [46] => Array ( [team] => Red Bull Racing [value] => 20 ) [47] => Array ( [team] => Red Bull Racing [value] => 30 ) ) [2018] => Array ( [0] => Array ( [team] => McLaren [value] => 0.25 ) [1] => Array ( [team] => McLaren [value] => 0.4 ) [2] => Array ( [team] => McLaren [value] => 0.4 ) [3] => Array ( [team] => McLaren [value] => 0.4 ) [4] => Array ( [team] => McLaren [value] => 0.4 ) [5] => Array ( [team] => McLaren [value] => 0.4 ) [6] => Array ( [team] => McLaren [value] => 0.4 ) [7] => Array ( [team] => McLaren [value] => 0.4 ) [8] => Array ( [team] => McLaren [value] => 0.4 ) [9] => Array ( [team] => McLaren [value] => 0.4 ) [10] => Array ( [team] => McLaren [value] => 0.4 ) [11] => Array ( [team] => McLaren [value] => 0.5 ) [12] => Array ( [team] => McLaren [value] => 0.5 ) [13] => Array ( [team] => McLaren [value] => 1 ) [14] => Array ( [team] => McLaren [value] => 1 ) [15] => Array ( [team] => McLaren [value] => 1 ) [16] => Array ( [team] => McLaren [value] => 1 ) [17] => Array ( [team] => McLaren [value] => 1 ) [18] => Array ( [team] => McLaren [value] => 2 ) [19] => Array ( [team] => McLaren [value] => 2 ) [20] => Array ( [team] => McLaren [value] => 3 ) [21] => Array ( [team] => McLaren [value] => 3 ) [22] => Array ( [team] => McLaren [value] => 3 ) [23] => Array ( [team] => McLaren [value] => 4 ) [24] => Array ( [team] => McLaren [value] => 5 ) [25] => Array ( [team] => McLaren [value] => 5 ) [26] => Array ( [team] => McLaren [value] => 10 ) [27] => Array ( [team] => McLaren [value] => 10 ) [28] => Array ( [team] => Red Bull Racing [value] => 0.2 ) [29] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [30] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [31] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [32] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [33] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [34] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [35] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [36] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [37] => Array ( [team] => Red Bull Racing [value] => 0.5 ) [38] => Array ( [team] => Red Bull Racing [value] => 1 ) [39] => Array ( [team] => Red Bull Racing [value] => 1 ) [40] => Array ( [team] => Red Bull Racing [value] => 1 ) [41] => Array ( [team] => Red Bull Racing [value] => 1 ) [42] => Array ( [team] => Red Bull Racing [value] => 1 ) [43] => Array ( [team] => Red Bull Racing [value] => 3 ) [44] => Array ( [team] => Red Bull Racing [value] => 4 ) [45] => Array ( [team] => Red Bull Racing [value] => 5 ) [46] => Array ( [team] => Red Bull Racing [value] => 7 ) [47] => Array ( [team] => Red Bull Racing [value] => 20 ) [48] => Array ( [team] => Red Bull Racing [value] => 30 ) [49] => Array ( [team] => Red Bull Racing [value] => 40 ) ) ) I then have the following code which outputs a table: $total_value_by_team = array(); foreach ($result as $year => $total) { $runningSum = 0; foreach($total as $key => $value){ if (is_numeric ($value['value'])): $runningSum += $value['value']; else: $runningSum += '0'; endif; $total_value_by_team[] = array( $value['team'], $year, $runningSum); } } $team_names = array(); foreach($total_value_by_team as $record): if(!in_array($record[0], $team_names)) : $team_names[] = $record[0]; endif; endforeach; sort($team_names); #Loop through records to create structured array $outputData = array(); foreach($total_value_by_team as $record): if(!isset($outputData[$record[1]])) : $outputData[$record[1]] = array_fill_keys($team_names, ''); endif; $outputData[$record[1]][$record[0]] = $record[2]; endforeach; #Create headers for table $headers = "<th></th>\n"; foreach($team_names as $team_name): $headers .= "<th>{$team_name}</th>\n"; endforeach; #Create the content for output $output = ''; foreach($outputData as $team => $team_names): $output .= "<tr>\n"; $output .= "<th>{$team}</th>\n"; foreach($team_names as $team_name => $value): $output .= "<td>{$value}</td>\n"; endforeach; $output .= "</tr>\n"; endforeach; This then outputs the following results table: Year | McLaren | Red Bull Racing 2016 | 79 | 175.25 2017 | 42.3 | 131.3 2018 | 57.25 | 175.95 The results for McLaren are correct but the Reb Bull Racing ones are totally out They should be (for 2016 - 2018 respectively). 96.25 89 118.7 It seems to be adding McLaren to Red Bull? Any help is much appreciated!
  20. Hi, How do you prevent the following happening: Array ( [] => Array So, effectively I want to remove that one from the main array. i.e. Array ( [] => Array ( [0] => Japanese GP [1] => Japanese GP [2] => Williams ) [2005] => Array ( [0] => Japanese GP [1] => Williams ) [2006] => Array ( [0] => Japanese GP ) ) Becomes: Array ( [2005] => Array ( [0] => Japanese GP [1] => Williams ) [2006] => Array ( [0] => Japanese GP ) ) I tried using: if (!empty($year['value'])): Within my foreach but sadly that didn't work Thanks
  21. @Barand Once again, many thanks for your time, assistance and patience. It has been very much appreciated!!
  22. Hi @Barand, I realised I had sort($group); which was screwing the results, removing this now shows exactly what I needed: Year: 2011 Total: 20 Year: 2012 Total: 35 Year: 2013 Total: 35 Year: 2014 Total: 45 Year: 2015 Total: 19 Year: 2016 Total: 19 Year: 2017 Total: 10 Year: 2018 Total: 12 Thank you once again for all your help
  23. Ok thanks @Barand Just to say, using your code: foreach ($group as $year => $total) { echo "Year: $year Total: $total<br>"; } Outputs the same as my code: Year: 0 Total: 10 Year: 1 Total: 12 Year: 2 Total: 19 Year: 3 Total: 19 Year: 4 Total: 20 Year: 5 Total: 35 Year: 6 Total: 35 Year: 7 Total: 45 So not sure what you mean? All I'm trying to do is about the above but replacing the Year: 0 with Year 2011 (for example)
  24. Hi @Barand, I had: foreach ($group as $k => $v) { echo "\$group[$k] => $v.\n"; } Which looks similar, but I can't get the year to show. I simply get: Year: 0 Total: 10 Year: 1 Total: 12 Year: 2 Total: 19 Year: 3 Total: 19 Year: 4 Total: 20 Year: 5 Total: 35 Year: 6 Total: 35 Year: 7 Total: 45 The key doesn't seem to contain the year, or once again, am I missing the obvious? I truly appreciate your help thought!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.