How to pass array values to a url from php - php

i have some thing like this in my html multi checkbox
<label for="Status"><h2>Status:</h2></label>
<input type="checkbox" name="status[]" value="new">New<br>
<input type="checkbox" name="status[]" value="assigned">Assigned<br>
<input type="checkbox" name="status[]" value="resolved">Resolved<br>
<input type="checkbox" name="status[]" value="verified">Verified<br>
<input type="checkbox" name="status[]" value="closed">Closed<br>
myphp
$status = $_POST["status"];
$url = "http://some-example.com/project="abc" and state in [".$status."]/";
header("Location:$url");
i want the url to be like:
$url = "http://some-example.com/project="abc" and state in ["new","assigned","resolved","verified","closed"]/
is it possible to do this? if so please help.

Use http_build_query :
$url = "http://some-example.com/" . http_build_query([
"project" => "abc",
"state" => ["new","assigned","resolved","verified","closed"]
]);
Output :
http://some-example.com/project=abc&state%5B0%5D=new&state%5B1%5D=assigned&state%5B2%5D=resolved&state%5B3%5D=verified&state%5B4%5D=closed

Related

How to insert radio, checkbox and textbox value in same variable post method name using codigniter?

my view page
<form action="<?php echo base_url()?>Quiz/add_stu_ans" method="POST">
<label>Who is usa president</label>
<input type="text" name="answer[]">
<label>Dhoni is cricket player player</label>
<input type="radio" name="answer[]" value="True">True
<input type="radio" name="answer[]" value="False">False
<label>Which is asian country</lable>
<input type="checkbox" name="answer[]" value="newyork">newyork
<input type="checkbox" name="answer[]" value="india">india
<input type="checkbox" name="answer[]" value="srilanka">srilanka
</form>
My controller page
public function add_stu_ans()
{
$values['stu_answer'] = $this->input->post('ans');
$this->Common_model->insert_record('student',$values);
$this->session->set_flashdata('message_name' , 'Your Data is Inserted');
redirect('Quiz/question');
}
And this is model
public function insert_record($table,$values) {
$this->db->insert($table,$values);
return $this->db->insert_id();
}
please anyone can tell me how to customize the code in controller
Only possible if you take another hiddent text field where your 'qstn_id' will be in value. take this field in array.
in coding part while taking foreach loop you have to take qstn_id as in "[key]" format otherwise you cant find skipped records.
for eg. :
foreach ($que as $key => $ansValue) {
$val2 = $ansValue[$key];
}
// $val2 store as your output.
This is code to save the data using json_encode. Let me know you get any error I have not run this code.
View File
<form action="<?php echo base_url()?>Quiz/add_stu_ans" method="POST">
<label>Who is usa president</label>
<input type="text" name="usa_president">
<label>Dhoni is cricket player player</label>
<input type="radio" name="cricket_player" value="True">True
<input type="radio" name="cricket_player" value="False">False
<label>Which is asian country</lable>
<input type="checkbox" name="country[]" value="newyork">newyork
<input type="checkbox" name="country[]" value="india">india
<input type="checkbox" name="country[]" value="srilanka">srilanka
</form>
Controller
public function add_stu_ans()
{
$usa_president = $this->input->post('usa_president');
$cricket_player= $this->input->post('cricket_player');
$country= $this->input->post('country');
$final_data = array(
'usa_president' => $usa_president,
'cricket_player' => $cricket_player,
'country' => $country,
);
$json_encode_data = json_encode($final_data);
$this->Common_model->insert_record('student',$json_encode_data);
$this->session->set_flashdata('message_name' , 'Your Data is Inserted');
redirect('Quiz/question');
}
Please check this at your end and time of fetch the data from the database then you need to pass that return data from the function json_decode.
$json_decode_data = json_decode($return_data_from_data_table);

Problems with passing data to twlio

