Results 1 to 6 of 6

Thread: The last entry causes an error

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    The last entry causes an error

    hi Guys. So I have a list of football matches, that are in certain tournaments, on certain dates - like this:
    date
    tournament
    match
    match
    date
    tournament
    match
    date
    tournament
    match


    So you see the number of matches in a tournament can change and the number of tournaments on a date can change - sometimes no tournaments/matches at all.
    I have managed to grab everything from the page, and I have stored it all in arrays. I can tell whether and object is an item or a date by the html formatting around it - so thats easy. I now have arrays of items and dates.
    however I am now trying to reprint everything on another page in the correct order from my arrays:
    Code:
    foreach($dayKeys as $a => $b)
    	{
    	echo "<table>";
    	
    		//echo "<b>" .strip_tags($matches[$dayKeys[$a]])."</b><br>";
    		$c = $a;
    		$d_check = 0;//checks whether day has been printed
    		foreach($tournKeys as $c => $d)
    		{//count number of tournaments
    			if (($tournKeys[$c] > $dayKeys[$a]) && ($tournKeys[$c] < $dayKeys[$a + 1]))
    			{
    					$e = $c;
    					$t_check = 0;//checks whether tournament has been printed
    					foreach($gameKeys as $e => $f)
    					{//count number of tournaments
    					
    						if (($gameKeys[$e] > $tournKeys[$c]) && ($gameKeys[$e] < $tournKeys[$c + 1]))
    						{
    the array that stores everything - regarless of whether it is a date/tournament/match is called $matches.
    Everything works perfectly, except for the last date - because no dates follow it
    so therefore the condition:
    Code:
    if (($tournKeys[$c] > $dayKeys[$a]) && ($tournKeys[$c] < $dayKeys[$a + 1]))
    fails because of $dayKeys[$a + 1] - there isnt a $dayKeys[$a + 1].
    and the same goes for
    Code:
    if (($gameKeys[$e] > $tournKeys[$c]) && ($gameKeys[$e] < $tournKeys[$c + 1]))
    						{
    however to print the list in the correct order again, I need to know whether the current match in the for loop comes before or after the next tournament heading, and also whether that comes before or after the next date heading.
    In other words, can I write this sort of thing:
    $tournKeys[$c + 1]
    another way so that if it is the last on the list, then it doesnt crash..?

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: The last entry causes an error

    You could add an alternative condition in your if statement:
    PHP Code:
    if ( (count($dayKeys) == $a+1) || ($tournKeys[$c] > $dayKeys[$a] && $tournKeys[$c] < $dayKeys[$a 1]) ) 
    That will evaluate to true if it's at the last item in $dayKeys.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: The last entry causes an error

    Thanks but I still get the error if I replace my condition with yours.

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: The last entry causes an error

    Then maybe a while():
    PHP Code:
    while(count($dayKeys) > $a+1){
      if ((
    $tournKeys[$c] > $dayKeys[$a]) && ($tournKeys[$c] < $dayKeys[$a 1]))
      { ... }


  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: The last entry causes an error

    Hmm, it still gives me an Undefined offset: error.
    This is more irratating than I thought it would be. There is a command next() for arrays isnt there? should I be using that or something? What about another if loop. Ie something like If($dayKeys[$a + 1] exists)
    although Im not sure how to do an "exists" check...

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: The last entry causes an error

    Actually your first solution solves the error probel:
    Code:
    if ( (count($dayKeys) == $a+1) || ($tournKeys[$c] > $dayKeys[$a] && $tournKeys[$c] < $dayKeys[$a + 1]) )
    However when it is the last one it prints the list again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width