Suggestion for checking old value against new value and commenting - php

I am trying to compare a value in a table against a new value given in a form then giving a response of what was changed but it's giving the value if true or not
$var1 (existing value in table pulled from a search to display currently value in the form)
$var2 (new value provided in form)
$comment1 (the response)
This is what it looks like now:
if ($data[var1]!='$_POST[var2]')
{
$comment1="$data[var1] changed to $_POST[$var2]";
// (this will be scrubbed but I have the simple form here)
}
else { $comment1=""; }
I have tried: if($data[var1]<>'$_POST[var2]')
I have also tried adding $old_var1=$data[var1];
Then changing to if($old_var1]!='$_POST[var2]') and if($old_var1]<>'$_POST[var2]')
But the comment still shows if they are the same or not, I only want the comment to apply if they are not the same.

You are comparing the data value to a string that is '$_POST[var2]' but not the data inside $_POST[var2]
Try: if ($data['var1']!=$_POST['var2']) if your keys are var1 and var2, or
if ($data[$var1]!=$_POST[$var2]) if the keys are stored in repective variables

Related

PHP while loop ignores its condition

At the moment i try to create a function in php which simply reads an int value of a field of an array and compares it to a max value.
If this max value is reached, it shall set the value of the field to zero, jump to the next field in the array and increase the int value stored inside.
If this value also reached the max value do the same as above.
If the above condition isn´t true it shall just increase the stored value in the array.
My Code looks like this:
if($sign_counter[0] === (count($pw_signs) - 1)){
$counter = 0;
while($sign_counter[$counter] === (count($pw_signs) - 1)){
$sign_counter[$counter] = "0";
$counter++;
}
$sign_counter[$counter]++;
}
else{
$sign_counter[0]++;
}
I allready tested this part of the function several times with different values on my website and browser. I also checked if the values were stored correctly in the array and inside needed variables.
Thats how my array looks like:
$sign_counter = array("38", "2");
For example:
$sign_counter array to store int values
(count($pw_signs) - 1) always equals 38 (because there are 39 fields in the counted array)
$counter used to determine the field position inside the array
Now if i store the value "38" in the first field and "2" in the second field of the array the code should detect, that the max value is reached in the first field, set the value of the field to 0, then jump to the next field and increase its value by 1.
Instead of what i want to achieve the code just increases the value of the first field of the array.
It looks like the while loop just ignores it's own condition but the values itself don't seem to be the problem.
I don't really get why the while loop behaves like this.
Do i completly miss something here?
Would appreciate any help.
Greetings Sleepy
The problem is that you're storing the values as strings, not numbers, and you're using the === operator, which doesn't perform type coercion between different types. count($pw_signs) - 1 will always be a number, not a string, all the === tests will fail.
Get rid of the quotes around all the numbers and it should work as desired. And if the source of the values is external, convert them to numbers with intval() before storing into the array.

TBS (TinyButStrong) get last value

I have a Word template, where I go through a TBS block and dynamically display the values. Now I'd like to compare the actual value with the last value displayed. Is there any possibility to solve this in word?
I was thinking of setting a variable and save the last value in this variable. So I only have to compare my own variable with the actual value. But I cant figure out, whether this is possible or not. Any help or other suggestions?
Example
*[myblock;block=begin]
[myblock.entry] // here I want to check if its the same as the last entry
[myblock;block=end]*
TinyButStrong cannot do that in native.
But you can use parameter « ondata » and a PHP user function in order to add the previous value in the current record.
You can also use a method of an object (see TBS documentation)
PHP :
function f_ondata_user($BlockName, &$CurrRec, $RecNum) {
static $entry_prev = '';
$CurrRec['entry_prev'] = $entry_prev;
$entry_prev = $CurrRec['entry'];
}
Template :
*[myblock;block=begin;ondata=f_ondata_user]
[myblock.entry]
[myblock.entry_prev] // here I want to check if its the same as the last entry
[myblock;block=end]*

Use a loop to look through POST variables

I'm trying to make a POST method that will receive a value from a table (that is dynamically generated). This value will be equal to a company name, and a hidden field will be there that is equal to company name + "id" appended to it.
Here's my code:
if(isset($_POST))
{
foreach ( $users as $balance_user ) {
if(isset($_POST[$balance_user]))
{
//update user meta with new balance
$newBalance = $_POST[$balance_user];
$postedID = $_POST[$balance_user.'id'];
update_user_meta($postedID, 'balance', $newBalance);
}
}
}
I keep getting the error Illegal offset type in isset or empty. Can I not pass variables in that way? For example if a company is called Acme, and that particularly named input has a value in it, I want to loop through all of the companies in the POST method, and if that part of the loop equals the company passed in the variable, it should do something.
Add these three lines to see the data, as others have indicated, clearly you are assuming some value is in $balance_user which is not there, or is different.
echo '<pre>:';
var_dump($balance_user);
echo ':</pre>';
if(isset($_POST[$balance_user]))
The pre makes it easier to read the debugging output. the :..: will show null values.
Once you run that, you will probably discover that one of your entries in $users is empty.
The output order will show you where that empty user value is.
However:
$postedID = $_POST[$balance_user.'id'];
That could be the error source as well, is there a post value that is. say, $balance_user == fred
fredid
if there isn't, of course you will instantly get that error. You aren't giving the line number of the error so I can't tell which it is, the line number will show it instantly.

passing jquery array to php - unexpected format and count() value and variable passing issue

I am passing a jquery array called 'selected' full of ids along with the opening of an ajax modal.
$('#dtDelete').on('click', function () {
$('#modal-ajax').load('/modals/m__delete.php?selected='+selected);
$('#modal-ajax').modal('show');
});
On the modal php page count($_GET['selected']); always returns 1 no matter what. I am trying to get an actual count of the number of values in the array. Turns out this is because the array is a string as noted below.
var_dump($_GET['selected']); returns something along the lines of string(69) "187419,187420,187413,187414,187415,187416,187417,187418,187421,187422" which is something I am not accustomed to (sort of new to jquery). I need to do processing in php using foreach on this array. Can I 'convert' this to a 'normal' php array so count() will work as expected and I can process it normally in php?
Lastly, this array may or may not be extremely large at times. The jquery function above opens an ajax modal (I am using the modal as a confirmation box for the user whether they really want to delete the entries in the selected array) and I know the $_GET method has limits to the amount of data it can pass. I can't do $_POST because this is a modal and I need to load it then show it... how else can I pass this data?
$_GET['selected']
returns the STRING after the attribute 'selected', and count(string) is 1 not matter what ( it's not a multi-dimension array to be greater than 1).
As for the comma separated string example you gave, you may use the following :
$selected = $_GET['selected'];
//test wether the string has contents
if(strlen($selected)!=0) {
$selected_array = explode(',',$selected); //this is the new array that you want
}
else {
//the string is empty
}
There are many string functions you may check at : http://www.php.net/manual/en/ref.strings.php

Serialize checkbox array, also get checkboxes with off-value

I was wondering if this is possible.
The serialize-string will be inserted in a mySQL-database, so it would be fine if all checkboxes would be inserted and not only the ones that is ticked(on).
it's possible to make on column in the database for each of the checkboxes, and use this if/else-statement:
if(isset($_GET['checkbox'])) {
// It's checked!
}
else {
// not checked!
}
but its a bit unappropriate...
Give all the checkboxes the same name (ending in [] since this is PHP)
Give all the checkboxes different values
Have an array of all possible values in the script (you can use the same array when generating the HTML for the form!)
Loop over it and use in_array to determine if you should set it to true or false
Pass the result to your database function

Categories