Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Cool insert variable into function

    I appreciate your taking a quick look at this. This template takes a couple of variables sent through the url and then generates a pdf for a ticket to one of our shows. What I am doing is setting up a random ad placement. The following code looks at the folder with the ads and selects one to include in the pdf:
    Code:
    //create variable for ad placement
    $imglst='';
    $img_folder="ticketimages/ads/";
    mt_srand((double)microtime()*1000);
    $imgs =dir($img_folder);
    
    while($file = $imgs->read()){
    	if (eregi("gif",$file) || eregi("jpg", $file) || eregi("png", $file))
         $imglist .= "$file ";
     } closedir($imgs->handle);
    
      //put all images into an array
     $imglist = explode(" ", $imglist);
     $no = sizeof($imglist)-2;
    
     //generate a random number between 0 and the number of images
     $random = mt_rand(0, $no);
     $adimage = $imglist[$random];
    The template then queries the database for information on the ticket purchases and then assembles that with the image files that comprise the ticket.

    Then on line 198 I want to insert the image that the code above retrieved with the use of a function
    Code:
    function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
    So I thought this might work replacing hte literall string with the variable $usethisimage

    Code:
    $this->Image($usethisimage,15,235,180,20,'jpg','http://www.someplace.net');
    But that didn't work. If I can get the variable into the function It will work.

    Any ideas?


    Here is the entire template:
    Code:
    <?
    
    function proper($txt)
    {
    	$txt = strtolower($txt);
    	$txt = ucwords($txt);
    	return($txt);
    }
    
    require("fpdf.php");
    
    //create variable for ad placement
    $imglst='';
    $img_folder="ticketimages/ads/";
    mt_srand((double)microtime()*1000);
    $imgs =dir($img_folder);
    
    while($file = $imgs->read()){
    	if (eregi("gif",$file) || eregi("jpg", $file) || eregi("png", $file))
         $imglist .= "$file ";
     } closedir($imgs->handle);
    
      //put all images into an array
     $imglist = explode(" ", $imglist);
     $no = sizeof($imglist)-2;
    
     //generate a random number between 0 and the number of images
     $random = mt_rand(0, $no);
     $adimage = $imglist[$random];
    
    $usethisimage = $img_folder.$adimage;
    
    class PDF extends FPDF
    {
    	
    //Page header
    function Header()
    {
    
    	// GET order id
    	if($_GET['oid'] != "")
    	{
    		$order_id = $_GET['oid'];
    	}
    	if($_GET['OID'] != "")
    	{
    		$order_id = $_GET['OID'];
    	}
    	
    	//get customer ID
    	if($_GET['cid'] != "")
    	{
    		$customer_id = $_GET['cid'];
    	}
    	if($_GET['CID'] != "")
    	{
    		$customer_id = $_GET['CID'];
    	}
    	
    	if( is_numeric($order_id) and is_numeric($customer_id) )
    	{
    		require_once("../includes/configure.php");
    		mysql_connect(DB_SERVER,DB_SERVER_USERNAME,DB_SERVER_PASSWORD);
    		mysql_select_db(DB_DATABASE);
    		
    		//get orders from mysql database for testing purposes
    		$rsOrders = mysql_query("SELECT * FROM orders WHERE customers_id = $customer_id AND orders_id = $order_id LIMIT 1") or die(mysql_error());
    		
    		if(mysql_num_rows($rsOrders) > 0)
    		{
    			$rsProducts = mysql_query("SELECT op.*, p.products_image, p.products_image_2 FROM orders_products op JOIN products p ON op.products_id=p.products_id WHERE orders_id = $order_id") or die(mysql_error());
    		} else {
    			$redirect = true;
    		}
    	} else {
    		$redirect = true;
    	}
    	
    	if( mysql_num_rows($rsOrders) == 1)
    	{
    		$order = mysql_fetch_array($rsOrders);
    	} else {
    		$redirect = true;
    	}
    	
    	if ($redirect == true)
    	{
    		header("location: http://dev.makeupmag.com/");
    	}
    
    	
    	$order_date = date("n/d/y",strtotime($order['date_purchased']));
    	$confirm = substr($order_id,0,1).substr($order_id,strlen($order_id) - 1,1);
    	$barcode_url = "http://dev.makeupmag.com/barcode.php?text=0&barcode=$order_id&confirm=$confirm";
    
    	//Logo
    	$this->Image('ticketimages/imat_top.jpg',10,18,190);
    	//Arial bold 15
    	$this->SetFont('Arial','B',15);
    
    	// order number
    	$this->Ln(11);
    	$this->Cell(145);
    	$this->Cell(10,10,$order_id);
    	
    	// order date
    	$this->SetFont('Arial','',11);
    	$this->Ln(30);
    	$this->Cell(30);
    	$this->Cell(10,10,$order_date);
    	
    	//barcode
    	$this->Image($barcode_url,135,34,40,20,"JPEG");
    	
    	// bill to											//ship to
    	$this->Ln(14);
    	$this->Cell(25);
    	$this->Cell(20,4,proper($order['customers_name']));	$this->Cell(57);		$this->Cell(20,4,proper($order['billing_name']),0,2);
    	$this->Ln(1);
    	$this->Cell(25);
    	$this->Cell(20,4,proper($order['customers_street_address']));	 $this->Cell(57);		$this->Cell(20,4,proper($order['billing_street_address']),0,2);
    	$this->Ln(1);
    	$this->Cell(25);
    	$this->Cell(20,4,proper($order['customers_city']).", ".proper($order['customers_state'])." ".$order['customers_postcode']);	$this->Cell(57);		$this->Cell(20,4,proper($order['billing_city']).", ".proper($order['billing_state'])." ".$order['billing_postcode'],0,2);
    	
    	
    	
    	//products
    	$this->Ln(23);
    		
    	$total = 0;
    	$product_ids = "";
    	while($product = mysql_fetch_array($rsProducts))
    	{
    		$product_ids .= $product['products_id'].",";
    		$qty = $product['products_quantity'];
    		$product_name = $product['products_name'];
    		$model = $product['products_model'];
    		$price = $product['products_price']; 
    		$tax = $product['products_tax'];
    		$total = $total + round($product['products_price'],2) * $qty;
    		
    		$this->Cell(8);
    		$this->Cell(13,4,$qty);
    		$this->Cell(95,4,$product_name);
    		$this->Cell(28,4); //$model
    		$this->Cell(14,4,number_format(round($tax,2),2,".",""));
    		$this->Cell(10,4,"$".number_format(round($price,2),2,".","")." USD");
    		$this->Ln(4);
    	}
    	
    	$bot_y = $this->GetY();
    	
    	// bottom
    	$this->Image('ticketimages/imat_bottom.jpg',9.6,$bot_y+20,190.7);
    	
    	// borders in middle
    	$this->Image('ticketimages/imat_mid_l.png',10,45,3.8,157.5);
    	$this->Image('ticketimages/imat_mid_r.png',195,45,5,157.5);
    	
    	//Total
    	$this->SetX(25);
    	$this->SetY($bot_y+24);
    	$this->Cell(157);
    	$this->Cell(30,4,"$".number_format(round($total,2),2,".","")." USD",0,2);
    	
    	//get location
    	
    		
    	$location = "";
    	$no_location = "\nCall for Exact Address of Event.\n\n";
    	if ( substr($product_ids,strlen($product_ids)-1,1) == "," )
    	{	$product_ids = substr($product_ids,0,strlen($product_ids)-1); }
    	
    	$rsLocations = mysql_query("SELECT ticket_event_location FROM products WHERE products_id IN ($product_ids) AND ticket_event_location IS NOT NULL;") or die(mysql_error());
    	if ( mysql_num_rows($rsLocations) < 1 )
    	{
    		$location = $no_location;
    	} else {
    		while( $locrow = mysql_fetch_array($rsLocations) )
    		{
    			if( $locrow['ticket_event_location'] != "" and $location == "" )
    			{
    				$location = $locrow['ticket_event_location'];
    			}
    		}
    		if ($location == "")
    		{
    			$location = $no_location;
    		}
    	}
    	
    	//create ad insert mechanism for this 
    
    	
    	
     
    	$this->Image($usethisimage,15,235,180,20,'jpg','http://www.imats.net');
    	
    	//print location to pdf
    	$this->SetY($bot_y + 35);
    	$this->Cell(9);
    	$this->SetFont('Arial','',8);
    	$this->MultiCell(100,3,$location);
    	
    		
    }
    
    //Page footer
    function Footer()
    {
    	//Position at 1.5 cm from bottom
    	$this->SetY(-15);
    	//Arial italic 8
    	$this->SetFont('Arial','I',8);
    	//Page number
    	$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }
    }
    
    //Instanciation of inherited class
    $pdf=new PDF();
    $pdf->AliasNbPages();
    //$pdf->AddPage();
    
    $pdf->Output("imats_eticket.pdf","D");
    
    
    ?>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I'm not good with OO, but can't you just overload the AliasNbPages() method to accept a string parameter, and send $usethisimage to the PDF class that way?

  • #3
    New Coder
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Huh? You tell me. I hope so. How would I go about that?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •