PHP search results not inserting on my table - php

I am creating a search module to show results from database, I am echoing out the data to check if I am receiving it from database.
here is my current output:
As you can see, the results are there but I wasn't able to display it on my table, and I also have that error for invalid argument for foreach(). Can anyone check what is the problem here?
echo form_open(site_url() . '/search/get_account',array('id' => 'formSearch'));
$data = array(
'name' => 'acctNum',
'id' => 'acctNum',
'type' => 'hidden',
'value' => set_value('acctNum',''),
);
$dataCertType = array($data);
$dataCertType[''] = '--';
if(! is_null($certType))
foreach($certType as $rowType)
$dataCertType[$rowType->certTypeId] = $rowType->certTypeName;
$formCertType = form_dropdown('certType', $dataCertType, set_value('certType'),'id="certType" class="dropdown"');

add brackets. echo out the form drop dropdown.
if(! is_null($certType)){
foreach($certType as $rowType){
$dataCertType[$rowType->certTypeId] = $rowType->certTypeName;
$formCertType = form_dropdown('certType', $dataCertType,set_value('certType'),'id="certType" class="dropdown"');
echo $formCertType ;
}
}
bonus points - do your is_null check in your controller, and then show an appropriate view.

Related

How to insert value with new increment in codeigniter?

public function myfuntion() {
if($_POST){
$urnno = "NU-62819100";
$data = array(
"name" => $this->input->post('name'),
"email" => $this->input->post('email'),
"phone" => $this->input->post('phone'),
"urnno" => $urnno + 1
);
$sql = $this->db->insert('student',$data);
if($sql){
echo "success";
}else{
echo "fail";
}
}
$this->loca->view('myform');
}
In this code I am simply insert form value but what happen here when I insert value then urnno must be auto increment after new insert like if when I insert value first time then urnno must be NU-62819101 second time it will be 102 then 103 like this. So, How can I do this? Please help me.
Thank You
You can first get the last urnno stored in the table, get its numerical part increment it and then save it in the table.
I've written a possible solution for your problem, comments are mentioned wherever necessary. See if it helps you.
public function myfuntion() {
if($_POST){
// get the last row saved in table
$last = $this->db->select('urnno')->from('student')->order_by('id', 'DESC')->get()->row(); // id or some other auto incremented field
// get urnno from the last row
$last_urnno = $last->urnno;
$last_arr = explode("-", $last_urnno); // make it an array to get the numerical part
$new = $last_arr[1] + 1; // increment the value
$urnno = "NU-{$new}"; // new value
$data = array(
"name" => $this->input->post(''),
"email" => $this->input->post(''),
"phone" => $this->input->post(''),
"urnno" => $urnno // new value
);
$sql = $this->db->insert('student', $data);
if($sql){
echo "success";
}else{
echo "fail";
}
}
$this->loca->view('myform');
}
if you only increment the column with value 1 then you can try with setting up the column auto incremented in database. it makes easier and remove unwanted calculations.

how to insert post in codeigniter

i want to ask. I made an automatic code that I will post to the database, but I have problems, the data does not enter the database.
here I make an automatic code for item code
$data['awb'] = $this->M_order->bikin_kode();
I want to enter the value into 'tracking_number'
$data['awb'] = $this->M_order->bikin_kode();
$resinya = $data['awb'];
$sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
$data = array();
$numrow = 1;
foreach($sheet as $row){
if($numrow > 1){
array_push($data,
array(
'tracking_number' => $resinya['awb'],
)
);
}
$numrow++;
}
$this->M_order->insert_multiple($data);
when I insert the data it doesn't enter
I think you have passing data wrongly to array. Use $data['awb'] instead of $resinya['awb'] for 'tracking_number'.like this
array_push($data,
array(
'tracking_number' => $data['awb'],
)
);
Now, your code insert the data outside the foreach. so it will execute the last data into the database table.

Gravity Forms get field entries

