This topic is locked

List last record

2/10/2008 2:22:56 PM
PHPRunner General questions
J
jclout author

On the add page I have a field called job_number I would like to show the last number that was entered or the biggest number that has been added in a label next to the text field. Not really sure how to do this. Can anyone help me??

A
alang 2/10/2008

We have a couple of similar situations where we want to add a new record with the default number being one more than the largest already in the database. Typically this would be done as follows:

  1. Use "Before Display" event on the Add page
  2. Query the database to find largest number: $rs=db_query("SELECT max(<yourfield>) FROM table",$conn); $row=db_fetch_numarray($rs);
  3. Add 1 to the result: $<your temp var> = $row[0]+1;
  4. Update the value to be displayed: $smarty->assign("value_<yourfield>,$<your temp var>")

J
jclout author 2/11/2008

I must be doing something wrong.
I get an error that conn is not declared and when I declare it it tells me that conn is already declared.

I am getting the same error whenever I try to add my own code in. Is it me or my server??

A
alang 2/11/2008

You do need to declare $conn in each function you want to use it in at the beginning. ie
global $conn;

J
jclout author 2/11/2008

Thanks for the help. I got it working now.
For those who need this this is what I did:
Table - jobs

Field - job_number

My Variable - $nxtjobnum
****

function BeforeShowAdd(&$smarty,&$templatefile)

{

global $conn;

$rs=db_query("SELECT max(job_number) FROM jobs",$conn);

$row=db_fetch_numarray($rs);

$nxtjobnum = $row[0]+1;

$smarty->assign('value_job_number',$nxtjobnum);

} // function BeforeShowAdd

****
You can call this from inside from inside the .htm file using:
{$nxtjobnum}