Jump to content

[SOLVED] Function in a Function - value not returning


Lokolo

Recommended Posts

This is my function:

 

function filenamecreator(&$newfilename)
{
	$chars = "abcdefghijkmnopqrstuvwxyz023456789";
    	srand((double)microtime()*1000000);
    	$i = 0;
    	$newfilename = '' ;
while ($i <= 7) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $newfilename = $newfilename . $tmp;
        $i++;
    }

 

I can use it in the main program it works

however i use it in another function, the code used below

 

$flag = 0;
	while ($flag = 0)
		{
			$flag = 1;
			filenamecreator($newfilename);
			$get_otherfilenames = mysql_query("SELECT * FROM logfileinfo");
			$num=mysql_numrows($get_otherfilenames);
			$count = 0;
			while ( $count < $num )
				{
					$otherfilename = mysql_result($get_otherfilenames, $count, "filename");
					if ( $newfilename == $otherfilename )
						{
						$flag = 0;
						}
					$count++;
				}
		}
	$add_logfile = mysql_query("INSERT INTO logfileinfo (hotelID, filename) VALUES ('$hotelID','$newfilename')");

 

However, its not adding $newfilename as anything (i.e. its "")

 

Hope someone can help!

 

Thanks

 

 

Should be ok in principle. This works (at least with PHP5)

<?php

function foo(&$bar)
{
    $bar = 'xyz';
}

function fubar() 
{
    foo($x);
    echo $x;

}

fubar();     //--> xyz
?>

 

edit : ok with 4.3 too

You want this

 

while ($flag == 0)

 

Single = is assignment, double == is comparison.  Triple === is strict comparison.

 

You could have found this bug by adding some print statements inside your while loop, and seen that they were not executed.

hm

 

i keep doing this. its not even funny anymore. im use to other programming. i'll try that but not sure if it'll work. (as you can see in the other if statement below i did use == rather than = so i do know some things ;) hehe )

 

i'll try that out :)

Try seperate functions and invoke the filenamecreator function from the first and let de filenamecreator return the filename to the first function which stores the returned filename in a variable:

 

function filenamecreator()	{
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
    	srand((double)microtime()*1000000);
    	$i = 0;
    	$newfilename = '' ;
while ($i <= 7) {
             $num = rand() % 33;
             $tmp = substr($chars, $num, 1);
             $newfilename = $newfilename . $tmp;
             $i++;
       }
       return $newfilename;
}

function addLogFile() {
   $flag = 0;
while ($flag == 0)  {
	$flag = 1;
	$newfilename = filenamecreator();
	$get_otherfilenames = mysql_query("SELECT * FROM logfileinfo");
	$num=mysql_numrows($get_otherfilenames);
	$count = 0;
	while ( $count < $num )	{
		$otherfilename = mysql_result($get_otherfilenames, $count, "filename");
		if ( $newfilename == $otherfilename )	{
			$flag = 0;
		}
		$count++;
	}
}
$add_logfile = mysql_query("INSERT INTO logfileinfo (hotelID, filename) VALUES ('$hotelID','$newfilename')");
}

 

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.