Jump to content

Recommended Posts

Hello,

 

Here's a good one.  The below script loops through the function create_cell() (2X) and the output goes into an RTF Word Doc (that's another story).  My problem is that the function should ADD the previous cell size result to the the next cell it creates after it loops through.  Let me explain:

 

The below solution has a cell size of 50.  The math in the create_cell() function returns the first cell size of 5400 (which Word understands). The second time around the size is placed in $previousResult for next cell creation.  So the total size of the next cell should now be 5400 + 5400 = 10800.  That's not happening.  Why wouldn't this work?  Here's the code;

 

The first cell is created fine.  The second cell, the math is getting all messed up.  Thanks for any help on this.

 

Call Function:

<?php 		

      do {	

		$output .= create_cell($list_ai['ai_name'], "50", "8" );

	} while ($list_ai = mysql_fetch_assoc($aiList));
?>

 

The Function:

 

<?php 

function create_cell($msg, $sizeCell, $cellColorId = NULL){

        ### Calculate the size of the cell
        $cellSize = (10800*($sizeCell/100)) + $previousResult;
        $previousResult = $sizeCell;

        ### Put the background color of the cell
        if ($cellColorId){$cell .= "\\clcfpatraw".$cellColorId."\\clshdngraw10000\n";}

        ### Assemble the cells of the table (the sum of the sizes of the cells should not exceed 21,600)
        $cell .= "\\cellx".$cellSize."\n";
        $cell .= "\\pard \\intbl ";
        $cell .= $msg." \\cell\n";

	return $cell;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/164671-math-problem/
Share on other sites

<?php 
function create_cell($msg, $sizeCell, $cellColorId = NULL) {

        static $total_size = 0;

        ### Calculate the size of the cell
        $cellSize = (10800*($sizeCell/100)) + $total_size;
        $total_size = $cellSize;
      
        ### Put the background color of the cell
        if ($cellColorId){$cell .= "\\clcfpatraw".$cellColorId."\\clshdngraw10000\n";}

        ### Assemble the cells of the table (the sum of the sizes of the cells should not exceed 21,600)
        $cell .= "\\cellx".$cellSize."\n";
        $cell .= "\\pard \\intbl ";
        $cell .= $msg." \\cell\n";
      
      return $cell;
}
?>

 

That code will allow you to save state between function calls. Usually a variable within a function only exists as long as the function runs.

Link to comment
https://forums.phpfreaks.com/topic/164671-math-problem/#findComment-868378
Share on other sites

$previousResult is a local variable in the create_cell() function, initialized every time the function is called.

You could try setting it in the calling procedure, and passing it in to create_cell() as a parameter:

function create_cell($msg, &$previousResult, $sizeCell, $cellColorId = NULL){

        ### Calculate the size of the cell
        $cellSize = (10800*($sizeCell/100)) + $previousResult;
        $previousResult = $sizeCell;

        ### Put the background color of the cell
        if ($cellColorId){$cell .= "\\clcfpatraw".$cellColorId."\\clshdngraw10000\n";}

        ### Assemble the cells of the table (the sum of the sizes of the cells should not exceed 21,600)
        $cell .= "\\cellx".$cellSize."\n";
        $cell .= "\\pard \\intbl ";
        $cell .= $msg." \\cell\n";

	return $cell;
}

$previousResult = 0;
do {
        $output .= create_cell($list_ai['ai_name'], $previousResult, "50", "8" );
} while ($list_ai = mysql_fetch_assoc($aiList));

 

 

or you could try defining it as static within the create_cell() function.

function create_cell($msg, $sizeCell, $cellColorId = NULL){
        static $previousResult = 0;

        ### Calculate the size of the cell
        $cellSize = (10800*($sizeCell/100)) + $previousResult;
        $previousResult = $sizeCell;

        ### Put the background color of the cell
        if ($cellColorId){$cell .= "\\clcfpatraw".$cellColorId."\\clshdngraw10000\n";}

        ### Assemble the cells of the table (the sum of the sizes of the cells should not exceed 21,600)
        $cell .= "\\cellx".$cellSize."\n";
        $cell .= "\\pard \\intbl ";
        $cell .= $msg." \\cell\n";

	return $cell;
}

do {
        $output .= create_cell($list_ai['ai_name'], "50", "8" );
} while ($list_ai = mysql_fetch_assoc($aiList));

 

Link to comment
https://forums.phpfreaks.com/topic/164671-math-problem/#findComment-868380
Share on other sites

Related Problem.

 

THANKS AGAIN FOR THE HELP SO FAR.  The first issue has been resolved successfully but we have a new problem.  My solution requires a return or the cells to start on the next line close_line() function which closes and returns the cells to the next line.  This should start the creating of new cells all over again at 0.

 

The following code allows this two happen but ADDS the total_size to the next line.  Moreover (which may not be related to this issue) I'm trying to figure out how to get the output of the query where data is stuffed into the first two cells then goes to the next line and continues until there is no data left in the query.  I have this somewhat working but the data is duplicated on each line in each cell.  For example:

 

Damian Clem (cell one) | Damian Clem (cell two) - Next line

John Doe (cell one) | John Doe (cell two)

 

The output should be:

 

Damian Clem (cell one) | John Doe (cell two) - Next line

Jim Smith (cell one) | etc.   

 

I'm really grateful for this forum and all the help it's been for me.  Learning a lot!  Here's the code:

 

Functions:

<?php 

function create_cell($msg, $sizeCell, $cellColorId = NULL) {

       ### Fixed by PHPFreaks.com on July 3, 2009
        static $total_size = 0;

        ### Calculate the size of the cell
        $cellSize = (10800*($sizeCell/100)) + $total_size;

       ### Fixed by PHPFreaks.com on July 3, 2009
        $total_size = $cellSize;
      
        ### Put the background color of the cell
        if ($cellColorId){$cell .= "\\clcfpatraw".$cellColorId."\\clshdngraw10000\n";}

        ### Assemble the cells of the table (the sum of the sizes of the cells should not exceed 21,600)
        $cell .= "\\cellx".$cellSize."\n";
        $cell .= "\\pard \\intbl ";
        $cell .= $msg." \\cell\n";
      
      return $cell;
  
  }
  
  
    function open_line(){

        ### Mount the table within the document
        $cell .= "\\trowd\\trgaph35\\trautofit1\\trpaddfr3\\trpaddfl3\\trpaddft3\\trpaddfb3\\trpaddr55\\trpaddl55\\trpaddb15\\trpaddt35\n";
  		return $cell;
    }

    function close_line(){
        ### Go to the next line
        $cell .= "\\pard \\intbl \\row\\pard\n";
  		return $cell;
    }
?>

 

Here is the code that calls the functions and should output the data in example explained above.

 

<?php 

	$aiList = mysql_query($query_ai, $contacts) or die(mysql_error());
	$num_ai = mysql_num_rows($aiList);
	$list_ai = mysql_fetch_assoc($aiList);

do {

		$listAI .= open_line();

	for ( $num_ai = 1; $num_ai <= 2; $num_ai++) {	

		$listAI .= create_cell($list_ai['ai_name'].'\line '.$list_ai['ai_address'], "50", "8" );

	}

		$listAI .= close_line();

} while ($list_ai = mysql_fetch_assoc($aiList));


?>

Link to comment
https://forums.phpfreaks.com/topic/164671-math-problem/#findComment-868410
Share on other sites

try

<?php 

	$aiList = mysql_query($query_ai, $contacts) or die(mysql_error());
	$num_ai = mysql_num_rows($aiList);
	$list_ai = mysql_fetch_assoc($aiList);

do {

		$listAI .= open_line();
                        $listAI .= create_cell($list_ai['ai_name'].'\line '.$list_ai['ai_address'], "50", "8" );//otput 1st data
	for ( $num_ai = 1; $num_ai < 2; $num_ai++) {	
	        $list_ai = mysql_fetch_assoc($aiList); // read next data row and output it	
		$listAI .= create_cell($list_ai['ai_name'].'\line '.$list_ai['ai_address'], "50", "8" );
                        
	}

		$listAI .= close_line();

} while ($list_ai = mysql_fetch_assoc($aiList));


?>

Link to comment
https://forums.phpfreaks.com/topic/164671-math-problem/#findComment-868420
Share on other sites

Thanks sasa.  That helped with the data stuffing.  Any idea's on the other issue with zeroing out the cell size on the next line?

 

My solution requires a return or the cells to start on the next line close_line() function which closes and returns the cells to the next line.  This should start the creating of new cells all over again at 0.  The following code allows this two happen but ADDS the total_size to the next line.

 

The cell size is being added to the creating of the next cell which is causing the cell to increase in size.  So if the first cell is 50 and the next cell is 100 the next line should be 50 and 100 and so on.  Currently 50 is being added to each cell after the next line is created.

 

REVISED since last post:

 

 
<?php 

function create_cell($msg, $sizeCell, $cellColorId = NULL) {

       ### Fixed by PHPFreaks.com on July 3, 2009
        static $total_size = 0;

        ### Calculate the size of the cell
        $cellSize = (10800*($sizeCell/100)) + $total_size;

       ### Fixed by PHPFreaks.com on July 3, 2009
        $total_size = $cellSize;
      
        ### Put the background color of the cell
        if ($cellColorId){$cell .= "\\clcfpatraw".$cellColorId."\\clshdngraw10000\n";}

        ### Assemble the cells of the table (the sum of the sizes of the cells should not exceed 21,600)
        $cell .= "\\cellx".$cellSize."\n";
        $cell .= "\\pard \\intbl ";
        $cell .= $msg." \\cell\n";
      
      return $cell;
     
     }
     
     
    function open_line(){

        ### Mount the table within the document
        $cell .= "\\trowd\\trgaph35\\trautofit1\\trpaddfr3\\trpaddfl3\\trpaddft3\\trpaddfb3\\trpaddr55\\trpaddl55\\trpaddb15\\trpaddt35\n";
        return $cell;
    }
   
    function close_line(){
        ### Go to the next line
        $cell .= "\\pard \\intbl \\row\\pard\n";
        return $cell;
    }
?>

 

Call to functions (REVISED)

 


<?php

   do {

         $listAI .= open_line();

              $listAI .= create_cell($list_ai['ai_name'].'\line '.$list_ai['ai_address'], "50", "8" );//otput 1st data
      
  for ( $num_ai = 1; $num_ai < 2; $num_ai++) {   
      
          $list_ai = mysql_fetch_assoc($aiList); // read next data row and output it   
      
          $listAI .= create_cell($list_ai['ai_name'].'\line '.$list_ai['ai_address'], "50", "8" );
                        
      }

               $listAI .= close_line();
   
   } while ($list_ai = mysql_fetch_assoc($aiList));

?>

Link to comment
https://forums.phpfreaks.com/topic/164671-math-problem/#findComment-868439
Share on other sites

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.