Controller
public function searchNew()
{
$sid = //account_sid;
$token = //auth_token;
$client = new Services_Twilio($sid, $token);
$country = $this->input->post('select_country');
$type = $this->input->post('type');
$params = array(
'Contains' => $this->input->post('contains'),
'SmsEnabled' => $this->input->post('SMS'),
'MmsEnabled' => $this->input->post('MMS'),
'VoiceEnabled' => $this->input->post('Voice'),
'ExcludeAllAddressRequired' => TRUE,
'ExcludeLocalAddressRequired' => TRUE,
'ExcludeForeignAddressRequired' => TRUE
);
$numbers = $client->account->available_phone_numbers->getList($country, $type, $params);
return $numbers;
}
View
<form action="<?php echo base_url('tenant/phone_numer/searhNew');?>" method="post">
<input type="text" name="contains" class="form-control mbottom1" id="user_firstname" placeholder="Enter a description name for your number">
<select name="select_country" id="select_country" class="form-control">
<option disabled selected>Country</option>
<option value="US">USA (+1)</option>
</select>
<input id="checkcap1" type="checkbox" name="Voice" class="radio1" value="True"/>Voice
<input id="checkcap2" type="checkbox" name="SMS" class="radio1" value="True"/>SMS
<input id="checkcap3" type="checkbox" name="MMS" class="radio1" value="True"/>MMS
<input id="checktype1" type="radio" name="type" class="radio2" value="Local"/>Local
<input id="checktype2" type="radio" name="type" class="radio2" value="Mobile"/>Mobile
<input id="checktype3" type="radio" name="type" class="radio2" value="TollFree"/>Toll-Free
<input id="checktype3" type="radio" name="type" class="radio2" value="National"/>National
<button type="Submit">Test Submit</button>
</form>
I need help in getting the $country,$type,$params from my view file. I have done a few testing and when I change $country = $this->input->post('select_country'); to $country = "US" and the other variables into a static value, the code works fine, but when I change it their respective inputs the error message I am receiving is
The requested resource
/2010-04-01/Accounts/acount_sid/AvailablePhoneNumbers//Tollfree.json
was not found
Thanks for any help.
Do it with step by step flow of codeigniter.. After submit form , In controller print your post data..
print_r($_POST);
Check all data come from Form.. And check all by isset and pass in params,type , category. It will working fine..One More thing , In twilio library all parameters passing are important e.g.
$params=array();
if(isset($this->input->post('Voice'))){
$params['VoiceEnabled']=$this->input->post('Voice');
}

How can I concat and insert two checkbox input arrays into db?

