Jump to content


Photo

foreach an insert into mysql


  • Please log in to reply
7 replies to this topic

#1 embsupafly

embsupafly
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 10 April 2006 - 05:42 PM

I have a series of session vars like (v_1, pr_1, q_1) and there could be from 0 to 10 of these groupings. v_x is a product id, pr_x is a price for the product id, and q_x is the quantity of the product id. I need to insert these groupings into one row of an sql table, could need to be done 0 times up to 10 times. I am thinking I need to foreach them, but have never done it with inserting anything into a database.
Eric

#2 Honoré

Honoré
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationAntwerp - Belgium

Posted 10 April 2006 - 05:55 PM

Inserting in one row seems a bad idea to me, inserting 0 to 10 rows seems better.

#3 embsupafly

embsupafly
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 10 April 2006 - 05:56 PM

[!--quoteo(post=363379:date=Apr 10 2006, 05:55 PM:name=Honoré)--][div class=\'quotetop\']QUOTE(Honoré @ Apr 10 2006, 05:55 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Inserting in one row seems a bad idea to me, inserting 0 to 10 rows seems better.
[/quote]


There are going to be 0 - 10 rows. the 3 groupings (v_x, pr_x, and q_x) will go into one row, with 0-10 different groupings = 0-10 different rows.
Eric

#4 Honoré

Honoré
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationAntwerp - Belgium

Posted 10 April 2006 - 06:30 PM

[!--quoteo(post=363380:date=Apr 10 2006, 07:56 PM:name=embsupafly)--][div class=\'quotetop\']QUOTE(embsupafly @ Apr 10 2006, 07:56 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
There are going to be 0 - 10 rows. the 3 groupings (v_x, pr_x, and q_x) will go into one row, with 0-10 different groupings = 0-10 different rows.
[/quote]
May be you could try something like this:
<?php
$_SESSION['v_1'] = "product1"; $_SESSION['pr_1'] = 1.1; $_SESSION['q_1'] = 1;
$_SESSION['v_2'] = "product2"; $_SESSION['pr_2'] = 2.2; $_SESSION['q_2'] = 2;
$_SESSION['v_3'] = "product3"; $_SESSION['pr_3'] = 3.3; $_SESSION['q_3'] = 3;

$row = 0;
$row_exists = true;
while ($row_exists) {
    $row++;
    if (array_key_exists('v_'.$row, $_SESSION)) {
        // insert a row in database
        $sql = 'INSERT v, pr, q INTO wo_lines VALUES("' . $_SESSION['v_'.$row] . '", "' . $_SESSION['pr_'.$row] . '", "' . $_SESSION['q_'.$row] . '")';
        // execute request
    } else {
        $row_exists = false;
    }
}
?>


#5 embsupafly

embsupafly
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 10 April 2006 - 06:45 PM


Thanks, but I am looking for a bit cleaner solution. Anyone else?
Eric

#6 embsupafly

embsupafly
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 10 April 2006 - 08:17 PM

Let me re-explain:

Ok,

I have some data stored in session vars that need to be added to a mysql table named wo_lines

I have data like this:

v_1 = BG-457
pr_1 = 9.99
q_1 = 3

Where v_1 is a product id, pr_1 is the price, and q_1 is the quantity.

The 1 indicates that these values are associated with each other and need to be inserted into the same row with each other.

I may have from 1 - 10 items to like this, and in some cases none, so we would have v_x, pr_x, q_x up to 10 times with x changing as determined and based upon how many items are selected.

I also have other session vars as well that are going into other tables, so we shouldn't explicitly try to do this to ALL session vars, just the ones matching the patterns above.
Eric

#7 mistergoomba

mistergoomba
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 10 April 2006 - 09:50 PM

may i suggest adding an extra dimension to the array? instead of $_SESSION['pr_1'], maybe $_SESSION['pr'][1]. It may make the code cleaner and easier to sort through the array.

I would organize it as $_SESSION['BG-457']['p'] = 9.99; $_SESSION['BG-457']['q'] = 3;

i dunno if that helps or answers the question, just throwing it out there

#8 Honoré

Honoré
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationAntwerp - Belgium

Posted 11 April 2006 - 08:03 AM

[!--quoteo(post=363461:date=Apr 10 2006, 11:50 PM:name=mistergoomba)--][div class=\'quotetop\']QUOTE(mistergoomba @ Apr 10 2006, 11:50 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
may i suggest adding an extra dimension to the array? instead of $_SESSION['pr_1'], maybe $_SESSION['pr'][1]. It may make the code cleaner and easier to sort through the array.
[/quote]
If you can organize your session variables differently then you have a lot of other possibilities.
Imagine organizing your session data as:
$_SESSION['rows'] = array(
    array('v' => 'BG-457', 'pr' => 9.99, 'q' => 3),
    array('v' => 'BG-123', 'pr' => 8.88, 'q' => 2),
    array('v' => 'BG-987', 'pr' => 7.77, 'q' => 4)
);
then you could use the following:
foreach ($_SESSION['rows'] as $value) {
    $sql = 'INSERT v, pr, q INTO mytable VALUES("' . $value['v'] . '", "' . $value['pr'] . '", "' . $value['q'] . '")';
    // execute SQL statement
}
Is this "a cleaner solution"?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users