A logged in user fills out a form several times. From his entries, I'm attempting to get all of his inputs for a specific field and put them into a PHP array.
For the sake of simplicity, assume the form has ID 10 with the first field called 'SomeField' and the user was logged in (and is still logged in) for all entries.
Here's my best attempt at creating an array of all SomeField entries from the user:
get_currentuserinfo();
$searchCriteria = array(
array(
'key' => 'created_by',
'value' => $current_user->user_login
),
array(
'key' => '1',
'value' => 'SomeField'
),
);
$form = GFAPI::get_entries( 10, $searchCriteria );
echo print_r($form);
Unfortunately, this print_r appears to display an empty array. I believe my searchCriteria is somehow incorrect.
I found it's easier to omit the second parameter ($searchCriteria) and simply use $form[0]['1'] for example which will display the first field of the first entry of the specified form.
I found with user23058230 great solution in the second answer I always got the latest form created, which worked well for new signups but not later on.
Assuming here that the form ID is number 1, you can search it with a for loop using the entry_id which is available in the user's login array as
$current_user->entry_id
No search query is needed other than the form you want returned - in this case form #1.
You can search other items using the keys you can see from your array dump.
Great solution which with the addition of this code solved my problem.
$form = GFAPI::get_entries( 1, '');
$what_i_want = 0;
for( $i = 0; $i < count($form); $i++ ){
if( $form[$i]['id'] == $current_user->entry_id ){
// the item I want
$what_i_want = $form[$i]['22'];
//echo " I " . $form[$i]['id'] . " F " . $form[$i]['22'] . " i " . $i . " T " . $what_i_want . '<br />';
break;
}
}

Why is my php returning an empty JSON array?

I'm trying to get Json data from Mysql using the following code. I've verified that the columns being selected exist.
I've searched several sites including SO and haven't noticed anything wrong with it.
I've used the example answer given in the following question:
Here
global $mysqli;
$response = array('faqtbl' => array());
// Query Db
$query = "SELECT ticketID, agentName FROM faqtbl";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
$response['faqtbl'][] = array(
'ticketID' => $row['ticketID'],
'agentName' => $row['agentName']
// 'name' => $row['name']
// 'number' => $row['number'],
// 'address' => $row['address'],
);
}
}
// I've added this but it doesn't help
//header('Content-Type: application/json');
echo json_encode($response);
If I remove 'agentName' => $row['agentName'] it works and pulls the data in this manner:
{"faqtbl":[{"ticketID":"8"},{"ticketID":"12"},....
I'm using the following: php 5.5.9, apache 2.4.7
Edit Update:
If I use the following within my while loop I get correct Data (without the json encode line):
echo "ticket: " . $row["ticketID"]. " " . $row["agentName"]." " .$row["ticketDate"]. "<br>";
Use json_last_error()
It should tell the problem.
Also you can use json_last_error_msg()
Most possible that your agent field contains some forbidden characters.

error Array to string conversion in php category and subcategory

i have complies php code which gives a array to string conversion error , creating category and sub category tree node but while compiling occurs the error , let me explain where i got the error.
<?php
$conn = mysqli_connect('localhost','res_user','Res#123','res_db');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = " SELECT * , category.id AS catId FROM category INNER JOIN sub_category ON category.id = sub_category.category_id
ORDER BY category.id, sub_category.category_id";
$res = mysqli_query($conn, $sql);
$categoryArray = array();
$oldCatId = 0;
while ($row = mysqli_fetch_assoc($res)) {
if ($row['catId'] != $oldCatId) {
$categoryArray[$row['catId']] = array(
'success' => true,
'category_' . $row['catId'] = array(
'cat_id' => $row["catId"],
'cat_name' => $row['cat_name'],
'cat_img' => $row['cat_img'],
'sub_category' => array(
'sub_id' => $row['category_id'],
'sub_name' => $row['sub_name']
)
)
);
$oldCatId = $row['catId'];
} else {
$categoryArray[$row['catId']]['category_' . $row['catId']]['sub_category'][] = array(
'sub_id' => $row['category_id'],
'sub_name' => $row['sub_name']
);
}
}
echo $categoryArray;
?>
you cant echo array's, as its for outputting one or more strings, you need to print_r or var_dump it, as:
print_r($categoryArray);
The reason for the error message is that the code is trying to display an array with echo. This construct was designed only for displaying strings, not arrays. As long a you provide PHP with a scalar value (boolean, int, string, float) , if the scalar's data type is not a string, PHP will temporarily promote it to being a string.
Normally you need to iterate through an array to display any scalar values of its elements, writing some kind of looping structure. PHP offers a great convenience for working with multidimensional arrays, the function array_walk_recursive() which works with a callback. Here's a variation of the array mentioned in the question to give you an idea about how to use array_walk_recursive() with a callback. Now that PHP supports anonymous functions, we can use one for the callback, as follows:
<?php
$categoryArray[0]['category_0']['sub_category'][] = array(
'sub_id' => 500,
'sub_name' => '1a'
);
array_walk_recursive($categoryArray,
function($value,$key){
echo "$key: $value\n";
});
// output:
sub_id: 500
sub_name: 1a
What's great about array_walk_recursive() is that it will walk through a multidimensional array and the callback displays any available data. Note, the callback needs its parameters in a certain order, the first parameter must correspond to an element's value while the second must represent its key.
Also see live demo here.

Categories