number from $_GET not being read into simplexml xpath query using php - php

THIS WORKS:
1 $number = 2;
2 $allofit = simplexml_load_file("thexmlfile.xml");
3 $thebook = $allofit -> booklist[$number] -> abook;
4 echo $thebook;
THIS FOLLOWING DOES NOT WORK:
But if I want to read $number from a from with method GET I set
6 $number=$_GET[thenumber]; // $number=2 from the form//
7 echo $number; // and properly shows $number=2 --*/
8 $allofit = simplexml_load_file("thexmlfile.xml");
9 $thebook = $allofit -> booklist[$number] -> abook;
10 echo $thebook;
The echo on line 10 reports nothing (no error, just blank space in the html) even though I can successfully echo $number in line 7... so its being set, but just not picked up in line 9.. although the equivalent in line 3 works (!).
Any ideas folks?
Thanks in advance
J

$number=$_GET[thenumber];
At this stage $number is a string.
Do you need to coerce it into an integer? Probably with intval? http://php.net/manual/en/function.intval.php

Try:
$number=intval($_GET[thenumber]);

PHP should convert your "2" to a 2 without a problem. My guess is that you're missing quotes: $number=$_GET[thenumber]. Try $number=$_GET['thenumber'];

Related

Use numbers starting with 0 in a variable in php

Hi i need to save a 010 number in $number and if i do like this php will remove the starting 0
$number = 010
And echo of this will return 10 how can i make it not to remove the initial 0
BR
Martin
Use it as a String:
$number = '010';
Use str_pad() function.
echo str_pad('10',3,'0',STR_PAD_LEFT)
http://php.net/manual/en/function.str-pad.php
Do remember that numbers starting with 0 can also be treated as octal number notation by the PHP compiler, hence if you want to work with decimal numbers, simply use:
$num = '010';
This way the number is saved, can be stored in the database and manipulated like any other number. (Thx to the fact that PHP is very loosely typed language.)
Another method to use would be:
Save number as $num = 10;
Later while printing the value you can use sprintf, like:
sprintf("%03d", $i);
This will print your number in 3 digit format, hence 0 will be added automatically.
Another method:
<?php
$num = 10;
$zerofill = 3;
echo str_pad($num, $zerofill, "0", STR_PAD_LEFT);
/* Returns the wanted result of '010' */
?>
You can have a look at the various options available to you and make a decision. Each of the method given above will give you a correct output.

How to convert a "decimal string" into an integer without the period in PHP

If I have, say, 8.1 saved as a string/plaintext, how can I change that into the integer (that I can do addition with) 81? (I've got to remove the period and change it into an integer. I can't seem to figure it out even though I know it should be simple. Everything I try simply outputs 1.)
You can also try this
$str = '8.1';
$int = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
echo $int; // 81
echo $int+1; // 82
DEMO.
If you're dealing with whole numbers (as you said), you could use the intval function that is built into PHP.
http://php.net/manual/en/function.intval.php
So basically, once you have your string parsed and setup as a whole number you can do something like:
intval("81");
And get back the integer 81.
Example:
$strNum = "81";
$intNum = intval($strNum);
echo $intNum;
// "81"
echo getType($intNum);
// "integer"
Since php does auto-casting, this should work:
<?php
$str="8432.145522";
$val = str_replace('.','', $str);
print $str." : ".$val;
?>
Output:
8432.145522 : 8432145522
Not sure if this will work. But if you always have something.something,(like 1.1 or 4.2), you can multiply by 10 and do intval('string here'). But if you have something.somethingsomething or with more somethings(like 1.42 and 5.234267, etc.), I don't know what to say. Maybe a function to keep multiplying by ten until it's an integer with is_int()?
Sources:
http://php.net/manual/en/function.intval.php
http://php.net/manual/en/function.is-int.php
Convert a string to a double - is this possible?

Divide money value for payment (PHP)

