[SOLVED] Accessing the first and last row values
Posted: Thu Jan 15, 2004 1:44 pm
This may seem simple to most but I've been banging around for hours and can't figure it out.
How do I assign a variable to the first and last row only of an array returned from MySQL? I can count the rows, I can print the reults of the array, but I can't figure out the code to say that the first result in the array = $first and the last result = $last.
What I have so far is:
This SELECT will produce a varying number of rows in different situations. However, one value is retrieved in each row and is assigned to $var.
Now I can print out each $var no problem, but what I want to do is take the first $var and the last $var and do some math on them.
$var[0] and $var[7] (or $var [$num-1]) aren't accessable - should I be doing something else inside the WHILE? Can I not access the contents of $var outside of the WHILE?
It seems that it should be simple to access the individual row values of an array, but it is escaping me - especially aggravating because I can print them by echoing $var inside the WHILE.
Thanks,
gord
How do I assign a variable to the first and last row only of an array returned from MySQL? I can count the rows, I can print the reults of the array, but I can't figure out the code to say that the first result in the array = $first and the last result = $last.
What I have so far is:
Code: Select all
$sum_sql = " SELECT SUM( snapshot.unit_value * snapshot.no_units ) AS sum"
. " FROM snapshot, snapdate"
. " WHERE security_id = 7 AND snapshot.date_id = snapdate.date_id"
. " GROUP BY snapdate.date_id"
. " ORDER BY snapdate.date_id DESC";
$sum_result = MySQL_query($sum_sql);
$num = mysql_numrows($sum_result);
$i=0;
while ($i < $num) {
$total = mysql_result($sum_result,$i,"sum");
++$i;
$var = $total;
}Now I can print out each $var no problem, but what I want to do is take the first $var and the last $var and do some math on them.
$var[0] and $var[7] (or $var [$num-1]) aren't accessable - should I be doing something else inside the WHILE? Can I not access the contents of $var outside of the WHILE?
It seems that it should be simple to access the individual row values of an array, but it is escaping me - especially aggravating because I can print them by echoing $var inside the WHILE.
Thanks,
gord