Jump to content

Turning a query into a function?


Shadow Wolf

Recommended Posts

I need help turning a query into a function. The query and code by itself works fine as a stand-alone .php file. The issue I'm having is trying to integrate it with an existing php script. It looks like the only real way to insert it is as a function, other methods create various syntax errors or the query doesn't produce any results. However the code by itself works fine. So I figured if I turn it into a function place that function in the php file, then call upon it then it would probably work properly.

<?
mysql_connect("localhost","USER","PASSWD") or die(mysql_error());
mysql_select_db("DATABASE") or die(mysql_error());

$query ="  SELECT  category_id,
                   directory,
                   masked_file_name
           FROM    ibf_gallery_images
           WHERE   category_id='60'
           ";

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


echo "<div id='lightbox-hide'>";

while($rowlb = mysql_fetch_array($resultlb)){
echo "<a href='http://www.mydomain.com/{$rowlb['masked_file_name']}' /></a>";
echo "<br />";
}

echo "</div>";
?>

 

If I can put it into a function or maybe it is called an array, then I should be able to call upon it easier. The place where I'd call upon the function looks like this:

if( $i['medium_file_name'] && $med_link )
	{
$img_tag = "<img src='{$img_url}{$i['medium_file_name']}' class='galattach' alt='{$thumb}{$i['masked_file_name']}' />";

return "<a href='{$img_url}{$i['masked_file_name']}' rel='lightbox[{$i['category_id']}]' target='_blank' class='gal'>{$img_tag}</a	
    <div id='lightbox-hide'>

  [WHERE I WOULD LIKE THE QUERY TO OUTPUT TOO]

    </div>";

   

I've spent hours trying to work on this and I can get the initial run to at least work on my test page but integrating it into the other script is where I'm having trouble. That and understanding functions. I've ran through various tutorials online and tried to apply them to this but they are either too basic or I'm missing that little bit of extra knowledge to learn how to better apply it. Any help would be greatly appreciated. Thank you.

 

Link to comment
https://forums.phpfreaks.com/topic/41908-turning-a-query-into-a-function/
Share on other sites

I'm trying to integrate Lightbox into the Gallery system I use. I have integrated it for the initial pop-up image. However it won't advance to the next or previous images. The reason being is because I need the links for the images to be on the webpage. So I created a hidden div tag to hide all the links. To make sure only the images in that particular category display, I need to query the database so that it will only output those category images.

 

I created a test.php page where I duplicated what I wanted with basic .php/html and it works as intended. Now I'm trying to integrate that query into the function that is used to display the image. Then the URL links will be hidden and the previous and next image links will work.

 

Where I'm trying to insert it into is here:

if( $i['medium_file_name'] && $med_link )

{

$img_tag = "<img src='{$img_url}{$i['medium_file_name']}' class='galattach' alt='{$thumb}{$i['masked_file_name']}' />";

return "<a href='{$img_url}{$i['masked_file_name']}' rel='lightbox[{$i['category_id']}]' target='_blank' class='gal'>{$img_tag}</a>

 

<div id='lightbox-hide'>

<a href='{$img_url}gallery_60_29440.jpg' rel='lightbox[{$i['category_id']}]'></a>

<a href='{$img_url}gallery_60_110410.jpg' rel='lightbox[{$i['category_id']}]'></a>

<a href='{$img_url}gallery_60_29896.jpg' rel='lightbox[{$i['category_id']}]'></a>

<a href='{$img_url}gallery_60_20234.jpg' rel='lightbox[{$i['category_id']}]'></a>

<a href='{$img_url}gallery_60_24532.jpg' rel='lightbox[{$i['category_id']}]'></a>

</div>";

 

  }  else  {

return "<img src='{$img_url}{$i['masked_file_name']}' class='galattach' alt='{$thumb}{$i['masked_file_name']}' />";

    }

  }  else  {

$img_url = "{$this->ipsclass->vars['board_url']}/index.php?automodule=gallery&req=viewimage&img={$i['id']}{$tn}";

if( $i['medium_file_name'] && $med_link ) 

    {

$img_tag = "<img src='{$img_url}&file=med' class='galattach' title='{$thumb}{$i['masked_file_name']}' />";

      return "<a href='{$img_url}' target='_blank' class='gal'>{$img_tag}</a>";

  } else    {

return "<img src='{$img_url}' class='galattach' />";

}  }  }  }

 

Demo URL

 

When you click on the main image it pops up the full size image using lightbox. The highlighted red part is what I have inserted into it to hardcode the lightbox effect, so that it will associate the next/prev images with that main image that popped up. The problem is of course if they go into another Gallery Category those links stay there. It won't change based on the category. So the query I initially created will ouput the file_name so when it enters into that category it will query it, output it to the hidden div field and the lightbox effect will work per category.

 

But I can't figure out how to integrate that query into it so that I can echo it out into the hidden div field.

