4 Replies - 619 Views - Last Post: 20 April 2012 - 02:59 AM

#1 Bestford  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-December 11

problem getting xml output to appear on to a index.php page

Posted 18 April 2012 - 04:36 AM

hello guys,

I need to use this code :
<?php
// include the file for the database connection
include_once("database_conn.php");

// if useXML was sent as part of the url then
if (isset( $_REQUEST['useXML'] )) {
    // echo whatever getXMLOffer returns to the browser or back to the ajax script
    echo getXMLOffer();

} else {    // otherwise just an html record is required

    // so echo whatever getHTMLOffer returns to the browser or back to the ajax script
    echo getHTMLOffer();
}

function getHTMLOffer() {
    // store the sql for a random special offer, the sql wraps things using concat in an html 'wrapper'
    $sql2 = "select concat('<p>“',bookTitle,'”<br />\n<span class=\"category\">Category: ',catDesc,'</span><br />\n<span class=\"price\">Price: ',bookPrice,'</span></p>') as offer from nbc_book_special_offers inner join nbc_category on nbc_book_special_offers.catID = nbc_category.catID order by rand() limit 1";

    // execute the query
    $rsOffer = mysql_query( $sql2 );

    // get the one quotation returned
    $offer = mysql_fetch_assoc($rsOffer);
    // return the quote
    return $offer['offer'];

}

function getXMLOffer() {
	$sql = "select bookTitle, catDesc, bookPrice from nbc_book_special_offers inner join nbc_category on nbc_book_special_offers.catID = nbc_category.catID order by rand() limit 1";
    $xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
    $rsOffer = mysql_query( $sql );
    // start to assemble an output string with the xml head and root element
    $output = $xmlHeader;
    $output .= "<root>\n";
    while ( $record = mysql_fetch_assoc( $rsOffer ) ) {
        // start a new record element in xml and add to the output
        $output .= "\t<offer>\n";
        // iterate through each record pulling out the fieldname and its value
        foreach ( $record as $field => $value ) {
            $value = htmlspecialchars( $value );
            // wrap up the fields and values as xml
            $output .= "\t\t<$field>$value</$field>\n";
        }
        $output .= "\t</offer>\n";
    }
    $output .= "</root>";
    return $output;
}
?>



to print the xml created which looks like:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
	<offer>
		<bookTitle>Actionscript with PHP</bookTitle>
		<catDesc>Flex &amp; Flash Programming</catDesc>
		<bookPrice>15.00</bookPrice>
	</offer>
</root>



but it needs to refresh every 5 seconds can anybody help me

Is This A Good Question/Topic? 0
  • +

Replies To: problem getting xml output to appear on to a index.php page

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3115
  • View blog
  • Posts: 4,675
  • Joined: 08-June 10

Re: problem getting xml output to appear on to a index.php page

Posted 18 April 2012 - 05:32 AM

There are two ways to refresh a page.

  • Use either a meta refresh, or a Javascript timer, to refresh the entire page.

  • Use AJAX to fetch data from the server and update the current page via the DOM.


Which one are you trying to use?

By the way, in your getHTMLOffer() function. It's a little weird to send the HTML template to the SQL server to be filled out. It's more efficient to just fetch the data from the SQL server and fill in the template with PHP, like you do in the getXMLOffer() function.
Was This Post Helpful? 1
  • +
  • -

#3 Bestford  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-December 11

Re: problem getting xml output to appear on to a index.php page

Posted 18 April 2012 - 06:14 AM

thnaks for the reply the getHTMLOffer is part of the file that im not allowed to edit and, I'm suppose to use ajax to display the data to the page.
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3115
  • View blog
  • Posts: 4,675
  • Joined: 08-June 10

Re: problem getting xml output to appear on to a index.php page

Posted 18 April 2012 - 06:57 AM

So what have you tried so far? That tutorial I linked you to should get you started.
Was This Post Helpful? 0
  • +
  • -

#5 Bestford  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-December 11

Re: problem getting xml output to appear on to a index.php page

Posted 20 April 2012 - 02:59 AM

Hello managed to get it working but theres a problem it formats correctly the first time but after that it does it on one line after that
function updateTarget( offer ){
		document.getElementById('XMLAd').innerHTML = offer;
  		window.setTimeout("getRequest('getOffers(CM0513).php?useXML', updateTarget)", 5000);}  //change (update) special offer (target) every 5 seconds


	function getHTMLAd(){
		var url = "getOffers(CM0513).php";
		if (window.XMLHttpRequest){
			http = new XMLHttpRequest();
		} else if (window.ActiveXObject){
			http = new ActiveXObject("Microsoft.XMLHTTP");
		}
		http.open("GET", url, true);
		http.onreadystatechange = useHttpResponse;
		http.send(null);

	}

	function useHttpResponse() {
	  if (http.readyState == 4) {
		var textout = http.responseText;
		HTMLAd = document.getElementById('HTMLAd');
		HTMLAd.innerHTML = textout;
	  }
	}

function getRequest( url, callbackFunction, useXML ) {
  var httpRequest;

  if (window.XMLHttpRequest) { // Mozilla, Safari, ...

    httpRequest = new XMLHttpRequest();
  }
  else if (window.ActiveXObject) { // IE
    try {
        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {}
    }
  }

  if (!httpRequest) {
    alert('Giving up :(/> Cannot create an XMLHTTP instance');
    return false;
  }

  httpRequest.open('get', url, true);
  if (useXML && httpRequest.overrideMimeType) {
    httpRequest.overrideMimeType('text/xml');
  }
  httpRequest.onreadystatechange = function() {
    var completed = 4, successful = 200;
    if (httpRequest.readyState == completed) {
      if (httpRequest.status == successful) {
         if (useXML) {
            returnValue = httpRequest.responseXML;
         } else {
            returnValue = httpRequest.responseText;
         }
         callbackFunction( returnValue );
      } else {
         alert('There was a problem with the request.');
      }
    }
  }
  httpRequest.send(null);
}

window.setInterval('getHTMLAd()', 5000)



for example

first instance
XML Advert

“ActionScript 3.0 Game Programming University”
Category: Programming
Price: 15.00

after that it turns to this
XML Advert
Sams teach yourself java 6 in 21 days Programming 12.00

can you help me with the formatting please
thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1