hi I have a very simple project to create a php/mysql contact list… Very simple , one table with 10 fields , name , address etc.
My question is, is it possible for me to effectivly have “next record” and " previous record" navigation buttons or links ie like you would get in a simple MS Access program so that users can sit and flick through record by record dispalying one record per screen(this is apparantly what they want) ?
If its actually a simple thing to do then great, if not then i suppose ill have to dust off the old copy of access. ( as you can tell im quite new to PHP)
what id want to do is take the user to a page that contains the template, and load the data into the template depending on the id.
e.g you have your db table:
id
user
email
address
etc
etc
and create a html template, containing maybe a table which displays whatever information you want to see, or input’s if you want them to be able to edit it (ive not used access before sorry) in a php page, then send a variable, say rowid as you flick through the pages… e.g
<?php
# Get Which Row You Want
$rowid = $_GET['id'];
# Change Into Integer
settype($rowid, "integer");
# Connect To Database
# Select Database Information
$result = mysql_query("SELECT * FROM table WHERE id = '$rowid'");
$row = mysql_fetch_array($result);
# Define Variables From Database
$name = $row['name'];
$email = $row['email'];
$address = $row['address'];
# .... etc etc
?>
And thats pretty much the basics… unless ive made a mistake
If not, youd probably like to add a bit more security to it, and do some checking to make sure that the row actually exists before you start tying to collect data from it!
thats great thankyou,just a few questions if I may…when you say
# Get Which Row You Want
$rowid = $_GET['id'];
What do you mean? im a little confused by this .presumably because its a $_GET youre saying id need to pass this rowid value on from an html form ? ie someone has to type a record number in?. How would I just get it to just return the first (or most recent )record in the database when going in to this view mode for the first time ?
There will indeed be a seperate data input form but ive got that bit covered already, this bit will be purely be view only
also, excuse my ignorance but what does the ?= mean before the variables, I havent seen this before?
To pass variables between forms there are a number of things you can do, the most common being form variables (where you enter data into a form, hit enter, and it then collects it) which is retreived by using the $_POST[‘var’] in PHP… the next most common being the url variable, which is where you pass a variable in the url of the page (e.g http://domain.com/page.php?var=value) which is retreived by using the $_GET[‘var’] in PHP.
the ? at the end of the url is telling you that you are sending url variables to that page, so youd enter page.php?variable=value
In our case we send the variable rowid with the value of the previous rowid +/- 1, by using $_SERVER[‘PHP_SELF’] . “?rowid=” . $rowid++/–;
The $_SERVER[‘PHP_SELF’] just assigns the value of the page your currently on… so it may as well say “page.php?rowid=rowvalue”.
If someone is viewing the page for the first time, youll want to add an if statement… such as:
<?php
# Get Which Row You Want
$rowid = $_GET['id'];
# See If Rowid Is Set, If Rowid Is Not Set, Set It To The First Result (if you wanted to set it to the last result entered youd want to run a mysql statement)
# e.g
# $query = mysql_query("SELECT id FROM table ORDERBY id LIMIT 1");
# $row = mysql_fetch_array['$query);
# $rowid = $row['id'];
#
# and put that inside the if statement instead of the $rowid = 1;
if (is_null($rowid))
{
$rowid = 1;
}
?>
Just out of interest when I load this page for the first time, the $rowid variable wont contain a value and hence the table wont contain any data and the navigation links at the bottom wont have a value to increment or decrement?
Im guessing that the link that loads this page must containan initial value for the $rowid variable
Ie and html page with a link ie “view database” which points to
page.php?$rowid=1
??? Is this correct?
Intersetingly rather than sending the page an initial static value for $rowid when its first launched, is there a way I can get it to give me the id of the last record in the table (ie the most recent)
would set the rowid to 1, if the page is viewed for the first time, so the next/prev links would have values based on that.
As i wrote in the comments above that loop, you would want to replace that with a variable you retreived from the database… e.g… as apposed to the above, in order to star with the last record added.
if (is_null($rowid))
{
$query = mysql_query("SELECT count(id) FROM `table`");
$row = mysql_fetch_array($query);
$rowid = (int)$Row[0];
}