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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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')");
}

 

Link to comment
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.