I have a form that submits details of multiple people i.e in the $_REQUEST I get :
title1 = Mr,
first_name1 = 'Whatever',
surname1 = 'Whatever',
title2 = Mr,
first_name2 = 'Whatever',
surname2 = 'Whatever'
There's obviously more but this explains the situation. There could be ten people being submitted and therefore it would go up to title10, first_name10, surname10...
I have been trying to use:
for ($x = 1; $x < 4; $x++) {
$a = new applicant();
$a->title = $_REQUEST['title'+$x];
$a->first_name = $_REQUEST['first_name'+$x];
$a->surname = $_REQUEST['surname'+$x];
$a->Save();
}
However it appears that you cannot do this +$x bit. I know there is a way around it since I remember doing this ages ago yet I don't have my code at work :/
Any ideas guys?
PHP uses . for concatenating strings, and + for adding numbers; this is different from some other languages which use + for both. Possibly confusing, but unlikely to change.
'title' + $x will try to add the parts as if they were numbers, casting if necessary.
'title' . $x should do what you seem to be looking for.
Read also: The Fine Manual
String concatenation in PHP uses the operator . not +. For example: $_REQUEST['title' . $x];
You can use the same attribute name in your HTML form for mulitple inputs like so:
<input name="title[]" />
...
<input name="title[]" />
Which when submitted to the PHP script will be available in the GET/POST as an array already.
you may try this:
$a = "first part";
$b = "second part";
$c = ${$a.$b};
or something like
$c = $_REQUEST[${$a.$b}];
Anyway, you got the idea.
Related
I have this code:
$i = '0001';
$j = $i + 1;
I want to get 0002 but it give me only 2
How can I plus $i and keep that 000?
Thank you
Maybe this can give you an idea:
$i = '0001';
$i = intval($i);
$i++;
$a = '000';
$result = $a.$i;
echo $result;
The . between $a and $i is concatenating the variables into a string, keeping every character in them.
The code above will output 0002
Check it here: http://sandbox.onlinephpfunctions.com/code/fc2bd5861a6c0d937568b067e98e06977741019c
If you want to keep all leading zeroes, you could count them separately and increment it like this. Of course, this is something whipped out quickly and I'm 100% certain there are much better ways of doing this out there.
You can use str_pad to keep the leading zeroes.
Code here,
<?php
$i = '0001';
$val = $i + 1;
echo str_pad($val,4,"0",STR_PAD_LEFT); // 0001
?>
Click to learn more about str_pad
PHP took a useful feature from Perl: the ability to increment strings, using ++. Unfortunately, it didn't do it very well. If you increment "A0001", you get "A0002"; but if you increment "0001", you get 2 (as opposed to original Perl, which would give you "0002"). But, you can cheat:
$i="0001";
$j=":$a";
$j++;
$j=substr($j, 1);
This is more to both illustrate a cool thing not many people might be aware of, and how it was misimplemented, than to provide a real solution; I would definitely prefer to see a str_pad or sprintf solution in my code.
I'm familiar with some of PHPs string functions, however I can't seem to find the right one to do what I want. What I'm trying to accomplish is to echo 22 characters before and after a string that I find. I'm able to find the starting position of the string as well as the string length...I'm thinking that these might be useful to accomplish what I need to. For example, let's say I find the string "hello". I want to echo "This is an example of hello how are you doing????" Will substr() accomplish this?
$var2 = 'hello';
$startingpos = strpos($var1, $var2));
$strlength = strlen($var2);
UPDATE: I've found the solution to my problem: Please see the below:
$additional_length = 2;
$startingpos = strpos($var1, $var2));
$strlength = strlen($var2);
if (($startingpos - $additional_length) < 0)
$start = 0;
else
$start = $startingpos - $additional_length;
echo substr($var2, $start, ($strlength + (2* $additional_length)))
You can easily set a constant inside your string, and then replace it with str_replace function.
For instance:
$results = str_replace("[FOUND]", "hello", "This is an example of [FOUND] how are you doing");
Working example:
function.onl/str_replace("[FOUND]", "hello", "This is an example of [FOUND] how are you doing");
Demo
Im making an insert record code in php then i need to make a auto code, or the user just can look at the prepared value of the textbox, Ill just use this code.
$ayos = mysql_fetch_array(mysql_query("select ib_code from item_brand order by ib_code desc limit 1"));
$new_val = $ayos['ib_code'] + 1;
when you display it just use:
echo $ayos['ib_code'];
for example the output is 006. the thing i cant resolve was when i try to display value of $new_val in the textbox, for sure our expected result if the $ayos['ib_code'] = 006 then the $new_val is equal to 007, but the problem is it is only 7 not 007, when displaying inside the textbox.. thx
Try
if you want to display a number left-padded with zeros, consider using printf() (or one of its numerous relatives).
Tested Example:
<?php
$a = '006';
$b = $a + 1;
?>
<input type="text" value="<?php printf("%03d", $b); ?>" />
Above code is tested.
sprintf() can do that:
echo sprintf('%03d', $ayos['ib_code']);
Or use str_pad() - but that's a little bit longer in code:
echo str_pad($ayos['ib_code'], 3, 0, STR_PAD_LEFT);
Response to Some User Comment
well if there are value like 0006 or 000006 then we can use above code is as below.
<?php
$a = '000006';
$b = $a + 1;
?>
<input type="text" value="<?php printf("%0".strlen($a)."d", $b); ?>" />
what i am doing is passing length dynamically so if in future number size increase code will still work as expected.
Using the + operator forces PHP to interpret the variable as a number, which will truncate any leading zeroes. You'll have to do some interesting string manipulation most likely.
The 006 ins converted to number and after that it adds 1 to it, It becomes 7 , When it becomes number it truncates zeros from left side.Use below code, it will pad 0 to left.
echo str_pad($ayos['ib_code'], 3, "0", STR_PAD_LEFT); //becomes 007
Use $realnumber = intval($new_val);
echo $realnumber;
Assign $new_val variable like below:
//GET count of leading 0's into variable
$leading_zero_count = strspn($ayos['ib_code'], "0");
$new_val = $ayos['ib_code']+1;
/**
* Pad the $new_val variable with 0's so that
* new length = total leading 0's + new length of $new_val
**/
$new_val = str_pad ($new_val, $leading_zero_count+strlen($new_val),0, STR_PAD_LEFT);
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.
I have a question about syntax in php. What is the mean of + in following line? Thanks!
+$array['key1']['key2'] = "value"
It has no meaning, it is superfluous. You can write the exact same statement without the plus:
$array['key1']['key2'] = "value"
If you have that from a unified diff file, it means that this line was added. So that plus is not PHP code, it is a marker for that line in the diff/patch. The other marker is minus - for removing a line.
It is used for showing the line diff. in different versions of a same file.
Deleted line can be shown as,
- $array['key1']['key2'] = "value";
Added line can be shown as,
+ $array['key1']['key2'] = "value";
Edit: Apparently I misunderstood the question, so this answer is invalid.
It look like a "shorthand" technique.
+$array['key1']['key2'] = "value"
should be the same as:
$array['key1']['key2'] = $array['key1']['key2'] + "value"
I have never seen it used like this, so I could be wrong. I know it as:
$x++;
is the same as:
$x += 1; or $x = $x + 1;
and I know that ++$x; exist also as a pre-increment