I'm a newbie to multidimensional array's
I have a form that stores data into an array.
I would like my users to re-use the form and store data to the array.
So my idea was a multidimensional array that stores a new array everytime the form is used.
But my problem is that I have no idea how to do this.
Here is my form:
$customer = '';
$customer .= '<tr><td>customername:<br/><input type="text" name="customer[customername]" value="" /> </td></tr>';
$customer .= '<tr><td>customertitle 1:<br/><input type="text" name="customer[customertitle1]" value="" /> </td></tr>';
$customer .= '<tr><td>customeremail 1:<br/><input type="text" name="customer[customeremail1]" value="" /> </td></tr>';
$customer .= '<tr><td>customertitle 2:<br/><input type="text" name="customer[customertitle2]" value="" /> </td></tr>';
$customer .= '<tr><td>customeremail 2:<br/><input type="text" name="customer[customeremail2]" value="" /> </td></tr>';
echo $customer;
This saves the form in an array:
if(isset($_POST['Submit'])) {
$customer = $_POST['customer'];
And this shows the first value of the array:
$customers = array(get_option('customer'));
foreach($customers as $customer){
echo $customer["customername"];
}
I hope this makes sence to anyone!!!!
Yes figured it out
Here goes for the ones that wish to know:
First create a function to get the current array.
Save the new value from the form with update_option
function savearray (){
if(isset($_POST['Submit'])) {
// get the option
$customers = get_option( 'customers' );
// add new data to the option
$customers[] = $_POST['customers'];
// save it back to the db
update_option( 'customers', $customers );
}
Then create a form that places data in arrays by name
<?php
$customers = '';
$customers .= '<tr><td>Name:<br/><input type="text" name="customers[name]" value="" /> </td></tr>';
$customers .= '<tr><td>Contact 1:<br/><input type="text" name="customers[contact1]" value="" /> </td></tr>';
echo $customers;
?>
So this works....
Now the next step.
I want to only show the name of the costumer and create a link to the data of the specific costumer.
So I did this:
<?php
$customers = get_option('customers');
foreach($customers as $val){
echo ''.$val["name"] . '<br>';
}
?>
This tells me that I want to view the name of all costumers in tha array and create a link.
I have no idea how to target the specific data in the array.
Anyone?????
I think you need to use ArrayObject
Ex:
<?php
// add a new consumer in your new array
$consumer["name"] = "John Doe";
$consumer["email"] = "toto#yopmail.com";
...
$a = new ArrayObject();
$a->append($consumer)
// for get a consumer in the array
foreach ($arr as $key => $value)
{
// Some instruction
}
?>
Hope it will help you
Related
I have a form that submits images and their titles. It is set up to check for errors via PHP validations on the image title form input elements and output any errors inside each image component (i.e. the instance in the while loop that represents a certain image). This all works OK.
What I would like to do is have it so that when one or more title is filled out correctly, but there are errors on one or more other titles, the $_POST value of the incorrect input is echoed out in the input element so the user doesn't have to re-type it. This has been easy to do on other forms on the site because there is no loop involved, e.g. on a sign up form etc. On a singular instance I would just do <?php echo $image_title; ?> inside the HTML value attribute, which is set referencing $image_title = $_POST['image-title'];]
So my question is, how do I have it so the $image_title $_POST value instances that pass the validations are outputted in their respective <input> elements, when other instances of the $image_title variable fail. If all the checks pass and there are no errors the form is then processed with PDO statements. The form submission button is placed outside of the main loop so all images are processed in one go when all the information is correct. I have a hidden input element that outputs the database image ID for each image, which can be used as a key in a foreach loop of some type. The problem of course being I can't get a foreach loop to work as I would like.
NOTE: To make the code simpler I've removed all the code relating to the outputting the images themselves.
<?php
if(isset($_POST['upload-submit'])) {
$image_title = $_POST['image-title'];
$image_id = $_POST['image-id']; // value attribute from hidden form element
// checks for errors that are later outputted on each image component
forEach($image_title as $index => $title) {
$id=$_POST['image-id'][ $index ];
if(empty(trim($title))){
$error[$id] = "Title cannot be empty";
}
}
if (!isset($error)) {
try {
// update MySQL database with PDO statements
header("Location: upload-details.php?username={$username}");
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
} else {
// prevents error being thrown before form submission if input elements are empty
$image_title = "";
}
?>
<form method="post" enctype="multipart/form-data">
<!-- IMAGE DETAILS COMPONENT - START -->
<?php
$user_id = $_SESSION['logged_in'];
$stmt = $connection->prepare("SELECT * FROM lj_imageposts WHERE user_id = :user_id");
$stmt->execute([
':user_id' => $user_id
]);
while ($row = $stmt->fetch()) {
$db_image_id = htmlspecialchars($row['image_id']);
$db_image_title = htmlspecialchars($row['image_title']);
?>
<div class="upload-details-component">
<?php
// output error messages from the validation above
if(isset($error)) {
foreach($error as $element_id => $msg) {
if($element_id == $id) {
echo "<p>** ERROR: {$msg}</p>";
}
}
}
?>
<div class="edit-zone">
<div class="form-row">
<!-- ** THIS IS THE INPUT ELEMENT WHERE I WOULD LIKE THE TITLE OUTPUTTED IF IT DOESN'T FAIL THE VALIDATION ** -->
<input value="<?php echo $image_title; ?>" type="text" name="image-title[]">
</div>
<div class="form-row">
<input type="hidden" name="image-id[]" value="<?php echo $db_image_id; ?>">
</div>
</div>
</div>
<?php } ?> <!-- // end of while loop -->
<!-- submit form button is outside of the loop so that it submits all of the images inside the loop in on go -->
<button type="submit" name="upload-submit">COMPLETE UPLOAD</button>
</form>
i think this is what you want:
<input value="<?php echo htmlentities($image_title); ?>" type="text" name="image-title[<?php echo $db_image_id; ?>]">
foreach ($_POST['image-title'] as $key => $value) {
$stmt->execute([
':image_id' => $key,
':image_title' => $value
]);
}
Edit: I have now created a minimized version and was able to test it successfully. I created the mysql table on my test-maschine to try it out. I hope this will help you now.
With <input name="img[id]" value="title"> an array will created with the following structure: $_POST['img'][id] = title. The id as array key and the title as array value. So the title is uniquely assigned to each id. The array can be walk through with a foreach key=>value loop.
Edit2: I added error checking. If the title is empty or is "testfail" for example, the title will not be written to the database. In addition, the input field keeps the last input (restore $_POST string).
<?php
$connection = new PDO(...);
$error = []; // declare empty array
if(isset($_POST['img'])) {
try {
$q = "UPDATE lj_imageposts SET image_title=:image_title
WHERE image_id = :image_id AND user_id = :user_id";
$stmt = $connection->prepare($q);
foreach($_POST['img'] as $key => $value) {
// here the error check, so that the wrong title
// is not written into the database
if(trim($value) === '' || $value === 'testfail') {
echo "Error image id $key title $value <br>";
$error[$key] = TRUE; // set error var with img-id
continue; // skip to next img, do not execute sql-stmt
}
$stmt->execute([
':image_id' => $key,
':image_title' => $value,
':user_id' => $_SESSION['logged_in']
]);
echo "Update image id $key title $value <br>";
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
?>
<form method="post" enctype="multipart/form-data">
<?php
$q = "SELECT * FROM lj_imageposts WHERE user_id = :user_id";
$stmt = $connection->prepare($q);
$stmt->execute([':user_id' => $_SESSION['logged_in']]);
while ($row = $stmt->fetch()) {
$id = htmlentities($row['image_id']);
// check if error with img-id is set above
if(isset($error[$row['image_id']])) {
// if error, fill in title from previous post
$title = htmlentities($_POST['img'][$row['image_id']]);
} else {
// if no error, fill in title from database
$title = htmlentities($row['image_title']);
}
?>
<input type="text"
name="img[<?php echo $id; ?>]"
value="<?php echo $title; ?>"><br>
<?php
// alternative version
//echo '<input type="text" name="img['.$id.']" value="'.$title.'"><br>';
}
?>
<button type="submit" name="upload-submit">Test</button>
</form>
This is harder than it needs to be, because you can't uniquely identify a particular title input except by the order it appears on the form, with the associated hidden input beside it. You need to do some careful juggling to correlate the IDs from the database with the $index of the $_POST arrays, to make sure your tiles and IDs match. You do have that all working, but this seems fragile and not a good solution IMO. I'd suggest using the database IDs as your form input index values, rather than relying on the order they appear on the form, as I did in my answer to a previous question of yours.
You're already tracking errors with the associated DB ID. So when it comes to displaying the input values, you can just check if there is an error for that DB ID. If there is not, this input passed validation, and you can re-display the value from the current $_POST:
<form method="post" enctype="multipart/form-data">
<!-- ... your code ... -->
while ($row = $stmt->fetch()) {
$db_image_id = htmlspecialchars($row['image_id']);
$db_image_title = htmlspecialchars($row['image_title']);
// Start with the value in the DB, this will display first time
// through.
$value = $db_image_title;
// If there's been a POST, and there is no $error for this ID,
// we know it passed validation.
if (sizeof($_POST) && ! isset($error[$db_image_id])) {
$value = $_POST['image-title'][$db_image_id];
}
?>
<!-- ... your code ... -->
<div class="edit-zone">
<div class="form-row">
<!-- Now we can use whatever value we set above -->
<input value="<?php echo $value; ?>" type="text" name="image-title[]">
</div>
Side Note
There's no need to iterate over all your errors to find the one you want. If you have its ID, you can address it directly. Instead of this:
if(isset($error)) {
foreach($error as $element_id => $msg) {
// Note your code above does not show what $id is here,
// AFAICT it is the same as $row['image_id']
if($element_id == $id) {
echo "<p>** ERROR: {$msg}</p>";
}
}
}
You can simply do:
if (isset($error) && isset($error[$row['image_id']])) {
echo "<p>** ERROR: " . $error[$row['image_id']] . "</p>";
}
I've got some code working that takes the text from my input box and moves it across to a function. I'm now trying to change it so I add another form element, a radio button and I want to access the choice within my functions.php file.
This is my current code which works for the post name, but what if I want to also grab the colours boxes that was selected too?
main.php
<?php
if (isset($_POST['submit'])) {
$data = $_POST['name']; // the data from the form input.
}
?>
...
<form action="/" method="post">
<input type="text" name="name" placeholder="Acme Corp"/>
<input name="colour" type="radio" value="red">Red<br>
<input name="colour" type="radio" value="blue">Blue<br>
<input name="colour" type="radio" value="green">Green<br>
<input type="submit" name="submit" value="Submit">
</form>
<img src="pngfile.php?data=<?php print urlencode($data);?>"
alt="png php file">
I guess I confused because currently it is calling this:
pngfile.php
<?php
require_once 'functions.php';
$inputData = urldecode($_GET['data']);
process($inputData);
exit;
?>
Which calls functions.php
<?php
function process($inputdata)
{
...
EDIT: What I have tried:
main.php [Change]
$data = $_POST['name'] && $_POST['colour']
But I'm not really sure how to progress.
Never trust user input. Sanitize and validate your inputs before using them.
This can be arranged better, but the basics are still true.
PHP Manual: filter_input_array()
PHP Manual: filter_var_array()
Small Function Library
function sanitizeArray($filterRules)
{
return filter_input_array(INPUT_POST, $filterRules, true)
}
function validateArray($filteredData, $validationRules)
{
return filter_var_array($filteredData, $validationRules, true);
}
function checkFilterResults(array $testArray, array &$errors)
{
if (!in_array(false, $testArray, true) || !in_array(null, $testArray, true)) {
foreach($testArray as $key => $value)
{
$errors[$key] = '';
}
return true;
}
if ($testArray['name'] !== true) { //You can make a function and do various test.
$errors['name'] = 'That is not a valid name.';
}
if ($testArray['clour'] !== true) { //You can make a function and do many test.
$errors['colour'] = 'That is not a valid colour.';
}
return false;
}
function processUserInput(array &$filteredData, array $filterRulesArray, array $validationRulesArray, array &$cleanData, array &$errors)
{
$filteredInput = null;
$tempData = sanitizeArray($filterRulesArray);
if (!$checkFilterResults($tempData, $errors)){
throw new UnexpectedValueException("An input value was unable to be sanitized.");
//Consider forcing the page to redraw.
}
$filteredData = $tempData;
$validatedData = validateArray($filteredData, $validationRulesArray);
if (!$checkFilterResults($validatedData, $errors)){
return false;
}
$errors['form'] = '';
$cleanData = $validatedData;
return true;
}
function htmlEscapeArray(array &$filteredData)
{
foreach($filteredData as $key => &$value)
{
$value = htmlspecialchars($value, ENT_QUOTES | ENT_HTML5, 'UTF-8', false);
}
return;
}
Basic Main Line
try {
$filterRulesArray = []; //You define this.
$filteredData = []; //A temporary array.
$validationRulesArray = []; //You define this.
$validatedData = null; //Another temporary array.
$results = null; //Input processing results: true or false.
$cleanData = null; //Filtered and validated input array.
$errors = []; //Any errors that have accumulated.
if (isset($_POST, $_POST['submit'], $_POST['colour']) && !empty($_POST)) {
$results = processUserInput($filteredData, $filterRulesArray, $validationRulesArray, $cleanData, $errors);
} else {
$errors['form'] = "You must fill out the form."
}
if ($results === true) {
$name = $cleanData['name']; //You can do what you want.
$colour = $cleanData['colour']; //You can do what you want.
//header("Location: http://url.com/registration/thankYou/")
//exit;
}
//Prepare user input for re-display in browser
htmlEscapeArray($filteredData);
} catch (Exception $e) {
header("Location: http://url.com/samePage/"); //Force a page reload.
}
Let the form redraw if input processing fails.
Use the $errors array to display error messages.
Use the $filteredData array to make the form sticky.
<html>
<head>
<title>Your Webpage</title>
</head>
<body>
<h1>My Form</h1>
<form action="/" method="post">
<!-- Make spots for error messages -->
<input type="text" name="name" placeholder="Acme Corp" value="PUT PHP HERE"/>
<!-- No time to display sticky radios! :-) -->
<input name="colour" type="radio" checked="checked" value="red">Red<br>
<input name="colour" type="radio" value="blue">Blue<br>
<input name="colour" type="radio" value="green">Green<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Tip:
It may be better to submit numbers for radios, as opposed to longer string values like (red, green, blue). Numbers are easier to sanitize and validate. Naturally, then you must translate the input number into its corresponding string. You would do that after validation has finished, but before using the values. Good luck!
you can access this using array like this.
$data[] = $_POST['name'];
$data[] =$_POST['colour'];
Or combine both variable
$data = $_POST['name'].'&'.$_POST['colour'];
Use Array in php for this process as follows:
if (isset($_POST['submit'])) {
$array_val = array(
"name"=> $_POST['name'],
"color"=> $_POST['color']
);
}
I use this snippet to get vehicle data from a external database:
<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>" class="vwe-kenteken-widget">
<p><input type="text" name="open_data_rdw_kenteken" value="<?php echo $_POST['open_data_rdw_kenteken'] ?>" maxlength="8"></p>
<p><input name="submit" type="submit" id="submit" value="<?php _e('Kenteken opzoeken', 'open_data_rdw') ?>"></p>
</form>
<?php if($data): ?>
<h3><?php _e('Voertuiggegevens', 'open_data_rdw') ?></h3>
<table>
<?php
$categories = array();
foreach ($data as $d) {
if( !is_array($fields) || in_array($d['name'], $fields) ) {
if( !in_array($d['category'], $categories) ) {
$categories[] = $d['category'];
echo '<tr class="open-rdw-header">';
echo '<td colspan="2" style="font-weight: bold;">';
echo ''.$d['category'].'';
echo '</td>';
echo '</tr>';
}
echo '<tr style="display:none">';
echo '<td>'.$d['label'].'</td>';
echo '<td>'.$d['value'].'</td>';
echo '</tr>';
}
}
?>
</table>
<?php endif; ?>
What i want to accomplish is that the data is loaded without the user have to enter a value and hit the submit button. The input value will get loaded based on the product page the user is viewing.
EDIT:
The data is loaded based on:
public function get_json() {
if ( isset( $_POST['kenteken'] ) ) {
$data = $this->rdw->get_formatted($_POST['kenteken']);
foreach ($data as $row) {
$json['result'][$row['name']] = $row['value'];
}
if ($_POST['kenteken']) {
if ($data[0]['value'] == '') {
$json['errors'] = __( 'No license plates found', 'open_data_rdw' );
}
else {
$json['errors'] = false;
}
}
else {
$json['errors'] = __( 'No license plate entered', 'open_data_rdw' );
}
header('Content-type: application/json');
echo json_encode($json);
die();
}
}
So instead of a $_POST action just get the data based on a pre-declared value that is different on each page.
Hard to answer - but I'll try to use my crystal ball.
$data comes from a database query, right?
I assume further, that the query takes the value from the open_data_rdw_kenteken form field to gather $data.
To have the table rendered, you have to fill $data with the right data. That implies that you must have a kind of default value for open_data_rdw_kenteken to get the data out of the DB. The default can be "all" which should reflect on your SQL Query or as your wrote "defined by product page".
Pseudo Code
$data = getData('BT-VP-41');
function getData($open_data_rdw_kenteken="")
{
$where = "";
if(!empty($open_data_rdw_kenteken)) {
$where = 'WHERE rdw_kenteken = "'.mysqli_real_escape_string($open_data_rdw_kenteken)';
}
$data = myslqi->query("SELECT * FROM dbTbl ".$where)
return $data;
}
As I wrote - this is pseudo code and will not run out of the box. You'll have to adapt that to your environment.
TL;DR: The line
<?php if($data): ?>
keeps you from rendering the table. To render you need $data filled with the right data.
Hope that will get you in the right direction.
In the phtml file shows all my database in a table including price, id etc , and then in the php file Details when the user try to press rent it takes the Id of the item and store it in the shop using the method add I keep getting this error
Fatal error: Function name must be a string.
The reason why I store the id of the item from the database so when the user check out I ll retrieve all the information using the id of the item been selected by the user. and also I am using array list to store more then one item, another thing to mention they way of getting the id of the item is by the local variable $tem = $_POST(['$dvdDetails->getDvdId()']); in the adding cart file but i am not really sure if its store the right value as there is a foreach loop and The duration of the variable will be distorted by the end of the method so how can i get the the id of the item been selected.
<?php
require_once('Models/Product.php');
class Shop {
private $_products = array();
public function getProducts()
{ return $this->_products;}
public function addProduct(Product $product)
{ $this->_products[] = $product;
return $this;
}
}
?>
<?php
class Option {
private $_optionKey;
private $_optionValue;
public function getKey()
{ return $this->_optionKey; }
public function getVlue()
{
return $this->_optionValue;
}
public function setOption($key, $value)
{
$this->_optionKey = $key;
$this->_optionValue = $value;
return $this;
}
}
?>
<?php
require_once('Models/Option.php');
class Product {
private $_options = array();
public function getOptions()
{ return $this->_options; }
public function addOption(Option $option)
{ $this->_options[] = $option;
return $this;
}
}
?>
//SHow all
<?php require('template/header.phtml') ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table class="datatable">
<tbody>
<?php foreach ($view->dd as $dvdDetails) {
echo '<tr> <td>'
.'<img src="images/'. $dvdDetails->getPhotoDetails() .'"alt="no picture" height="240" width="820" />' .'<br><br>'.'<font size="3" color="#2E2E2E"><center>'. $dvdDetails->getDvdPhoto(). '</center></font>'.
'<br><br><br>'. '<font size="2" color="blue"><strong>Genre: </strong></font>'. $dvdDetails->getDvdGenre() .
'<br><br>'. '<font size="2" color="blue"><strong>Director: </strong></font>'. $dvdDetails->getDvdDirector() . '<br><br>' .'<font size="2" color="blue"><strong>Ritels: </strong></font>'. $dvdDetails->getDvdRitels() .
'<br><br>' . '<font size="2" color="blue"><strong>Price for rent: </strong></font>'. $dvdDetails->getDvdId()) .
'<br><br>' .
'<div class="ghassar">' .
'<div id="op"> <label>Number of days </label> <select name="days" > <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option>
</select>
<br><br><br><br>
</div>
<div>
<input type="submit" value="Add to the basket" name="rent" id="buttom1" />
</div> ' . '<br><br>' .
'<br><br>' .'<div>'.
'</td> </td> </tr>';
} ?>
</tbody>
</table>
<?php require('template/footer.phtml') ?>
// adding to the cart
<?php
require_once('Models/Dvd_sql.php');
require_once('Models/Shop.php');
$view = new stdClass();
$view->dd = 'SQL';
$dvd_sql = new Dvd_sql();
$view->dd = $dvd_sql->fetchAllStudents(); //->fetchAllStudents();
if (isset($_POST['rent']))
{
$tem = $_POST(['$dvdDetails->getDvdId()']);
$shop = new Shop(); $shop->addProduct($tem);
}
require_once('Views/dvdDetails.phtml');
I think what you're trying to do is get the ID of the DVD from the form page into the form processing page. Assuming that's it, there are a couple of problems with the way you're going about that.
First, $_POST['$dvdDetails->getDvdId()'] is looking in the post data for a string key literally matching the string '$dvdDetails->getDvdId()'. Strings in single quotes aren't interpolated by PHP, and even with double-quotes you really want to stick with simple variables and/or use {$brackets}.
Second, it seems to me that the object $dvdDetails does not even exist in the form processing script. I only see it in the form display code, and that does not automatically transfer it over to the processing script. You'll need to explicitly pass the data you need in the form. For that, you can do something like this:
In the form (following your string concatenation style)
'<input type="hidden" name="item_id" value="' . $dvdDetails->getDvdId() . '">' .
in the processing script
$item_id = $_POST['item_id'];
Edit
The first time around, I missed the fact that you're looping over many DVD items. In that case, my advice would lead to many item_id inputs and only one (the last, I think) will be seen by the processing script. Your "days" selector will suffer from the same problem. In this case, I think simplest solution would be to create a separate form for each DVD and use my advice above. That way only one copy of "days" and "item_id" get sent when you submit the form for a particular DVD.
I have two tables ::: tbl_product and tbl_featured_product. I did view all product (from tbl_product) information in view pages by checkbox system. So that I can submit data to tbl_featured_product by checked as I need.
I can view all information to a view page in checkbox.. BUT can't save them in database. saving only last one row. please help me out to save multiple data in same time:::
view:::
<?php foreach($all_product as $values) { ?>
<input type="checkbox" name="product_name[]" value="<?php echo $values->product_name;?>" /> <?php echo $values->product_name;?> <br>
<input hidden="hidden" name="product_id[]" value="<?php echo $values->product_id;?>" />
<input hidden="hidden" name="product_price[]" value="<?php echo $values->product_price;?>" />
<?php } ?>
<input type="submit" name="btn" value="Save">
My Controller:::::
public function save_featured_product()
{
$data=array();
if ($this->input->post()) {
$data['featured_id']=$this->input->post('featured_id',true);
$data['product_id']=$this->input->post('product_id',true);
$data['product_name']=$this->input->post('product_name',true);
$data['product_price']=$this->input->post('product_price',true);
$this->sa_model->save_featured_product_info($data);
$sdata=array();
$sdata['message']='Save product Information Successfully !';
$this->session->set_userdata($sdata);
redirect('super_admin/add_featured_product');
}
My Model ::::
public function save_featured_product_info($data)
{
$this->db->insert('tbl_featured_products',$data);
}
Please let me know the solutions from your side. Thank you
Your problem is that the input $this->input->post('product_name') are arrays, so you need to insert one row for each, like:
(in the model)
public function save_featured_product_info($data)
{
if (isset($data['product_name']) && is_array($data['product_name'])):
foreach ( $data['product_name'] as $key=>$value ):
$this->db->insert('tbl_featured_products', array(
'product_id'=>$data['product_id'][$key],
'product_name'=>$data['product_name'][$key],
'product_price'=>$data['product_price'][$key],
'featured_id'=>$data['featured_id'] // assuming this are the same for all rows?
));
endforeach;
endif;
}
Do something like this, you may need to do some changes accordingly:
function save_featured_product_info($data){
if( isset( $data['product_id'] ) && is_array( $data['product_id'] ) ){
foreach( $data['product_id'] as $key => $each ){
$temp[] = array(
'featured_id' =>$data['featured_id'][$key],
'product_id' =>$data['product_id'][$key],
'product_name' =>$data['product_name'][$key],
'product_price'=>$data['product_price'][$key],
);
}
if( isset( $temp ) ){
$this->db->insert_batch('tbl_featured_products', $temp);
}
}
}
You're trying to save arrays, you either need to implode the values or loop and save them individually or batch insert them
$this->db->insert_batch('your_table', $temp);