Hi all,

Any ideas on how i can calculate the position that a crew came?

I have managed to do it basically by sorting by event and then time and then counting the crews in order. This works fine.

But if I then sort the data by just time, results do not come out correctly. This was the initial code that i used;

if ($eventid == ''){
				$eventid = $row_view['results_eventid'];
				$counter = '0';

			}
		if ($eventid == $row_view['results_eventid']){
			$counter = $counter +1;
			}
		elseif ($eventid != $row_view['results_eventid']){
			$eventid = $row_view['results_eventid'];

			$counter = 1;
			}

I need to get a seperate counter for each event if possible.

Thanks,
James

    i dont really know what your problem is but if you need a seperate counter for each event i would say use a array?

      I tried using a array;

        <?php do {
      	if ($eventid == ''){
      		$eventid = $row_view['results_eventid'];
      		$counter = array($eventid,'0');
      		}
      	if ($eventid == $row_view['results_eventid']){
      		$counter = array($eventid,$counter[$eventid]+1)
      		}
      	elseif ($eventid != $row_view['results_eventid']){
      		$eventid = $row_view['results_eventid'];
      		$counter = array($eventid, '1');
      	        }
      		?>
      

      Am i just implementing this incorrectly?
      Thanks

        first of all, i dont have alot of php experience, im more of a java programmer
        but wouldnt this be better?

        	counter = array();
        	if ($eventid == ''){
        		$eventid = $row_view['results_eventid'];
        		$counter[$eventid] = 0;
        		}
        	if ($eventid == $row_view['results_eventid']){
        		$counter[$eventid]++;
        		}
        	elseif ($eventid != $row_view['results_eventid']){
        		$eventid = $row_view['results_eventid'];
        		$counter[$eventid] = 1;
        	        }
        

          That works great when the results are sorted in event order. but does not work when the results are not posted in order.

          This is the code that im using in full;

             <?php 
          		$counter = array();
          		$eventid = '';?>
                  <?php do {
          			if ($eventid == ''){
          				$eventid = $row_view['results_eventid'];
          				$counter[$eventid] = 0;
          			//	$counter = '0';
          
          			}
          		if ($eventid == $row_view['results_eventid']){
          			$counter[$eventid]++;
          		//	$counter = $counter +1;
          			}
          		elseif ($eventid != $row_view['results_eventid']){
          			$eventid = $row_view['results_eventid'];
          			$counter[$eventid] = 1;
          
          		//	$counter = 1;
          			}
          	?>
          
                <div id="crew<?php echo $row_view['results_crewid']; ?>" <?php if ($counter.$eventid =='1'){?> class="winner"<?php }?>>
          
                    <div class="crewno <?php if ($counter.$eventid =='1'){echo " winner";}?>"><a href="#" name="crewno<?php echo $row_view['results_crewid'];?>" id="raceno<?php echo $row_view['results_crewid'];?>" onclick="showdetailedresults(<?php echo $id;?>,<?php echo $row_view['results_crewid']; ?>)" title="Detailed Results"><img src="images/detailed_icon.jpg" class="img_detail" alt="Detailed Results" /></a> <?php echo $row_view['results_crewid']; ?></div>
                  <div class="event <?php if ($counter.$eventid =='1'){echo " winner";}?>"><?php echo $row_view['results_eventid']; ?></div>
                  	<div class="club <?php if ($counter.$eventid =='1'){echo " winner";}?>"><?Php echo $row_view['club_name'];?></div>
                    <div class="time<?php if ($counter.$eventid =='1'){echo " winner";}?>"><?php echo $row_view['results_time']; ?></div>
                    <div class="position <?php if ($counter.$eventid =='1'){echo " winner";}?>"><?php if ($counter== '1'){ echo "<strong>Winner</strong>";}else { echo $counter[$eventid];}?></div>
          
                    </div>
                <?php } while ($row_view = mysql_fetch_assoc($view)); ?></div>

          When i print the array it only seems to add the counter up when the events are listed after each other;
          IE;
          Event | TIME | POSITION
          1 | 2.22.79 | 1
          1 | 2.38.95 | 2
          4 | 3.44.11 | 1
          4 | 3.45.64 | 2
          2 | 4.3.97 | 1
          3 | 4.5.83 | 1
          2 | 4.8.54 | 1
          3 | 4.24.04 | 1
          2 | 4.24.48 | 1

          So we can see that the event #2 & #3 does not seem to have addded to the counter.

          Any ideas as to why?
          Thanks

            yes i do 🙂

            the problem lies in your elseif

            	elseif ($eventid != $row_view['results_eventid']){
            				$eventid = $row_view['results_eventid'];
            				$counter[$eventid] = 1;
            }
            
            

            what happens:
            first time event 2 comes (after 4)
            elseif (4 != 2){
            $eventid = 2;
            $counter[2] = 1;
            }

            now 3 comes
            elseif (2 != 3){
            $eventid = 3;
            $counter[3] = 1;
            }

            now 2 comes again....
            elseif (3 != 2){
            $eventid = 2;
            $counter[2] = 1; <-----
            }

            so you need to check if there is already a value in $counter[$eventid]
            if so, add, if not it has to be 1.

            hope this helps

              Legend, good spot,

              works like a treat now. many thanks for your assistance.

              James

                Write a Reply...