removing item from multidimensional array using attribute - php

I am trying to display an array of data produced from an sql query. this works fine. However I now wish not to display 1 item where the id attribute = a value.
code:
<?php foreach($sheet_list as $key => $sheet) : ?>
<?php if($sheet['id'] == $selected1){unset ($sheet_list[$key]);}?>
<tr>
<td><?=$sheet['title'];?></td>
<td><?php echo date('d F Y', strtotime($sheet['startDate'])); ?></td>
<td><?php echo date('d F Y', strtotime($sheet['endDate'])); ?></td>
<td><input type="radio" name="selected2" id="selected2" value="<?=$sheet['id'];?>" <?php
if ($key == 0) echo ' checked ';
?>/></td>
</tr>
<?php endforeach; ?>
so this is producing a table which is fine, however it is in the foreach loop where i want NOT to display 1 array item and all its attributes where the "id" field is = to the variable $selected1
I have seen lots of questions posted here, however many for 1 or two dimensional arrays.
Please help.

You're close, you should be using continue; instead of unset():
<?php if($sheet['id'] == $selected1){ continue; } ?>
continuePHP Docs makes the loop skip to the next iteration, effectively skipping the current iteration, which is what you want to do.
The benefit here is that you never modify for you $sheet_list array, unlike with unset() where you'll remove that item from the array.

If you really want to unset $sheet_list[$selected1] why don't you just do it before you enter the loop?
If you just want to skip output for this item, then use continue as suggested by nickb.

Related

CakePHP: Create array of checkboxes and update field based on whether it's set

