Exclude specific value(s) from php array - php

If I want to exclude 2 specific field ids from this code, what needs to change??
// foreach field, print the option
echo '<select name="sort" id="pref_sortby">';
if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'name')) {
echo '<optgroup label="'.get_string('fields', 'data').'">';
foreach ($fields as $field) {
if ($field->id == $sort) {
echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
} else {
echo '<option value="'.$field->id.'">'.$field->name.'</option>';
}
}
echo '</optgroup>';
}

// foreach field, print the option
echo '<select name="sort" id="pref_sortby">';
if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'name')) {
echo '<optgroup label="'.get_string('fields', 'data').'">';
foreach ($fields as $field) {
// add this condition, replacing 50 and 60 with the IDs you want to exclude
if (in_array($field->id, array(50, 60))) {
continue;
}
if ($field->id == $sort) {
echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
} else {
echo '<option value="'.$field->id.'">'.$field->name.'</option>';
}
}
echo '</optgroup>';
}

Related

PHP--How do I put form information into an HTML table once it has been saved to a text file?

This is just a block of code I am struggling with. Not sure if I am correct while trying to get this to format into an HTML table. I've messed with this for the last two hours trying to get it to work, I still don't have a clue what I am doing wrong. I am trying to get the information that comes from my array posted into the HTML table.
$ExistingSig = array('Name'=>'1','Version'=>'2','Hardware'=>'3', 'System'=>'4', 'Frequency'=>'5', 'Solution'=>'6');
$Report = array('Name'=>'a','Version'=>'b','Hardware'=>'c', 'System'=>'d', 'Frequency'=>'e', 'Solution'=>'f');
echo "<table border='1'>";
$count1=0;
foreach ($ExistingSig as $key => $value)
{
echo "<tr>";
if(!is_array($value))
{
if($key == 'Name' or $key == 'Version' or $key == 'Hardware' or $key == 'System' or $key == 'Frequency' or $key == 'Solution')
{
echo "<td>";
echo $key . ':' . $value;
echo "<br />\n";
echo "</td>";
}
}
$count2=0;
foreach ($ExistingSig as $key => $value)
{
if($count1==$count2){
//to eliminate array to string conversion error
if(!is_array($value))
{
if($key == '$AddName' or $key == '$AddVer' or $key == '$AddHard' or $key == '$AddSys' or $key == '$AddFreq' or $key == '$AddSol')
{
echo "<td>";
echo $key . ':' . $value;
echo "<br />\n";
echo "</td>";
}
}
}
$count2++;
}
echo "</tr>";
$count1++;
}
echo "</table>";
See if this works for you
<?php
$ExistingSig = array('Name'=>'1','Version'=>'2','Hardware'=>'3', 'System'=>'4', 'Frequency'=>'5', 'Solution'=>'6');
$Report = array('Name'=>'a','Version'=>'b','Hardware'=>'c', 'System'=>'d', 'Frequency'=>'e', 'Solution'=>'f');
$wantedKeys = array('Name','Version','Hardware','System','Frequency', 'Solution');
$thead = "<thead>";
$tbody="<tbody>";
foreach ($ExistingSig as $key => $value){
if(in_array($key,$wantedKeys)){
$thead .= "<th>$key</th>";
$tbody .= "<td>$value</td>";
}
}
echo "<table border='1'>$thead</thead>$tbody</tbody></table>";
?>

Php How to auto increment character?

<?php
try {
$stmt2 = $db->query("SELECT cid, parent, name FROM category");
$row_count = $stmt2->rowCount();
if ($row_count) {
$rows = $stmt2->fetchAll(PDO::FETCH_ASSOC);
}
} catch (PDOException $e) {
echo $e->getMessage();
}
$items = $rows;
$id = '';
echo "<select>";
foreach ($items as $item) {
if ($item['parent'] == 0) {
echo "<option><a href='#'>".$item['name']."</a>";
$id = $item['cid'];
sub($items, $id);
echo "</option>";
}
}
echo "</select>";
function sub($items, $id){
foreach ($items as $item) {
if ($item['parent'] == $id) {
$x = '-';
$x++;
echo "<option>".$x."<a href='#'>".$item['name']."</a>";
sub($items, $item['cid']);
echo "</option>";
}
}
}
?>
This is my dropdown menu code. I want this "-" character in each parent item and auto increment this character in each parent item.
Like this select menu:
I think you need to use str_repeat()
example:
foreach ($items as $item) {
if ($item['parent'] == 0) {
echo "<option><a href='#'>".$item['name']."</a>";
$id = $item['cid'];
sub($items, $id, 1);
echo "</option>";
}
}
echo "</select>";
function sub($items, $id, $counter){
foreach ($items as $item) {
if ($item['parent'] == $id) {
echo "<option>".str_repeat("-",$counter)."<a href='#'>".$item['name']."</a>";
sub($items, $item['cid'],$counter+1);
echo "</option>";
}
}
}

