Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.