I'm not sure if what I'm asking for is possible, but here's the scenario:
I have a foreach() loop that returns some values in HTML format:
<?php foreach($stuff as $s) { ?>
<tr>
<td><?php echo $s['Model']['data']; ?> </td>
</tr>
<?php } ?>
What I need to do is put a checkbox next to each $s['Model']['data'], preferably with $s['Model']['data'] as the label, and be able to set a value in a table based on whether the box is checked. So, my edit.ctp would look like this:
<?php foreach($stuff as $s) { ?>
<tr>
<td><?php echo $this->Form->input('flag', array('type'=>'checkbox, 'default'=>'', label=>' $s['Model']['data']', 'value'=>1 )); ?> </td>
</tr>
<?php } ?>
I have no trouble displaying the above. My problem is that I don't know how to tell my controller to update the necessary field if the labeled box is checked. Here's my first run at it:
if($this->request->save($this->request->data)) {
foreach($stuff as $s) {
$this->Model->id = $s['Model']['id'];
if(isset($this->request->data['FormName']['flag'])) {
$this->Model->set('flag', 1);
$this->Model->save('flag', true);
}
}
}
No errors at runtime, but nothing happens. The flag field in the Model I need to update remains = 0. I can debug $stuff and see data, so I know it's there. I also know that my edit function is hitting the foreach() loop. What might I be missing? Do I need to assign the checkbox a different value or check for whether it's set some other way?
It seems like you need to identify each checkbox to the current item by modifying the name of each checkbox, otherwise it will just use the value of the last checkbox for all the items.
<?php foreach($stuff as $i => $s) { ?>
<tr>
<td><?php echo $this->Form->input('flag'.$i, array('type'=>'checkbox, 'default'=>'', label=>' $s['Model']['data']', 'value'=>1 )); ?> </td>
</tr>
<?php } ?>
Then in the controller look for that specific flag
if($this->request->save($this->request->data)) {
foreach($stuff as $i => $s) {
$this->Model->id = $s['Model']['id'];
if(isset($this->request->data['FormName']['flag'.$i])) {
$this->Model->set('flag', 1);
$this->Model->save('flag', true);
}
}
}
Which assumes that ht elements in $stuff are always in the same order.

In my php program i am using an Array called $COL_array[] , its not working the second time in select?

I can't seem to add the $COL_array in the second select option. Why not?
I am using two selection options that are reading back the array and second section option is not calling the array back?
<tr>
<td><br><p>Location A:</td> //
<td><select>
<?php
$key1 = '';
while (list ($key1, $val1) = each($COL_array) ){
echo "<option value ='$key1'>$key1</option>";
}
?>
</select>
</td>
</tr>
<td><br><p>Wages in Location A :</td> <td> $ <input type="text" name="wage " >
<tr>
<td><br><p>Location B:</td>
<td><select>
<?php
$key = '';
while (list ($key, $val2) = each($COL_array) ){
echo"<option value ='$key'> </option>";
}
?>``
</select>
</td>
</tr>
Because each advances the array cursor, you'll need to
reset($COL_array);
between the loops to move the cursor back to the beginning.
From the PHP manual for each:
After each() has executed, the array cursor will be left on the next element of the array, or past the last element if it hits the end of the array. You have to use reset() if you want to traverse the array again using each.

Trouble getting a value from <a> link - PHP

My problem is that I need to save a value from the link as a variable which will then do something. Below is my main code for this problem.
<table>
<? foreach ($csv as $row) : ?>
<tr>
<td><? echo $row[0]; ?></td>
</tr>
<? endforeach; ?>
</table>
Basically, it prints the first column from my array $csv. However I want to save the '$row[0]' for each link - depending on which one is clicked.
This happens here:
<?php
if (isset($_GET['GoToProfile'])) {
}
?>
This works. E.g. when something is clicked it prints something. But I cannot find a way to save the values from each link. Depending on which one is clicked. I have tried many different methods online, but none seem to work.
I have even tried:
<? echo $row[0]; ?>
Any help would be greatly appreciated.
Thanks!
Use an ampersand (&) instead of a question mark
<? echo $row[0]; ?>
The ? indicates the beginning of the query string, which is the data sent on a GET request. In most cases it is a collection of name/value pairs, separated with & s.
A simple example of a GET request
http://example.com?first=1&second=fifty
You would get the value of the parameters in PHP with $_GET
$first = $_GET['first'];
$second = $_GET['second'];
To see what the server is receiving, you can use var_dump
var_dump($_GET)

Displying Multiple Arrays Data

I am displaying the list of users in the select box. There are two types of users i-e selected users and non selected users. The values of these users are coming from the database in two arrays i-e One array contains selected users record and other array contains all users record. Now i want if the page loads the selected users record should be shown as selected in the select box and non selected users will be shown as non selected. Here is my code:
if ($selected != false ){
foreach ($selected as $select)
{}
foreach ($data as $rows) { echo $rows->id."<br />"; echo $select->id; ?>
<option value="<?php echo $rows->id; ?>" <?php if ($rows->id == $select->id) echo "selected";?>><?php echo $rows->username; ?></option>
<?php } } else{
foreach ($data as $rows) ?>
<option value="<?php echo $rows->id; ?>" <?php if ($rows->id == $select->id) ?>><?php echo $rows->username; ?></option>
<?php } ?>
The $selected object contains the selected users list and the $data object contains non selected/total number of users
If i am not wrong you have two array like
$arr1 = array(array('id'=>1,"name"=>'abc1',"user"=>'select'),array('id'=>2,"name"=>'abc2',"user"=>'select'),array('id'=>3,"name"=>'abc3',"user"=>'select'),array('id'=>4,"name"=>'abc4',"user"=>'select'),array('id'=>5,"name"=>'abc5',"user"=>'select'),array('id'=>6,"name"=>'abc6',"user"=>'select'),array('id'=>7,"name"=>'abc7',"user"=>'select'));
$arr2 = array(array('id'=>8,"name"=>'abc8',"user"=>'noselect'),array('id'=>9,"name"=>'abc9',"user"=>'noselect'),array('id'=>10,"name"=>'abc10',"user"=>'noselect'),array('id'=>11,"name"=>'abc11',"user"=>'noselect'),array('id'=>12,"name"=>'abc12',"user"=>'noselect'),array('id'=>13,"name"=>'abc13',"user"=>'noselect'),array('id'=>14,"name"=>'abc14',"user"=>'noselect'));
$arraymerege = array_merge($arr1,$arr2); // Merge the array into one
check out the html it display the user selected. i'll just put the condition under select box if user is noselect then it show selected. Please check the image for loop
you are closing your first foreach before you actually get inside the loop...try this. (rewritten with a little shortcode added)
<select name="users" type="multiple">
<?php
//no need to write $selected == false,
//this is the same, a ! will compare this var to false
//also i use : instead because its much nicer to read i.m.o
//its also a little easier to tell the difference from a closing
//if and foreach this way if you have a lot of nested comparisons.
if (!$selected) :
//you want to loop through all rows first.
foreach ($data as $rows) :
//now look at each selected user per user
foreach ($selected as $select) : ?>
//here is where you make the comparison. no need for any other loops
//this is called a turnary comparison. its basically short code for
//if(blah) else this;
//read up on it here:
// http://www.php.net/manual/en/language.operators.comparison.php
//the <?= is the same as <?php echo. This does require for you to have
//short codes turned on in your main php.ini. It usually is though.
<option value="<?= $rows->id; ?>
<?= (($rows->id == $select->id) ? 'selected' :''); ?>
<?= $rows->username; ?>
</option>
endforeach;
endif;
?>
</select>
This is all the code you need. You should only require two loops. The very first loop would iterate through each user, and the nested loop would then compare each of those users to each selected user. Also, the ability to write cleaner code requires planning. I would suggest investing in a white board or a notepad to draw out flow charts, diagrams etc before you start writing your code. You will get a basic picture in your head. There are always 100 million ways to do one thing, but only one way that is right for you. And its up to you to find that way.
From how I read the question, he is showing all users and highlighting the selected ones in a multiple select box. He has multiple selected users so you couldn't merge the arrays together. You would have to compare each user in the table to each selected user then decide if you need to highlight that user or not.

How do I pass rows from HTML form to PHP?

I'm written a PHP program to display a tab delimited file. The purpose of this is to allow the user to views the rows and select which ones they want by checking the checkbox given in the row for each record. After they hit submit I have a PHP program to display the values, but the problem is only the last row's ID is being passed. However, when the user hits the SUBMIT button I can see all the values for the rows checked:
process_form.php?download=5108&download=5110&download=5114
How should I parse this in process_form.php? I've done a var_dump of $_POST and also
$_REQUEST but it only shows the last value which is 5114. I kind of understand the problem, most of the time in forms programmers only get one value per input field, but what happens when there are many records? It doesn't seem they should all have their own unique 'name'.
<td align=center><input type="checkbox" name="download" value="<?php echo $row['ID']; ?>"></td>
I'm doing something wrong here, but I'm not sure what. Is there a way to pass an array (I'm guessing) of IDs? Or should I be looking at parsing the URL of ?download=5108&download=5110&download=5114 because it has all the values I need there? If so, how do I do that? Thanks!
This is my solution:
<td align=center><input type="checkbox" name="download[]" value="<?php echo $row['ID']; ?>"></td>
Notice that download is now download[], thus creating an array to be passed to the PHP program to process the form.
Then using this demo PHP code I was able to get access to the array:
$my_array = ($_REQUEST["download"]);
print_r($my_array);
echo "<P>";
foreach ($my_array as $value)
{
echo $value . "<BR>";
}
I'd rather use your rowID to identify the name of <input> instead of the value.
<td align=center><input type="checkbox" name="download_<?php echo $row['ID']; ?>" value="1"></td>
Then you can process your request array like:
foreach( $_REQUEST as $key => $value ) {
if( preg_match('/^download_([0-9]+)$/', $key, $reg ) {
$rowId = $reg[1]; // Your row ID
$isChecked = $value; // State of checkbox
}
}
The row ID is parsed from variable name using regexp.
EDIT:
As mentioned in comments, this is not the simplest way to read an array of checkboxes. The simpliest is to name checkboxes download[] and parse this array in PHP then.
However, this is more universal, for example when you need to get array of input texts instead of checkboxes.

Categories