Transferring parameters to a function

Discussion in 'PHP' started by yoda699, May 22, 2012.

  1. #1
    Hi,
    I'm trying to get a function to use several parameters which have been predefined. I'm running a loop while getting some results from a database and drawing circles on html5 canvas. There could be many circles and that's why I need the loop. The predefined parameters are coordinates (x,y). What I am trying to accomplish is that as the loop progress (and $i increases) the parameter in the function also increases and uses the correct predefined coordinates. I am doing it wrong though. I hope this is clear and you might have a good solution or a better method to handle this. Thanks!

    
    $Lx21 = 400;
    $Ly21 = 300;
    $Lx22 = 300;
    $Ly22 = 220;
    $Lx23 = 120;
    $Ly23 = 190;
    // draw circles:
    function drawcircle() {
    global $circleX, $circleY;
    $circle_draw1 = " context.beginPath();\n";
    $circle_draw2 = " context.fillStyle = \"#66FF66\";\n 
                   context.fill();\n                
    context.lineWidth = 1;\n                
    context.strokeStyle = \"black\";\n";
    $circle_draw_arc = " context.arc(".$circleX.", ".$circleY.", Cradius, 0, 2 * Math.PI, false);\n";
    $circle_draw_draw = " context.stroke();\n";  
          echo $circle_draw1;  
      echo $circle_draw_arc;  
      echo $circle_draw2;   
     echo $circle_draw_draw;
    }
    $i=1; 
     while($row2 = mysql_fetch_array($result2))  { 
           drawcircle($circleX = $L2.$i, $circleY = $L2.$i); 
           $i++;  }
    
    PHP:

     
    yoda699, May 22, 2012 IP
  2. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #2
    
    $params = array(
        array(400, 300),
        array(300, 220),
        array(120, 100),
    );
    
    // draw circles:
    function drawcircle($circleX, $circleY) {
    $circle_draw1 = " context.beginPath();\n";
    $circle_draw2 = " context.fillStyle = \"#66FF66\";\n 
                   context.fill();\n                
    context.lineWidth = 1;\n                
    context.strokeStyle = \"black\";\n";
    $circle_draw_arc = " context.arc(".$circleX.", ".$circleY.", Cradius, 0, 2 * Math.PI, false);\n";
    $circle_draw_draw = " context.stroke();\n";  
          echo $circle_draw1;  
      echo $circle_draw_arc;  
      echo $circle_draw2;   
     echo $circle_draw_draw;
    }
    $i=1; 
     while($row2 = mysql_fetch_array($result2))  { 
           drawcircle($params[$i][0], $params[$i][1]); 
           $i++;  }
    
    PHP:

    this is how the parameters should be passed....
     
    e-abi, May 22, 2012 IP
  3. yoda699

    yoda699 Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks. However, it doesn't work.
    I'm only getting the value of $i for the parameters:
    drawcircle(1,1);
    drawcircle(2,2);
    .....

    What I would like to get is:
    drawcircle($Lx21, $Ly21);
    drawcircle($Lx22, $Ly22);
    ....

    The values of these variables are predefined in the beginning of the code. Thus, the function should interpret them as:
    drawcircle(400, 300);
    drawcircle(300, 220);
     
    yoda699, May 22, 2012 IP
  4. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #4
    This code is mockup, not the final product.
    This mockup works, and value of $i should reference array index.

    For example $params[0][0] is equal to 400
    For example $params[0][1] is equal to 300
    .....
     
    Last edited: May 23, 2012
    e-abi, May 23, 2012 IP
  5. yoda699

    yoda699 Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    :)It works great!CarlosStanger, it might not work for you because the rest of the mySql code is not in the code (sorry about that, but it wasn't important for the php part).Thanks e-abi!!!
     
    yoda699, May 23, 2012 IP