last day (14 days later) » 

3:36 AM
0
A: Extract values and insert into MYSQL from Array

John RuddellI would do a regular for loop assuming the data is in this format.. $items = array(); foreach($_POST['item'] as $data) { $items[] = $data; for($i = 0; $i < count($items[0]); $i++){ $insert="INSERT INTO table (meta_id,meta_data) VALUES ('$items[2][$i]','$items[0][$i]')"; ...

 
thanks im checking this now. I have much more security in place. I just need to understand the logic. Thanks heaps for this.
am i supposed to this? $your_array = $_POST['item'];
 
@jonnypixel if the $_POST['item'] is that outer most array with the three arrays inside it then yes
 
im still not getting it - with that it wont insert now i this $your_array = array($_POST['item']); But i get the table rows with array[0] array[1] and array[2] in my table column for meta_data
The array comes from a print_r
 
@jonnypixel I'm not sure what the issue is. I edited with a way for you to test it. just paste into the code block and run that.. it echos out the id and name for each row. what is the print_r on? like what did you print_r() on?
 
i did it on $items = array();foreach($_POST['item'] as $data) { $items[] = $data;} print_r($items);
 
3:36 AM
@jonnypixel see my edit
 
Just checking now
Hi John, i noticed it said to move it to chat. Im still getting this in the database
meta_id meta_data
0 Array[0]
0 Array[1]
 
hey
ok so stop inserting into the db
test it first to see if its correct before doing that
so echo out the info
aka echo $item[0][$i];
 
checking now
I have added two items of boags and i get six in that echo
Boags Draught 24 375ml
Boags Draught 24 375ml
Boags Draught 24 375ml
Boags Draught 24 375ml
Boags Draught 24 375ml
Boags Draught 24 375ml
$items = array();
foreach($_POST['item'] as $data) {
$items[] = $data;
for($i = 0; $i < count($items[0]); $i++){
/* $insert="INSERT INTO #__iorder_packageitems (pid,meta_id,meta_data) VALUES ('$pid','$items[2][$i]','$items[0][$i]')";
$db->setQuery($insert);
$db->query(); */

echo $items[0][$i].'<br/>';
}
}
 
ok now try echo "id is: ", $items[2][$i], " -- name is: ", $items[0][$i], "<br>";
 
aha i get the six results with the last two results howing the id with them
id is: -- name is: Boags Draught 24 375ml
id is: -- name is: Boags Draught 24 375ml
id is: -- name is: Boags Draught 24 375ml
id is: -- name is: Boags Draught 24 375ml
id is: 2 -- name is: Boags Draught 24 375ml
id is: 3 -- name is: Boags Draught 24 375ml
 
3:47 AM
ok maybe thats because you ran this a few times? hmm
try $data = $_POST['item'];
 
Ill clear the console
 
print_r $data;
print_r($data); i mean
 
Array
(
[0] => Boags Draught 24 375ml
[1] => Carlton Draught or VB 10/375ml
)
Array
(
[0] => Boags Draught 24 375ml
[1] => Carlton Draught or VB 10/375ml
)
Array
(
[0] => 78
[1] => 25
)
Array
(
[0] => 78
[1] => 25
)
Array
(
[0] => 2
[1] => 3
)
Array
(
[0] => 2
[1] => 3
)
Thats when i do teh print_r inside the for loop
 
what is $_POST['item']?
 
<table>
<tbody class="input_fields_wrap">
<tr class="row_2">
<td><select name="item[product][]" style="width:99%;">
<option value="Boags Draught 24 375ml">
Boags Draught 24 375ml
</option>

<option value="Carlton Draught or VB 10/375ml">
Carlton Draught or VB 10/375ml
</option>
</select>
</td>

<td>
<input class="input-small" name="item[price][]" type="txt"><input name=
"item[id][]" type="hidden" value="2"><a class="remove_field pull-right"
href="#"><span class="icon-cancel"></span></a>
</td>
</tr>
That is automatically generated rows as i click add row
<select name="item[product][]" >
 
3:50 AM
for some reason its duplicating the data
 
<input name=
"item[id][]" type="hidden" value="2">
Well initally i had selected boag for both drinks
Thats a screengrab of what ot looks like
I have a hidden field for the id
 
yea theres not much I can do without being able to edit your system / data.
but one thing you can try is this
inside your for loop check for the value of each id / name
 
Yes thats kind of wat i wanted in teh array

name="item[product][]"
but i dont know how to format the select and input values to get the names in the array
 
oh... ummmm you could send it like this maybe
nvm that wouldn't work
try adding this to the for loop i wrote
if($items[2][$i] && $items[0][$i]){ execute the query here}
 
bummer - why does it keep just putting Array[0],Array[1] into teh rows
 
3:59 AM
not sure... are you escaping the variable?
 
It didnt work man. Thanks heaps for your time. I noticed it was 10pm in Dallas
Im in Hobart Tasmania Australia 3pm in the future lol
 
haha hows monday? :P
 
yeh :(
 
Wifes pregnant
 
4:01 AM
ok so we need to fix your input params
dude awesome!
 
Would you be intersted in workong together? on projects?
Oh yeh but its our 5th kid
so it is awesome byt tiring
 
i'm from a family of 12 :D
but yea I know what you mean about it being tiring
 
yes the input params is what i need to format correctly so i can grab those values by their names
Are you intrested in working on projects together?
 
yea you are passing it incorrectly. I can try and help. i'm not a php expert or anything lol
 
I have indians and philippinos contsantly emailing me but it would be great to have someone i can work with
Thanks John i appreciate your help
I have some jquery where i dynamically create the input fields. I need to re-word the name=""
 
4:04 AM
well first thing is on the html side what is name= "item[id][]"?
like what is id? and is that creating an array at the array index for an id?
 
thats the one i really need to get. That is the dymaicall generate x=id form my jquery


var max_fields = 50; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID

var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append(''
+'<tr class="row_'+x+'">'
I think my keyboard battery is dying
My words dont sometimes come out complete lol sorry
$items in that snippet is coming from a mysql query loop and is not related to teh the next page. Just incase you were wondering why it had the same name
 
hmm. what is item?
not $items but item in this snippet?
 
well thats the thing
item is how im passing everything
Heres how it looks in fire bug
item[id][] 2
item[id][] 3
item[price][] 78
item[price][] 25
item[product][] Boags Draught 24 375ml
item[product][] Carlton Draught or VB 10/375ml
 
no I mean what is it? where do you declare it?
 
I use this to post the form

$.post( "<?php echo $process_path;?>/custom.process.php", $( "#form-package" ).serialize() );
The serialize is grabbing all the dynamic custom fields for me
I sthat what you mean?
 
4:11 AM
lol nope...
 
I have other stuff with the form but all of tehm are fixed fields so its easy to handle them in a seperate table and insert
 
you have item[product]
do you do something like var item = [];
 
ahm no
Basically items value gets generated when the seralize is fired off
 
oh ok gotcha
 
its like i need id,product,price to be inside th array and not just my post output in firebug
but if i give the select field and the inout fields unique names theni can iterate through can i?
like for example:

<select name="product[]"

<input name="price[]

<input name="id[]
 
4:16 AM
one sec i'm creating the html table and trying to serialize it
 
oh ok
 
ok I have an ide
idea
so instead of creating each of those names that you use why not just pass in one array?
what I mean is serialize only one specific field
 
no its the whole form
<form id="form-package"
 
i know
you can specify which fields to serialize
 
Aha sorry
 
4:25 AM
so when you do serialize() on the form just specify it like this
 
Yes when i try that it wont send it because they are added dynamically
ok ill try
 
$( "#form-package" ).find('input[name=your_given_name]', this).serialize();
and then store the values in that hidden field
so something like this for the hidden elements
<input name="item[id][]" value="2" />
<input name="item[id][]" value="Boags Draught 24 375ml" />
<input name="item[id][]" value="75" />
 
But how do i get the drink value inside the hidden input?
 
when the item is selected you can create a hidden input with javascript
or when you go to post the data
right before you post it create a hidden input
that way the data would be an array( [2]=> array( 2, 'Boags Draught 24 375ml', 75))
 
oh yes of course
I now have this though
Array
(
[0] => Boags Draught 24 375ml
[1] => Carlton Draught or VB 10/375ml
)
Array
(
[0] => 34
[1] => 23
)
Array
(
[0] => 2
[1] => 3
)
I just renamed the select and inpits to unique names

product[]
price[]
id[]
On the php file i did this

print_r($_POST['product']);

print_r($_POST['price']);

print_r($_POST['id']);
oh yes your way makes sense
Ill do right now
The only problem is that the select field is generating the value from the option fields?
How do i create a dynamic hidden inout from a selection made?
 
4:36 AM
on selection you create the input field for price right?
 
Yes
 
ok so create a hidden input as well with the name of the selection
 
on click a new table rows is inserted with select field and price input and hidden id field
 
so the hidden id field should be created and then a hidden name of the selection field too
<input name="item[id][]" value="2" />
<input name="item[id][]" value="Boags Draught 24 375ml" />
 
But wouldnt i have to write jquery code to generate the hidden inout for teh drinks value?
What about his?

Im getting this from print_r['item'] now
Array
(
[product] => Array
(
[0] => Boags Draught 24 375ml
[1] => Carlton Draught or VB 10/375ml
)

[price] => Array
(
[0] => 3
[1] => 9
)

[id] => Array
(
[0] => 2
[1] => 3
)

)
 
4:41 AM
no because on the input for price you just make the name be name="item[id][]"
 
So right now i have this:

+'<td><select style="width:99%;" name="item[product][]" ><?php echo $items;?></select></td>'
+'<td><input type="txt" name="item[price][]" class="input-small"><input type="hidden" name="item[id][]" value="'+x+'" >'
 
this is the dynamic stuff right?
 
yes
 
ok
so change it to this.
+'<td><select style="width:99%;" name="item[id][]" ><?php echo $items;?></select></td>'
+'<td><input type="txt" name="item[id][]" class="input-small"><input type="hidden" name="item[id][]" value="'+x+'" >'
there may be an issue with this... if the $items being pulled out in the select are all getting serialized. you would have to change it up a little
but we can do that if we need to
 
OK
Array
(
[id] => Array
(
[0] => Boags Draught 24 375ml
[1] => 58
[2] => 2
[3] => Carlton Draught or VB 10/375ml
[4] => 12
[5] => 3
)

)
Im getting that now from print_r($_POST['item']);
 
4:46 AM
oops sorry try this.
+'<td><select style="width:99%;" name="item[x][]" ><?php echo $items;?></select></td>'
+'<td><input type="txt" name="item[x][]" class="input-small"><input type="hidden" name="item[x][]" value="'+x+'" >'
 
Array
(
[x] => Array
(
[0] => Boags Draught 24 375ml
[1] => 27.99
[2] => 2
[3] => Carlton Draught or VB 10/375ml
[4] => 20.00
[5] => 3
)

)
sorry my bad
ill change the x to dynamic
 
yea
thats what i meant lol
escape it :)
 
Array
(
[2] => Array
(
[0] => Boags Draught 24 375ml
[1] => 12
[2] => 2
)

[3] => Array
(
[0] => Carlton Draught or VB 10/375ml
[1] => 90
[2] => 3
)

)
cool
now that makes more sense
 
yea so from here all you have to do is your foreach
 
yes
 
4:50 AM
foreach $_post as $data
 
The one i have is doing better but not quite there
 
echo $data[0] // should be Boags Draught 24 375ml
 
$items = array();
foreach($_POST['item'] as $data) {
$items[] = $data;

for($i = 0; $i < count($items[0]); $i++){

$insert="INSERT INTO #__iorder_packageitems (pid,meta_id,meta_data) VALUES ('$pid','$items[2][$i]','$items[0][$i]')";
$db->setQuery($insert);
$db->query();

}
}
What needs to go there?
 
no... you dont need all that anymore since we fixed it... this is what you need
first try this.
foreach($_POST['item'] as $data) {
echo $data[0];
}
 
Boags Draught 24 375mlCarlton Draught or VB 10/375ml
the two drinks
 
4:53 AM
ok awesome
 
So that must be the zero value
 
then this is what you want
foreach($_POST['item'] as $data) {
$insert="INSERT INTO table (meta_id,meta_data) VALUES ('$data[2]','$data[0]')";
$db->setQuery($insert);
$db->query();
}
i fixed it so you can just insert it in directly without needing the other loop :)
also make sure you delete the rows that you inserted that were bad aka the ones that showed array[0]
 
Yes i had phpmysdmin open in another monitor
Its workng
 
SWEET
 
and now i just do another for price and then i can match the price to the drink via the meta_id
You are the man
 
4:57 AM
awesome dude
 
Buy this texan a drink!!!
Dude
 
haha
 
And i bet you texans drink too
 
yea lol i drink a lot
i'm slightly drunk right now :P
 
Thanks heaps John! But seriously - do you do websites in the USA?
 
4:58 AM
yea i'm one of our server guys at my work
i ran the front end website for a year (was the only dev on it)
now im on the backend and sql database management
if you update your question with some of the relevant data i'll fix my answer :)
 
Oh ok so you got yourself a steady job. All good. If you are evr intersted in working internationally on a project please tell me. I can quote bigger jobs and we can tackle the work as a sdie thing. I would sen you money via paypal and always let you know the project and then work out a quote that we are both happy with
Before i quote my clients i mean
Its just me here and i get quite a bot of work but im old and slow
 
cool. well i'm cool with doing a little side work here and there but you would probably have to do most of it.
 
Cani have one of your emails please?

I am john@mediarama.com.au
 
i do some stuff on the side here and there :)
johnruddell@gmail.com
 
No probs sounds great thanks man.

How will i best and easiest re-word my stack post?
 
5:04 AM
just say you are sending the data through a post form serialization and post the old code and i'll post the solution :)
you're welcome too been a pleasure solving the issue with you
 
And a pleasure talking with a patient Texan! thanks heaps matey. As us down under kangaroo jumping skippys would say
 
oh can you put the code you have in this chat for me to put in the answer?
since you added the escaping for the x variable
 
yes
+'<td><select style="width:99%;" name="item['+x+'][]" ><?php echo $items;?></select></td>'
+'<td><input type="txt" name="item['+x+'][]" class="input-small"><input type="hidden" name="item['+x+'][]" value="'+x+'" >'
 
cool I updated with an answer :) good question it was a little challenging!
i'll throw you a few +1's
you should leave the question with more of a problem
since the answer solves it.. that way others can see the issue you were having and how to solve it
so like the critical part where you were doing name=[product][] ... etc should be in the question so that others can see we changed it to [x] :)
 
ok thanks, re-wording my question again - youre right
 
5:20 AM
yea put back the bad array you had originally and how it was sent. my answer includes how the data looks now as well :)
cheers man! good luck on your project :)
 
Yes cool - thanks heaps man :)
I kind reworded to show how it wasnt working even though it has changed my question a but. But its now just showing what wasnt working
 
yea
its cool
as long as its not the answer haha
i sent you an email
just so you have my address in your email already
 
Sweet as. Yes no i made it bad. I tried to word it as accurately to match your answer
LOL
I think i need coffee
 
haha its the afternoon there! :P
 
Yeh its weird taling to someoine in teh past
I can tell you though that there is no iphone 7 yet
 
5:36 AM
bahaha
well its not like its the past
because I can't tomorrow make a bet on something that is supposed to happen over there
its just a different time :)
 
Yes true
Bummer
 
haha
ok i fixed your question
should be a bit better now :)
 
And there i was thinking we could work out some horse racing strategies
Cool thanks heaps
I guess ill catch you later. I have to get this finsihed off now. But its one in teh snippets are for sure. Thanks for your patience
 
yea buddy sure thing!
 
 
1 hour later…
6:48 AM
Hey man you still awake there in texas?
 

last day (14 days later) »