Jump to content

foreach an insert into mysql


embsupafly

Recommended Posts

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.
Link to comment
Share on other sites

[!--quoteo(post=363379:date=Apr 10 2006, 05:55 PM:name=Honoré)--][div class=\'quotetop\']QUOTE(Honoré @ Apr 10 2006, 05:55 PM) [snapback]363379[/snapback][/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.
Link to comment
Share on other sites

[!--quoteo(post=363380:date=Apr 10 2006, 07:56 PM:name=embsupafly)--][div class=\'quotetop\']QUOTE(embsupafly @ Apr 10 2006, 07:56 PM) [snapback]363380[/snapback][/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:
[code]
<?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;
    }
}
?>
[/code]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[!--quoteo(post=363461:date=Apr 10 2006, 11:50 PM:name=mistergoomba)--][div class=\'quotetop\']QUOTE(mistergoomba @ Apr 10 2006, 11:50 PM) [snapback]363461[/snapback][/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:
[code]
$_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)
);
[/code]
then you could use the following:
[code]
foreach ($_SESSION['rows'] as $value) {
    $sql = 'INSERT v, pr, q INTO mytable VALUES("' . $value['v'] . '", "' . $value['pr'] . '", "' . $value['q'] . '")';
    // execute SQL statement
}
[/code]
Is this "a cleaner solution"?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.