I have a multiple checkbox in my html table with $number as index
">
I get the value of the multiple checkbox:
$checklist = $_POST['checkbox'];
The value of the $checklist are correct when i do echo.
$i = $checklist[0];
echo $i; // 2 for example
Now i try to get the node number $i (2) in my xml file
$participants = simplexml_load_file($fileName);
Then this line echo $participants->participant[$i]->name; display blank
Or if define $j = 2; then echo $participants->participant[$j]->name; display the name of the participant 3 ($j + 1) and that is correct.
That mean $check or $i = $checklist[0]; don't give a simple number like $j, any idea please ? Thanks in advance.
The problem is just because php type the variable, so $check is not int type, $j is int type.
Just do $i= (int)$check and it works well when doing $participants->participant[$i]->name;
Thansk for your participation.
Related
Generate N number of varible based of my array length in PHP.
for($i; $i<n; $i++){
//varible should be there and should be unique on every index.
}
Use This
for($i=0; $i<$n; $i++){
${'Name'.$i} = "10$i";
}
On every time when loop executed $i value will be changed
if n = 2
then Variable should be
$Name0 = "100";
$Name1 = "101";
I am trying to be able to make a loop that lets me place stars on a website, to show a rating (1 to 5) on the website, that you can put down with fields. However, I do not know how to convert the field correctly.
I am using WordPress with Custom Field types, this is were I define the variable.
I have a variable that I get from somewhere else, and I have tried the following two ways:
<?php
$star = intval(the_field('rating'));
for ($i=0; $i < $star; $i++){
intval(the_field('rating'));
}
?>
and the second one:
<?php
$star = number_format(the_field('rating');
for ($i=0; $i < $star; $i++){
number_format(the_field('rating'));
}
?>
Thank you very much for you help in advance.
First the_field echos the value, you need get_field to return the value. Then just repeat whatever string you're using to display a star or empty star:
$stars = get_field('rating');
echo str_repeat('*', $stars) . str_repeat('O', 5-$stars);
With a loop just check if your counter is less than or equal to the star rating:
$stars = get_field('rating');
for ($i=1; $i <= 5; $i++){
echo ($i <= $stars) ? "*" : "O";
}
How come this code below echo's 2 and does not give an error, does it just ignore +1+2+3+4 ?
I've searched but couldn't find an answer.
<?php
$i = 1;
$i+++1+2+3+4;
echo $i;
That line:
$i+++1+2+3+4;
Says:
Increment $i
Add the value of $i pre increment to +1+2+3+4, but don't store the result anywhere.
Hence $i == 2.
If you wouldn't want it to be ignored, you should store the result:
$i = $i+++1+2+3+4;
You never assign the completed operation anywhere:
These two are functionally equivalent:
$i++;
$i = $i + 1;
both will increment $i by 1, and save that incremented value in $i
With $i+++1+2+3+4 you're essentially executing
($i++) + 1 + 2 + 3 + 4
which is
$i = $i + 1;
1 + 2 + 3 + 4; // useless, result not stored anywhere
so which increments $i by 1, saves that to $i, then does the other additions. But since those aren't being saved anywhere, the result is thrown away.
if you had
php > $i = 1;
php > $i = $i+++1+2+3+4;
^^^^^----add this
php > echo $i;
11
then it would have worked as you expect.
All is fine. You just forgot the assignment, so i is affected only by ++ operator:
<?php
$i = 1;
$x = $i+++1+2+3+4;
echo "{$i} vs "{$x}";
would return
2 vs 11
$i++ means add 1 to $i.
and like python, the +1+2+3+4 means add the value of $i pre increment to +1+2+3+4 but don't store it anywhere.(so no memory address or anything like that...).
so what you get is just $i==2
I'm looking to increment a number after a certain letter.
I have a list of own Ids and i would like to increment it without write it manually each time i add a new id.
$ids = array('303.L1', '303.L2', '303.L3', '303.L4');
so i use the END() function to extract the last id from this array.
this is what i've tried but i cannot get a result.
$i = 0;
while($i <= count($ids)){
$i++;
$new_increment_id = 1;
$final_increment = end($last_id) + $new_increment_id;
}
echo $final_increment;
New method, but it is adding me double dot between number and letter.
$i = 0;
while($i <= count($ids)){
$i++;
$chars = preg_split("/[0-9]+/", end($ids));
$nums = preg_split("/[a-zA-Z]+/", end($ids));
$increment = $nums[1] + 1;
$final_increment = $nums[0].$chars[1].$increment;
}
//i will use this id to be inserted to database as id:
echo $final_increment;
Is there another way to increment the last number after L ?
Any help is appreciated.
If you don't want a predefined list, but you want a defined number of ids returned in an $ids variable u can use the following code
<?php
$i = 0;
$number_of_ids = 4;
$id_prefix = "303.L";
$ids = array();
while($i < $number_of_ids){
$ids[] = $id_prefix . (++$i); // adds prefix and number to array ids.
}
var_dump($ids);
// will output '303.L1', '303.L2', '303.L3', '303.L4'
?>
I'm a bit confused because you say "without write it manually". But I think I have a solution:
$ids = array('303.L1', '303.L2', '303.L3', '303.L4');
$i = 0;
while($i <= count($ids)){
++$i;
//Adding a new item to that array
$ids[] = "303.L" . $i;
}
This would increment just that LAST number, starting at zero. If you wanted to continue where you left off, that'd be simple too. Just take $i = 0; and replace with:
//Grab last item in array
$current_index = $ids[count($ids) - 1];
//Separates the string (i.e. '303.L1') into an array of ['303', '1']
$exploded_id = explode('.L', $current_index);
//Then we just grab the second item in the array (index 1)
$i = $exploded_id[1];
I have the next situation, lets say i have an object with 10 attributes, named r1, r2, r3...r10. Now i want to extract the value of each attribute dynamically. for that i make a for like this and know it will work
$sum = 0;
for($i = 1; $i <= 10; $i ++){
$key = "r{$i}";
$sum += $this->$key;
}
This is a representative example, what i want to know is if instead of doing that, i could do something like
for($i = 1; $i <= 10; $i ++){
$sum += $this->r{$i};
}
and take the extra line off... i have tried several forms of concatenate this like that but i cant figure it out. Can any one tell me if it is possible and how.
That's because you don't use +=, use .= when concatenating :-)
Have a read of this: http://www.php.net/manual/en/language.operators.string.php
You can do that :
$sum += $this->{'r'.$i};
Having multiples attributes named like that sounds like a problem for me. Why don't you use an array ?