PHP foreach array into option value - php

Without needing to give too much info on the API in use, I'm trying to get the option value to match what's being wrapped inside the option tag.
Something like this:
<option value="Foo">Bar</option>
Here's the PHP:
<?php
$counter = 0;
$numbers = $client->account->available_phone_numbers->getList('US', 'Local', array(
"AreaCode" => $_POST["areacode"]));
echo "<select>";
foreach($numbers->available_phone_numbers as $number) {
echo "<option value=''>";
echo $number->phone_number;
$counter++;
echo "</option>";
echo "<br>";
}
echo "</select>";
echo "<br>";
echo $counter;
?>
With this form:
<form action="index.php" method="post">
Area Code:<br>
<input type="text" name="areacode" value=""><br>
<input type="submit" value="Submit">
</form>

Your syntax is incorrect.
echo '<select>';
foreach($numbers->available_phone_numbers as $number) {
echo '<option value="' .$number->phone_number .'">';
echo $number->phone_number;
echo '</option>';
}
echo '</select>';
Also, you shouldn't have a br tag in your select element.

Change echo "<option value=''>"; to echo "<option value='{$number->phone_number}'>";

I think you need to move the select inside the form tag in order to submit the selected data also.
<form action="index.php" method="post">
echo "<select>";
foreach($numbers->available_phone_numbers as $number) {
echo "<option value='".$number->phone_number."'>";
echo $number->phone_number;
$counter++;
echo "</option>";
echo "<br>";
}
echo "</select>";
echo "<br>";
echo $counter;
?>Area Code:<br>
<input type="text" name="areacode" value=""><br>
<input type="submit" value="Submit">
</form>
Hope this help

<?php
$counter = 0;
$numbers = $client->account->available_phone_numbers->getList('US', 'Local',
array(
"AreaCode" => $_POST["areacode"]
));
?>
<form action="index.php" method="post">
<?php
echo "<select>";
foreach($numbers->available_phone_numbers as $number) {
echo "<option>" . $number->phone_number . "</option>";
$counter++;
}
echo "</select>";
?>
<input type="submit" value="Submit">
</form>

Related

Generate multiple checkboxes and evaluate them