Ok I have a set of checkboxes with corresponding text inputs next to them. POST apparently sets a variable as an associative array, I want to be able to concat the values for the checkbox with the corresponding year by using a for loop and putting like indexes together. I'm currently getting an "unexpected [" error in the if statement. I looked into doing exactly this and found a few examples that went about this in the same way with success, what am I doing wrong?
Here is the form section
<form>
<label><input type="checkbox" name="check_list[]" Value = "Castle Connolly Top U.S. Doctor" id ="ccn"/>Castle Connolly Top U.S. Doctor</label>
<input type="text" name="check_year" value="<?php echo decode_text($check_year);?>"/>
<label><input type="checkbox" name="check_list[]" Value = "Vitals Patients Choice"/>Vitals Patient's Choice</label>
<input type="text" name="check_year" value="<?php echo decode_text($check_year);?>"/>
<label><input type="checkbox" name="check_list[]" Value = "Vitals Compassionate Doctor"/>Vital's Compassionate Doctor Recognition</label>
<input type="text" name="check_year" value="<?php echo decode_text($check_year);?>"/>
<label><input type="checkbox" name="check_list[]" Value = "Super Doctor"/>Super Doctor</label>
<input type="text" name="check_year" value="<?php echo decode_text($check_year);?>"/>
</form>
Then in my handler I have this code to get the values into separate arrays and concat them to another variable that I insert
$check_list = isset($_POST['check_list']) ? prepare_input($_POST['check_list']) : '';
$check_year = isset($_POST['check_year']) ? prepare_input($_POST['check_year']) : '';
$check_year_array = array_values($check_year);
$check_list_array = array_values($check_list);
$notable_awards .= ', ';
for($n=0; $n<5;$n++){
if(!empty($check_list_array[$n])){
$notable_awards .= $check_list_array[$n].','.$check_year_array[$n];
}
}
$sql = mysql_query("UPDATE" .TABLE_DOCTORS. " SET
notable_awards = '".$notable_awards."' ,
notable_publications = '".$notable_publications."'
last_logged_ip = '".$user_ip."'
WHERE id =".$doctor_id);

Combine arrays depending on checked boxes

I have a form with check boxes.
I want it so that when a check box is checked, it includes an array.
<input type="checkbox" name="main" value="main" checked> Main/unsorted<br />
<input type="checkbox" name="art" value="art" checked> Art/literature/music<br />
<input type="checkbox" name="games" value="games" checked> Games/gaming<br />
If main is checked include the array 'main', if art is checked include the array 'art', etc.
I've tried, but I can't find a function that would work for this scenario.
Edit: I'm cheating a bit and am now doing it like so.
foreach($_GET as $get) {
$end = array_merge($end, $$get);
}
From your information it sounds like you want to merge an array depending on which checkboxes have been ticked? Am I correct in assuming this?
Is something like this what you are looking for?
<?php
$combinationArray = array();
$mainArray = array('item1','item2','item3');
$artArray = array('item4','item5','item6');
$gamesArray = array('item7','item8','item9');
if(isset($_POST['main']) && $_POST['main']=='main'){
$combinationArray = array_merge($combinationArray,$mainArray);
}
if(isset($_POST['art']) && $_POST['art']=='art'){
$combinationArray = array_merge($combinationArray,$artArray);
}
if(isset($_POST['games']) && $_POST['games']=='games'){
$combinationArray = array_merge($combinationArray,$gamesArray);
}
?>
HTML:
<form action="yourpage.php" method="post">
<input type="checkbox" name="main" value="main" checked> Main/unsorted<br />
<input type="checkbox" name="art" value="art" checked> Art/literature/music<br />
<input type="checkbox" name="games" value="games" checked> Games/gaming<br />
<button>
Submit
</button>
</form>

insert batch data array?

I want insert following data by insert_batch as in following example in database table (mysql):
HTML:
<input name="u_id[0][0]" value="76">
<input name="un[0][0]" value="1">
<input type="text" name="ue[0][0]" value="11">
<input type="text" name="up[0][0]" value="111">
<input name="u_id[1][0]" value="77">
<input name="un[1][1]" value="2">
<input type="text" name="ue[1][1]" value="22">
<input type="text" name="up[1][1]" value="222">
<input name="un[1][2]" value="3">
<input type="text" name="ue[1][2]" value="33">
<input type="text" name="up[1][2]" value="333">
PHP:
$u_id = $this->input->post('u_id');
$un = $this->input->post('un');
$up = $this->input->post('up');
$ue = $this->input->post('ue');
$data = array();
foreach ($un as $idx => $name) {
$data[] = array(
'u_id' => $u_id[$idx],
'un' => $un[$idx],
'up' => $up[$idx],
'ue' => $ue[$idx],
);
};
$this -> db -> insert_batch('units', $data);
I want insert they as this:
How should change php code and html code? what do i do?
I am assuming you are using CodeIgniter and that the name of the database table that you want to insert to is called 'units' and that its 'id' column is autoincrement.
I am basing off my solution from CodeIgniter User Guide Version 2.0.3 using a call to a helper ($this->db->insert_string()) of the Database class.
foreach ($data as $row)
{
$error_code = $this->db->insert_string('units', $row);
}
Refer to http://codeigniter.com/user_guide/database/helpers.html
The insert_string function that the Database class provides appears to take an associative array, build an INSERT statement from the elements inside, execute it and then return a numerical error code.
LOL, it's not pretty, but it might work sometimes:
<input name="u_id[]" value="76">
<input name="un[]" value="1">
<input type="text" name="ue[]" value="11">
<input type="text" name="up[]" value="111">
<input name="u_id[]" value="77">
<input name="un[]" value="2">
<input type="text" name="ue[]" value="22">
<input type="text" name="up[]" value="222">
<input name="un[]" value="3">
<input type="text" name="ue[]" value="33">
<input type="text" name="up[]" value="333">
$u_id=$this->input->post('u_id');
$un=$this->input->post('un');
$up=$this->input->post('up');
$ue=$this->input->post('ue');
for($i=0;$i<count($u_id);$i++){
for($ii=0;$ii<count($un[$i]);$ii++){
(count($un[$i])>1)?$unn=$un[$i][$ii+1]:$unn=$un[$i][$ii];
(count($ue[$i])>1)?$uen=$ue[$i][$ii+1]:$uen=$ue[$i][$ii];
(count($up[$i])>1)?$upn=$up[$i][$ii+1]:$upn=$up[$i][$ii];
$this->db->insert('units', array(//use db insert here
'u_id'=>$u_id[$i][0],
'un'=>$unn,
'ue'=>$uen,
'up'=>$upn,
));
}
}
I'd go so far as to suggest you not use it. But perhaps it might inspire someone to offer a better solution.
Cheers.

Categories