sessions variables not changing in a for loop

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

    sessions variables not changing in a for loop

    I am working on a small graphing application. In the process of
    graphing I use 3 seperate scripts for getting the job done. The first
    is the page that the use sees and selects all the data from. This
    selected data gets passed internally using session variables to the
    next page. This page takes the passed data, runs it for error
    checking and determines where to send it next. I have gotten most of
    the desired results through this method with acceptable performance,
    but I have run across a problem.

    Currently my graphing script is set up to accept only an array
    containing one or two variables passed in $_SESSION['dataVariables'].
    If somebody where to select three variables for graphing I want them
    to be able to see all three graphs on one page as the result. My
    solution to this answer was to write a loop:

    if(count($dataA rray) > 2)
    {
    $count = count($data);
    for($i = 0; $i < $count; $i++)
    {
    $_SESSION['dataArray'] = array($dataArra y[$i]);
    echo '<img src="graph.php" alt="graph" /><br />';
    }
    }

    Through testing I know that the value inside of $_SESSION['dataArray']
    is changing on each loop iteration, but my graphs aren't being
    outputted accordingly. Each graph ends up being graphed using
    whatever the last value in the array is and for the life of me I
    cannot figure out where the problem is.

    Any help would be greatly appreciated.

    -z0ink
  • Pedro Graca

    #2
    Re: sessions variables not changing in a for loop

    z0ink wrote:[color=blue]
    > Currently my graphing script is set up to accept only an array
    > containing one or two variables passed in $_SESSION['dataVariables'].
    > If somebody where to select three variables for graphing I want them
    > to be able to see all three graphs on one page as the result. My
    > solution to this answer was to write a loop:
    >
    > if(count($dataA rray) > 2)
    > {
    > $count = count($data);
    > for($i = 0; $i < $count; $i++)
    > {
    > $_SESSION['dataArray'] = array($dataArra y[$i]);
    > echo '<img src="graph.php" alt="graph" /><br />';
    > }
    > }
    >
    > Through testing I know that the value inside of $_SESSION['dataArray']
    > is changing on each loop iteration, but my graphs aren't being
    > outputted accordingly. Each graph ends up being graphed using
    > whatever the last value in the array is and for the life of me I
    > cannot figure out where the problem is.[/color]

    Ok, imagine, for the sake of argument, the user's browser is configured
    to NOT display images.

    It gets this HTML

    <img src="graph.php" alt="graph" /><br />
    <img src="graph.php" alt="graph" /><br />
    <img src="graph.php" alt="graph" /><br />

    and the user sees three "broken images".

    Now, if the user were to select /any/ of those images and get it from
    the server, /all/ would be updated to the same image.

    Well ... this is exactly what happens when the browser is showing
    images. It will only ask for them when it's ready and at that time your
    $_SESSION data has the last value from the for loop.


    So, you need a way to keep the data available for longer than the time
    it takes PHP to execute the loop ... and you also need a way to tell the
    browser which data to use for which image.

    I'd do that by putting the graph data into arrays, like this

    $_SESSION['dataArray'][$i] = array($dataArra y[$i]);

    so that you have the session variables
    $_SESSION['dataArray'][0]
    $_SESSION['dataArray'][1]
    $_SESSION['dataArray'][2]


    Now you just have to match each piece of data with each graph. So send a
    parameter in the URL ...

    <img src="graph.php? data=0" alt="graph" /><br />
    <img src="graph.php? data=1" alt="graph" /><br />
    <img src="graph.php? data=2" alt="graph" /><br />

    and in graph.php, instead of using $_SESSION['dataArray'] use

    $_SESSION['dataArray'][$_GET['data']]


    HTH
    Happy Coding :-)

    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    Working...