Welcome Guest, Not a member yet? Register   Sign In
Using implode function to insert into database
#1

[eluser]itz4mesays[/eluser]
Please i need someone to put me through on how to use the implode function in storing array values into the database.
#2

[eluser]Stefan Hueg[/eluser]
That's not how it should be done.

Use this instead
#3

[eluser]Ckirk[/eluser]
or is itz4mesays saying that he wants to store an array into a single field?

If that's the case you could either preserve the array with:

Code:
$yourField = serialize($yourArray);
http://us2.php.net/serialize

or if you prefer json
Code:
$yourField = json_encode($yourArray);
http://php.net/manual/en/function.json-encode.php
#4

[eluser]itz4mesays[/eluser]
@ckirk, below is what i am trying to achieve

View
echo form_open('admin/add_courses');?>

<table border="0" cellspacing="2" cellpadding="0">
<tr>
<th>Course Code</th>
<th>Course Title</th>
<th>Course Unit</th>
<th>Course Status</th>
<th>Level</th>
<th>Semester</th>

</tr>
&lt;?php
for($i=0; $i<3; $i++){?&gt;

<tr>
<td>&lt;input type="text" name="course_code" value=""&gt;&lt;/td>
<td>&lt;input type="text" name="course_title" value=""&gt;&lt;br></td>
<td>
<select name="course_unit">
<option value="">Select course unit</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">4</option>
</select>
</td>
<td>
<select name="course_status">
<option value="">Select course status</option>
<option value="E">Elective</option>
<option value="C">Core</option>
</select>
</td>

<td>
<select name="level">
<option value="">Select level</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="300">300</option>
<option value="400">400</option>
<option value="500">500</option>
<option value="600">600</option>
</select>
</td>
<td>
<select name="semester">
<option value="">Select semester</option>
<option value="First Semester">1st</option>
<option value="Second Semester">2nd</option>
</select>
</td>
</tr>

&lt;?php
}
echo '</table>';
echo form_submit('btnsubmit', 'Add Course(s)');
echo form_close();



?&gt;

Looking for a way to insert multiple rows into the database. Any help?
#5

[eluser]xtremer360[/eluser]
This is what I found directly from the link that was provided to you.

Code:
$this->db->insert_batch();

Generates an insert string based on the data you supply, and runs the query. You can either pass an array or an object to the function. Here is an example using an array:
$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name' ,
      'date' => 'My date'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name' ,
      'date' => 'Another date'
   )
);

$this->db->insert_batch('mytable', $data);

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')

The first parameter will contain the table name, the second is an associative array of values.

Note: All values are escaped automatically producing safer queries.
#6

[eluser]itz4mesays[/eluser]
I guess this will only work if am dealing with constant values. I am getting values from the users, so it varies. well, lemme give it a try..Thanks....Any other idea will be welcomed.




Theme © iAndrew 2016 - Forum software by © MyBB