Forum Moderators: coopster
What I'm looking to do is create a page that queries my mysql database and displays a page inside an iframe. When the user clicks on a next button, then will be forwarded the next result. Here is the details:
Database: com
Table: koc
Field1: id (just an ID field, auto increment)
Field2: link (the link to the document or to the URL)
Field3: name (Name of the site or person who posted it)
This is a very simple part of my plan, but I need a kick start. If anyone can help, I'd be most appreciatative. I can't seem to get a good start on this.
[edited by: jatar_k at 5:01 am (utc) on May 12, 2004]
[edit reason] sorry no personal urls thanks [/edit]
The code below isn't tested, but it ought to get you started.
<script>
pagenum=0;
<?php
$i=0;
$result=mysql_query("SELECT * FROM Database_table");
while ($row=mysql_fetch_array($result)){
print("links[".$i."]=".$row['link']."\n");
$i++;
}
?>
</script><iframe id='pages'>
<BR>
<input type='button' value='prev' onClick='pagenum--;pages.location.href=links[pagenum]'>
<input type='button' value='next' onClick='pagenum++;pages.location.href=links[pagenum]'>
You might also tidy up the code by making a javascript function goto(), where the argument is 1 (next page) or -1 (prev page)
good luck!
<?php// Database Connection
include 'db.php';
// If current page number, use it
// if not, set one!
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// Define the number of results per page
$max_results = 1;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
echo "<center>";
$sql = mysql_query("SELECT * FROM koc LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){
// Build your formatted results here.
echo $row['id']." - ";
echo $row['name']." - ";
echo $row['link']."<br />";
}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM koc"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<center>Select a Link<br />";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><< Prev</a> ";
}
//for($i = 1; $i <= $total_pages; $i++){
// if(($page) == $i){
// echo "$i ";
// } else {
// echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
// }
//}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next >></a>";
}
echo "</center>";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Clicker by Lighthater</title>
</head>
<body>
<p align="center"><iframe name="iframe" width="640" height="480" src="<?php echo $UNKNOWN_VARIABLE>">
Your browser does not support inline frames or is currently configured not to display inline frames.
</iframe></p>
</body>
</html>
<?
echo "Link #$page";
?>