Trying to get the last value in mysql fetch arry

Hello,
I'm new to php and learning from places like this, so please be patient.

I''m working on an application where a list of items are displayed on the page, ordered by a sequence number (seq).  I have up/down buttons on each record so that users can change the order of the items.  But before I get to that code, I first want to check for the first record and hide the UP button, and then check the last record and hide the DOWN button.  

Below is my attempt to start by finding the first and last records. First I created a new array to contain all my sequence numbers at once (otherwise with PHP I can only fetch one row at a time, right?)   The Min function works nicely to find the first value, but the END function does not seem to be working.  I've been trying to follow advise given at http://www.php.net/end, but I'm not getting anywhere - suspect there is something basic I don't understand?

Sure would appreciate some expert help!
Thx.
<?php
 
$queryAllSeqs = ("SELECT * FROM selections WHERE collectID=3 ORDER BY seq ASC");
mysql_select_db($database_connWebCMS, $connWebCMS);
$rsAllSeqs = mysql_query($queryAllSeqs, $connWebCMS) or die(mysql_error());
$number_of_recs = mysql_num_rows($rsAllSeqs);
echo "number_of_recs is this: $number_of_recs <br />";
 
$new_array = array();
while($row = mysql_fetch_assoc($rsAllSeqs)){
  $new_array[] = $row["seq"];
}
mysql_free_result($rsAllSeqs);
 
/* this shows my new array 1, 2, 3
foreach ($new_array as $seq){
   echo $seq . "<br>";
}*/
 
$start = min($new_array);
echo "start is this: $start <br />";
//reset($new_array);
$end = end($new_array);
echo "last is this: $last <br />";
//reset($new_array);
?>

Open in new window

samiam80304Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
see attached code
<?php
$queryAllSeqs = ("SELECT * FROM selections WHERE collectID=3 ORDER BY seq ASC");
mysql_select_db($database_connWebCMS, $connWebCMS);
$rsAllSeqs = mysql_query($queryAllSeqs, $connWebCMS) or die(mysql_error());
$number_of_recs = mysql_num_rows($rsAllSeqs);
echo "number_of_recs is this: $number_of_recs"; 
//to obtain first seq, extract first row
$row = mysql_fetch_assoc($rsAllSeqs);
$first =$row['seq']; 
//then advance pointer to last record;
mysql_data_seek($rsAllSeqs,($number_of_recs-1)); 
//extract last record
$row = mysql_fetch_assoc($rsAllSeqs);
$last = $row['seq']; 
//now you can reset the pointer to the beginning of the recordset
//NOTE: if you want to skip to the second record use mysql_data_seek($rsAllSeqs,1);
mysql_data_seek($rsAllSeqs,0); 
 
/* this shows my new array 1, 2, 3
foreach ($new_array as $seq){
   echo $seq . "<br>";
}*/
  
echo "start is this: $start ";
//reset($new_array); 
//here you can iterate over all the results if you wish
while( $row = mysql_fetch_assoc($rsAllSeqs) )
{
	echo $row['seq']."<br />";
}
echo "last is this: $last ";
//reset($new_array); 
mysql_free_result($rsAllSeqs);
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
samiam80304Author Commented:
Thank you for the quick and functional reply.  You made my day.
0
samiam80304Author Commented:
Hi again,
Solution is perfect, but I wonder about the PHP END function.  Was I using it wrong or does it not work as advertised in documentation?
Thanks again!
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

hieloCommented:
>>Was I using it wrong
Yes

>>or does it not work as advertised in documentation?
It does work. You are either not understanding the function OR not understanding what mysql_query does.

From the manual:
"end  Set the internal pointer of an array to its last element"

Clearly it is meant to work on an array. When you do:
$result = mysql_query("SELECT id, firstname FROM Table");

$result is NOT an array. It IS a resource - an identifier that points to the result set. So you cannot use end() with $result because $result is NOT an array.

If you do:
$result = mysql_query("SELECT id, firstname FROM Table");
$row = mysql_fetch_assoc($result);

then $row IS an array, but it contains only the first row. If your SELECT returned this:
1 john
2 mary
3 sally
4 pete

when you do this the first time:
$row = mysql_fetch_assoc($result);
then $row is an array that contains
1 john

so end($row) would give you john. What you need to do was to advance $result not $row.
0
samiam80304Author Commented:
excellent - that explanation helps alot.  Thanks agian.
0
hieloCommented:
you are welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.