PHP Navigation Buttons

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

PHP Navigation Buttons

Post by jeva39 »

Please, what is the syntax to create navigation Buttons (Next, Previous, First, Last) in PHP using mySql?

Thanks for your help...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There isn't much of a syntax for buttons in PHP. HTML has buttons.

Generally, the form they are associated with will be told which button was pressed, therefore the targeted receiving script will know what was selected by the submission data.

The term to search for is called pagination.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

One method....
First you need to know the number of records by doing say a count query = $numrec
Then you need to know how many records you want to display = $display
Number of pages is a division. $np=$numrec/$display - round it up!
Then a variable for current page $cp
Pass those in the URL link with a bit of logic for what you want
ie previous link if $cp>1
next if $cp<$np etc

The first time round $np,$cp haven't been set so $_GET would be empty, so do the initial setup above
Subsequently those would have been set, so do the query having escaped the variables with

Code: Select all

LIMIT $a,$display
where $a=$np*$display

Have a go at coding and come back!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP Navigation Buttons

Post by RobertGonzalez »

jeva39 wrote:Please, what is the syntax to create navigation Buttons (Next, Previous, First, Last) in PHP using mySql?

Thanks for your help...
Search these forums and google for pagination.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I believe it was JCart (could be wrong) who had a really nice pagination class on here a while back. Might be in the code snippets forum.

[edit: dang my memory is good!...sometimes] viewtopic.php?t=38830&highlight=pagination
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

Post by jeva39 »

OK. I found a good (and simple) tutorial in PHP Freaks and at the time I try to understand this code and, obvious, understand Pagination. (really the process is very different to ASP)

The code: (I adapt they to my Databases)

Code: Select all

<?php

    @mysql_connect("localhost", "root", "*****") or die("ERROR--CAN'T CONNECT TO SERVER");
    @mysql_select_db(midis) or die("ERROR--CAN'T CONNECT TO DB");
    
    $limit          = 25;
    $query_count    = "SELECT * FROM temas";
    $result_count   = mysql_query($query_count);
    $totalrows      = mysql_num_rows($result_count);
   
    if(empty($page)){
        $page = 1;
     }


    $limitvalue = $page * $limit - ($limit);
    $query  = "SELECT * FROM temas order by tema LIMIT $limitvalue, $limit";
    $result = mysql_query($query) or die("Error: " . mysql_error());

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    $bgcolor = "#E0E0E0"; // light gray

    echo("<table>");

    while($row = mysql_fetch_array($result)){
        if ($bgcolor == "#E0E0E0"){
            $bgcolor = "#FFFFFF";
        }else{
            $bgcolor = "#E0E0E0";
        }

    echo("<tr bgcolor=".$bgcolor."><td>");
    echo($row["tema"]);
    echo("</td><td>");
    echo($row["autor"]);
    echo("</td><td>");
    echo($row["ritmo"]);
    echo("</td></tr>");
    }

    echo("</table>");

    if($page != 1){
        $pageprev = $page--;
        

        echo("<a href=\"pagina.php&page=$pageprev\">PREV".$limit."</a> ");
    }else{
        echo("PREV".$limit." ");
    }

    $numofpages = $totalrows / $limit;

    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"pagina.php?page=$i\">$i</a> ");
        }
    }


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"pagina.php?page=$i\">$i</a> ");
        }
    }

    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page++;
        $page=$pagenext;

        echo("<a href=\"pagina.php?page=$pagenext\">NEXT".$limit."</a>");
    }else{
        echo("NEXT".$limit);
    }

    mysql_free_result($result);

?>
Now, the problem: The code work fine but ALLWAYS OUTPUT THE PAGE 1 (first 25 records). Really the variable $page in this section of code always appear empty:

Code: Select all

if(empty($page)){
   $page = 1;
 }
Please, any idea?

Thanks
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Try changing $page to $_GET['page']
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

Post by jeva39 »

Thanks but if I use $_GET['page'] I receive this error:

....Undefined index: page....
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

try

Code: Select all

$page = (empty($_GET['page']) ? 1 : $_GET['page']);
instead. The reason you want to make this switch is because that script was designed with register globals set to on, which is no longer the default on newer php builds.
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

Post by jeva39 »

Yes. I have settings globals to off (In my Host Server Globals is off).

Your code work!!! The only thing that don't work is PREVIOUS. The error is: The webpage cannot be found

I don't know much about syntax in PHP but I see a peculiar thing :

In PREVIOUS the syntax is:

....pagina.php&page=.....(&)

And the rest is:

....pagina.php?page=.....(?)


That's correct?
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

That's not a PHP syntax per se it's a URL syntax

