public function dcr() {
$query = $this->db->query("SELECT * FROM church_repo");
foreach ($query->result() as $row) {
$data = array('churchName' => $row->church_name,
'streetAddress' => $row->street_address,
'locationalState' => $row->locational_state,
'locationalZIP' => $row->locational_zip,
'locationalCountry' => $row->locational_country,
'locationalCity' => $row->locational_city,
'overseerAccountId' => $row->overseer_account_id,
'taxExemptionNumber' => $row->tax_exemption_number,
'accountStatus' => $row->status,
);
$this->load->view('admin-request', $data);
}
}
In my Html I am outputting the above Php. Also in my Html, I have two buttons that have id's of 'pass' and 'fail'. What the script is doing when I have multiple rows is output those two buttons with the same id's on each outputted row. That's good, because it is doing what it needs to be doing, but now I'm not able to identify the outputted rows. So I need to be able to match each button set with the outputted Php. Any ideas?
The way i typically do what you are attempting is by creating new forms for each row. and including a hidden input field in each row with the id.
<input type="hidden" name="row_id" value="<?= $overseerAccountId;?>"/>
when the user clicks pass/fail you simply use CI/PHP to get that value
<?php
echo $this->input->post('row_id');
//do work son.
?>
the only difference between your code and what i typically do, is i move my foreach into the view and iterate through each row creating new <tr><td> instead of creating new tables like you are doing. But this could be considered against the MVC standard
Related
I have a FORM with method POST. In the form I have two select drop-down with multiple='multiple' enabled in both so that user can make multiple selections.
Now I am using MySQL database to store data.
I am using foreach loop to iterate all the selections made.
Here is the image Select drop-down image
My code
$cid = $row['cid'];
$work_location = $_POST['work_location'];
$interest = $_POST['interest'];
foreach ($work_location as $value) {
$work = $value;
}
foreach ($interest as $value) {
$int = $value;
}
$insert_oth_data = "INSERT INTO resume_spec_data
(cid, work_location, interest)
VALUES('$cid', '$work', '$int')";
mysqli_query($con, $insert_oth_data);
echo var_export($work_location);
echo var_export($interest);
echo var_export($work);
echo var_export($int);
echo var_export( $insert_oth_data );
Here work_location and interest are the fields that will carry the multiple select values. I don't want to use implode function because it will make data extraction difficult as table grows. I want to store the values in saperate rows of database.
The problem I am facing is that data is being inserted, but the problem is when I select multiple options only one data is being inserted not all.
The result I get after inserting the data is:
array ( 0 => 'Delhi', 1 => 'NCR', )array ( 0 => 'Software development', 1 => 'Business analyst', 2 => 'Web development', )'NCR''Web development''INSERT INTO resume_spec_data(cid, work_location, interest) VALUES(\'1\', \'NCR\', \'Web development\')'
As you can compare from the image I have made multiple selections. But single data is being taken here
For reference
$work_location contains array ( 0 => 'Delhi', 1 => 'NCR', )
$interest contains
array ( 0 => 'Software development',
1 => 'Business analyst',
2 => 'Web development',
)
$work contains NCR
$int contains Web development only
$work and $int should also contains same as $work_location and $interest respectively.
One thing that I found here is that I have applied Insert query outside the foreach loop maybe that is why only one value is being taken. Since I am using multiple select drop-down here with multiple='multiple' enabled, I don't know how to do this to achieve the result I want.
Any help would be appreciated.
Your INSERT command happens once, and it occurs after your loops have all finished. So in the loops you're pointlessly re-assigning new values to the same variable, and then discarding them again without using them. Obviously if you wait until after a loop has finished, then you can't use the values which are present when the loop is running. You'll only ever see the last one which was assigned in the last run of the loop.
And you've explained that you're struggling to fix this because you don't know how to include the insert query inside the loops, because there are 2 different foreach loops.
So really I think the root cause here is that your database structure is flawed. It seems you permit different numbers of values in your form (e.g. 2 entries for work locations, then 3 entries for domains of interest, per your screenshot), and there's no particular relationship between them (e.g. does "Delhi" belong with "software development, or "business analyst", or the other one? There's no logic to explain that).
And yet, you're trying to store them in a table which inherently relates them together by storing them next to each other in rows. This will result in poor data quality because a) there's no way to decide which belongs with which, and b) if you have different numbers of entries in each field then you'll end up with gaps.
You need separate tables to store each list - one table for locations, and one table for interests, both with the Cid to link them to the master record.
This will then make it easy to use the data in you loops e.g.
foreach ($work_location as $value) {
$work = $value;
//...write query code to insert into work locations table
}
foreach ($interest as $value) {
$int = $value;
//...write query code to insert into interests table
}
I have been using a formula to uniquely secure id's gathered from database before they are presented to the client.
However, as my code grown complex, I've fallen into this pitfall: I have two separate cases returning json that need to use the same id. Because my securing function produces a unique hash and key at each perform, hashed ID gathered from one case cannot be encrypted in the other one that needs to use it. Therefore as a solution, I thought that sending hashed id gathered from first case back to first case again, decrypt it there and then somehow pass it to the other case without client never having chance to catch the real id.
All the codes work fine, my problem is matching the id drawn from first case that is to be used in the second case that also sends data back before case break, which is simply a client-triggered loop. I am providing codes in case you would ask it. The problem is simply matching the same id with different unique hash in two separate php cases. Sorry if I made this more complicated than it should have.
This is the first case I am using for filling a dropdown select.
case "tutorRefresh":
$tutorSelectSql = "SELECT id, tname, tsurname FROM tutors";
$tutorSelectQry = pg_query($tutorSelectSql);
while($row = pg_fetch_array($tutorSelectQry)){
$id = lockPandoraBox($row['id']);//encrypt the id
$response[] = array("id" => $id,
"tname" => $row['tname'],
"tsurname" => $row['tsurname']);
};
if(isset($response)){
echo json_encode($response);
} else {
$response = array("val" => 0);
echo json_encode($response);
}
break;
This is the function used by the second case that updates the table data, since it is too long and complex for a single issue to post it all here, I only shared relevant part of the code. I have to match the id encrypted in above code with the one encrypted here since this code fills in the table while the code above just fills in the dropdown select.
$crypted = lockPandoraBox($row["appid"]);
$tutorID = lockPandoraBox($row["tutorid"]);//encrypting id
$clientID = lockPandoraBox($row["clientid"]);//same method for another id, ignore this.
$fApp["hours"][] = array("recId" => $crypted,
"hour" => $row["hour"],
"tutor" => $tutorArr["tname"]." ".$tutorArr["tsurname"],
"tutorId" => $tutorID,// id that I need to use
"client" => $clientArr["cname"]." ".$clientArr["csurname"],
"clientId" => $clientID,
"department" => $dept,
"degree" => $deg,
"purpose" => $purposeArr["pname"],
"purposeId" => $row["purpose"],
"contact" => $clientArr["cgsm"],
"email" => $clientArr["cemail"],
"tutorAbsCheck" => $tutorAbsArray["id"],
"tutorAbsReason" => $tutorAbsArray["reason"],
"clientAbsCheck" => $clientAbsArray["id"],
"clientAbsReason" => $clientAbsArray["reason"]
);
/* */
}
return json_encode($fApp);
}
Lastly, this is the code in my main page which works in click event function that triggers the event I need. It simply changes select box's selection for the matching clicked record. It picks the id from table and tries to match it with the id in select box. Thanks in advance.
$("#tutorEdit").val(dayData["hours"][$(el.currentTarget).attr("key")].tutorId).trigger("change");
I think it will be better to change the structure a bit to combine both cases in order to achieve my goal. I wanted to know if I could get around it.
My code is pretty basic. I'm using an array to generate a datasheet for a product based on it's SKU and a filepath.
My array looks like this:
$a=array(
"/images/ManualSheets/factSheetCLASSIC.pdf"=>"KE800/6",
"/images/ManualSheets/factSheetMICRO.pdf"=>"KE800/12",
"/images/ManualSheets/factSheetSMALL.pdf"=>"KE4000/12",
"/images/ManualSheets/factSheetMEDIUM.pdf"=>"KE8000/12",
);
Where the first Key is the filepath, and the second Key is the SKU (as generated by the system) I then use an if/else to generate a button - so if a product is not in the array it returns a blank value and doesn't have a button which leads to nowhere
$factsheetweblink_url = array_search($product_sku,$a);
if ($factsheetweblink_url==false) {
echo " ";
}
else {
echo "<div class='productpagestockistBTN'>
<img src='/images/FactSheet_btn.png' >
</div>";
}
?>
This code works fine. The catch comes when I have products with different SKUs but the same datasheet file, (same brand and make but a different model). Currently I can only get it to work by uploading multiple copies of the datasheets with different names, but it's becoming a big waste of space.
I have tried using an array as a key to hold multiple values to the one key..
"/images/ManualSheets/factSheetMEDIUM.pdf"=> array("KE8000/12","KE7000/12"),
but it doesn't seem to be working... I'm not quite sure if I need to refine my if statement to search within the sub arrays as well or..?
Any help would be appreciated, thanks in advance.
You should use arrays like this:
$products = array(
0 => array(
"pdf" => "/images/ManualSheets/factSheetCLASSIC.pdf",
"skus" => array("KE800/6","KE900/6")
),
1 => array(
"pdf" => "/images/ManualSheets/factSheetCLASSIC3.pdf",
"skus" => array("KE100/6","KE200/6"))
);
This is because array_search returns just first row whit that key.
Then just do your own search function like:
function findBySku($items, $sku) {
$pdf = ""; // return empty if not found.
foreach($items as $row) {
if (in_array($sku, $row['skus'])) {
$pdf = $row['pdf'];
break;
}
}
return $pdf;
}
and call that function:
$pdf = findBySku($products, "some sku");
If I have a plugin in my server account which has this code:
function script_options() {
$scripts = array(
'Forum' => array(
'403' => 'phpBB2',
'5' => 'phpBB3',
'417' => 'Simple Machines Forum',
),
'Blog' => array(
'410' => 'b2evolution',
'418' => 'Movable Type',
'409' => 'Textpattern',
'400' => 'WordPress',
),
'Wiki' => array(
'413' => 'DokuWiki',
),
);
$result = '<option value="">'.__rp('none','none').'</option>';
foreach ($scripts as $group => $list) {
$result .= "<optgroup label=\"$group\">";
foreach ($list as $script_key => $script_value) {
$result .= "<option value=\"$script_key\"".($script_key == isset($_GET['script'])?$_GET['script']:false ? ' selected="selected"':'').">$script_value</option>";
}
$result .= '</optgroup>';
}
return $result;
}
How can I make the first choice be ('400' 'Wordpress'); if the user did not choose anything it chooses Wordpress by itself.
It looks like you don't fully understand what this code does, so let me boil it down for you.
Let's start at the end and work our way backwards: When the function returns, the $result variable contains an HTML fragment with a bunch of <optgroup> tags in it, and those contain <option> tags. I assume this HTML gets pasted into a <select> tag somewhere outside this function (BTW, <select> is an HTML thing; PHP has a select() function that is completely unrelated to this, so to avoid confusion, don't refer to HTML's <select> as "select option in PHP").
The foreach loops right before that construct the $result value by concatenating individual chunks of HTML, and those are in turn derived from the nested associative array declared at the beginning of the function. If you follow those loops carefully, you'll see that the tree structure of the resulting HTML follows that of the nested array: each top-level element becomes an <optgroup>, and the name is derived from the array key; each second-level element becomes an <option>, where the key goes into the value attribute (which determines the value used when the containing form is submitted), and the value goes into the user-visible tag content. The array elements are visited in order, so what comes first in the array also comes first in the resulting HTML.
There are two things you need to know about <select> in this context: first, you can define which option is selected by default by adding a selected tag to it (the standard way to express this is <option selected="selected">...</option>); there should be at most one selected option in your <select>. And second, if none of your options has a select attribute, the first one becomes the default.
Now, what does this mean for you? Simple: you can either make it so that your code sets the selected attribute on the WordPress entry; this way, your options remain ordered exactly as they are now, optgroups and all, but WordPress will be preselected. Or you can put the WordPress one as the very first element, which puts it first in the <select>, and should get it preselected because no option has a selected attribute.
I know this has been asked before, but I just cant seem to find an answer... or solution.
I have many select boxes using 'multiselect'. The dropdowns are being populated from the database, and the first value in the array is always 'Select One'. This I cannot change, I am rewriting an app and am not changing the database at all.
Everything works just fine, but they always come out as 'optgroup' tags with a label, which always puts a '0' at the top of the list. The boxes always say 'Select One', which is great, but when expanded you see the '0' at the top... which is the 'label' attribute to the optgroup tag.
I do it all somehting like this...
$Criteria = new Criteria();
$Criteria->add( DictionaryPeer::CATEGORY, 'Progress Notes: Program Status' );
$Criteria->addAscendingOrderByColumn( 'Ordinal' );
$ProgramStatuses = DictionaryPeer::doSelect($Criteria);
$ProgramStatusList = array();
foreach ($ProgramStatuses as $ProgramStatus) {
$ProgramStatusList [ $ProgramStatus->getDictionaryID() ] = $ProgramStatus->getWord();
}
$form->programstatus->addMultiOptions( array(
$ProgramStatusList ));
echo $form->programstatus->renderLabel() . $form->programstatus->renderViewHelper();
I just want to remove the '0' for presentation purposes only...
Any help is always appreciated...
Thanks!
If you want to get rid of the OPTGROUP, you just need to pass a simple array as parameter to addMultiOptions() as follows:
$form->programstatus->addMultiOptions($ProgramStatusList);
Because if you pass a multidimensional array, Zend_Form will indirectly consider each index of the parent array as a group of options (using the FormElement View Helper).