I can't figure out why this function keeps coming up blank any suggestions would be appreciated I think it's pretty easy to tell what I'm trying to do with this function.

function productCode($maxCode, $database_trustinme, $trustinme){
	//Creates possible's variables to be used with the product code.
	$codepossible = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
	$code_max = count($code_possible);

for($ione=0; $ione == 1; $ione++){
	for($i=0; $i == $maxCode; $i++){
		$prod = rand(1, $codepossible);
		$productCodearray[$i] = $codepossible[$prod];
	}
	$productCode = implode(",", $productCodearray);

	$selectSQL = "SELECT * FROM category WHERE productCode = '$productCode'";

	mysql_select_db($database_trustinme, $trustinme);
	$Result1 = mysql_query($selectSQL, $trustinme) or die(mysql_error());
	if(mysql_num_rows($Result1) >= 1){
		$ione = 0;
	}
}

session_start();
$_SESSION["productCode"] = $productCode;
}

Any help is greatly appreciated thank you.

    your for() loop conditions are suspect #1

    for($i=0; $i == $maxCode; $i++){

    means set $i to 0 then loop while $i==$maxcode.

    But $i is 0 , not $maxcode, so it exits the loop

    Try

    for($i=0; $i <= $maxCode; $i++){

      I tried this and also changed:

      			$prod = rand(0, $code_max);
      

      still didn't work

        In fact, you have the same problem with the for loop above that:

        for($ione=0; $ione == 1; $ione++){ 

        "Set $ione to 0, and then do the following if $ione is 1." Obviously, this means that the for() loop is never executed. You might even try a structure such as this:

        while(TRUE) {
            // rest of the code here
        
        
            if(mysql_num_rows($Result1) == 0){ 
                break;
            }
        }

        There is no such variable called $code_possible that you're trying to cout()... did you perhaps mean $codepossible ?

        You have the mysql_select_db() statement inside the loop, so that each time it loops it will try to "switch" databases. To minimize the overhead, move this statement before the loop.

        Instead of doing a "SELECT *" (a query you shouldn't ever do), you could probably just select some arbitrary value such as "SELECT 1" - that way MySQL doesn't actually try to load any data into the result set. Another optimization would be to add a "LIMIT 1" onto the end of the query, since 1 or more matches will eliminate the current code as a possibility.

          didyou change this loop too

          for($ione=0; $ione == 1; $ione++){

            I changed my code to the following but it's still not working.

            I've changed my code several times now and I can not figure out how to randomly select from this array and generate a 6 digit code based on the array any suggestion would be helpful. I could do this if I knew how to check if it existed at all in my inventory because each ProductCode has to be unique.

            <?php
            //Product Code Generator
            function productCode($maxCode, $database_trustinme, $trustinme){
            	//Creates possible's variables to be used with the product code.
            	$codepossible = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
            	$code_max = count($codepossible);
            
            mysql_select_db($database_trustinme, $trustinme);
            $selectSQL = "SELECT productCode FROM inventory";
            $Result1 = mysql_query($selectSQL, $trustinme) or die(mysql_error());
            
            for($i=0; $i >= mysql_num_rows($Result1); $i++){
            	if($Result1[$i] == $productCode){
            		for($i=0; $i >= $maxCode; $i++){
            			$prod = rand(0, $code_max);
            			$productCode .= $codepossible[$prod];
            		}
            	} 
            }
            
            session_start();
            $_SESSION["productCode"] = $productCode;
            
            echo $productCode;
            }
            ?>
            

              Why don't you you just tell us what you are trying to accomplish?

                Er, your earlier version(s) of the code was much closer. Try this:

                function productCode($maxCode, $database_trustinme, $trustinme){ 
                    //Creates possible's variables to be used with the product code. 
                    $codepossible = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); 
                    $code_max = count($codepossible); 
                    $productCode = array();
                    mysql_select_db($database_trustinme, $trustinme);
                
                while(TRUE) { 
                    for($i=0; $i < $maxCode; $i++){ 
                        $prod = rand(0, $code_max); 
                        $productCode[$i] = $codepossible[$prod]; 
                    } 
                    $productCode = implode(",", $productCode); 
                
                    $selectSQL = "SELECT 1 FROM category WHERE productCode = '$productCode' LIMIT 1"; 
                    $Result1 = mysql_query($selectSQL, $trustinme) or die(mysql_error()); 
                    if(mysql_num_rows($Result1) == 0){ 
                        break;
                    } 
                } 
                
                session_start(); 
                $_SESSION["productCode"] = $productCode; 
                }

                I saw that you're using a comma in the implode() statement... is this really what you want? Product codes such as "A,5,C,3,3,W" etc. ? If not, use an empty string as the "glue" in the implode() statement.

                EDIT: Also, to save some typing (and some space), you could shorten your $codepossible line to:

                $codepossible = array_merge(range('A', 'Z'), range(0, 9));

                  Your the man you got it. Thank you so much. That's great I'm resolving the issue now.

                    Just note in future that you had written ">=" when the suggestion was to write "<=".

                      Write a Reply...