Home > Archive > PHP SQL > October 2005 > Sample of navigation bar (PHP & MySQL)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Sample of navigation bar (PHP & MySQL)
|
|
|
| Hello to all
I like to put on the bottom of some screen a navigation bar
with the following button:
next previous new delete
Could somebody send me a sample script?
Many thanks in advance for your help.
Otto
| |
| Stefan Rybacki 2005-10-10, 6:58 pm |
| Otto wrote:
> Hello to all
>
> I like to put on the bottom of some screen a navigation bar
> with the following button:
> next previous new delete
>
> Could somebody send me a sample script?
<?php
print('<a href="next.htm">next</a><a href="prev.htm">previous</a><a
href="new.htm">new</a><a href="delete.htm">delete</a>');
?>
Regards
Stefan
>
> Many thanks in advance for your help.
>
> Otto
>
| |
|
| Hello Stefan
> <?php
>
> print('<a href="next.htm">next</a><a href="prev.htm">previous</a><a
> href="new.htm">new</a><a href="delete.htm">delete</a>');
>
> ?>
>
I don't have problem with this. I'm looking for a good sample with
PHP & MySQL source for the mentioned buttons.
I already did it for the new save and delete button but have some
problem with next and previous. It would be nice if I could get all in
one script!
Someting like that:
// new record
if($mode == "new")
{
$query = "INSERT INTO ...";
$req = mysql_query($query);
$id = mysql_insert_id();
}
// save record
if($mode == "save")
{
$id = $last_id;
$query = "UPDATE ....
$req = mysql_query($query);
}
// Delete record
if($mode == "Delete")
{
$id = DeleteEnr($last_id, $famille);
$query = "DELETE FROM adresse WHERE ID=$id";
$req = mysql_query($query);
}
// Next record
if($mode == "next")
{
}
// Previous record
if($mode == "previous")
{
}
// Check passed ID
if ($ID && !$id){$id = $ID;}
// Query ID
$query = "SELECT FROM .. WHERE ID=$id order by name";
$req = mysql_query($query);
// Form
?>
Form top
...
...
...
...
Form bottom
Button on the bottom of the screen
<input type='submit' name=mode value='next'>
<input type='submit' name=mode value='previous'>
<input type='submit' name=mode value='save'>
<input type='submit' name=mode value='delete'>
<input type=hidden name=last_id value='<?= $id ?>'>
</form>
</body></html>
Do you think that's possible ?
Regards
Otto
| |
| Stefan Rybacki 2005-10-11, 6:57 pm |
| Otto wrote:
> Hello Stefan
>
>
> I don't have problem with this. I'm looking for a good sample with
> PHP & MySQL source for the mentioned buttons.
>
> I already did it for the new save and delete button but have some
> problem with next and previous. It would be nice if I could get all in
> one script!
>
> Someting like that:
>
> // new record
> if($mode == "new")
> {
> $query = "INSERT INTO ...";
> $req = mysql_query($query);
> $id = mysql_insert_id();
> }
>
> // save record
> if($mode == "save")
> {
> $id = $last_id;
> $query = "UPDATE ....
> $req = mysql_query($query);
> }
>
> // Delete record
> if($mode == "Delete")
> {
> $id = DeleteEnr($last_id, $famille);
> $query = "DELETE FROM adresse WHERE ID=$id";
> $req = mysql_query($query);
> }
>
>
> // Next record
> if($mode == "next")
> {
> }
>
> // Previous record
> if($mode == "previous")
> {
> }
>
>
> // Check passed ID
> if ($ID && !$id){$id = $ID;}
>
> // Query ID
> $query = "SELECT FROM .. WHERE ID=$id order by name";
> $req = mysql_query($query);
>
> // Form
> ?>
>
> Form top
> ..
> ..
> ..
> ..
> Form bottom
>
> Button on the bottom of the screen
>
> <input type='submit' name=mode value='next'>
> <input type='submit' name=mode value='previous'>
> <input type='submit' name=mode value='save'>
> <input type='submit' name=mode value='delete'>
> <input type=hidden name=last_id value='<?= $id ?>'>
>
> </form>
> </body></html>
>
>
> Do you think that's possible ?
>
Look at the following discussion, we had that already, maybe it helps you.
http://groups.google.com/group/alt....f053fb2a6da0bd6
http://groups.google.com/group/alt....5282c4333c7081a
Regards
Stefan
PS: it is obvious that my answer is more a joke than an decent answer, it came because of
your non saying question. You didn't provide any information of what you're trying to do.
So for future question please try to explain a bit what you're trying to achieve, not just
saying I want next, prev and delete buttons.
> Regards
>
> Otto
>
| |
|
| Hello Stefan
> Look at the following discussion, we had that already, maybe it helps you.
>
Yes I'm reminder about that. But until yet I was not able to resolve my
problem.
I know, that I'm asking about the same. I tried find a solution by myself.
But I think if I will find a good sample, it would help me a lot.
Once again, Many thanks for your help.
Otto
| |
| Hilarion 2005-10-13, 9:55 pm |
| >> Look at the following discussion, we had that already, maybe it helps you.
>
> Yes I'm reminder about that. But until yet I was not able to resolve my
> problem.
> I know, that I'm asking about the same. I tried find a solution by myself.
> But I think if I will find a good sample, it would help me a lot.
I think we allready posted samples in previous discussion:
http://groups.google.com/group/alt....f053fb2a6da0bd6
<quote>
previous:
SELECT *
FROM adresse
WHERE (nom < 'Dupont') OR (nom = 'Dupont' AND id < 123)
ORDER BY nom DESC, id DESC
LIMIT 1
next:
SELECT *
FROM adresse
WHERE (nom > 'Dupont') OR (nom = 'Dupont' AND id > 123)
ORDER BY nom ASC, id ASC
LIMIT 1
</quote>
PHP example (assuming you are using "ORDER BY nom ASC, id ASC"):
<?php
error_reporting( E_ALL );
// some connection code here
if (isset($_REQUEST['id']))
$id = intval( $_REQUEST['id'] );
else
$id = 0;
if (isset($_REQUEST['mode']))
$mode = $_REQUEST['mode'];
else
$mode = '';
// Possible modes:
// new - entering data for new record (emit <form> )
// insert - saving data for new record (after 'new')
// edit - edit data for existing (current) record (emit <form> )
// update - save modifications for existing (current) record (after 'edit')
// delete - delete existing (current) record
// first - display first record
// previous - display previous record
// last - display last record
// next - display next record
// - (empty or none from above) display selected record
function data_valid()
{
// Put data passed by $_REQUEST in global variables and validate
// it for inserting new record or updating existing one.
// Return TRUE if data is valid.
// When data are invalid echo error messages and return FALSE.
}
function load_data()
{
// Query current ID
$query =
'SELECT date_enr, no_adr, famille, politesse, nom, ' .
' adr1, adr2,rue,cd_pays, pays, np, canton, ' .
' lieu, tel, fax, tel_opt, tel_opt_no,info1, ' .
' info2, info3, groupe1, groupe2, groupe3, ' .
' groupe4,groupe5, groupe6, groupe7, groupe8, ' .
' email, web ' .
'FROM adresse ' .
'WHERE id = ' . $GLOBALS['id']
;
$req = mysql_query($query);
if (!$req)
{
die( mysql_error() );
}
// load data to global variables
}
// Function which is able to return ID of previous, next, first
// and last record according to "ORDER BY" and current record ID.
function get_other($mode)
{
$id = $GLOBALS['id'];
if (($mode == 'previous') || ($mode == 'next'))
{
// this only makes sense if we have current record
if ($id)
{
// get current record "nom"
$query = "SELECT nom FROM adresse WHERE id = $id";
$req = mysql_query($query);
if (!$req)
die( mysql_error() );
$result = mysql_fetch_row( $req );
}
else
$result = false;
if ($result)
$nom = $result[0];
else
{
// if there's no previous or next we go to first or last
if ($mode=='previous')
return get_other( 'first' );
else
return get_other( 'last' );
}
}
// choosing apropriate query
switch($mode)
{
case 'first':
$query = "
SELECT id
FROM adresse
ORDER BY nom ASC, id ASC
LIMIT 1
";
break;
case 'previous':
$query = "
SELECT id
FROM adresse
WHERE (nom < '$nom') OR (nom = '$nom' AND id < $id)
ORDER BY nom DESC, id DESC
LIMIT 1
";
break;
case 'next':
$query = "
SELECT id
FROM adresse
WHERE (nom > '$nom') OR (nom = '$nom' AND id > $id)
ORDER BY nom ASC, id ASC
LIMIT 1
";
break;
case 'last':
$query = "
SELECT id
FROM adresse
ORDER BY nom DESC, id DESC
LIMIT 1
";
break;
}
$req = mysql_query($query);
if (!$req)
die( mysql_error() );
$result = mysql_fetch_row( $req );
if ($result)
return $result[0];
// if there's no previous or next we go to first or last
if ($mode=='previous')
return get_other( 'first' );
if ($mode=='next')
return get_other( 'last' );
}
function DeleteEnr()
{
// What is "DeleteEnr"? if you are removing "subdata", then no "famille" is needed.
// You need only ID (which you have as global).
}
function show_navigation_links()
{
$id = $GLOBALS['id'];
if ($id)
{
if (get_other('previous'))
{
$links[0] = array( 'fist', '?mode=first' );
$links[1] = array( 'previous', 'mode=previous&id=' . $id );
}
else
{
$links[0] = array( 'fist', '' );
$links[1] = array( 'previous', '' );
}
$links[2] = array( 'new', '?mode=new&id=' . $id );
if ($GLOBALS['mode'] != 'edit')
$links[3] = array( 'edit', '?mode=edit&id=' . $id );
else
$links[3] = array( 'edit', '' );
$links[4] = array( 'delete', '?mode=delete&id=' . $id );
if (get_other('next'))
{
$links[5] = array( 'next', 'mode=next&id=' . $id );
$links[6] = array( 'last', '?mode=last' );
}
else
{
$links[5] = array( 'next', '' );
$links[6] = array( 'last', '' );
}
}
else
{
if (get_other('first'))
$links[0] = array( 'fist', '?mode=first' );
else
$links[0] = array( 'fist', '' );
$links[1] = array( 'previous', '' );
$links[2] = array( 'new', '' );
$links[3] = array( 'edit', '' );
$links[4] = array( 'delete', '' );
$links[5] = array( 'next', '' );
if (get_other('last'))
$links[6] = array( 'last', '?mode=last' );
else
$links[6] = array( 'last', '' );
}
foreach( $link as $i => $links )
{
if ($link[1]!='')
$links[$i] = '<a href="' . htmlspecialchars( $_SERVER['PHP_SELF'] . $links[1] ) . '">' . $link[0] . '</a>';
else
$links[$i] = $link[0];
}
echo implode( ' | ', $links );
}
function show_data()
{
?>
<html>
<body>
Name: <?php htmlspecialchars( $GLOBALS['nom'] ); ?><br />
<!-- etcetera -->
<?php
show_navigation_links();
?>
</body>
</html>
<?php
}
function show_form()
{
?>
<html>
<body>
<form action="<?php htmlspecialchars( $_SERVER['PHP_SELF'] ); ?>">
<input type="hidden" name="id" value="<?php echo $GLOBALS['id']; ?>" />
<input type="hidden" name="mode" value="<?php echo $GLOBALS['mode']; ?>" />
Name: <input type="text" name="nom" value="<php echo htmlspecialchars( $nom ); ?>" /><br />
<!-- input fields for other data -->
<input type="submit" value="save" />
</form>
<?php
show_navigation_links();
?>
</body>
</html>
<?php
}
switch($mode)
{
case 'insert':
if (data_valid())
{
$query = "INSERT INTO ...";
if (mysql_query($query))
{
$id = mysql_insert_id();
header( 'Location: ' . $_SERVER['PHP_SELF'] . '?id=' . $id );
die();
}
else
echo mysql_error() . "<br />";
}
$mode = 'new';
show_form();
break;
case 'update':
if (data_valid())
{
$query = "UPDATE ....";
if (mysql_query($query))
{
header( 'Location: ' . $_SERVER['PHP_SELF'] . '?id=' . $id );
die();
}
else
echo mysql_error() . "<br />\n";
}
$mode = 'edit';
show_form();
break;
case 'new':
$mode = 'insert';
show_form();
break;
case 'edit':
load_data();
$mode = 'update';
show_form();
break;
case 'delete':
if (DeleteEnr())
{
$query = 'DELETE FROM adresse WHERE id=' . $id;
if (mysql_query($query))
{
$id = get_other('next');
if ($id)
header( 'Location: ' . $_SERVER['PHP_SELF'] . '?id=' . $id );
else
header( 'Location: main_page.html' );
die();
}
else
echo mysql_error() . "<br />\n";
}
$mode = '';
show_form();
break;
case 'first':
case 'previous':
case 'next':
case 'last':
$id = get_other($mode);
if ($id)
header( 'Location: ' . $_SERVER['PHP_SELF'] . '?id=' . $id );
else
header( 'Location: main_page.html' );
break;
default:
load_data();
show_data();
break;
}
?>
Hilarion
| |
|
| Hello Hilarion
> I think we allready posted samples in previous discussion:
>
Yes, I'm reminder. But, I didn't work much this summer on
this script and restarted again.
Many thanks for the wonderful sample.
Have a nice w end.
Otto
|
|
|
|
|