Show only ones that do not match PHP

I'm trying to use a multiple foreach and if statements to give me a list of list of people that have not been matched. I have the below code, I am able to get this to successfully give me a list of people it does match.
What I want to do is it echo each the ID from the $tenant_id foreach that have not been found in the $value2 foreach, am I doing something wrong? It will only output nothing?
foreach($array_93 as $value) {
$tenant_id = $value['id'];
$limit = 0;
foreach($obj->response->entries as $value2) {
if($limit==1) break;
if ($value2->{100} == 'true' && $value2->{114} == $tenant_id)
{echo $value['id']; // This should echo ID's that have not been found.}
$limit++;
}
}
};
UPDATE >>
After continuing to try and get this working I have got to this point, I am able to to use this to show which ID's are all 'n' as per screenshot after. The first one is all n's so this has not matched, how can I now make just the ones with all n's ID show?
foreach($array_93 as $value) {
echo '<b>'.$value['id'].'</b>';
echo '<br />';
foreach($obj->response->entries as $value2) {
if (strpos($value2->{114}, $value['id']) === false)
{
echo '<i>n</i>';
} else {
echo '<b>Y</b>';
}
}
echo '<br />';
};
Use a flag with Y-found state:
foreach($array_93 as $value) {
$Yfound = false;
foreach($obj->response->entries as $value2) {
if (strpos($value2->{114}, $value['id']) !== false) {
$Yfound = true;
}
}
if(!$Yfound) {
echo $value['id'] . ' has n`s only<br>';
}
}
Hi you should not echo right away :
foreach($array_93 as $value) {
//echo '<b>'.$value['id'].'</b>';
//echo '<br />';
//don't print yet
$output = ""; //this to store your n and Y strings.
$n = 0; //Here you store the number of times Y appears
foreach($obj->response->entries as $value2) {
if (strpos($value2->{114}, $value['id']) === false)
{
$output .= '<i>n</i>';//concatenating
} else {
$output .= '<b>Y</b>';
$n++;
}
}
//then test if there is a y and echo output.
if($n == 0){
echo '<b>'.$value['id'].'</b>';
echo '<br />';
echo $output;
echo '<br />';
}
};

Limit number of sub_menu items to 4

I am trying to limit the number of sub_menu items (li) to maximum of 4. I'm no php developer but gave it a go with some code as provided below.
This is the existing code, which will just keep displaying it all, no limit set right now.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
foreach ($sub_menu_array as $sub_menu_row) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
Here is what I tried but it ended up displaying nothing instead.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
if (++$i == 3) break;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
Syntax of PHP foreach statement has two different variants
foreach (array_expression as $value){
statement
}
foreach (array_expression as $key => $value)
statement
When you chanched code from
foreach ($sub_menu_array as $sub_menu_row) {
to
foreach ($sub_menu_array as $sub_menu_row => $v) {
You also changed values what appropriated to $sub_menu_row. For example:
$sub_menu_array = array ('a','b');
In the first variant at first iteration
$sub_menu_row=='a'
and in second variant
$sub_menu_row==0 #array's element key
$v=='a' #value
There are two solutions
Simply remove '=> $v'
Change $sub_menu_row to $v inside foreach statement
You have changed foreach, you should now use $v as a value.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($v['categoryName']).'</li>';
if (++$i == 3) break;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
This should be enough for the menu part:
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
if($i < 4) {
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
}
$i++;
}
echo '</ul></nav>';
Insert this line before displaying the menu
$sliced_sub_menu_array = array_slice($sub_menu_array, 0, 4);
and then
foreach ($sliced_sub_menu_array as $sub_menu_row => $v) {
// displaying here
}
Use this code .
<?php
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
if ($i == 3)
{
break;
}
$i++;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
?>

Looping through an array of arrays, changing output on a given line(s)

This is what im using to loop through an array of arrays.
$csvpre = explode("###", $data);
$i = 0;
$bgc = 0;
foreach ( $csvpre AS $key => $value){
$info = explode("%%", $value);
$i++;
if($i == "1"){
echo "<tr bgcolor=#efefef><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
} else {
if($bgc=1) { $bgcgo = "bgcolor=\"#b9b9b9\"" ;} else { $bgcgo = "bgcolor=\"#d6d6d6\""; }
echo "<tr $bgcgo><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
$bgc++;
}
}
How can i add an if/elseif statement to the last foreach, so that the output changes on a given line of the array.
Say i want <td>$value</td> for all unless specified, but on line 30, i want <textarea>$value</textarea>
You mean like this:
<?php
.......
echo "<tr $bgcgo><td></td>";
$j = 0; //you need a counter
foreach ( $info as $key => $value ) {
$j++;
if ($j != 30) {
echo "<td>$value</td>";
} else {
echo "<textarea>$value</textarea>";
}
}
echo "</tr>";

Categories