Undefined offset: -1

I use PDO to connect the db and extract record from table’s .

I want extract only latest Record but when I run following code don’t echo anything …

SELECT * FROM uds ORDER BY price DESC LIMIT 1
my php code

<?php $params =null; //or any params $mrkfPDO = new PDO('mysql:host=localhost;dbname=usd', 'root', '', array( PDO::ATTR_PERSISTENT => true, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" ) ); $mrkfStatement = $mrkfPDO->prepare("select * from uds order by id desc limit 1 "); $mrkfStatement->execute($params); $pays = $mrkfStatement->fetchAll(PDO::FETCH_ASSOC); for($i=0; $i $pays[($i-1)]['price']) $color = 'green'; else $color = 'black'; echo " " .$pays[$i]['price']. " "; } ?>

Description code:

With curl every 1 min go to the other website and get the USD price then insert in to the database …

My db table have 2 field … id (AUTO_INCREMENT) $ price …

In top code:

If new price = latest price echo the number with back color.

because write for($i=0; …) to echo latest record but give erro Undefined offset: -1

If new price > latest price echo the number with green color.

and

If new price < latest price echo the number with red color.

Now. with for loop echo all records . I want only latest record (2000)…

when
Table

id | price
1 | 2300
2 | 2300
3 | 2301
4 | 2290
5 | 2000

I

you want to compare the current value (index 0) with it’s previous value (index –1), but that doesn’t exist for the first value in the array.

How can fix that?

only compare with the previous value, if there is one (i.e. if $i > 0).

I modifies my to this but wen run echo

$color = 'black';
 
                if($pays ['price'] < 0)
                    $color="red";
                elseif($pays ['price'] > 0)
                    $color = 'green';
                else
                    $color = 'black';
 
                echo "<tr>
                         
                        <td>
                            <span style='color: $color'>"
                              .$pays ['price'].
                            "</span>
                        </td>
                      </tr>"; 

Undefined index: price

If you’re only returning one result from your query, I can’t see how that will work because you’re not retrieving the previous price, only the latest one. If you change the query to LIMIT 2, that would give you the current and latest record so you can do the comparison.

Have you changed the way you retrieve the data from your query? In your latest code you’ve removed the reference to the count element of the array, the [$i] section, so that may cause the error you are seeing.

ok.

I don’t have idea for compare 2 latest query

Easy. Change your query to use LIMIT 2 instead of 1, this will return zero, one or two results. Then pseudo-code:

if resultcount = 0 do nothing
if resultcount = 1, set colour to black (nothing to compare to)
if resultcount = 2, set colour according to your rules
if resultcount > 0, display price
$colour = 'black';
$resultcount = count($pays);
if ($resultcount == 2) {
  if ($pays[1]['price'] > $pays[0]['price']) $colour = 'red';
  if ($pays[1]['price'] < $pays[0]['price']) $colour = 'green';
  }
if ($resultcount > 0) { // can't display with no results
 // display the price
  }

when echo

if ($resultcount > 0) { // can't display with no results
 // display the price
	echo $pays[0]['price'];
  }

echo number with black color

if ($resultcount > 0) {
  echo "<tr><td><span style='color: $color'>"  .$pays [$resultcount-1]['price'] . "</span></td></tr>";
  }

You need to echo the latest price, which will be whatever the number of rows returned is, minus 1, because you’re sorting by ID.

or just use [FPHP]end[/FPHP] for it’s intended purpose :wink:

for the record:

if(isset($pays[0])) { // We have at least 1 value.
  $start = start($pays); 
  $end = end($pays); //Note that this will be the same value as $start if only 1 record was returned.
  echo "<span style='color:".(($start['price'] < $end['price']) ? "red" : (($start['price'] > $end['price']) ? "green" : "black"))."'>".$start['price']."</span>"; 
} else { echo "No Price Data"; }

(Note that your current code does not explicitly handle the case of $pays[0][‘price’] = $pays[1][‘price’].)

If only 1 value is returned into the $pays array, $start and $end will have the same value - and so the price will be equal, and thus black. (not >, not <). This also covers the case of there being identical pricing twice in a row.

Call to undefined function Start()

color don’t changed…

er sorry, that should be reset() not start().

Thanks Thanks Thanks very very very much … work .

:joy:

For me, that was because I’d pre-set the colour variable to “black”, and only changed it if the latest was smaller or greater than the previous price. So if it was neither, it would remain at “black”. Useful to note end() function, I hadn’t seen that.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.