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?
 
hieloConnect With a Mentor Commented:
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
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.