temporary store values?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Garry Jones

    temporary store values?

    I want to simplify the following code.

    I have 4 variables which are to be read in up to ten times. When read in
    these are to be written to an SQL database. The value of $ct tells me when
    to stop reading in these 4 variables. Each time I read in the group of 4
    variables I need to increase $fx by 1 and read in additional values for each
    group of variables again until $fx is equal to $ct.

    I intend to use a while loop and read in these postings of four variables
    until my requirement is met. Do I have to open the SQL database and write in
    the four values for each passing of the loop? Or is there a way of storing
    the values and writing in everything at the same time.

    This is the code so far...

    <?php

    $ct=$_POST['totalnum'];

    $fx = 1;
    $insql1=$_POST["fardvag$fx "];
    $insql2=$_POST["fornamn$fx "];
    $insql3=$_POST["efternamn$ fx"];
    $insql4=$_POST["postnum$fx "];
    $fx++

    (I would like to "while" loop here until $fx=$ct, but don't see how I can
    get the values into variables to be written to the database. As for each
    passing of the loop new values will be assigned to each variable)

    mysql_connect(" localhost", "myusername ", "mypassword ") or
    die(mysql_error ());
    mysql_select_db ("my_databas e") or die(mysql_error ());
    mysql_query("IN SERT INTO `animals` VALUES ('$insql1', '$insql2', '$insql3',
    '$insql4')");
    Header("Locatio n: http://www.mydomain.co m/");

    (I can "while" loop here until $fx=$ct, but that means opening the database
    for writing several times)

    ?>

    Any help greatly appreciated.

    Garry Jones
    Sweden



  • Kimmo Laine

    #2
    Re: temporary store values?

    "Garry Jones" <garry.jones@mo rack.se> wrote in message
    news:e2nim6$7d5 $1@yggdrasil.gl ocalnet.net...[color=blue]
    >I want to simplify the following code.
    >
    > I have 4 variables which are to be read in up to ten times. When read in
    > these are to be written to an SQL database. The value of $ct tells me when
    > to stop reading in these 4 variables. Each time I read in the group of 4
    > variables I need to increase $fx by 1 and read in additional values for
    > each group of variables again until $fx is equal to $ct.
    >
    > I intend to use a while loop and read in these postings of four variables
    > until my requirement is met. Do I have to open the SQL database and write
    > in the four values for each passing of the loop? Or is there a way of
    > storing the values and writing in everything at the same time.
    >
    > This is the code so far...
    >
    > <?php
    >
    > $ct=$_POST['totalnum'];
    >
    > $fx = 1;
    > $insql1=$_POST["fardvag$fx "];
    > $insql2=$_POST["fornamn$fx "];
    > $insql3=$_POST["efternamn$ fx"];
    > $insql4=$_POST["postnum$fx "];
    > $fx++
    >
    > (I would like to "while" loop here until $fx=$ct, but don't see how I can
    > get the values into variables to be written to the database. As for each
    > passing of the loop new values will be assigned to each variable)
    >
    > mysql_connect(" localhost", "myusername ", "mypassword ") or
    > die(mysql_error ());
    > mysql_select_db ("my_databas e") or die(mysql_error ());
    > mysql_query("IN SERT INTO `animals` VALUES ('$insql1', '$insql2',
    > '$insql3',
    > '$insql4')");
    > Header("Locatio n: http://www.mydomain.co m/");
    >
    > (I can "while" loop here until $fx=$ct, but that means opening the
    > database for writing several times)
    >[/color]

    You only need to open the connection once, it stays open until page is
    finished or it is closed. So just open the connection before entering the
    while loop, and inside the loop run the mysql_query() statements.

    in pseudocode:

    connect server
    select database

    loop until condition
    get parameters
    create query
    execute query
    end loop

    --
    "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
    spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


    Comment

    Working...