pagename.extension?variable1=value1&variable2=value2

The first variable has a ? before it subsequent ones have a &

so the "previous" link should use a "?" not an & as it is the first variable in the URL

interestingly you can use it for mailto:email@address.com?subject=whatever&body=bodywhatever
Which will create an email link with specified subject and body - OFF TOPIC alert!!!!
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

Post by jeva39 »

Another syntax problem

I need to include the variable $fecha in the VALUE of this line of a function that create a table:

Code: Select all

function tabla{
......more code....
echo '<tr><td width="48%" valign="middle" align="left"><strong>Fecha (dd/mm/yyyy)</strong></td><td width="52%" valign="middle" align="left"><input type="text" name="fecha" value=""size="35"></td></tr>';
.....more code......

}
I try with:

....value="<?php echo $fecha; ?>"....

But don't work. Can you help me, please?
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

When you echo using single quotes it comes out as is. You need to use double quotes to parse the variable value...

Code: Select all

<?php
$fecha=22;
echo '$fecha'; //would output $fecha
echo "$fecha"; // would output 22
?>
But when you have double quotes in the text you are echoing they need to be escaped with a \

eg

Code: Select all

echo "blah blah \"rhubarb\" blah ";
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If it needs to be accessible inside the function you need to pass it to the function as a parameter (prefered method) or make it a global variable (not prefered method).

Code: Select all

<?php
function myfunction($fecha) {
    echo $fecha;
}
?>
jeva39
Forum Commoner
Posts: 45
Joined: Sun Aug 27, 2006 11:45 pm
Location: Panamá

Post by jeva39 »

This is the complete code of Function:

Code: Select all

$fecha= date("M d, Y");

function tabla() {
    if (!isset($_POST['tema'])) $_POST['tema'] = '';
    if (!isset($_POST['arreglo'])) $_POST['arreglo'] = '';
    if (!isset($_POST['autor'])) $_POST['autor'] = '';
    if (!isset($_POST['ritmo'])) $_POST['ritmo'] = '';
    if (!isset($_POST['archivo'])) $_POST['archivo'] = '';
    if (!isset($_POST['clase'])) $_POST['clase'] = '';
    if (!isset($_POST['fecha'])) $_POST['fecha'] = '';
    
    echo '<center><p><h2><font color="red">Todos los campos son requeridos.</font></h2></p></center>';
    echo '<form name="Buscar" method="POST" action="nuevoreg.php">';
    echo '<table border="1" cellpadding="10" cellspacing="1" bgcolor="#f7efde" ALIGN=CENTER>';
    echo '<tr><td width="48%" valign="middle" align="left"><strong>Tema</strong></td><td width="52%" valign="middle" align="left"><input type="text" name="tema" size="35" ></td></tr>';
    echo '<tr><td width="48%" valign="middle" align="left"><strong>Arreglo</strong></td><td width="52%" valign="middle" align="left"><input type="text" name="arreglo" size="25"></td></tr>';
    echo '<tr><td width="48%" valign="middle" align="left"><strong>Autor</strong></td><td width="52%" valign="middle" align="left"><input type="text" name="autor" size="35"></td></tr> ';
    echo '<tr><td width="48%" valign="middle" align="left"><strong>Ritmo</strong></td><td width="52%" valign="middle" align="left"><input type="text" name="ritmo" size="35"></td></tr> ';

// This is the line of problem

    echo '<tr><td width="48%" valign="middle" align="left"><strong>Fecha (dd/mm/yyyy)</strong></td><td width="52%" valign="middle" align="left"><input type="text" name="fecha" value="????????????"size="35"></td></tr>';


    echo '<tr><td width="48%" valign="middle" align="left"><strong>Nuevo</strong></td><td width="52%" valign="middle" align="left"><input type="checkbox" name="nuevo" CHECKED></td></tr>';
    echo '<tr><td width="48%" valign="middle" align="left"><strong>Clase</strong></td><td width="52%" valign="middle" align="left"><input type="text" name="clase" size="2"></td></tr>';
    echo '<tr><td width="48%" valign="middle" align="left"><strong>KAR</strong></td><td width="52%" valign="middle" align="left"><input type="text" name="kar" size="3"></td></tr>';
    echo '<td width="48%" align=center colspan=2><center><BR></B></FONT><input type="submit" name="submit" value="Submit">        <input type="reset" name="cancel" value="Cancelar"><BR><BR></center></td></tr>';
    echo ' ';
    echo '</table>';
    echo '</form>';
}
I try the methods above but I don't know why don't work. My problem is how to include the value of variable $FECHA in the input FECHA Value="?????????" because I need the user see the date of the actual day and use this by default.

Thanks....
Post Reply