Undefined offset: 5 in [file location] on line 22

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JayEm
    New Member
    • Feb 2013
    • 2

    #1

    Undefined offset: 5 in [file location] on line 22

    Code:
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title></title>
        </head>
        <body>
            <?php
            $table = array ("1", "2", "3", "4", "5");
            $count = count($tablet); 
            echo "<table border='1'>";
            $rows = 6;
            for($i=0; $i <= $count; $i = $i + $rows)
            {
                echo "<tr>";
                for($z = 0; $z < $rows; $z++)
                {
                    if ($table[$i + $z] !=0)
                        echo "<td>This is row {$table[$i + $z]}</td></tr>";
                    else
                        echo "<td>&nbsp;</td></tr>";
                }
            }
            echo "</table>";
            ?>
        </body>
    </html>
    That's the code, but when I run it, all else is viable, but, the following pops up after the table:
    Code:
    Notice: Undefined offset: 5 in [file location] on line 22
    Sorry for being a noob but I haven't found answers so far...
  • Michal Gawlas
    New Member
    • Aug 2011
    • 4

    #2
    Arrays are indexed starting with 0. Consequently, your array initialization has populated indexes 0 through 4:
    Code:
    $table = array ("1", "2", "3", "4", "5");
    is equivalent to
    Code:
    $table[0]="1";
    $table[1]="2";
    $table[2]="3";
    $table[3]="4";
    $table[4]="5";
    Except you're trying to access index number 5, which does not exist:
    Code:
    $rows = 6;
    for($z = 0; $z < $rows; $z++)
        if ($table[$i + $z] !=0)
          (snip)
    That loop will be executed for $z values 0 through 5, and that's assuming $i remains 0.

    There isn't a nice way of telling you that, but the code you posted doesn't make sense in some parts. What exactly are you trying to achieve with it? What is $taulukko supposed to be?

    Comment

    • JayEm
      New Member
      • Feb 2013
      • 2

      #3
      oops, forgot to translate that $table over there.
      What I'm trying to achieve is to create a table that creates each row with the same text and calling it the row it is.

      EDIT: Managed to solve it somehow :_D

      Comment

      • Michal Gawlas
        New Member
        • Aug 2011
        • 4

        #4
        If that's all you need, a single for loop will be sufficient:
        Code:
        <!DOCTYPE html>
        <html>
        	<head>
        		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        		<title></title>
        	</head>
        	<body>
        		<table border='1'>
        		<?php
        		$rows = 6;
        		for($i=0; $i < $rows; $i++)
        			echo "<tr><td>This is row {$i + 1}</td></tr>";
        		?>
        		</table>
        	</body>
        </html>

        Comment

        Working...