How to make array of textbox fields in PHP? - php

I'm using the CodeIgniter framework.
I'm using the form_input function to make a 2d array of textboxes and pulldowns.
The function produces HTML like this:
<input type="box" name= "variable">
I need to create 30 rows of 5 textboxes (time, event, supplies, sucess{yes/no}, comment).
My plan was to somehow be able to uniquely identify them all so at a later stage when I $Post them to another page it wont get confused as to which textbox is time1 or which textbox is time2.
I'm trying to make this array of texboxes in php so that when I use a for loop with (ISSET) I can stop when I get to a row that is not completed by the user.
This is my code here but im not too sure if its spot on
for ($i =0; $i< 30; $i++)
{
//time part of event field
echo form_input ($events['time',$i]),
//the event itself
form_input ($events['event',$i]),
//supplies used
form_input ($events['supplies',$i]),
//successful?
form_dropdown ($events['success',$i] $success),
//comment if necessary
form_input ($events['time',$i]);
echo '<br/>';
}

I really don't understand either but regarding the $_POST object; this is from the CI docs:
CodeIgniter comes with three helper
functions that let you fetch POST,
COOKIE or SERVER items. The main
advantage of using the provided
functions rather than fetching an item
directly ($_POST['something']) is that
the functions will check to see if the
item is set and return false (boolean)
if not. This lets you conveniently use
data without having to test whether an
item exists first. In other words,
normally you might do something like
this:
if ( ! isset($_POST['something']))
{
$something = FALSE;
}
else
{
$something = $_POST['something'];
}
With CodeIgniter's built in functions
you can simply do this:
$something = $this->input->post('something');

Related

Pref-fill fields on Gravity Forms

I would like to pre-fill a registration form for 2021 school year using last year's info and send out invitations to all parents.
The form all "Allow prefill" turned on for all needed fields. The prefill field name the the 'pf'&the admin name.
I tried sending myself an email with a URL containing the prefill fields=last year's data.
The obvious problem is security since the link contains personal information. So that won't work.
Next I wrote a php function (I initially put the data into an array during development) that will read a csv file on my server that contains the personal information.
I installed it into Gravity forms as a plug-in. Unfortunately, I am not familiar enough with php functions so I was unable to filter out extraneous calls to the function. It needs to run only for a specific form ID. In addition, I could not understand the part in the Gravity forms documentation regarding the sub function, so it ran every time I clicked on the web site and bombed the site.
My plan was to send an email with an account number and password, and once logged in, the function will run and prefill the fields for that one customer.
I would appreciate help either with this function, or a better way to prefill the fields.
Thanks for your help.
Here's the code:
<?php
// for testing
$account = 'B-613048';
$data_array = Array(
Array("pfAccountNumber","B-613001","B-613002","B-613003","B-613004","B-613005","B-613006","B-613007","B-613008","B-613009","B-613010","B-613011","B-613012","B-613013","B-613014","B-613015","B-613016","B-613017","B-613018","B-613019","B-613020","B-613021","B-613022","B-613023","B-613024","B-613025","B-613026","B-613027","B-613028","B-613029","B-613030","B-613031","B-613032","B-613033","B-613034","B-613035","B-613036","B-613037","B-613038","B-613039","B-613040","B-613041","B-613042","B-613043","B-613044","B-613045","B-613046","B-613047","B-613048","B-613049","B-613050","B-613051","B-613052","B-613053","B-613054","B-613055","B-613056","B-613057","B-613058","B-613059","B-613060","B-613061","B-613062","B-613063","B-613064","B-613065","B-613066","B-613067","B-613068","B-613069","B-613070","B-613071","B-613072","B-613073","B-613074","B-613075","B-613076","B-613077","B-613078","B-613079","B-613080"),
Array("pfLastYear","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes"),
Array("pfPG1Prefix","Mrs.","Ms.","Mrs.","","Mr.","Ms.","Ms.","Mr.","Ms.","Mrs.","Ms.","Mrs.","Mrs.","Ms.","Mrs.","Mrs.","Mrs.","Mrs.","Ms.","Mrs.","Mrs.","Ms.","Mrs.","Mr.","Mrs.","Mrs.","Mrs.","Ms.","Mrs.","Mrs.","Mrs.","","Mrs.","Mrs.","Mrs.","Mrs.","Mrs.","Mrs.","Mrs.","","Mrs.","Mrs.","","Ms.","Mrs.","Mrs.","Ms.","Mr.","Mrs.","Mr.","Mrs.","Ms.","Mr.","Mr.","Mr.","Ms.","Ms.","Dr.","Mrs.","Mrs.","Mr.","Mrs.","Mrs.","Mrs.","","","","","","","","","","","","","","","","")
);
$num_rows=count($data_array,0);
$num_cols=(count($data_array,1)-$num_rows)/$num_rows;
echo "num_rows= ".$num_rows."</br>";
echo "num_cols= ".$num_cols."</br>";
for ($f = 0; $f < $num_cols;$f++){$fields[$f] = $data_array[0][$f];}
for($c = 0; $c < $num_cols; $c++) {
if($account == $data_array[0][$c]){
for ($r = 0; $r < $num_rows;$r++){
$account_data[$r] = $data_array[$r][$c];
$filter = 'gform_field_value_'.$fields[$r];
add_filter( $filter, 'populate_function' );
function populate_function() {
return $account_data[$r];
};
}
break;
};
}
?>
<?php
function populate_function($g) {
echo "g= ".$g;
return $account_data[$g];
};
?>
I would suggest using the gravity_form function which allows you to pass an array of dynamic population parameter keys with their corresponding values to be populated.
Furthermore, if you did not want to require a login prior to accessing the form you could assign a unique ID to each user which would be defined as a URL parameter to identify and retrieve the required field data.
For example:
www.school.com/signup/?user=unique_complex_user_identier
Hits a template with a gravity form
URL parameter read and decoded to identify corresponding user, else fail.
User data passed into form

