Understanding VBScript - php

I have a VBScript that I`m converting to PHP, I hae some part that I don`t understand and don`t know the output of ... Also if possible, provide me with a similar method in HTML/PHP
TextBox1.Value = 1#
txtTurnoverIncl = TextBox1
Format(CDbl(txtTurnoverExcl.Text) * _
CDbl(txtRoyalty.Text) / 100, "#,##0.00")
If txtTurnoverExcl.Text <> "" Then
Format(Round(.Text * 14 / 114, 2), "#,##0.00")
TextBox1 = Now()
TextBox3 = Date
TextBox4 = Format(MyDate, "dddd")
And this function:
Private Function SumCashUp() As Double
Dim i As Long
Dim tmp As Double
For i = 10 To 12
With Me.Controls("TextBox" & i)
If IsNumeric(.Text) Then
tmp = tmp + CDbl(.Text)
End If
End With
Next i
SumCashUp = tmp
End Function
I guess thats all.

TextBox1.Value = 1# - Assign the value 1 in format Double to the textbox control. (thanks MikeD)
txtTurnoverIncl = TextBox1 - Assigning reference to the control TextBox to variable called txtTurnoverIncl
CDbl(txtTurnoverExcl.Text) - Convert the text inside the textbox txtTurnoverExcl to double i.e. numeric value with decimal point e.g. 2.6 - this is useful if you want to perform mathematical operations on the value for example.
Format(..., "#,##0.00") - Format number to look like this: 2.60 or 8.25 i.e. with two digits after decimal point.
Round(.Text * 14 / 114, 2) - The .Text means you're inside With (somecontrol) block, so it's actually somecontrol.Text i.e. taking the Text of the control. Round function will round the number for example Round(662.791, 2) will return 662.79 and Round(662.796, 2) will return 662.8
Now() - Returns the current date and time on the machine where the code is executing
Date or Date() - Like Now() but only with the date, time will be 00:00:00
Format(MyDate, "dddd") - Get name of the day of week of MyDate, according to the Culture on the machine. For example for Hebrew culture it will return יום שלישי for English culture it will return Tuesday. In general, Format() given date and string will Format the date according to the string e.g. Format(Now(), "dd/MM/yyyy") will return 14/12/2010

The last function returns the sum of the values of the textboxes named "Textbox10", "Textbox11", and "Textbox12" in a pretty complicated way. I guess in PHP you would do something like this (assuming you are POSTing a form):
function sumCashUp() {
return (double) $_POST['Textbox10'] + (double) $_POST['Textbox11'] + (double) $_POST['Textbox12'];
}

first of all it would be better to analyze what the whole thing is doing (semantically) rather than looking at sequences of code. So the remainder of this post is a bit speculative ....
There are a couple of textboxes displayed on the screen
TextBox1 ... initialized with value 1(double), later containing current time (now()) (imho a sin in itself - brrrr - hope there is good business logic explanation for this)
TextBox3 ... initialized with the current date
TextBox4 ... initialized with "something we don't know" - hopefully a date (MyDate), and formated as Weekday ("dddd")
TextBox10 - TextBox12 ... seem to be used to calculate a variable SumCashUp
we have some more variables which may be textboxes as well (as sometimes we see a .Text added in the code)
txtTurnoverIncl
txtTurnoverExcl
txtRoyalty
SumCashUp
and a code fragment that calculates a 14% margin from a Gross (*14/114), rounds and formats the result ... and we have no clue about where this result is used. We can speculate that it may be another form field (because of .Text) - maybe txtRoyalty - but we don't know.
Basically all the code fragments are about putting values into textboxes displayed on the screen and/or using the values of that text boxes to compute something (like the SumCashUp or a 14% GM).
So I guess the path to the solution must be
get the source layout of textboxes
understand the business logic
create a HTML page containing a form with similar objects (textboxes, a submit button, etc.)
write PHP code that implements the business logic - most probably as a reaction to a POST event triggered by a Submit button
You already received a couple of code fragments, but one needs to put this into a greater context, otherwise the code blocks won't help.

Related

dynamic calculation of simple math expression

We are taking simple math expression as inputs from user and want to evaluate it. Total number of fields are also dynamic. Each field contains the specific css class as per their index. For example, 1st field has css field "col1", 2nd field has "col2" and so on.
Users gives us input in the form of
"col5 = col4 * col3"
We are converting it to
jQuery(".col5").val(jQuery(".col4").val() * jQuery(".col3").val())
using str_replace function. To do so, we need to do loop for total no of fields. (below is php code example)
for($colLoop = 0; $colLoop < $total_cols; $colLoop++){
$formula = str_replace("col$colLoop","parseFloat(jQuery('.col$colLoop input').val())", $formula);
}
This works but we are looking for some proper solution as it's loops unnecessary for all fields. Is it possible using some other methods? Let us know

Calculating and storing user's height using Swift, PHP, MySQL

I am an app where users can search for other users within a specified height range. In Swift I have a UIPickerView with the following values:
seekingminHeightdata = ["Under 4'","4'1","4'2","4'3","4'4","4'5","4'6","4'7","4'8","4'9","4'10","4'11","5'","5'1","5'2","5'3","5'4","5'5","5'6","5'7","5'8","5'9","5'10","5'11","6'","6'1","6'2","6'3","6'4","6'5","6'6","6'7","6'8","6'9","6'10","6'11","7'","Above 7'"]
Once a user makes a selection that value is passed to a php file and later stored in the MySQL database. My problem is I have to run a search on this field that would be something like this:
select * from users WHERE minheight < $minheight and maxheight >= $maxheight
I don't know how to perform the search with the ' in the values. I know if I convert it to inches it will simplify what I want to do, but with the special characters in Swift UIPickerView I'm stumped. Any help?
You can create a procedure and use it to compare two values. Here is an example that shows you can split a string using ' character and compare 2 parts of it.
CREATE FUNCTION SPLIT_STRING(str VARCHAR(255), delim VARCHAR(12), pos INT)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(str, delim, pos),
LENGTH(SUBSTRING_INDEX(str, delim, pos-1)) + 1),
delim, '');
SELECT #a1:= SPLIT_STRING("4'1", "'", 1);
SELECT #a2:= SPLIT_STRING("4'1", "'", 2);
SELECT #b1:= SPLIT_STRING("3'2", "'", 1);
SELECT #b1:= SPLIT_STRING("3'2", "'", 2);
SELECT #a1 > #b1;
SELECT #a2 > #b2;
You can use this procedure in your code and it depends on what you need.
You need to separate the value shown in a picker view from the value sent to your PHP script. As you stated, all of the calculations will be much simpler on the PHP side if you use inches (or centimeters or any other single unit value).
You also need to consider that most locales are metric and you should show users heights in centimeters, not feet/inches.
I suggest you keep an array of numbers, say in inches, from 48 to 84. Use that as your data. Then for display in the picker view you can convert that number to an appropriate display string such as feet/inches or cm.
When the user picks a row in the picker, you use the index to get the number from the array and pass that number to the PHP script where is can easily compare the numbers.

How to compare (min & max) Float values with MySQL?

I've a database with lon and lat geo location data.
Both are saved as float / decimal attribute in the mysql table.
Now I want to compare this stuff like:
(u.user_location_lat <= ".$lat_max." AND u.user_location_lat >= ".$lat_min.") AND
(u.user_location_long <= ".$long_max." AND u.user_location_long >=".$long_min.")
But it does not show any result (and it should!) - but also no error.
How to EASILY solve this (I actually don't want to work with spatial indexes - at least I do not understand how to do)?
Thanks.
I recommend you verify what that statement looks like, after you do the variable substitution.
That is, echo or vardump the contents of the variable containing the SQL text, before you prepare/execute the SQL statement.
There doesn't appear to be anything wrong with the form of the predicates. (These could be written using equivalent BETWEEN comparators, but it's not a problem what you have written.)
It's possible you have the min and max values swapped, or have the longitude and latitude swapped. If that's not the issue, then I suspect that the variables being substituted may be represented in scientific notation, rather than as decimal values.
e.g. the SQL text gets generated
... u.lat <= 1.241E+2 ...
rather than
... u.lat <= 124.1 ...
In the former case, MySQL is going to evaluate that literal as decimal value of 1.241.
(There's a corner case issue when the bounding box crosses the +180/-180 boundary, but I don't see that's likely a problem for most of your values, that's going to be an exceptional case, which you would probably need to setup special test case to actually have happen.)
In order to debug this, you need the actual SQL text that's being sent to the database to be prepared/executed.
(There's not enough information in your question to identify the actual problem.)
Q: How do I get the actual SQL text?
A: Construct your SQL statement as a string into a variable; and then var_dump that variable for debugging:
$sqltext = "SELECT ... WHERE u.lat <= ". $lat_max . " AND u.lat ... ";
var_dump($sqltext);
$stmt = mysqli_prepare($db, $sqltext);

MySql - bigints, php and auto string/int casting flip-flopping

I asked a question about bigints yesterday which was kindly answered. However, I have been observing some weird behaviour and would like to understand what is going on.
In my php I have an array which I send back to a javascript web client program which uses it.
In the php
sendBack = null;
sendBack[0]['TimeStamp'] = $Time; // A bigint got from a mysql table milliseconds from 1970
sendBack[0]['Text'] = $Message; // A varchar message got back from mysql
// I am guessing at this point you have worked out this is a text-chatroom thing going on
sendBack[1]['TimeStamp'] = 0; // A zero indicates an admin issue - note its an int but a small one
sendBack[1]['Text'] = $MessageAdmin;
// And I pack it up to send back
echo json_encode($sendBack);
In the js I unpack it to use with:
var json = eval('(' + data + ')');
The problem is, the 0 index TimeStamp in the js is being treated as a string but the index 1 Timestamp is being treated as an int.
From an educational point of view does anyone know what is going on?
I believe values returned from a database with PHP are always strings. In order to convert your zero-index timestamp you'd need to do something like:
sendBack[0]['TimeStamp'] = parseInt($Time, 10);
which will convert it to an integer value (base-10).
Obviously the 1-index timestamp is being set as zero directly hence the reason it's returning as an int.

float not working in mysql database

i am using $_GET['var'] to get a variable then compare it with a variable in my database. the variable is 1.1 the var is set to "float" on the database so i know it can handle decimals but when i compare them with the code below i get nothing.
include 'connect.php';
$sql=mysql_query("SELECT * FROM table WHERE stuff='$stuff'");
while ($row=mysql_fetch_assoc($sql)) {
$start=$row['start'];
}
echo $start; //nothing happens
From what I know float type isn't precise. It doesn't show you that actual value so 1.1 that you saved may not be the actual value stored. Trying setting your field as decimal and give it a length of say, 10,1 where 10 is the maximum number of digits (the precision) and 1 is the number of digits to the right of the decimal point (the scale). It should work doing query like stuff='1.1' or stuff=1.1.
WHERE stuff = '$stuff' is a String comparison.
Compare number like so
WHERE stuff = $stuff
Don't use float( even if you insert 1.1 into the table, the actual value for float type is not 1.1, but something like 1.100000023841858) . Change it to double in database (or decimal)
You might not be seeing any output because your echo is outside the loop.
The scope of your variable $start would be confined to the loop.
Change the stuff field to DOUBLE type.
Then,
SELECT * FROM table WHERE stuff=$stuff
this should be the sql query

Categories