Passing a function argument to another function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rocknrisk
Forum Newbie
Posts: 16
Joined: Tue Mar 14, 2006 8:09 pm

Passing a function argument to another function

Post by rocknrisk »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi all,

I'm new to PHP and MySQL...

I need help on variable scope and passing a function argument to another function and returning a variable. I'm missing something so my code is not working.

I have a class (the theory of what I'm doing):

Code: Select all

class myClass(){
  var $arrArray1 = array ("A", "B", "C");
  var $arrArray2 = array ("1", "2", "3");

  var $txtVariable = fctFunction2($abc);

  function fctFunction1() {
    foreach ($this->arrArray1 as $abc) {
    // ** send $abc out as a variable here to fctFunction2
      echo "blah blah: ".$abc; // Simple. Works. Variable in this function
      echo "blah blah: ".$this->txtVariable;  // doesn't work
    }
  }

  function fctFunction2($xyz) {
    if $xyz = "A" {
      $this->txtVariable = "apple.php";
    } else {
      $this->txtVariable = $xyz.".php"; // so B.php or C.php
    return $this->txtVariable;
  }
}

$objObject = new myClass();
$objObject -> fctFunction1();
Basically, I need $abc to be passed to fctFunction2, worked on, then returned as a dependant variable $txtVariable, and finally, printed in fctFunction1.

Whew, hope that makes sense. Please help.

Clinton


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your class is attempting to set a class variable at definition with a function, this is not allowed. You could set it at instantiation time however using the class constructor.
rocknrisk
Forum Newbie
Posts: 16
Joined: Tue Mar 14, 2006 8:09 pm

while should be do... while, I'm sure, but how???

Post by rocknrisk »

Hi feyd,

Nice one. Thank you.

Although I have taken another approach but still have hassles. This is part of classdfd.php (a php document only for functions and classes. I created a class to create a left-hand menu:

Code: Select all

class clsMenuItem {
	
	var $objMenuPic = array (	"images/menu_purple.jpg",
				"images/menu_blue.jpg",
				"images/menu_green.jpg");

	function fctCreateMenu () {
	  require_once('Connections/damsel.php');
	  mysql_select_db($database_damsel, $damsel);
		
	  $query_rstMenuItems = "SELECT * FROM ltblMenuItem";
	  $rstMenuItems = mysql_query($query_rstMenuItems, $damsel) or die(mysql_error());
	  $row_rstMenuItems = mysql_fetch_assoc($rstMenuItems);
	  $totalRows_rstMenuItems = mysql_num_rows($rstMenuItems);
		
	  while ($arrMenuItem = mysql_fetch_array($rstMenuItems)) {
	    $txtMenuItem = $arrMenuItem ['txtMenuItem'];
	    $lnkLinkText = $arrMenuItem ['txtLinkURL'];
			
	    echo "<tr>";
	    echo "<td width=\"28\"><img src=".$this->objMenuPic[0]." width=\"28\" height=\"20\"></td>";
	    echo "<td width=\"10\"><img src=\"images/spacer.gif\" width=\"3\" height=\"20\"></td>";
	    echo "<td width=\"112\" class=\"style2\"><a href=\"".$lnkLinkText."\">".$txtMenuItem."</a></td>";
	    echo "</tr>";
	  }
		
	  mysql_free_result($rstMenuItems);
	}
	
} // End Class
This is the viewed document - left.php (the menu items - I want them from the database so it is easy to add caterogories that will be added to the menu if new product is added)

Code: Select all

....
<td><table width="150" border="0" cellspacing="0" cellpadding="0">

<?php
  include("classdfd.php");
  $objMenu = new clsMenuItem();
  $objMenu -> fctCreateMenu();
?>

</table></td>
    ....
There should be 16 rows. Only rows 2 to 16 show... Although, sometimes the image shows but not the first link. CRAZZZZY... This leads me to believe that it is because of the while statement. I guess I would need to change this to a

Code: Select all

do... while
statement. How do I do this please. Any help is greatly appreciated.

Clinton
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

his leads me to believe that it is because of the while statement. I guess I would need to change this to a
Code:
do... while
statement. How do I do this please. Any help is greatly appreciated.
You need to remove this line:

Code: Select all

$row_rstMenuItems = mysql_fetch_assoc($rstMenuItems);
$row_rstMenuItems was where your first row has gone.
Post Reply