/    Sign up×
Community /Pin to ProfileBookmark

HowTo?: MySql query result in Combo for pagination

hi all

below is my code for displaying member names from database in pagination form.

everything is working fine i.e. [First], [Previous], 1 2 3 4…, [Next], [Last]

I wish to have all those page numbers in a combo box so that the user can navigate the result pages by selecting any page number from the combo box.

Can anyone help me in this matter?
Thanks in advance

Below is my code:

[CODE]<?php

//Connect to DB
mysql_connect(“localhost”,”username”,”password”) or die(“Unable to connect to SQL server”);
mysql_select_db(‘mic52co_DatabaseName’) or die(“Unable to SELECT DB”);

// how many rows to show per page
$rowsPerPage = 3;

// by default we show first page
$pageNum = 1;

// if $_GET[‘page’] defined, use it as page number
if(isset($_GET[‘page’]))
{
$pageNum = $_GET[‘page’];
}

// counting the offset
$offset = ($pageNum – 1) * $rowsPerPage;

$query = “SELECT * FROM user LIMIT $offset, $rowsPerPage”;
$result = mysql_query($query) or die(‘Error, query failed’);

echo “<table border=’1′>”;
echo “<tr bgcolor=’#C0C0C0′> <th> <b><font face=’Tahoma’ size=’2′> UserID </font></b> </th> <th><b><font face=’Tahoma’ size=’2′> Full Name </font></b></th> <th><b><font face=’Tahoma’ size=’2′> Email </font></b></th> </tr>”;
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo “<tr><td width=’150′ ><font face=’Tahoma’ size=’2′>”;
echo $row[‘userid’];
echo “</font></td><td width=’205′><font face=’Tahoma’ size=’2′>”;
echo $row[‘fullname’];
echo “</font></td><td width=’205′><font face=’Tahoma’ size=’2′>”;
echo $row[’email’];
echo “</font></td></tr>”;
}

echo “</table>”;

// how many rows we have in database
$query = “SELECT COUNT(userid) AS numrows FROM user”;
$result = mysql_query($query) or die(‘Error, query failed’);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row[‘numrows’];

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

// print the link to access each page
$self = $_SERVER[‘PHP_SELF’];
$nav = ”;
for($page = 1; $page <= $maxPage; $page++)

{
if ($page == $pageNum)
{
$nav .= ” $page “; // no need to create a link to current page
}
else
{
$nav .= ” <a href=”$self?page=$page”>$page</a> “;

}
}

// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($pageNum > 1)
{
$page = $pageNum – 1;
$prev = ” <a href=”$self?page=$page”>[Previous]</a> “;

$first = ” <a href=”$self?page=1″>[First]</a> “;
}
else
{
$prev = ‘&nbsp;’; // we’re on page one, don’t print previous link
$first = ‘&nbsp;’; // nor the first page link
}

if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = ” <a href=”$self?page=$page”>[Next]</a> “;

$last = ” <a href=”$self?page=$maxPage”>[Last]</a> “;
}
else
{
$next = ‘&nbsp;’; // we’re on the last page, don’t print next link
$last = ‘&nbsp;’; // nor the last page link
}

// print the navigation link
echo $first . $prev . $nav . $next . $last;

?>
[/CODE]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@neil9999Aug 21.2007 — Something like this should do the trick:

<i>
</i>&lt;form method="get" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt;
&lt;select name="page"&gt;
&lt;?php
//replace 10 with number of pages
for($i=1;$i&lt;=10;$i++){
echo "&lt;option value="".$i.""&gt;".$i."&lt;/option&gt;";
}
?&gt;
&lt;/select&gt;
&lt;input type="submit" value="Go" /&gt;
&lt;/form&gt;


Neil
Copy linkTweet thisAlerts:
@jack001authorAug 21.2007 — thnx dear

i got it done with this code

[CODE]echo "<form method='get' action=''>";
echo "<select name='page'>";

//replace 10 with number of pages
for($page = 1; $page <= $maxPage; $page++){
echo "<OPTION VALUE=$page>$page</option>";
}

echo "</select>";
echo "<input type='submit' value='Go' />";
echo "</form>";
[/CODE]


but when the selected page is displayed the selected value(page number) in the combobox is 1 always..... y?

and i dont want a button i need to select the desired page just by clicking on the page number from the combo list

any further help???

thnx in advance

Jack




Something like this should do the trick:

<i>
</i>&lt;form method="get" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt;
&lt;select name="page"&gt;
&lt;?php
//replace 10 with number of pages
for($i=1;$i&lt;=10;$i++){
echo "&lt;option value="".$i.""&gt;".$i."&lt;/option&gt;";
}
?&gt;
&lt;/select&gt;
&lt;input type="submit" value="Go" /&gt;
&lt;/form&gt;


Neil[/QUOTE]
Copy linkTweet thisAlerts:
@neil9999Aug 21.2007 — It is because, by default, the first option in a select box is selected.

Here's a way to do what you want:

echo "&lt;form name='pageform' method='get' action=''&gt;";
echo "&lt;select name='page' onchange="document.pageform.submit();"&gt;";

//replace 10 with number of pages
for($page = 1; $page &lt;= $maxPage; $page++){
echo "&lt;option value=".$page.(($pageNum==$page)?" selected="selected"":"")."&gt;$page&lt;/option&gt;";
}

echo "&lt;/select&gt;";
echo "&lt;input type='submit' value='Go' /&gt;";
echo "&lt;/form&gt;";


However, the code that makes it change page when you select the page number is javascript, hence if the user has javascript disabled, they will need to click the button. Since you have links to the different pages as well though, you shouldn't need this button.

To make an option selected by default, you add selected="selected" to the option tag. Here, this is outputed when $pageNum==$page, hence this option is selected.

Neil
×

Success!

Help @jack001 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...