I'm new to PHP and Yii framework. how to insert multiple questions in database
This is Form view code
<?php echo $form->textField($model,'questions',array('id'=>"content_#index#_question textBox")); ?>
<?php echo $form->textField($model,'questions',array('id'=>"content_#index#_question textBox")); ?>
Here is My Controller
public function actionAdd_quick()
{
$model=new Question;
$answers=new Answers;
if(isset($_POST['Question'],$_POST['Answers']))
{
$model->attributes=$_POST['Question'];
$answers->attributes=$_POST['Answers'];
foreach ($model['questions'] as $value) {
$model->questions = $value;
}
$model->save();
Yii::app()->user->setFlash('add_quick','Thank you for ');
$this->refresh();
}
$this->render('add_quick',array('model'=>$model,'answers'=>$answers));
}
actually my process is to create multiple questions and answers ,but now getting error like
"Invalid argument supplied for foreach()" by using my code and how to send multiple questions and answers to mysql tables...
change this
foreach ($model['questions'] as $value) {
$model->questions = $value;
}
$model->save();
on this
foreach ($model['questions'] as $value) {
$model->questions = $value;
$model->save();
}
Try this
<?php echo $form->textField($model,'questions[]',array('id'=>"content_#index#_question textBox")); ?>
Related
I have a form in my website (Wordpress) with several fields. I need to get the data from each field to process it in the logic of my program.
I have tried to use this code and other similar ones that they provide in their documentation but after 8 hours of work I have not succeeded.
Any ideas?
add_action("wpforms_process_complete", 'function_save_custom_form_data');
function function_save_custom_form_data($params) {
foreach($params as $idx=>$item) {
$field_name = $item['name'];
$fiel_value = $item['value'];
// Do whatever you need
echo $field_name;
}
return true;
}
I want to create a dynaic page, I have created model and controller and also data subitted in database successfully. Now, i'm having problem while displaying that data on front end.
Here is my Modal:
function getcorporate(){
$q="SELECT * from corporate";
$query=$this->db->query($q);
return $query->result_array();
}
Here is my Controller:
function corporate()
{
$popular['popular'] = $this->auth_model->getPopularcourses();
$data1['corporate'] = $this->auth_model->getcorporate();
$data["institute_details"] = $this->auth_model->getInstitutedetails();
$data1['course'] = $this->auth_model->getcoursesdetailes();
$this->load->view('nulearnFront/header', $data);
$this->load->view('nulearnFront/corporate', $data1);
$this->load->view('nulearnFront/footer', $popular);
}
Try this
First you can print_r() the data you receive.
print_r($corporate);
After that you can use foreach to display all the data
foreach($corporate as $value)
{
////do code according to your requirement
}
I hope this may be help out to solve your problem
Add View file this code
<?php
if (isset($corporate) && !empty($corporate)) {
foreach ($corporate as $cdata) {
echo $cdata->YourValue(db column name);
}
}
?>
You are so close to the answer. You are passing the data from your Controller class. So what you have to do is just get that data as the follows,
I get the corporate values as it is returning an array data. So here you go,
In your view.php file,
<?php
if (isset($corporate)) { // Check if the data is set or not
foreach ($corporate as $corporateData) {
?>
// Your HTML goes here, table or etc.
<?php echo $corporateData->databaseColumnName // Value that need to print from the database ?>
<?php
}
}
?>
Hope this helps you.
print the query and run it to check
function getcorporate(){
$q="SELECT * from corporate";
$query=$this->db->query($q);
print_r($this->db->last_query());die();
return $query->result_array();
}
if query works fine then you can foreah the query
foreach($corporate as $corporate)
{
echo corporate;
}
if it does not return result then change result_array() to result() in model
I've created a basic CMS system using PHP and MySQL. I've run into a roadblock where I want the ability to create a custom field on a record.
Creating the field was easy, the issue I'm having is adding the custom field to the array which is sent in the mysql query.
Here's what I tried so far.
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
try {
$record->create('test_record', array(
foreach ($cust_field->results() as $val) {
$val->{'Field'} => Input::get($val->{'Field'}),
}
));
} catch(Exception $e) {
die($e->getMessage());
}
}
}
I know now that using a foreach loop inside an array doesn't work. But I can't seem to find anything that will work.
this should work:
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
try {
$fields = array();
foreach ($cust_field->results() as $val) {
$fields[$val->{'Field'}] = Input::get($val->{'Field'});
}
$record->create('test_record', $fields);
} catch(Exception $e) {
die($e->getMessage());
}
}
}
i am new in codeigniter. And i am trying to get field name of a table with a query.
I have write a query
"select * from user"
and pass it to $this->db->query() function. i am getting records. But i want to get field names of table. so how can i get that?
Can anybody help me please. Thanks in advance.
using database library write this code to list all fields:
$this->db->list_fields('table')
take a look here: https://codeigniter.com/userguide3/database/results.html#CI_DB_result::list_fields
Some Times this may helpful
$fields = $this->db->field_data('table_name');
foreach ($fields as $field)
{
echo $field->name;
echo $field->type;
echo $field->max_length;
echo $field->primary_key;
}
What you did is to get the datas from the table....
here your table is user so
in your model function do this...
function get_field()
{
$result = $this->db->list_fields('user');
foreach($result as $field)
{
$data[] = $field;
return $data;
}
}
in your controller do this
function get_field()
{
$data['field'] = $this->model_name->get_field();
$this->load->view('view_name',$data);
}
in your view do this
foreach($field as $f)
{
echo $f."<br>"; //this will echo all your fields
}
hope this will help you
you can use the below code to fetch the field from db
$fields = $this->db->list_fields('table_name');
foreach ($fields as $field)
{
echo $field;
}
with the help of this code we can fetch the field names from db
include('db.php');
$col="SHOW COLUMNS FROM `camera details`";
$output=mysqli_query($db,$col);
$kal=array();
while($row=mysqli_fetch_array($output))
{
if($row['Field']!='id')
{
?><div class="values"><?php echo $row['Field'];
echo "<br>";?></div><br><br><?php
array_push($kal, $row['Field']);
}
}
?>
<?php**
The answer given above by Anwar needs modification, there is no need for foreach loop in model function as it will only give the first element despite using foreach, which means the foreach loop is not bringing the fields using $data[], below code should give you 100% result
in your model function do this...
function get_field()
{
$result = $this->db->list_fields('user');
return $result();
}
in your controller do this
function get_field()
{
$data['field'] = $this->model_name->get_field();
$this->load->view('view_name',$data);
}
in your view do this
foreach($field as $f)
{
echo $f."<br>"; //this will echo all your fields
}
im having function that will display both buy and sell listings. Im trying to display both of these in one page.
im getting the values from 2 different tables and i wanr to pass both the values into the same template
Can someone please suggest how to do it?
controller
function leads(){
$this->load->model('listings');
$data['mylists']=$this->member_functions->mine();
$data['mylists2']=$this->member_functions->mine();
$data['heading']='headings/heading_view';
$data['body']='listbody';
$data['nav']='right';
$this->load->view('includes/template',$data);
}
Model
function mine(){
$mylists=$this->db->get('buy');
if ($mylists->num_rows()>0){
foreach ($mylists->result() as $a)
{
$data[]=$a;
}
return $data;
}
$mylists2=$this->db->get('sell');
if ($mylists2->num_rows>0)
{
foreach ($mylists->result() as $b)
{
$data[]=$b;
}
return $data;
}
}
View
<h2>Buy leads</h2>
<?php foreach ($mylists as $mylist):?>
<p><?php echo "$mylist->type1 in $mylist->country as $mylist->buyid" ?></p>
<?php endforeach;?>
</div>
<br />
<h2>Sell leads</h2>
<?php foreach ($mylists2 as $mylist2):?>
<p><?php echo "$mylist2->type1 in $mylist2->country" ?></p>
<?php endforeach;?>
You cannot use 2 return statements within the same function, since whenever the first is encountered...well, the function returns, and stops there. Try returning a single array with the 2 results instead, such as:
Model:
function mine(){
$mylists=$this->db->get('buy');
if ($mylists->num_rows()>0){
foreach ($mylists->result() as $a)
{
$data['res1'][]=$a;
}
}
$mylists2=$this->db->get('sell');
if ($mylists2->num_rows>0)
{
foreach ($mylists->result() as $b)
{
$data['res2'][]=$b;
}
}
return $data;
}
Controller:
$data['lists']=$this->member_functions->mine();
In your view, this array should be called like $lists['res1'] adn $lists['res2']