Codeigniter dropdown form with form_helper input is index/key instead of the value selected

I am new to codeigniter and trying to make a dropdown using form helper. I have made regular input forms but now I would like to retrieve options from my database to populate my dropdown. It seemed like I had most of this working(If I make the input a regular text field I can make changes), but when I try to run my update function I get a database error because for some reason I'm getting the index/key of the selection rather than the actual value that I am trying to use to update the database(I'm also trying to use jquery to dynamically affect another dropdown but am not quite there yet...).
My part of my form that is related to this question:
echo form_label('Stage');
echo form_dropdown('stage_name', $stage_options, $stage[0]->stage_name,
'id=
stagesDrp"');
My model methods that are related:
function get_stage_options($id){
$this->db->select('stages.stage_name')->from('stages')-
>join('prices', 'prices.id=stages.price_id')-
>join('events','events.price_id=prices.id')->where('events.id', $id);
$stage_options=$this->db->get();
if($stage_options->num_rows()>0)
{
foreach($level_options->result() as $row)
{
$options[]=$row->stage_name;
}
return $options;
}
function update_stage($id){
$stage=array(
'stage_name'=>$this->input->post('stage_name')
);
$this->db->where('events.id', $id);
$this->db->update('stages', $stage_name);
}
When I try to submit my update form, I get a database error because it's trying to set stage_name = an index value (i.e. 0, 1, 2) based on which selection is made, instead of equal to the actual stage_name.
Maybe I am missing a simple step? I really appreciate the help!
Set the name as index to the array. Try with -
foreach($level_options->result() as $row)
{
$options[$row->stage_name] = $row->stage_name;
}

Showing progress bar while fetching data [duplicate]

I have this while loop, that basically loops through a lot of records in a database, and inserts the data in another:
$q = $con1->query($users1) or die(print_r($con2->errorInfo(),1));
while($row = $q->fetch(PDO::FETCH_ASSOC)){
$q = $con2->prepare($users2);
$q->execute(array($row['id'], $row['username'])) or die(print_r($con2-errorInfo(),1));
}
(The script has been shortened for easy reading - the correct one has a much longer array)
I would like to do this more graphical, and show a progress bar on how far it has went, instead of just seeing a page loading for a few minutes (there are ~20.000 rows in this one - I have tables with much more data)
I get that you could get the total number from the old database, and I could also easily put the current number into a variable like this:
$q = $con1->query($users1) or die(print_r($con2->errorInfo(),1));
$i = 0;
while($row = $q->fetch(PDO::FETCH_ASSOC)){
$q = $con2->prepare($users2);
$q->execute(array($row['id'], $row['username'])) or die(print_r($con2-errorInfo(),1));
$i++;
}
But now I need to actually fetch $i and display it - or something like it.
How is this "easily" done?
The code for the progress bar can either be in the same document as the while loop, or in another if easier.
You can do a "master" file that does an ajax to this first file to run a single query. You could get all the entry id's in this master file, and then pass it as a parameter to the second file that does a single query. Store these ids in a javascript array.
Create a function that does this, and when the first ajax is done, move to the second element of the id array, and do another ajax with a second parameter. That's how magento imports are done by the way :)
If you need further explanations, let me know, I tried my best to explain, but may have not been perfectly clear.
// you generate this javascript array using php.
// let's say you have all the ids that have to be processed in $Ids php array.
Ids = [<?php echo implode(',', $Ids); ?>];
function doAjax(i) {
$.ajax({ // using jquery for simplicity
'url': "ajax.php?id=" + Ids[i],
}).done(function(){
if ( i >= 0 ) {
// at the point you know you're at ((Ids.length-i)/(Ids.length) * 100) percent of the script
// so you can do something like this:
// $('.progressbar').css('width', ((Ids.length-i)/(Ids.length) * 100) + '%');
doAjax(i-1);
}
});
}
doAjax(Ids.length); // starting from the last entry
So, just to explain what this does. It starts by declaring a global javascript array that has all the ids that will need to be changed.
Then I declare a recursive ajax function, this way we can make sure that only one ajax runs at any single time (so the server doesn't blow up), and we can have a fairly accurate progress. This ajax function does the following:
Sends a request to ajax.php?id=xxx - where xxx is one of the ids in the javascript array.
In the file, we get the id ($_GET['id']), you take it from the old database, and insert it in the new one. This is only for one entry.
when the ajax is done, it goes to the done() function. Since we start the doAjax() function with the last element, we do the next iteration doAjax(i-1). Since we're going backwards in the array, we check if the key is positive. If it's not, the script will stop.
That's about it.
You can't. The php is first interpreted by the server and then send to the user as HTML-Code.
The only possibility would be creating a html-page and call the php-script with AJAX.

Create a variable in one function and pass it to another in PHP

Let me first say I've spent a day reading three google pages of articles on this subject, as well as studied this page here.
Ok, here's my dilemma. I have two functions. Both called upon via AJAX. This first one assigns a value to the variable and the second one uses that variable. Both functions are triggered by two separate buttons and need to stay that way. The AJAX and the firing off of the functions work fine, but the variable isn't passed. Here is my code:
if( $_REQUEST["subjectLine"] ) //initiate first function
{
$CID = wpCreateChimpCampaign();
echo $CID; //this works
}
if( $_REQUEST["testEmails"] ) //initiate second function
{
echo $CID; //does not return anything but should contain "apple"
wpSendChimpTest($CID);
}
function wpCreateChimpCampaign () //first function
{
$CID = "apple";
return $CID;
}
function wpSendChimpTest ($CID) //second function
{
echo $CID; //does not return anything but should contain "apple"
}
I'm open to using a class but I haven't had much luck there either. I was hoping to solve this issue without using classes. Thanks for the help in advance!
If you are making 2 separate calls to this file, it may be helpful for you to visualise this as being 2 functions in 2 totally separate files. Although they exist in the same PHP file, because they used called in different calls, they don't retain the value of the variable $CID. Once the file has run, the variable is destroyed and when you call the file again, the value is null again.
So you need to store that variable between calls. You can either store it in a database or store it in a session variable.
So call session_start(); at the beginning of the file, then rather than use $CID, just use $_SESSION['CID'];
I'm not sure where the hold up is. The code you have will work:
$CID = wpCreateChimpCampaign(); // returns 'apple'
wpSendChimpTest($CID); // echos 'apple'
The code looks fine, but are you certain that all requirements are being met so both functions execute?
In other words are you supplying values for both $_REQUEST["subjectLine"] and $_REQUEST["testEmails"]?

Progress bar while running while loop

I have this while loop, that basically loops through a lot of records in a database, and inserts the data in another:
$q = $con1->query($users1) or die(print_r($con2->errorInfo(),1));
while($row = $q->fetch(PDO::FETCH_ASSOC)){
$q = $con2->prepare($users2);
$q->execute(array($row['id'], $row['username'])) or die(print_r($con2-errorInfo(),1));
}
(The script has been shortened for easy reading - the correct one has a much longer array)
I would like to do this more graphical, and show a progress bar on how far it has went, instead of just seeing a page loading for a few minutes (there are ~20.000 rows in this one - I have tables with much more data)
I get that you could get the total number from the old database, and I could also easily put the current number into a variable like this:
$q = $con1->query($users1) or die(print_r($con2->errorInfo(),1));
$i = 0;
while($row = $q->fetch(PDO::FETCH_ASSOC)){
$q = $con2->prepare($users2);
$q->execute(array($row['id'], $row['username'])) or die(print_r($con2-errorInfo(),1));
$i++;
}
But now I need to actually fetch $i and display it - or something like it.
How is this "easily" done?
The code for the progress bar can either be in the same document as the while loop, or in another if easier.
You can do a "master" file that does an ajax to this first file to run a single query. You could get all the entry id's in this master file, and then pass it as a parameter to the second file that does a single query. Store these ids in a javascript array.
Create a function that does this, and when the first ajax is done, move to the second element of the id array, and do another ajax with a second parameter. That's how magento imports are done by the way :)
If you need further explanations, let me know, I tried my best to explain, but may have not been perfectly clear.
// you generate this javascript array using php.
// let's say you have all the ids that have to be processed in $Ids php array.
Ids = [<?php echo implode(',', $Ids); ?>];
function doAjax(i) {
$.ajax({ // using jquery for simplicity
'url': "ajax.php?id=" + Ids[i],
}).done(function(){
if ( i >= 0 ) {
// at the point you know you're at ((Ids.length-i)/(Ids.length) * 100) percent of the script
// so you can do something like this:
// $('.progressbar').css('width', ((Ids.length-i)/(Ids.length) * 100) + '%');
doAjax(i-1);
}
});
}
doAjax(Ids.length); // starting from the last entry
So, just to explain what this does. It starts by declaring a global javascript array that has all the ids that will need to be changed.
Then I declare a recursive ajax function, this way we can make sure that only one ajax runs at any single time (so the server doesn't blow up), and we can have a fairly accurate progress. This ajax function does the following:
Sends a request to ajax.php?id=xxx - where xxx is one of the ids in the javascript array.
In the file, we get the id ($_GET['id']), you take it from the old database, and insert it in the new one. This is only for one entry.
when the ajax is done, it goes to the done() function. Since we start the doAjax() function with the last element, we do the next iteration doAjax(i-1). Since we're going backwards in the array, we check if the key is positive. If it's not, the script will stop.
That's about it.
You can't. The php is first interpreted by the server and then send to the user as HTML-Code.
The only possibility would be creating a html-page and call the php-script with AJAX.

Categories