/    Sign up×
Community /Pin to ProfileBookmark

PHP/AJAX next database entry

I’m looking to create a PHP/AJAX page that will display the data of a MySQL database one row at a time, with the inclusion of a link ‘next’ which will then display the next row in the database on the same page. Obviously I will also look to include a ‘previous’ link in the future.
This is fairly similar to the w3schools [URL=”http://www.w3schools.com/PHP/php_ajax_database.asp”]example[/URL], which displays each row from a dropdown box.
I hope I have explained myself and thanks to anyone who can help.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@criterion9Jun 07.2009 — Have you started coding something or are you looking for someone to do it for you?
Copy linkTweet thisAlerts:
@mhmhcauthorJun 08.2009 — I'm not looking for someone to code it all for me but perhaps set me in the right direction. I'm looking to add next/previous buttons to a player profiler which selects random person from my database and displays their stats. The script is:

include 'random.php';

$details = mysql_query("SELECT * FROM players WHERE PlayerID = '$random'")

or die(mysql_error());

$data = mysql_query("SELECT COUNT(PlayerID) FROM playerfixtures WHERE PlayerID = '$random'")

or die(mysql_error());

while($info = mysql_fetch_array( $details ))

{

Print "<li class="profile"><img src="".$info['PlayerPhoto'] . "" alt="Player Photo" /></li>";

Print "<li>Name : ".$info['PlayerName'] . "</li>";

Print "<li>Number : ".$info['PlayerNumber'] . "</li>";

}

while($info = mysql_fetch_array( $data ))

{

Print "<li>Games Played : ".$info['COUNT(PlayerID)'] . "</li>";

}

$details = mysql_query("SELECT * FROM players WHERE PlayerID = $random'") or die(mysql_error());
while($info = mysql_fetch_array( $details ))
{
Print "<li>Goals Scored : ".$info['GoalsScored'] . "</li>";
}



It might be a bit too much to get this to work with the random function included, but would be good to get it to work even if it just starts from the first entry in the database.

Any help is appreciated.
Copy linkTweet thisAlerts:
@criterion9Jun 08.2009 — If it is just random then the link can point to the same page again passing the previous random number. Example your page looks up random player #2 and at the bottom you have a link for "next" as same_page.php?last_num=2. Then when you get your new random number just make sure it isn't the same as the last one.
Copy linkTweet thisAlerts:
@mhmhcauthorJun 09.2009 — Wouldn't this be more or less the same as refreshing the page?

What I really want to do is have the players listed by PlayerID and then you click next to go to the next player in the list.

I'm also guessing this would be best done using ajax so that there is no browser refresh.

Thanks for helping out
Copy linkTweet thisAlerts:
@criterion9Jun 09.2009 — Do you want them random or in order? I guess I'm a little confused.

I usually think of Ajax solutions as page refreshes first. Then you can easily move the refresh "behind the curtain" so to speak. I usually find it much easier to debug that way first. You can also fall back on code that is not dependent on javascript as well.
Copy linkTweet thisAlerts:
@mhmhcauthorJun 09.2009 — Sorry I don't think I was being clear either.

I thought it would be easier to do it without the random function and have it as an ordered list. Ultimately after a random row is found, I want the next link to click onton the next row in the ordered list.

I guess you mean to have a profile.php file within the overall webpage, which then refreshes just that file and not the page?
Copy linkTweet thisAlerts:
@criterion9Jun 09.2009 — You can have a named div such as <div id="myDiv"></div> then use .innerHTML to populate that div with appropriate content returned by the php script. It may be easier to first get it to work the way you want without AJAX and add the AJAX once you get everything working properly.

http://www.w3schools.com/Ajax/Default.Asp
Copy linkTweet thisAlerts:
@mhmhcauthorJun 24.2009 — Ok so I have the code working great but just without the ajax functionality. I've had a look at some AJAX/PHP paging tutorials but they don't seem to explain it very well. Here's my script:

[code=php]<?php
include 'config.php';
include 'opendb.php';
include 'random.php';

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

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

// 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 players ORDER BY PlayerName ASC" .
" LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');

while($row = mysql_fetch_array($result))
{
Print "<ul><li class="profile"><img src="".$row['PlayerPhoto'] . "" alt="Player Photo" /></li>";
Print "<li>Name : ".$row['PlayerName'] . "</li>";
Print "<li>Number : ".$row['PlayerNumber'] . "</li>";
Print "<li>Games : ".$row['GamesPlayed'] . "</li>";
Print "<li>Goals : ".$row['GoalsScored'] . "</li></ul>";
}

// how many rows we have in database
$query = "SELECT COUNT(GamesPlayed) AS numrows FROM players";
$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 = '';

// 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"><div class="previous"></div></a>";
$first = "<a href="$self?page=1"><div class="first"></div></a>";
}
else
{
$prev = "<a href="$self?page=$maxPage"><div class="previous"></div></a>"; // we're on page one, link to last
$first = "<div class="first"></div>";
}

if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = "<a href="$self?page=$page"><div class="next"></div></a>";
$last = "<a href="$self?page=$maxPage"><div class="last"></div></a>";
}
else
{
$next = "<a href="$self?page=1"><div class="next"></div></a>"; // we're on the last page, link to first
$last = "<div class="last"></div>";
}

// print the navigation link
Print "<div class="profiler-buttons">";
echo $first . $prev . $next . $last;
Print "</div>";

// and close the database connection
include 'closedb.php';

?>[/code]


If somebody could lead me along the right tracks then that would be great
×

Success!

Help @mhmhc 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.16,
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,
)...