Subtract 2 random numbers, unable to compare - random

I'm new to coding so it's probably a stupid mistake but I just can't figur out what is going wrong. I made an email form with a function to check if it's a human sending the message.
They have to answer a simple math question generated with random numbers.
But when I try to check if the input is correct, something goes wrong.
The relevant parts of the code I'm using:
First I generate two random numbers, calculate the correct outcome and turn the answer into a variable:
$number = array( mt_rand(1,5), mt_rand(6,10) );
$outcome = $number[1] - $number[0];
$human = $_POST['message_human'];
The part where to put the answer:
<label for="message_human">
<input type="text" style="width: 44px;" placeholder="…" name="message_human"> + <?php echo $number[0];?> = <?php echo $number[1];?>
</label>
The part to check if the answer is correct, and perform action:
if(!$human == 0){
if($human != $outcome) my_contact_form_generate_response("error", $not_human); //not human!
else { //validate presence of name and message
if(empty($message)){
my_contact_form_generate_response("error", $missing_content);
} else { //ready to go!
$message = "http://url.com" . $url . "\n\n" . $message;
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
if($sent) my_contact_form_generate_response("success", $message_sent);
else my_contact_form_generate_response("error", $message_unsent);
}
}
} else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
I keep getting the "not human error".
I tried making $outcome an array and all kind of operators but nothing works.
When I give the $outcome a fixed value like = "2"; everything works fine, but I want it to be a random number. Some help would be much appreciated.

If I understand your code right, you are not saving those random numbers, right?
So how could you get the correct comperison, when you send the answer to a previous generated set of random numbers when you just generate new ones?
A possible solution may be to save those values in a session variable.
session_start();
$_SESSION['outcome'] = $outcome;
and compare it later with this variable, but make sure it is not overwritten by a new generated set of random numbers.
if($_POST['message_human'] == $_SESSION['outcome']){
//correct
}

Related

Codeigniter 4 hasChanged() - I was expecting no change but hasChanged() returns true, it has changed

