How I can get multiple textbox value by foreach in php - php

Hello can you please help that how i can get description by this loop ?
$timepicker = $_POST['timepicker'];
$discription = $_POST['description'];
foreach( $timepicker as $key => $n )
{
print "".$n." ".$discription[$key]."\n";
}

foreach( $discription as $value)
{
print $value."\n";
}

<?php
for ($i = 0, $i<= count($_POST['timepicker']); $i++){
echo $timep=$_POST['timepicker'][$i]."<br>";
echo $name = $_POST['fullname'][$i]."<br>"; // get name
echo $job = $_POST['description'][$i]."<br>"; // get jobtitle
}
?>

The correct Answer is :-
foreach( $discription as $value)
{
print $value."\n";
}

Related

How do I get final value of += operator in php?

I want to get final value of $score in the code below
foreach($request->jawaban as $key => $value){
$soal = Soal::find($key);
$kunci = $soal->kunci;
$score = 0;
if($value === $kunci){
$score+=1;
echo $score;
}
}
but its produce value
123456789101112131415161718192021
how do I get 21 value only?
Based on what others have said: But just to show you the full code
$score = 0; //define this before the loop
foreach($request->jawaban as $key => $value){
$soal = Soal::find($key);
$kunci = $soal->kunci;
if($value === $kunci) $score+=1; //simplify this, as I am lazy coder.
}
echo $score; //echo the final value, after the loop finishes.
Good Luck.
You need to echo after loop. As below:
$score = 0; //define this variable before loop
foreach($request->jawaban as $key => $value){
$soal = Soal::find($key);
$kunci = $soal->kunci;
if($value === $kunci){
$score+=1;
}
}
echo $score; // echo after loop end
Hope it helps you.
Initialize $score before the loop and also print the output after loop.
$score = 0;
foreach($request->jawaban as $key => $value){
$soal = Soal::find($key);
$kunci = $soal->kunci;
if($value === $kunci){
$score+=1;
}
}
echo $score;

Loop into an array with PHP

I have the following array:
$array = [
['2017-02-26', '2017-02-27'],
['2017-03-01'],
['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'],
['2017-01-05', '2017-01-06', '2017-01-07']
];
I'm looking to loop into this array to have something like this:
// When several dates
From 2017-02-26 to 2017-02-27.
// When only one date
On the 2017-03-01.
What I tried:
foreach ($array as $key => $value) {
$count = count($array[$key]);
if($count==1) {
echo "On the $key[$value]";
}
else {
$first = reset($array);
$last = end($array);
echo "From ".$first." to ".$last.;
}
}
But it doesn't work when there is only one date in the row.
You are looping by foreach() so it will display last echo string .Store result to one variable Eg($display) will be more easy to display that
$display = "";
foreach ($array as $key => $value) {
$count = count($array[$key]);
if($count==1) {
$display .= "On the $value[0] <br>";
}
else {
$first = $value[0];
$last = $value[$count-1];
$display .= "From ".$first." to ".$last."<br>";
}
}
echo $display;
Try this:-
foreach ($array as $key => $value) {
$count = count($value);
if($count==1) {
echo "On the ".$value[0];
}
else {
$first = reset($value);
$last = end($value);
echo "From ".$first." to ".$last;
}
}
Or just copy paste this code, it will work. Your main inside array to play with is $value.

How to get sub String from String in Php

I have a string like
$totalValues ="4565:4,4566:5,4567:6";
Now i want only values after ':' with coma separated ,i.e i want string like $SubValues = "4,5,6"
Thanks
using array_map, implode and explode
$totalValues ="4565:4,4566:5,4567:6";
echo implode(',',array_map(function($val){
$val = explode(':',$val);
return $val[1];
},explode(',',$totalValues)));
try this code it is working
<?php
$str="4565:4,4566:5,4567:6";;
$data =explode(",", $str);
echo '<pre>';
print_r($data);
$newstring="";
foreach ($data as $key => $value) {
preg_match('/:(.*)/',$value,$matches);
$newstring.=$matches[1].",";
}
echo rtrim($newstring,",");
Output
i have updated my code please check it
<?php
$str="4565:4,4566:5,4567:6";
$data1=explode(",", $str);
$newstring1="";
foreach ($data1 as $key1 => $value) {
preg_match('/(.*?):/',$value,$matches);
$newstring1.=$matches[1].",";
}
echo rtrim($newstring1,",");
you might want it this way:
$totalValues ="4565:4,4566:5,4567:6";
$temp = explode(':', $totalValues);
$subValues = $temp[1][0];
$x = count($temp);
for ($i = 2; $i < $x; $i++) {
$subValues .= ',' . $temp[$i][0];
}
echo $subValues;