First I thought this was a stupid question, and i should do some search and it would be easy to solve. But I am afraid I just ain't getting anywhere!
The thing i need to do is simple. I have a U$ value and i want to divide it by 12. Thats it.
Well, the thing is that this value is outputed by a function, and echoes ok, look:
<?php
$preconormal = wpsc_the_product_price(); // it echoes like 99.90
$precoja = str_replace (".", "", $preconormal);
echo $precoja; //echo ok -> 9990
$quantas = '12';
$parcela = $precoja/$quantas; // ok, so divide 9990 by 12, right?
echo $parcela; //no!!!!! it echoes 0 :(
?>
I really hope you can help me!
You are trying to divide strings, if you used numbers say
$quantas = 12;
$precoja = 9990;
What happens?
It should fix the division, in which case, prior to the mathmematics, convert your vars to integs by
$quantas = intval($quantas);
$precoja = intval($precoja);
//your manipulation here..l
Remove the quotes...
$quantas = '12';
to
$quantas = 12;
$precoja = floatval($preconormal)*100;
$preconormal = $precoja / 12;
I'd change your 5th line by removing the single quotes and/or 6th line with $parcela = (int)$precoja / (int)$quantas; because as soon as you use the function str_replace then $precoja becomes a string. Also having the single quotes earlier on = '12' it is also a string and that division returns 0.

PHP 8 digit number question

How to make a number 8 digit as standard digit. The number will be get to a user id from database.
Example
user_id = 1 // This should should be echo as 00000001
user_id = 11 // This should should be echo as 00000011
user_id = 111 // This should should be echo as 00000111
How can I code this? Please help thanks.
You can use printf function with %08s as the format string:
printf("%08s",$user_id);
If you want to store the result back in the string you can use sprintf as:
$user_id = sprintf("%08s",$user_id);
The format specifier %08s says:
s : Interpret the argument as a string
8 : Print the string left justified within 8 alloted places
0 : Fill the unused places with 0
You could use printf:
printf("%08d", $user_id);
PHP has sprintf:
$user_str = sprintf("%08d", $user_id);
echo $user_str;
You can do with str_pad
echo str_pad($user_id,8,'0',STR_PAD_LEFT);
$user_id = str_pad($user_id, 8, "0", STR_PAD_LEFT);
function leadingZeroes($number, $paddingPlaces = 3) {
return sprintf('%0' . $paddingPlaces . 'd', $number);
}
Source.

How to convert some character into numeric in php?

I need help to change a character in php.
I got some code from the web:
char dest='a';
int conv=(int)dest;
Can I use this code to convert a character into numeric? Or do you have any ideas?
I just want to show the result as a decimal number:
if null == 0
if A == 1
Use ord() to return the ascii value. Subtract 96 to return a number where a=1, b=2....
Upper and lower case letters have different ASCII values, so if you want to handle them the same, you can use strtolower() to convert upper case to lower case.
To handle the NULL case, simply use if($dest). This will be true if $dest is something other than NULL or 0.
PHP is a loosely typed language, so there is no need to declare the types. So char dest='a'; is incorrect. Variables have $ prefix in PHP and no type declaration, so it should be $dest = 'a';.
Live Example
<?php
function toNumber($dest)
{
if ($dest)
return ord(strtolower($dest)) - 96;
else
return 0;
}
// Let's test the function...
echo toNumber(NULL) . " ";
echo toNumber('a') . " ";
echo toNumber('B') . " ";
echo toNumber('c');
// Output is:
// 0 1 2 3
?>
PS:
You can look at the ASCII values here.
It does indeed work as in the sample, except that you should be using php syntax (and as a sidenote: the language that code you found most probably was, it did not do the same thing).
So:
$in = "123";
$out = (int)$in;
Afterwards the following will be true:
$out === 123
This may help you:
http://www.php.net/manual/en/function.ord.php
So, if you need the ASCII code you will need to do:
$dest = 'a';
$conv = ord($dest);
If you want something like:
a == 1
b == 2
.
.
.
you should do:
$dest = 'a';
$conv = ord($dest)-96;
For more info on the ASCII codes: http://www.asciitable.com/
And for the function ord: http://www.php.net/manual/en/function.ord.php
It's very hard to answer because it's not a real question but just a little bit of it.
But if you ask.
It seems you need some translation table, that defines links between letters and numbers
A -> 2
B -> 3
C -> 4
S -> 1
or whatever.
You can achieve this by using an array, where keys would be these letters and values - desired numbers.
$defects_arr = array(
'A' -> 2,
'B' -> 3,
'C' -> 4'
'S' -> 1
};
Thus, you can convert these letters to numbers
$letter = 'A';
$number = $defects_arr($letter);
echo $number; // outputs 1
But it still seems is not what you want.
Do these defect types have any verbose equivalents? If so, why not to use them instead of letters?
Telling the whole story instead of little bit of it will help you to avoid mistakes and will save a ton of time, both yours and those who to answer.
Out of this question, if you are looking for convert RT0005 to 5
$max = 'RT0005';
return base_convert($max,10,10);
// return 5

Categories