Jump to content

seperating output buffers?


poleposters

Recommended Posts

This is a simplified version of a script I'm using.

 

I select records from a database and print them inside a buffer. Then I output the buffer to the included template.php file.

 

 

 

 

 


$query="SELECT * FROM location WHERE type='1'";
$result=mysql_query($query);
if(mysql_num_rows($result)>0)

{
ob_start();
  while($selectlinks=mysql_fetch_array($result))
  {
   $name=$selectlinks['name'];
   $address=$selectlinks['address'];
   $lat=$selectlinks['lat'];
   $lng=$selectlinks['lng'];
   print "$name,$address,$lat,";
  }
$output=ob-get_clean();
}

include("template.php");

 

 

I'm adding a new feature to the script which requires that I output the retrieved records to be also used as part of some javascript in the head of the template.php file. Specifically I want to write this piece of javascript for each record returned.

 

 

  
        var marker = new GMarker(new GLatLng([b]lat[/b], [b]lng[/b]));
        GEvent.addListener(marker, "click", function() {
        var html= '[b]name[/b]<br />[b]address[/b]';
        marker.openInfoWindowHtml(html);
        });
        map.addOverlay(marker);

 

 

Whats the best way to do this?

 

Is it possible to create  two separate buffers using the single query and then just outputting one to the body of the template and one to the head?

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/186210-seperating-output-buffers/
Share on other sites

Why are you even using buffers? You know you can just append output to a string and be fine. But here is what I think you are after (note without the buffers):

 

<?php
$query="SELECT * FROM location WHERE type='1'";
$result=mysql_query($query);
$output = ""; // initialize
$script = "";

if(mysql_num_rows($result)>0) {
while($selectlinks=mysql_fetch_array($result)) {
	$name=$selectlinks['name'];
	$address=$selectlinks['address'];
	$lat=$selectlinks['lat'];
	$lng=$selectlinks['lng'];
	$output .= "$name,$address,$lat,";
	$script .= "var marker = new GMarker(new GLatLng({$lat}, {$lng}));
		GEvent.addListener(marker, \"click\", function() {
		var html= '{$name}<br />{$address}';
		marker.openInfoWindowHtml(html);
		});
		map.addOverlay(marker);";
}

// $output .= $script;  // Only use this if it is what you wanted
}

include("template.php");

?>

 

Now I am unsure if you wanted the script output to be included after each lat long printing, if you did then change $script to $output, if you wanted them combined together then uncomment the $output .= $script line above. Else, you can use $script in the template file to display the javascript.

 

Hope that helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.