PHP Remove <br> in last line with

Currently I have this code:
<?php
echo '<meta name="robots" content="noindex">';
$arr = json_decode(file_get_contents("http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json"),true);
foreach($arr as $item) {
$label = $item['label'];
$cost = $item['cost'];
$id = $item['paper_item_id'];
$member = $item['is_member'];
if ($member == "1") {
$member = "Yes";
}else{
$member = "No";
}
echo "$label = $id = $member = $cost";
echo ",<br>";
}
?>
What this code does it that it adds a tag after every line. This is the snippet that it happens in:
echo "$label = $id = $member = $cost";
echo ",<br>";
What I want is at the last line for the line echo ",<br>"; to NOT be ran. How do I do this? Please help!
Try this:
$result = array();
foreach($arr as $item) {
...
$result[] = "$label = $id = $member = $cost";
}
echo implode('<br />', $result);
Idea is to store all rows in array and join items with BR tag after your loop finished working.
Try this one:
<?php
$str = '';
$str .= '<meta name="robots" content="noindex">';
$arr = json_decode(file_get_contents("http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json"),true);
foreach($arr as $item) {
$label = $item['label'];
$cost = $item['cost'];
$id = $item['paper_item_id'];
$member = $item['is_member'];
if ($member == "1") {
$member = "Yes";
}else{
$member = "No";
}
$str .= "$label = $id = $member = $cost";
$str .= ",<br>";
}
echo substr($str, 0, -5);
?>
I like ioseb's explode solution, but here's another easy way:
foreach($arr as $item) {
$first = isset($first) && print ",<br>";
// ...
echo "$label = $id = $member = $cost";
}
This prints the line break on all but the first iteration - but before the text, just make sure $first isn't set before running it.
I would recommend storing all your items in a string rather than echoing as you go. then you can remove whatever trailing characters you want when you're done before you echo just once at the end with a simple regex like...
echo preg_replace('/\<br\/\>$/','', $myTotalOutputString);
A small rewrite of your code:
<?php
echo '<meta name="robots" content="noindex">';
$arr = json_decode(file_get_contents("http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json"), true);
$outArray = array();
foreach($arr as $item) {
$label = $item['label'];
$cost = $item['cost'];
$id = $item['paper_item_id'];
$member = ($item['is_member'] == '1') ? "Yes" : "No";
$outArray[] = "{$label} = {$id} = {$member} = {$cost}";
}
echo implode(',<br>', $outArray);
?>
Quick one-liner using rtrim:
echo rtrim('test <br />', '<br />');
But I voted for the implode answer above.

PHP - Detect change on string

I have an array which i do a foreach($array as $key => $value)
in my $key i get
name[1][1]
name[1][2]
name[1][3]
name[2][1]
name[2][2]
how can I add detect when the first index changes from [1][3]->[2][1]
any help is appreciated.
What i want to achieve is this:
<h4>Header</h4>
name[1][1]
name[1][2]
name[1][3]
<h4>Header</h4>
name[2][1]
name[2][2]
<h4>Header</h4>
name[3][1]
name[3][2]
name[3][3]
name[3][4]
Don't know if it is the best option, but this is how i managed to do it.
<?php $k = 1; $flag1 = 0; $flag2 = 1;foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
<?php
$endpos = strpos($option_name,']');
$asd = substr($option_name,5,$endpos-5);
$this->firephp->log($asd);
if($asd % 2)
{
if($flag1 === 0)
{
echo ' <h4>Header '. $k .'</h4>';
$flag1 = 1;
$flag2 = 0;
$k++;
}
}
else
{
if($flag2 === 0)
{
echo ' <h4>Header '. $k. '</h4>';
$flag2 = 1;
$flag1 = 0;
$k++;
}
}
?>
You can try like
foreach($name as $parent_key => $parent_value){
echo "<h4>Header</h4><br/>";
foreach($name[$parent_key] as $key=>$value)
{
echo $name[$i][$key]."<br/>";
}
}
foreach($array as $key => $value){
$valuevalues = array();
foreach($value as $val){
if($val != "" && !isset($valuevalues[$key][$val]))
$valuevalues[$key][$val] = "different-than-previous";
if(!isset($valuevalues[$key][$val]))
$valuevalues[$key][$val] = "equal-to-first-value";
}
}

Categories