I have a table that displays every unapproved leave, it looks like this:
For each unapproved there will be a new row, so another checkbox, but the name of the checkbox will be the same or slightly different(name="approve1"). Personally I don't think that's good practice...
Also the evaluation isn't really good, because it has to go into the database.
<form role="form" method="post" action="" id="form">
<h2><?php echo $title ?></h2>
<?php
$page = date('F');
$page = lcfirst($page);
echo "<p><a href='$page.php'>Back</a></p>";
?>
<hr>
<?php
if($i == 0){
echo "<h3>No unapproved leaves.</h3>";
}else{
echo '<table class="table table-striped table-hover">';
echo '<thead>';
echo '<th>';
echo 'Start';
echo '</th>';
echo '<th>';
echo 'End';
echo '</th>';
echo '<th>';
echo 'Employee';
echo '</th>';
echo '<th>';
echo 'Approve';
echo '</th>';
echo '</thead>';
echo '<tbody>';
for($j =0; $j < $i; $j++){
echo '<tr>';
echo '<td>';
echo $unapproved[$j]['start'];
echo '</td>';
echo '<td>';
echo $unapproved[$j]['end'];
echo '</td>';
echo '<td>';
$id = $unapproved[$j]['employee_FK'];
$result = mysql_query("select name, surname from employee where employee_ID = $id");
while ($row = mysql_fetch_assoc($result)) {
$employee[] = $row;
}
echo $employee[0]['name'], " ", $employee[0]['surname'];
echo '</td>';
echo '<td>';
echo '<input type="checkbox" name="approve" value="1" id="approve">';
echo '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
}
?>
<input type="submit" name="submit" value="Submit" class="btn" >
</div>
</div>
</form>
Can anyone think of a better, easier solution?
The practice for tabular data in most frameworks is to name them Model[id][field], so in your example it would look like "<input type='checkbox' name='leave[$j][approved]' value='1' id='leave_$j_approved'>".
Then you can easily access your data by iterating the post data like so:
foreach($_POST['leave'] as $id => $values) {
}
use checkbox name like this
<input type="checkbox" name="approve[]" value="<?php $your_row_id; ?>" id="approve">
here $your_row_id means unique id of a row in database.
And in post you will get an array named approve with all the selected values.
Parse that array using foreach.
You can generate checkboxes using a array notation in the "name" attribute.
<input type="checkbox" name="approve[]" value="1" id="approve">
After on send, you can recover values using array notation $_GET['approve'][0], ...
You can transfer arrays via HTTP :
echo '<input type="checkbox" name="approve[]" value="', $id, ']" id="approve">';
Now $_GET['approve'] will be an array which only contains values of checked boxes.
You can also specify the key associated with the value:
echo '<input type="checkbox" name="approve[', $id, ']" value="', $id, ']" id="approve">';

echo with attribute name for query purposes php pdo

<form method="POST" action="include/crud.php" enctype="multipart/form-data" >
<?php
foreach (LoadAnouncements() as $value){
/*echo "<div id='bb'></div>";*/
echo "<hr/>";
echo $value['searchresultwhat'];
echo "<br/>\n";
echo $value['searchresultwhen'];
echo "<br/>\n";
echo $value['searchresultwhere'];
echo "<hr/>";
/*echo "<div id='bb'></div>";*/
}
?>
</form>
I have this form that show an echo is there a way that I can add the attribute name in this echo so I can use it to query the database? I search for ideas if putting attribute to echo is possible but I haven't found anything yet any suggestion is appreciated
You are looking it?
<input type="text" name="searchresultwhat[]" value="<?php echo $value['searchresultwhat']; ?>" />
<input type="text" name="searchresultwhen[]" value="<?php echo $value['searchresultwhen']; ?>" />
<input type="text" name="searchresultwhere[]" value="<?php echo $value['searchresultwhere']; ?>" />
Edited
<?php
echo '<table>';
echo '<tr><th>What </th><th> When</th><th> Where</th><tr>';
foreach (LoadAnouncements() as $value){
echo "<tr><td>".$value['searchresultwhat']."</td>
<td>"$value['searchresultwhen']."</td>
<td>".$value['searchresultwhere']."</td>
</tr>";
}
echo '</table>';
?>
Do you want to add attribute to certain value in PHP? But you can't do it because $value['searchresultwhat'] and others of the kind are simple strings - as is! They can't send separated data (or attributes) with themself.
EDIT:
You can send seprated data about certain value using array:
$value['searchresultwhat'] = array("name", "value");
$value['searchresultwhen'] = array("name2", "2014-09-01");
$value['searchresultwhere'] = array("name3", "kittens");
echo $value['searchresultwhat'][0] . " is " . $value['searchresultwhat'][1];
echo $value['searchresultwhen'][0] . " = " . $value['searchresultwhen'][1];
echo $value['searchresultwhere'][0] . " has " . $value['searchresultwhere'][1];

How to fetch POST values of a dynamically generated form in php

this is my dynamically generated form that allows the user to input scores of students.my challenge is how to capture the specific subjectCode and its score because am using the subjectcode to store in the database .
<?php
$attributes=array('class'=>'','id'=>'');
echo form_open('scoreInsertion/insertScores',$attributes);
echo "<table>";
echo "<tr>";
echo "<td>";?>
<label for="admNo>">Admission Number<span class="required">*</span></label>
<?php echo form_error('admNo');
echo "</td>";
echo "<td>";?>
<input type="text" name="admNo" id="admNo" value="<?php echo set_value('admNo')?>" />
<?php echo "</tr>";
echo "<tr>";
echo "<td>";
?>
<label for="studentName>">Student Name<span class="required">*</span></label>
<?php echo form_error('studentName');
echo "</td>";
echo "<td>";?>
<input type="text" name="studentName" id="studentName" value="<?php echo $studentName?>" />
<?php echo "</tr>";
echo "<tr></tr>";
echo "<tr>";
echo "<td>";
?>
<label for="examCategory">Exam Category<span class="required">*</span></label>
<!--<tr><td><?php echo form_error('examCategory')?></td></tr>-->
<td>
<select name="examCategory">
<option value="">--Select Category--</option>
<?php
print_r($exams);
if(isset($exams) && is_array($exams)){
foreach($exams as $exam){
$examId=$exam->id;
$examName=$exam->category;
echo "<option value='$examId'>$examName</option>";
}
}
?>
</select></td><?php
echo "<tr></tr>";
echo "<tr>";
echo "<td>";
?>
<label for="term">Academic Term<span class="required">*</span></label>
<!--<tr><td><?php echo form_error('examCategory')?></td></tr>-->
<td>
<select name="term">
<option value="">--Academic Term--</option>
<?php
if(isset($terms) && is_array($terms)){
foreach($terms as $term){
$termId=$term->termId;
$termName=$term->name;
echo "<option value='$termId'>$termName</option>";
}
}
?>
</select></td>
<?php
echo "<tr>";
//loop through the subjects array and display them on the view
if(isset($subjects) && is_array($subjects)){
foreach($subjects as $subject){
$subjectName=$subject->subName;
$subjectCode=$subject->subCode;
echo "<tr>";
echo "<tr></tr>";
echo "<td>";?>
<label for="<?php echo $subjectName?>"><?php echo $subjectName;?><span class="required">*</span></label>
<?php echo form_error($subjectName);
echo "</td>";
echo "<td>";?>
<input type="text" name="<?php echo $subjectCode?>" id="<?php echo $subjectName?>" value="<?php echo set_value($subjectCode)?>" />
<?php echo "</tr>";
}
}
?>
<?php echo "<tr>";echo "<td>";
echo form_submit('submit','Submit');
echo "</td>";
echo "</tr>";
?>
</p>
<?php
echo "</table>";
echo form_close();
?>
PHP script to process the above POST details is below:
function insertScores(){
$admNo=$this->input->post('admNo');
$term=$this->input->post('term');
//get the post variables from the Form
$posted = $this->input->post();
$x = array_keys($posted);
foreach($x as $y) {
echo $y ." = ". $_POST[$y]."<br/>";
echo "<br>";
//write the sql
$form_data=array(
'admNo' =>$admNo,
'subCode'=>$y,
'termId'=>$term,
'score'=>$_POST[$y]
);
print_r($form_data);
$this->SaveForm($form_data);
}
}
If that really has to be, you could set a specific prefix for the name attribute and then look for a key starting with that prefix among your $_POST keys. Something like that:
<input type="text"
name="subject_<?php echo $subjectCode?>"
id="<?php echo $subjectName?>"
value="<?php echo set_value($subjectCode)?>" />
Then search for it in the $_POST array.
$subjectCode = null;
foreach (array_keys($_POST) as $index) {
if (strpos($index, 'subject_') === 0) { // you found the needed element
// get its value and save it in a variable
$subjectCode = $_POST[$index];
break;
}
}
This code solved my problem, I fetch the subCode using admNo in the db then compare with the POST names.
$subjects=$this->subjectModel->get_subjects();
foreach($subjects as $subject){
$subCode= $subject->subCode;
foreach($_POST as $key => $value){
//print_r($key);
//print_r($value);
$form_data=array();
if($key == $subCode){
//echo $subCode. "=". $value;
//write the sql for insertion into the database
$form_data = array(
'admNo'=>$admNo,
'subCode'=>$subCode,
'termId'=>$term,
'formId'=>$formstudy,
'score'=>$value,
'year'=>$year
);
$this->SaveForm($form_data);
}
}
}

PHP. Building a drop down menu . Passing around object array is sloppy how do I simplify this

I'm trying to grasp OOP and I decided to build a site that accesses a sql database.
It's been working out great so far I have several tables, but I've run into a snag
I access the database and create an object array with
$Dog_array = $sth->fetchAll(PDO::FETCH_CLASS, 'Dog');
Simplified but my class looks like this.
class Dog {
private $name;
function get_name {
return $this->name;
}
list that build my table
function edit_table($Dog) {
echo "<table><tr><form action='update.php' method ='post'>";
echo "<input type='hidden' name='id' value='" . $id ."'>";
echo "<td>". $Dog->get_id() ."</td>";
?>
<td><input type='text' name='rname' value="<?php echo $Dog->get_rname(); ?>"></td>
<td><input type='text' name='cname' value="<?php echo $Dog->get_cname(); ?>"></td>
<td><input type='text' name='dob' value="<?php echo $Dog->get_dob(); ?>"></td>
<td><input type='radio' name='gender' value='male' <?PHP if($Dog->get_gender() == 'male'){ echo "checked=\"checked\""; } ?> /> Male
<input type='radio' name='gender' value='female' <?PHP if($Dog->get_gender() == 'female'){ echo "checked=\"checked\""; } ?> />Female</td>
<td><input type='text' name='sire' value="<?PHP echo "builddropdown" ?> "></td>
<td><input type='text' name='dam' value="<?PHP drop_menu() ?>"></td>
<?php
if ($Dog->get_available()) {
$checked = "checked=\"checked\"";
} else {
$checked = NULL;
echo "<td><input type=\"checkbox\"" . $checked . "name=\"available\" value=\"TRUE\"/></td>";
if ($Dog->get_display()) {
$checked = "checked=\"checked\"";
} else {
$checked = NULL;
}
echo "<td><input type=\"checkbox\"" . $checked . "name=\"display\" value=\"TRUE\"/></td></tr></table>";
?>
<input type='submit' value='change image'/></form>
<FORM METHOD="LINK" ACTION="upload.php">
<INPUT TYPE="submit" VALUE="Change Image Thumb">
</FORM>
<input type='submit' value='change image'/></form>
<FORM METHOD="LINK" ACTION="upload.php">
<INPUT TYPE="submit" VALUE="Change Image Thumb">
</FORM>
<?php
}
?>
<input type='submit' value='update'/></form>
<FORM METHOD="LINK" ACTION="index.php">
<INPUT TYPE="submit" VALUE="Back">
</FORM>
<?php
}
?>
My drop down
function drop_menu() {
?>
<form name ="dropdown">
<select ="alldogs">
<?php
foreach ($Dog_array as $Dogs) {
?>
<option value="<?php echo $Dogs->get_id() . "\">" . $Dogs->get_rname(); ?></option>
<?php } ?>
</select>
</form>
<? }
My drop down menu creates a list of all of the dogs.
I get the error Invalid argument supplied for foreach
I get why I get the error I just don't know how to clean this up so I don't have to pass my Object array into my edit_table function so I can pass it into drop down function
.
I thought about creating a child class but the menu is built from an array of the objects not one object so I don't know how to make this work either. New to forum posting, so feel free to recommend some corrections in how I ask for help.
It's a problem with the arrage $Dog_array scope.
You need to pass the $Dog_array to the drop_menu function.
<?php
function drop_menu($Dog_array) {
?>
<form name ="dropdown">
<select ="alldogs">
<?php
foreach ($Dog_array as $Dogs) {
?>
<option value="<?php echo $Dogs->get_id() . "\">" . $Dogs->get_rname(); ?>></option>
<?php } ?>
</select>
</form>
<? }
And whenever you need to call the function, pass the array normally.
Also you can pass by reference
<?php
function drop_menu(&$Dog_array) {?>
In this case any manipulation to the array inside the function will impact the original array directly.
function dropdown( $name, array $options, $selected=null )
{
//array_push($options, "");
/*** begin the select ***/
//<select name="dropdownarray" id="dropdownarray">
$dropdown = '<select name="'.$name.'" id="'.$name.'">';
$selected = $selected;
/*** loop over the options ***/
echo "<b>$name : </b>"; // Title
$dropdown .= '<option value=></option>'."\n"; //empty data
foreach( $options as $key=>$option )
{
/*** assign a selected value ***/
$select = $selected==$key ? ' selected' : null;
/*** add each option to the dropdown ***/
$dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
}
/*** close the select ***/
$dropdown .= '</select>'."\n";
/*** and return the completed dropdown ***/
return $dropdown;
}
enter code here

Retain dropdown list(dynamic not hard coded) value after submitting the form php

I need to retain the dropdown list value after post submit where the values for the dropdown list are read populated from an array, not hardcoded.
This is my code for populating the dropdown list:
<?php
foreach ($catalogueArray as $cataloguePDFName) {
echo '<option value="'. $cataloguePDFName . '">' . $cataloguePDFName . '<option />';
}
?>
<label>Template</label>
<select name="templatepdfs" />
<?php
foreach ($templateArray as $templatePDFName) {
echo '<option value="'. $templatePDFName . '">' . $templatePDFName . '<option />';
}
?>
<input type="submit" name="submit" value="Submit">
Can somebody guide me how can I echo the selected dropdown value. I have seen examples how it can be done when the values are hardcoded, but for some reason im running into errors when trying for my dynamic dropdown list. Any help would be appreciated.
Thanks
Did you mean something like this?
Put your code in :
if(array_key_exists('submit' , $_POST))
{
echo $_POST['templatepdfs'];
}
else
{
?>
<form method='post' action=''>
<--Your code here-->
</form>
<?php
}
OR:
<form method='post' action=''>
<label>Template</label>
<select name="templatepdfs" />
<?php
foreach ($templateArray as $templatePDFName) {
echo '<option value="'. $templatePDFName . '"';
if($templatePDFName == $_POST['templatepdfs']) echo ' SELECTED';
echo '>' . $templatePDFName . '<option />';
}
?>
<input type="submit" name="submit" value="Submit">
</form>

Categories