Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to display one result from a db result set at a time

Status
Not open for further replies.

andyfresh

Technical User
Oct 4, 2005
33
GB
Hi

Im currently trying to learn javascript and have used the following code followed by a second file containing a php script. The idea of this script is as follows - the php script pulls in a number of results from a database. I then would like the java script to help display only one comment at a time. As you will be able to see there is a next button which when pressed I would like it to go to the next comment. How can I ask the java script to find the next 'commentid' in the results and display the contents??

Regards

Andy

Code 1
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<script language="javascript" type="text/javascript">
function getPage(page){
var xmlhttp=false; //Clear our fetching variable
        try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
        } catch (e) {
                try {
                        xmlhttp = new
                        ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
            } catch (E) {
                xmlhttp = false;
                        }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
        }
        var file = 'text1.php?page='; //This is the path to the file we just finished making *
    xmlhttp.open('GET', file + page, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                var content = xmlhttp.responseText; //The content data which has been retrieved ***
                if( content ){ //Make sure there is something in the content variable
                      document.getElementById('content').innerHTML = content + "<br><br><font color=\"#CCCCCC\" size=\"1\" ><a href=\"[URL unfurl="true"]http://www.test.com\">PoweredBy</a></font>";[/URL] //Change the inner content of your div to the newly retrieved content ****
                }
        }
        }
        xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}
</script>
</head>
 
<body onload="getPage('157')">
<div id="content">
 
</div>
<div id="links" align="left">
<font size="1"><a href="javascript:getPage()">Next-></a></font>
</div>

</body>
</html>


Code 2

Code:
<? 
require_once ('../../../templates/mysql_connect.php');
$page = $_GET["page"]; //This is the variable we retrieve through GET to know which row of content to retrieve

$sql = "SELECT commentsummary FROM comments where companyID = $page and rss = 1";

$query = mysql_query($sql) or die(mysql_error());

$r=mysql_fetch_assoc($query); //Set a mysql fetching variable for the query
  echo $r["commentsummary"]; //Echo out the content of the page we want
  
  ?>
 
You would have to return the next ID to retrieve to the page and then put that into the href link (so that the backend knows what record to retrieve). You might find it easier to ad an ID to the href link and use document.getElementById() to then set it's onclick event. You'd have to parse the results of the backend script (rather than just setting the whole thing into the div innerHTML). You may wish to investigate JSON for this kind of thing. Another suggestion is to output the link as well (assuming the markup is able to be done that way). I have no idea how you are going to retrieve the id for the next record (unless you already store this in the record you are retrieving - kind of like a linked list).

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top