Jump to content

assign user defined function return value to a variable in a while loop


needsomephphelp

Recommended Posts

I have created a user defined function to return a string value

 


 

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());
}

 

The line of code that stops the execution of my loop is $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.

 

Why does my user defined function not process through the while loop to assign the $Model variable the return value of the getModel function?

 

Please help!


 

Edited by Zane
Link to comment
Share on other sites

My first guess is that PHP doesn't think the function is defined.

 

Very first debugging steps: make sure you have

error_reporting = -1
display_errors = on
in your php.ini, restart the web server if not (and after you add them), then try the page again.

 

Now, what error(s) do you see?

Link to comment
Share on other sites

 

My first guess is that PHP doesn't think the function is defined.

 

Very first debugging steps: make sure you have

error_reporting = -1
display_errors = on
in your php.ini, restart the web server if not (and after you add them), then try the page again.

 

Now, what error(s) do you see?

Interesting, I tried that and now it appears that my script breaks when I try to assign the $Model variable the result of the getModel function.  I dont get any errors, just a broken script.  If I comment my $Model = getModel($part); line out, it processes.  So theoretically I should be able to create a function return, and call that function on another page with an include to my function library and have a while loop pass through my $part array and assign the return value of that function to a variable?

 

I dont know if the return is exiting the loop?  Seems like it my problem is strictly tied to the line where I assign $Model the value of my getModel function. 

 

Link to comment
Share on other sites

EDIT: Why are you looping at all?  Your query makes it look like you're only trying to obtain one value.  If that's the case, don't loop:

function getModel($part) {
    $root = substr($part,0,4);
 
    $query = "SELECT Model FROM tbl_Root WHERE Root = '$root'";
    $result = mysql_query($query) or die ("Query Root failed: " . mysql_error());
    $rec = mysql_fetch_array($result, MYSQL_ASSOC);


    if (in_array($root, $rec)) {
        $Model = $rec['Model'];
    } else {
        $Model = 'UNKNOWN';
    }
 
    return $Model;
}
Edited by KevinM1
Link to comment
Share on other sites

Just to clarify, my function getModel() should only return one value for each part number.  Essentially what I am trying to do is have the script read into a list of part number strings ($part) and find various attributes of the part numbers.  In this case, my getModel function will return the model string value of one $part.  One $part will only ever have one Model, returned from the getModel() function.  I have a script that can read one $part and it properly finds the correct getModel.  The stumbling block that I have is how to really make use of my getModel function by finding the getModel for hundreds or thousands of part numbers.  I have created several functions that get the $part passed to them to find the one return value.

 

So the reason for my loop then is to take several hundred or more $part (part numbers) and pass it through to find out what the model is, etc.

 

I hope I am making sense here....

Link to comment
Share on other sites

You should never ever never put a query inside a loop.

 

You should grab all of your part values into an array, and then put that array as a parameter to your function, Of course, you will have to code around that though.  You might start by renaming your function to getModels and have it return an array of models....

Link to comment
Share on other sites

Why not just let MySQL do everything?

Something like:

Un-Tested

 

REPLACE INTO tbl_Attributes (Part, Model) SELECT b.A,a.Model FROM TempAttributes AS b JOIN tbl_Root AS a ON SUBSTR(b.A,0,4) = a.Root GROUP BY b.A

 

Not saying it will work without tweaking, but it is worth a shot.

Link to comment
Share on other sites

You should never ever never put a query inside a loop.

 

You should grab all of your part values into an array, and then put that array as a parameter to your function, Of course, you will have to code around that though.  You might start by renaming your function to getModels and have it return an array of models....

Not quite sure I understand here.  Are you saying I have to let the function pass in an array of my part numbers or return an array of return values?  Not exactly sure how to go about doing that?

Link to comment
Share on other sites

Why not just let MySQL do everything?

Something like:

Un-Tested

 

REPLACE INTO tbl_Attributes (Part, Model) SELECT b.A,a.Model FROM TempAttributes AS b JOIN tbl_Root AS a ON SUBSTR(b.A,0,4) = a.Root GROUP BY b.A

 

Not saying it will work without tweaking, but it is worth a shot.

I would agree that if every function of finding part number attributes were that straight forward, letting MySQL do the work would be much easier.  The problem is, not every attribute can use one layer of substr logic.  The substr is a key function here, but then depending on the part model, each piece of the part number string can mean something different.

Link to comment
Share on other sites

Howabout you show us the kinds of part number data you're dealing with?

 

Zane and jcbone's point is still right on, however: you have two problems you need to solve -

 

1. Taking part number data and parsing it into something useable

2. Passing that parsed/useable part number data to the database in order to get a model number/type

 

They don't need to (and likely shouldn't) be the same function.  You should have a single function that handles just the parsing of part numbers in all their variations.  Pseudo-code of the general workflow:

 

 

function parsePartNumber($part)
{
    // regex/explode/use string functions on $part
 
    return $part;
}
 
function getModels($partList)
{
    $parsedParts = array();
 
    foreach ($partList as $part) {
        $parsedParts[] = parsePartNumber($part);
    }
 
    foreach ($parsedParts as $parsed) {
        $query = // generate a SQL query on them
    }
 
    $result = mysql_query($query); // NOTE: you should really use MySQLi or PDO here as the mysql_* functions are deprecated.  Also note that it's a singular query, NOT queries executed in a loop
 
    // grab the models from the result
 
    return $models; // an array
}
 
// so, at the end, all you should do in your main code would be:
 
$models = getModels($parts);
Edited by KevinM1
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.