Jump to content

Function Return Through A While Loop?


ecattau

Recommended Posts

I have greatly improved my PHP knowledge recently, but have reached a major stumbling block. I have created several functions that read into a string of characters which basically represents a product part number. From these part number strings, the function reads what different attributes of the part number is. Each function has a return value since each part number would have only one of each given attribute.

 

That all works fine. I can pass one part number string into the function and get the proper value from it.

 

Now the problem is, when I want to take a list of these part number strings and loop them through the function to read the attributes of the part number, the loop ends after one string. I have read into this enough to know that the return statement will end the loop. This is where I dont have a solution. How can I get my function to return values from an array of these part number strings?

 

I greatly appreciate any and all help I can get, and hopefully I have presented my problem clearly.

Thanks

Link to comment
Share on other sites

Sure.

 

From my function library, here is my first function to find the Model of the part number:

 

function getModel($part) {

 

$root = substr($part,0,4);

 

$query = "SELECT Root, Model FROM tbl_Root

WHERE Root = '$root'";

 

$result = mysql_query($query) or die ("Query Root failed: " . mysql_error());

 

while ($rec = mysql_fetch_array($result, MYSQL_ASSOC)) {

 

if (in_array($root, $rec)):

$Model = $rec['Model'];

else:

$Model = 'UNKNOWN';

endif;

}

 

return $Model;

}

 

When trying to find the attributes of several hundred part numbers, I pass them to a MySQL temp table that would then select the distinct part numbers and loop them through my functions.

 

$query = "SELECT DISTINCT A FROM TempAttributes";

 

$result = mysql_query($query) or die ("Query failed: " . mysql_error());

 

while ($rec = mysql_fetch_array($result, MYSQL_ASSOC)) {

 

$part = mysql_escape_string($rec['A']);

$Model = getModel($part);

 

$query = "REPLACE INTO tbl_Attributes

(Part, Model)

VALUES

('$part', '$Model')";

 

$endresult = mysql_query($query) or die ("Query Load failed: " . mysql_error());

}

 

Obviously this while loop is what doesnt work because the function return value when finding the Model exits the loop. Ultimately, my function library finds several other attributes, but I am trying to get this to work with just the Model for now. I know there must be some way of doing this!

 

Thanks for the input.

Link to comment
Share on other sites

Sure. (reply to original message)

 

From my function library, here is my first function to find the Model of the part number:

 

function getModel($part) {

 

$root = substr($part,0,4);

 

$query = "SELECT Root, Model FROM tbl_Root

WHERE Root = '$root'";

 

$result = mysql_query($query) or die ("Query Root failed: " . mysql_error());

 

while ($rec = mysql_fetch_array($result, MYSQL_ASSOC)) {

 

if (in_array($root, $rec)):

$Model = $rec['Model'];

else:

$Model = 'UNKNOWN';

endif;

}

 

return $Model;

}

 

When trying to find the attributes of several hundred part numbers, I pass them to a MySQL temp table that would then select the distinct part numbers and loop them through my functions.

 

$query = "SELECT DISTINCT A FROM TempAttributes";

 

$result = mysql_query($query) or die ("Query failed: " . mysql_error());

 

while ($rec = mysql_fetch_array($result, MYSQL_ASSOC)) {

 

$part = mysql_escape_string($rec['A']);

$Model = getModel($part);

 

$query = "REPLACE INTO tbl_Attributes

(Part, Model)

VALUES

('$part', '$Model')";

 

$endresult = mysql_query($query) or die ("Query Load failed: " . mysql_error());

}

 

Obviously this while loop is what doesnt work because the function return value when finding the Model exits the loop. Ultimately, my function library finds several other attributes, but I am trying to get this to work with just the Model for now. I know there must be some way of doing this!

 

Thanks for the input.

Link to comment
Share on other sites

My impression is that your "SELECT DISTINCT A FROM TempAttributes" query isn't working the way you'd expect -- perhaps it is only returning one result, meaning this section:

 

while ($rec = mysql_fetch_array($result, MYSQL_ASSOC)) {

$part = mysql_escape_string($rec['A']);
$Model = getModel($part);

$query = "REPLACE INTO tbl_Attributes
(Part, Model)
VALUES
('$part', '$Model')";

$endresult = mysql_query($query) or die ("Query Load failed: " . mysql_error()); 
}

only loops once?

 

The "return" line only returns from the getModel() function -- it doesn't affect the while loop above. All the getModel() function does is insert a string into the $Model variable.

Link to comment
Share on other sites

I did test that possibility out and proved that my query is returning what I expected.

 

Here is the key piece that I found. The line of code that stops the execution of my loop is indeed $Model = getModel($part);

I executed the loop with this line of code in and it stops after the first part number. When I comment this line of code out, my loop processes all of my part numbers, but of course not with the return values I am looking for.

 

I strongly believe that processing my function getModel is preventing the loop from executing properly. I am completely stumped how I can get my part numbers to process through my while loop and find the return value (attribute) of my part number and load it to the table?

 

Is there anything in my getModel function that would prevent a loop from executing properly? I really believe the problem may come from the fact that once the first part number passes through the while loop and therefore being passed into the getModel function, the return statment ends the loop because the function return can only return one value and then ends the loop. If this is the case, I dont know how to get this to work. I created my function library to find each part number attribute, but it is no good to loop through more than one part number to find what I am looking for...

 

Thanks for the help!

Edited by needsomephphelp
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...