I'm trying to loop through an array inside of an array, so that I can display either phone numbers or extension number. For example: If a user has both (phone number and extension number) then I should ONLY display phone number, but sometimes a user has only a extension number then I should display the extension number.
And here's my code:
<table style="padding: 40px;margin-left: -10px;margin-top:-38px;display: inline-block;">
<div style="margin-top:16px;margin-left:10px;">
<input type="checkbox" id="checkAll"/>
</div>
<div style="padding:20px;">
#foreach($resultArray as $key => $value)
#foreach($value as $key2 => $value2)
#if(is_array($value2))
#foreach($value2 as $key3 => $value3)
<?php
// echo var_dump($value3);
if (in_array($value3['phoneNumber'], $value3)) {
if (strlen($value3['phoneNumber']) === 11) {
$value3['phoneNumber'] = ltrim($value3['phoneNumber'], 1);
}
}
else{
$value3['phoneNumber'] = $value3['extension'];
}
?>
<tr>
<td>
<input class="input_checkbox" type="checkbox"
id="{{$key3}}customer-name-checkbox" name="{{$key3}} "
value="yes"><span style="padding-left:40px;"></span>
</td>
<td>{{$value3['firstName']}} {{$value3['lastName']}}</td>
<td>{{$value3['phoneNumber']}}}</td>
<td><input style="margin-left:60px;float: right;" type="email" class="styled-text rounded" name="{{$key3}}" id="{{$key3}}customer-name-inputField" placeholder="" value=""/><br/><br/>
</td>
</tr>
#endforeach
#endif
#endforeach
#endforeach
</div>
</table>
Can someone tell me what I'm doing wrong please? Thank you so much in advance!!
From your requirements, I think you need something like this:
<?php
foreach ($resultArray['searchUserResults']['searchUserResult'] as $key => $data)
{
if (isset($data['phoneNumber']))
echo $data['phoneNumber'];
else if (isset($data['extension']))
echo $data['extension'];
}
?>
This will output either of the two, but only phoneNumber if both are present.
You do not need all the nested foreach loops to accomplish this. Instead you iterate over the sub-array only.
Related
I have made the following form in HTML:
<form method="post" action="survey.php">
<div>
<label class="prompt">Favorite CS courses:</label>
<label>
<input type="checkbox" name="courses[]" value="course-web">Web Development
</label>
<label>
<input type="checkbox" name="courses[]" value="course-net">Networking
</label>
<label>
<input type="checkbox" name="courses[]" value="course-gui">GUI
</label>
<label>
<input type="checkbox" name="courses[]" value="course-oop">OOP
</label>
</div>
<div>
<input type="submit" value="Submit Survey">
</div>
</form>
When the user submits the form, it submits to a PHP file that I have. In the file, I have the following variable assignment to receive the courses[] from HTML:
$courses = $_POST["courses"];
In my PHP code, I have the following foreach loop so that I can print out the values from the array:
// output the user's favorite courses
echo "<p>Favorite Courses:</p> <ul>";
foreach ($courses as $course)
{
echo "<li><strong>$course</strong></li>";
}
echo "</ul>";
The problem is that when I ouput the courses, it comes out like this:
Favorite Courses:
course-web
course-net
course-gui
course-oop
I would like to change my code so that it outputs it like this:
Favorite Courses:
Web Development
Networking
GUI
OOP
How should I go about doing this without changing any of the HTML code.
The best solution that I have come up with for this problem is the following code, but I feel like it could be improved a lot:
// output the user's favorite courses
echo "<p>Favorite Courses:</p> <ul>";
foreach ($courses as $course)
{
if ($course == 'course-web')
{
echo "<li><strong>Web Development</strong></li>";
}
if ($course == 'course-net')
{
echo "<li><strong>Networking</strong></li>";
}
if ($course == 'course-gui')
{
echo "<li><strong>GUI</strong></li>";
}
if ($course == 'course-oop')
{
echo "<li><strong>OOP</strong></li>";
}
}
echo "</ul>";
Create an array that maps values submitted by the form to human-readable names:
$names = [
'course-web' => 'Web Development',
'course-net' => 'Networking',
'course-gui' => 'GUI',
'course-oop' => 'OOP',
];
Loop through the array of values received and look up the human-readable name in the array:
foreach ($courses as $course)
if ( strlen( $name = $names[ $course ] ) ) { // Yes, assign
echo "<li><strong>$name</strong></li>";
}
}
Hi I'd like to ask if my query is correct.
#foreach ($indemnity_benefits->where(['indemnity_benefit_type' => 'Ee', 'indemnity_benefit_group' => 'Basic Benefits'])->get() as $key => $benefit)
<tr>
<td>
<input type="hidden" name="bBenefitEeDesc[]" value="{{$benefit->indemnity_benefit_desc}}">
<span class="p-2">{{$benefit->indemnity_benefit_desc}}</span>
</td>
<td><input style="width: 75px;" name="bBenefitEeMaxDays[]" value="{{old('bBenefitEeMaxDays.'.$key, $benefit->indemnity_benefit_maxdays)}}" class="form-control form-control-sm"></td>
{{-- #for ($index = 0; $index < $noOfCols; $index++)
<td><input name="bBenefitEeValue{{$key}}[]" value="{{old('bBenefitEeValue'.$key.'.'.$index, '')}}" type="text" class="form-control form-control-sm"></td>
#endfor --}}
#foreach ($indemnity_values->where('indemnity_benefit_id', $benefit->id)->get() as $item)
<td><input name="" value="{{$item->indemnity_benefit_value}}" type="text" class="form-control form-control-sm"></td>
#endforeach
</tr>
#endforeach
When I put $benefit->id in the second for each only one row with columns appear. Something like this.
Here are the queries:
$sob_indemnity = SobGhIndemnityComprehensive::where('rfq_version_id', $rfq_version_id)->where('type', 'Indemnity')->first();
//Indemnity Levels
$sob_indemnity_levels = SobghIndemnityLevel::where('sob_gh_ind_comp_id', $sob_indemnity->id);
$indemnity_benefits = SobghIndemnityBenefit::where('sob_gh_ind_comp_id', $sob_indemnity->id);
$indemnity_values = SobghIndemnityBenefitsValue::where('sob_gh_ind_comp_id', $sob_indemnity->id);
Your $indemnity_values is a mutable object, so on your first line you are calling:
$indemnity_values
->where('indemnity_benefit_id', 1)
->get()
And on your second line you are calling the following that is obviously returning an empty collection.
$indemnity_values
->where('indemnity_benefit_id', 1)
->where('indemnity_benefit_id', 2)
->get()
you need to clone your query:
#foreach ((clone $indemnity_values)->where('indemnity_benefit_id', $benefit->id)->get() as $item)
i have a little problem with my program and not so experienced in php. I want to transfer an array of objects with a html form to my php file. First i will let you take a look on my code:
location.php (with executable forms)
<form action="action/add_items.php" method="POST">
<table border="1" width="20%">
<thead>
<th>
<?= $lang["location_task"][$location->getProperty("location_id")][$location_task->getProperty("location_task_id")]; ?>
</th>
</thead>
<tbody>
<?php
$location_task_items = $location_task->getProperty("location_task_items");
foreach ($location_task_items as $location_task_item) {
?>
<tr>
<td>
<?= $lang["item"][$location_task_item->getProperty("location_task_item_id")]; ?>
</td>
<td>
<?= $location_task_item->getProperty("location_task_item_value"); ?>
</td>
</tr>
<?php
}
?>
<tr>
<td></td>
<td>
<input type="hidden" value="<?php print_r($location_task_items); ?>" name="location_task_items"/>
<input type="submit" value="start"/>
</td>
</tr>
</tbody>
</table>
</form>
You can see that i only print the array in the input hidden value. Is that right?
add_items.php
$location_task_items = $_REQUEST["location_task_items"];
foreach($location_task_items as $location_task_item) {
if($Player_Has_ItemsClass->getObjectByIds($player_id, $location_task_item->getProperty("location_task_item_id")) == null) {
$player_has_items_attributes = array();
$player_has_items_attributes["player_has_items_player_id"] = $player_id;
$player_has_items_attributes["player_has_items_item_id"] = $location_task_item->getProperty("location_task_item_id");
$player_has_items_attributes["player_has_items_item_value"] = $location_task_item->getProperty("location_task_item_value");
$player_has_items = new Player_Has_Items($player_has_items_attributes);
$Player_Has_ItemsClass->insertObject($player_has_items);
} else {
}
}
I only get the array as string and this exception on the foreach in add_items.php:
Invalid argument supplied for foreach()
I also tried it with json_encode and json_decode:
print_r(json_encode($location_task_items))
json_decode($_REQUEST["location_task_items"]);
but only get (object attributes were public):
Call to undefined method stdClass::getProperty()
Thanks for you help :)
In your hidden field use json_encode and then in your destination use json_decode:
<input `type="hidden" value='<?php echo json_encode($location_task_items); ?>' name="location_task_items"/>`
You could do this
<input type="hidden" value="<?php echo serialize($location_task_items); ?>" name="location_task_items"/>
and then
$location_task_items = unserialize($_REQUEST["location_task_items"]);
In on of my views i have the following form:
<table align="center" border="1">
<tr>
<td>Identificação</td>
<td>Conc, %</td>
<td>Classificação 67/548/CEE</td>
<td>Classificação 1272/2008 (CLP)</td>
</tr>
<tr>
<td>
<textarea rows="4" cols="30" name="componentes[0][identificacao]"></textarea>
</td>
<td>
<textarea rows="4" cols="30" name="componentes[0][conc]"></textarea>
</td>
<td>
<textarea rows="4" cols="30" name="componentes[0][classificacao_cee]"></textarea>
</td>
<td>
<textarea rows="4" cols="30" name="componentes[0][classificacao_clp]"></textarea>
</td>
</tr>
</table>
<div id="outro_curso"></div>
<p class="submit">
<button id="novo_curso">Add Curso</button>
</p>
As you can see, I'm using multidimensional arrays. I'm doing that, because when I click the "Add Curso" button I call a jquery function that generates another table like the previous one. The names of the text areas will be componentes[1][identificacao], componentes[1][conc], etc...
NOTE: I can use the button to generate tables the number of times I want.
Now, my problem is how to get this data treated in my CodeIgniter model. I tried saving the data to the $componentes array (to later insert into the database) But I guess there is something wrong with my code:
$componentes;
foreach ($this->input->post('componentes') as $key => $value){
$componentes[$key]['identificacao']=$value[$key]['identificacao'];
$componentes[$key]['conc']=$value[$key]['conc'];
$componentes[$key]['classificacao_cee']=$value[$key]['classificacao_cee'];
$componentes[$key]['classificacao_clp']=$value[$key]['classificacao_clp'];
}
Can someone give me a little help please?
EDIT:
I forgot to mention, I'm getting the error:
Invalid argument supplied for foreach().
So I don't know if my foreach ($this->input->post('componentes') as $key => $value){ is correct or if is some problem in the lines inside.
$componentes_post = $this->input->post('componentes');
if(is_array($componentes_post)&&!empty($componentes_post))
{
foreach ($componentes_post as $key => $value)
{
$temp_componentes['identificacao']=$value['identificacao'];
$temp_componentes['conc']=$value['conc'];
$temp_componentes['classificacao_cee']=$value['classificacao_cee'];
$temp_componentes['classificacao_clp']=$value['classificacao_clp'];
$componentes[] = $temp_componentes;
}
}
Now see,
print_r($componentes);
You should get what you need.
Next, you are getting error Invalid argument supplied for foreach() since the array you have iterated on foreach is empty, make sure value is coming on that element by using print_r($this->input->post('componentes'))
I would like to pass an array from a form by changing its values (inputs).
the user can change the values of the inputs,
how could retrieve the array changed?
for example...
<?php $vector = array("product1" => 150, "product2" => 120); ?>
<table>
<form action="page2.php" method="get">
<?php foreach ($vector as $key => $value) {
echo "<tr><td>Product: $name</td><td><input type='text' name='$key'
value='$value'/>
</td>";
}
<tr>
<td><input type="submit" name="process" value="Submit" /></td>
</tr>
</form>
</table>
?>
// on the other page...page2.php
if (isset($_GET['process'])){
$foo = $_GET[$vector]; // the array i want
echo var_dump($foo);
}
HTTP, by design, allows arrays through POST/GET. Simply have the related items with the same name and ending with two opposing square brackets, like so:
<input type="text" name="data[]" value"First"/>
<input type="text" name="data[]" value"Second"/>
And on the server...
print_r($_REQUEST['data']);
Prints...
Array
(
[0] => First
[1] => Second
)
Pretty convenient, hmm?
Parameters are passed as an array form. So you have to parse the requested variables
Here is the revised version of your code.
<?php $vector = array("product1" => 150, "product2" => 120); ?>
<table>
<form action="page2.php" method="get">
<?php foreach ($vector as $key => $value) {
echo "<tr><td>Product: $name</td><td><input type='text' name='$key'
value='$value'/>
</td>";
}
<tr>
<td><input type="submit" name="process" value="Submit" /></td>
</tr>
</form>
</table>
?>
// on the other page...page2.php
if (isset($_GET['process'])){
unset($_GET['process']);
$foo = $_GET ;
echo var_dump($foo);
}
Try to just make it
$foo = $_GET ;
If you don't want the 'process' to be in the array, first call
unset($_GET['process']);