Yes the return part is nested in a function, from what I can tell it is part of this function, make_image_tag.

	function make_image_tag( $i, $tn=0, $med_link=0 )
{	  
	//------------------------
	// Thumbnail?
	//------------------------

	$tn		= $tn 	? '&tn=1' 	: '';
	$thumb  = $tn 	? 'tn_' 		: '';

	//------------------------
	// Directory?
	//------------------------

	$dir 		= $i['directory'] ? "&dir={$i['directory']}/" 	: "";
	$directory 	= $i['directory'] ? "{$i['directory']}/" 			: "";

	if( !file_exists( $this->ipsclass->vars['gallery_images_path'].'/'.$directory.$thumb.$i['masked_file_name'] ) )
	{
		$tn 	= '';
		$thumb	= '';
	}

	if( !file_exists( $this->ipsclass->vars['gallery_images_path'].'/'.$directory.$thumb.$i['medium_file_name'] ) )
	{
		$med_link = 0;
	}		

	//------------------------
	// Update bandwidth, if required
	//------------------------

	if( $this->ipsclass->vars['gallery_detailed_bandwidth'] && $this->ipsclass->input['req'] == 'si' )
	{
	  	if( ! ( $tn && ! $this->ipsclass->vars['gallery_bandwidth_thumbs'] ) )
	  	{
			$cut = time() - ( $this->ipsclass->vars['gallery_bandwidth_period'] * 3600 );

			if( $this->del_bandwidth != 1 )
			{
	 			$this->ipsclass->DB->do_delete( 'gallery_bandwidth', "bdate < {$cut}" );
	 			$this->del_bandwidth = 1;
	 		}

	 		$filename = ( $tn == 0 AND $med_link == 1 ) ? $i['medium_file_name'] : $i['masked_file_name'];

			$insert = array( 'member_id' 	=> $this->ipsclass->member['id'], 
							 'file_name' 	=> $i['masked_file_name'],
							 'bdate'	 	=> time(),
							 'size'	 		=> intval( @filesize( $this->ipsclass->vars['gallery_images_path'].'/'.$directory.$thumb.$filename ) )
							);

			$this->ipsclass->DB->do_shutdown_insert( 'gallery_bandwidth', $insert );
	  	}
	}

	//------------------------
	// Is this media?
	//------------------------

	if( $i['media'] ) 
	{
		$this->load_media_cache();

		$ext  = '.' . strtolower( array_pop( $explode_result = explode( ".", $i['masked_file_name'] ) ) );
		$media = $this->media_thumb_cache[$ext];

		if( $i['media_thumb'] )
		{
			$img_url = "{$this->ipsclass->vars['gallery_images_url']}/{$directory}";

			return "<img src='{$img_url}{$i['media_thumb']}' class='galattach' alt='{$i['media_thumb']}' />";	
		}
		else
		{
			return "<img src='{$this->ipsclass->vars['board_url']}/style_images/<#IMG_DIR#>/{$media['icon']}' />";
		}
	}
	else 
	{
		if( $this->ipsclass->vars['gallery_web_accessible'] == 'yes' )
		{
			$img_url = "{$this->ipsclass->vars['gallery_images_url']}/{$directory}{$thumb}";

			if( $i['medium_file_name'] && $med_link )
			{
				$img_tag = "<img src='{$img_url}{$i['medium_file_name']}' class='galattach' alt='{$thumb}{$i['masked_file_name']}' />";

				return "<a href='{$img_url}{$i['masked_file_name']}' rel='lightbox[{$i['category_id']}]' target='_blank' class='gal'>{$img_tag}</a>

<div id='lightbox-hide'>
<a href='{$img_url}gallery_60_29440.jpg' rel='lightbox[{$i['category_id']}]'></a>
<a href='{$img_url}gallery_60_110410.jpg' rel='lightbox[{$i['category_id']}]'></a>
<a href='{$img_url}gallery_60_29896.jpg' rel='lightbox[{$i['category_id']}]'></a>
<a href='{$img_url}gallery_60_20234.jpg' rel='lightbox[{$i['category_id']}]'></a>
<a href='{$img_url}gallery_60_24532.jpg' rel='lightbox[{$i['category_id']}]'></a>
</div>";

			}
			else
			{
				return "<img src='{$img_url}{$i['masked_file_name']}' class='galattach' alt='{$thumb}{$i['masked_file_name']}' />";		
			}

		}
		else
		{
			$img_url = "{$this->ipsclass->vars['board_url']}/index.php?automodule=gallery&req=viewimage&img={$i['id']}{$tn}";

			if( $i['medium_file_name'] && $med_link )
			{
					$img_tag = "<img src='{$img_url}&file=med' class='galattach' title='{$thumb}{$i['masked_file_name']}' />";

					return "<a href='{$img_url}' target='_blank' class='gal'>{$img_tag}</a>";
			}
			else
			{
					return "<img src='{$img_url}' class='galattach' />";				
				}
			}
	}
}

So it would look something like this?

function database()
{

//establish a connection
    mysql_connect("localhost","USER","PASSWD") or die(mysql_error());
    mysql_select_db("DATABASE") or die(mysql_error());

//then perform the query operations
  $sql = "SELECT  category_id,
                   directory,
                   masked_file_name
           FROM    ibf_gallery_images
           WHERE   category_id='60'
         ";
         
  $result = mysql_query ($sql);

  return $result;
}

 

So how does that function loop until there are no more results? It seems that would only query and give back 1 result. So I'm not getting the effect of the following when I call on the function:

while($row = mysql_fetch_array($result)){
echo "{$row['masked_file_name']}";
echo "<br />";
}

 

And to call the function later on in the code, it would just be the following right?

database();

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.