Jump to content

Undefined offset: -1 with PDO in for loop


tazeha

Recommended Posts

Hello

 

when I run my code give error Undefined offset: -1 in 3 line :

 

 

if($pays[$i]['Amount'] < $pays[($i-1)]['Amount'])

 

elseif($pays[$i]['Amount'] > $pays[($i-1)]['Amount'])

 

 

.$pays[$i]['price'].

<?php            $params =null; //or any params            $mrkfPDO =  new PDO('mysql:host=localhost;dbname=YOUR_DB_NAME', 'DB_USER', 'DB_PASS',                array(                    PDO::ATTR_PERSISTENT => true,                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"                )            );            $mrkfStatement = $mrkfPDO->prepare("SELECT Date, Amount FROM payments limit 0,100");            $mrkfStatement->execute($params);            $pays = $mrkfStatement->fetchAll(PDO::FETCH_ASSOC);            for($i=0; $i<=count($pays); $i++)            {                $color = 'black';                if($pays[$i]['Amount'] < $pays[($i-1)]['Amount'])                    $color="red";                elseif($pays[$i]['Amount'] > $pays[($i-1)]['Amount'])                    $color = 'green';                else                    $color = 'black';                echo "<tr>                        <td>" . $pays[$i]['Date'] . "</td>                        <td>                            <span style='color: $color'>"                              .$pays[$i]['Amount'].                            "</span>                        </td>                      </tr>";              }            ?> 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...