I am validating a form with Codeigniter 4. On submission, the first thing I check is if there is anything to update. If there isn't anything to update, warning message stating there is nothing to update appears. The problem is, I have not changed anything but hasChanged returns TRUE stating it has indeed changed. Is there any easy way to echo out what has changed? Here is my code;
public function post_update($post_id)
{
$original_post = $this->model->find($post_id);
if ($this->request->getMethod() === 'post') {
// get the form data
$update_post = $this->request->getPost();
$original_post->fill($update_post);
if (!$original_post->hasChanged()){
return redirect()->back()
->with('warning', 'Nothing to Update')
->withInput();
} else {
$this->model->save($update_post)
}
} // end method
I have echo'd out $original_post before the fill and after the fill. The fields sent are different but what is being sent has not changed, as far as I can see. WOuld like a to know what hasChanged() seems as being changed.
Also, I added the below just before if (!$original_post->hasChanged() to see what has changed:
echo 'post_id'.$original_post->hasChanged('post_id');echo '</br>';
echo 'post_category_id'.$original_post->hasChanged('post_category_id');echo '</br>';
echo 'post_user_id'.$original_post->hasChanged('post_user_id');echo '</br>';
echo 'post_title'.$original_post->hasChanged('post_title');echo '</br>';
echo 'post_slug'.$original_post->hasChanged('post_slug');echo '</br>';
echo 'post_body'.$original_post->hasChanged('post_body');echo '</br>';
echo 'post_is_publish'.$original_post->hasChanged('post_is_publish');echo '</br>';
echo 'post_image'.$original_post->hasChanged('post_image');echo '</br>';
echo 'post_created_at'.$original_post->hasChanged('post_created_at');echo '</br>';
echo 'post_updated_at'.$original_post->hasChanged('post_updated_at');echo '</br>';
echo $original_post->hasChanged();
In the above, it returns empty (meaning false meaning no change) for everything except echo $original_post->hasChanged(); which comes back as 1 meaning it has changed. How can I find out what changed??? There are no more fields in my table.
How can I find out what changed???
The Entity class provides you with a pair of methods called toArray and toRawArray:
public function toArray(bool $onlyChanged = false, bool $cast = true, bool $recursive = false): array
public function toRawArray(bool $onlyChanged = false, bool $recursive = false): array
You can use the first boolean parameter to get only what the Entity thinks has changed. If you're performing any implicit casts using Entity magic, you can use the raw version to bypass them.
I will say that hasChanged uses strict comparison, which catches a lot of people (myself included) off-guard the first time they use it; you need to be careful that you're not changing the types of data either (e.g. the integer 1 to the string '1') because hasChanged will catch that.

How to if elseif correctly with current SMARTY code?

I was converting everything from our existing bash scripts to use our new configuration. So after spending an hour, I noticed the bash scripts weren't even used! I noticed this after making changes didn't work. * facepalm*
Anyways, apparently the interface we use is based mainly on SMARTY templates. Now I want to create several rules, but I am not sure on the proper formatting. Maybe someone can jump in and advice me how to format it correctly?
The original piece of code is this:
if (($data['mem'] != $_SESSION['vps'][$data['veid']]['orig']['memory']) && ($data['mem'] > 0))
{
$parameter .= " --kmemsize ".($data['mem']*203636).":".($data['mem']*224000);
$query[] = "memory=".$data['mem'];
}
Now what I want to do is something like this:
$mem < 768
then this should happen:
$parameter .= " --kmemsize ".($data['mem']*2120000).":".($data['mem']*2140368);
$mem < 2048
then this should happen:
$parameter .= " --kmemsize ".($data['mem']*706672).":".($data['mem']*727040);
else:
$parameter .= " --kmemsize ".($data['mem']*203636).":".($data['mem']*224000);
How do I apply that based on the original code above?
Maybe someone can give me a workable example which I can use to apply it to the above code and create all the rules I need.
I am a bit worried, if I use wrong pieces of code, that I will mess up things. This is something I don't want obviously.
Thanks in advance.

Getting Helper Data php in magento

Have been trying to get a variable percentage from two helper functions inside .phtml file in magento
basically i have two variables based on two helper functions that on their own output/echo static numbers. Problem is in the below php they are just displaying as the value and not dividing/multiplying. So like i said the helper data & module work is using the data as variables to do/complete the equation within the .phtml file. See code below
get->FunctionA() just equals a round number value (from a collection)
get->FunctionB() just equals a round number value as well (from a collection)
probs not the best way but this just outputs two values from helper data and not dividing.
echo Mage::helper('module/data')->getFunctionA() / Mage::helper('module/data')->getFunctionB();
Also this does not work either, just produces the same result and is probs the best/easiest way
$dataA = Mage::helper('module/data')->getFunctionA();
$dataB = Mage::helper('module/data')->getFunctionB();
$result = ($dataA / $dataB) * 100;
echo $result;
Like i said above the values can be echoed (in either helper function or phtml files but the actual calculation does not wont to work
Any help would be great
Ok sorted the problem out. What was happening was that in the helper file the getFunctionA() & getFunctionB() value was being echoed when i should have been just returning the value then echoing the helper function in the .phtml file. Doh! See below the helper function example that is now working Woohoo!!!
public function getFunctionA()
{
$FunctionA = Mage::getModel('module/collection')->getCollection();
$FunctionA->addFieldToFilter('attribute', 'value_to_filter');
$FunctionA->addFieldToFilter('status','1');
return ''.count($FunctionA) . ''; //this line was the problem cause i was echoing & not returning the value
}
Now the value can be echoed in the phtml & the math equation is confirmed working
$dataA = Mage::helper('module/data')->getFunctionA();
$dataB = Mage::helper('module/core')->getFunctionB();
$result = ($dataA / $dataB) * 100;
echo $result;
this code does the math and now i can rest.

Format Output of Placeholder

I am creating a dynamic list of placeholders, some of the values held in these place holders are decimal numbers that are supposed to represent money.
What I'm wondering is if there is a way I can format them to display as such?
Something like [[+MoneyField:formatmoney]]
I see http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-(output-modifiers) but I do not see a way to do this here.
You most definitely can, under the header "Creating a Custom Output Modifier" on the link you posted it's described how you can place a snippet name as a output modifier. This snippet will recieve the [[+MoneyField]] value in a variable called $input.
So you'd have to create this custom snippet which could be as simple as
return '$'.number_format($input);
Another version of doing this is calling the snippet directly instead of as an output modifier like so:
[[your_custom_money_format_snippet ? input=`[[+MoneyField]]`]]
I'm not sure if theres any difference between the two in this case. Obviously you can pass any value into the number format snippet when calling it as a snippet instead of an output modifier. And i'm sure theres a microsecond of performance difference in the two but i'm afraid i don't know which one would win. ;)
Update:
Actually found the exact example you want to implement on this link;
http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-%28output-modifiers%29/custom-output-filter-examples
Snippet:
<?php
$number = floatval($input);
$optionsXpld = #explode('&', $options);
$optionsArray = array();
foreach ($optionsXpld as $xpld) {
$params = #explode('=', $xpld);
array_walk($params, create_function('&$v', '$v = trim($v);'));
if (isset($params[1])) {
$optionsArray[$params[0]] = $params[1];
} else {
$optionsArray[$params[0]] = '';
}
}
$decimals = isset($optionsArray['decimals']) ? $optionsArray['decimals'] : null;
$dec_point = isset($optionsArray['dec_point']) ? $optionsArray['dec_point'] : null;
$thousands_sep = isset($optionsArray['thousands_sep']) ? $optionsArray['thousands_sep'] : null;
$output = number_format($number, $decimals, $dec_point, $thousands_sep);
return $output;
Used as output modifier:
[[+price:numberformat=`&decimals=2&dec_point=,&thousands_sep=.`]]

Requesting Picture for Event

I've been trying to grab event pictures for a few days now, with no luck. I'm making a normal request just like all the other event elements:
https://graph.facebook.com/eventid/picture
Here's a non-functioning example (in explorer):
https://developers.facebook.com/tools/explorer/?method=GET&path=107798885938238%2Fpicture
However parent, attending, declined, all work..
Thank you!
I've had the same problem as you have.
After some looking into it, I found the solution.
Use http://graph.facebook.com/eventid/?fields=picture&type=large
You can choose small, normal, large or square as type.
You can call this as a separate api call,
$event = $facebook->api('/'.$event['id']);
$picture = $facebook->api('/'.$event['id'].'?fields=picture&type=large');
$picture = $picture['picture'];
echo '<img src="'.$picture.'">';
echo $event['name'];
echo $event['end_time']; // will work, no need to call
or call for other fields at the same time. But then you'll have to define every field you want to call.
$event = $facebook->api('/'.$event['id'].'?fields=name,start_time,picture&type=large');
echo '<img src="'.$event['picture'].'">';
echo $event['name'];
echo $event['end_time']; // will not work, end_time hasn't been called

Resources