Jump to content

[SOLVED] Function tidy-up


scottybwoy

Recommended Posts

Hi All,

 

I'm just updating a function of mine but there are a few problems I'd like to clear.  First the code then I'l point out the issues.

 

<?php
// Checks if $arr1 has anything extra than $arr2 then writes those diffrences
// to $tbl at the given location
function updateDiff($arr1, $arr2, $tbl, $col, $val, $optionalCol, $optionalVal) {

if (!empty($diff)) {
	$colvals = "";

	foreach ($diff as $k => $v) {
		trim($v);
		$this->cleanSql($v);
		if (is_int($v)) {
			$colvals .= $k . " = $v, ";
		} else {
			$colvals .= $k . " = '" . $v . "', ";
		}

	}

	$colvals = substr($colvals, 0, -2);
	$sql = "UPDATE $tbl SET " . $colvals . " WHERE $col = '" . $val . "'";

	if (isset($optionalCol)) {
		$sql .= " AND $optionalCol = '" . $optionalVal . "'";
	}

	mssql_query($sql) or trigger_error("Failed to Update $tbl where $col = $val!", E_USER_ERROR);

	$keys = array_keys($diff);
\\-- Think I need a foreach() here!
	$keys = implode(', ', $keys);
	$old = implode(', ', $arr1);
	$new = implode(', ', $diff);

	$msgType = 3;
	$userId = $_SESSION['USER_ID'];
	$msg = "Changed $old data was updated with this $new data";
	$qry = "SELECT * FROM $tbl WHERE $col = '" . $val . "'";

	if (isset($optionalCol)) {
		 $qry .= " AND $optionalCol = '" . $optionalVal . "'";
	} 

	$msgcols = "(msgType, userId, msg, qry)";
	$msgvals = "(3, \'" . $userId . "\', \'" . $msg . "\', \'" . $qry . "\')";

	$sql = "INSERT INTO msgLog $msgcols VALUES $msgvals";
	mssql_query($sql) or trigger_error("Failed to Log Confirmation Message", E_USER_WARNING);
} else {
	trigger_error("Nothing to change", E_USER_NOTICE);
}
}
?>

 

First of all in the supplied arguments for the func, is there a way of specifying the them to be optional for instance like with [] but I tried that already, and I know the func will work anyway so isn't too important.

 

Second, where I put the comment for a foreach() I'd like to change my $msg to read :

"Changed $keys in original row from $oldvals to $newvals"

Now I have $keys and $newvals but to get $oldvals I need to extract just the keys from $arr2 that are in $diff and put the values into a string in the same format,  I'm a bit stuck on how to go about that.

 

Lastly, I want to store a query in my table, but it doesn't like the syntax, can anyone tell me the correct way to do it,  thanks for your help.

Link to comment
Share on other sites

OK I solved my second problem which I thought would be the hardest by using array_intersect_keys()

 

So that section now looks like so :

 

<?php

$keys = array_keys($diff);
$keys = implode(', ', $keys);
$keys = substr($keys, 0, -2);
$old = array_intersect_key($arr1, $diff);
$old = implode(', ', $old);
$new = implode(', ', $diff);

$msgType = 3;
$userId = $_SESSION['USER_ID'];
$msg = "Changed $keys from : $old to : $new where $col = $val in $tbl";
echo $msg . "<br>";

?>

 

Any updates on the other issues?

Link to comment
Share on other sites

OK I got around the other two errors.

 

For the optional arguments within the func spec i added = false.  Like so :

<?php
function test($foo, $bar = FALSE) {
    echo $foo . $bar;
}
?>

 

second error, I now use my predefined database functions and store the parameters in a csv string in the database like so :

<?php
$msgType = 3;
$userId = $_SESSION['USER_ID'];
$msg = "Changed $old data was updated with this $new data";
$qryParams = "SELECT_ALL,$tbl,$col,$val,";

if (isset($optionalCol)) {
 $qry .= ",$optionalCol,$optionalVal";
}

$msgcols = "(msgType, userId, msg, qry)";
$msgvals = "(3, '" . $userId . "', '" . $msg . "', '" . $qry . "')";

$sql = "INSERT INTO msgLog $msgcols VALUES $msgvals";
mssql_query($sql);
?>

 

Then to call it I just use :

<?php
if(preg_match("SELECT_ALL", $qryParams) {
    $this->select($tbl, $col, $val);
}
?>

 

Although I still thinks thats a bit messy if some one can see a clearer case still I'd be glad.

 

Thanks

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.