Hi,
I use a query which is identical throughtout my application, only thing that differs is the select part of the query,

so for example:

 mysql_select_db("mydatabase", $con);
 $result4 = mysql_query("SELECT * FROM nap where Exchange  LIKE '$majorvalue%'");
 while($row = mysql_fetch_array($result4))
 {
$psba = $row['Site'];
$status = $row['Status'];
$spectrum = $row['InSpectrum']; 
$exchange = $row['Exchange'];
$exchange = str_replace("_"," ",$exchange);
$street = $row['Street'];
$city = $row['City'];
$county = $row['County'];
$zip = $row['Zip_Postal_Code'];
$circuit = $row['Circuit Details'];
$groupname = $row['Site_Group'];
// LETS GET RID OF UNDERSCORES AND SPACES ETC..
$expr = '/(?<=\s|^)[a-z]/i';
preg_match_all($expr, $groupname, $matches);
$newresult = implode('', $matches[0]);
$newresult = strtoupper($newresult);
/* echo statements here*/
}

The only bit that ever will change is the select statements, so I wanted to put the repeat code as a function and call it after the select statement so it would read:

    mysql_select_db("mydatabase", $con);
    $result4 = mysql_query("SELECT * FROM nap where Exchange LIKE '$majorvalue%'");
    while($row = mysql_fetch_array($result4))
    {
    myfunction();
    }

I just cant seem to get it too work?

Recommended Answers

All 4 Replies

How does your myfunction() code look like?

If it's just about getting the $row variable into the function, so it can be deal with in there, pass it in there as a function parameter.

// Function definition
function myFunction($row) {
    // Use $row here...
}

// Calling code
while ($row = ...) {
    myFucntion($row);
}

my function code is just the extracted variables like this:

function mycode(){
$psba = $row['Site'];
$status = $row['Status'];
$spectrum = $row['InSpectrum'];
$exchange = $row['Exchange'];
$exchange = str_replace("_"," ",$exchange);
$street = $row['Street'];
$city = $row['City'];
$county = $row['County'];
$zip = $row['Zip_Postal_Code'];
$circuit = $row['Circuit Details'];
$groupname = $row['Site_Group'];
// LETS GET RID OF UNDERSCORES AND SPACES ETC..
$expr = '/(?<=\s|^)[a-z]/i';
preg_match_all($expr, $groupname, $matches);
$newresult = implode('', $matches[0]);
$newresult = strtoupper($newresult);
/* echo statements here*/
}

Ooooh!

It works now, really appreciate your help there!

You're welcome :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.