I have a script that generates random data into an array then uses a while loop to display the data. I have a separate script that takes a row from that array and inserts it into a MySQL database. The problem I am having, is that every time the page refreshes the data changes. So instead of inserting the original row I wanted into the database, it inserts the refreshed row. I can post code if need be, but I am just looking for a way to:

a. Either keep the random script from generating automatically or, in intervals, on a particular event, etc.

b. Or, find a way to preserve those variables somehow so they don't refresh when the page is reloaded. Possibly store them in a session?

Any recommendations are appreciated.

Storing the data in a session would be the easiest way to overcome your problem.

OK, the problem I have had with this method, is that every time the query runs, it changes the session variable, thus doing no good. There must be something simple I am missing. How would I separate the session variable from the array? Here is the code:

$results = mysql_query("SELECT column1, column2, date, time FROM users ORDER BY RAND() LIMIT 1");
$row_data = mysql_fetch_assoc($results);
$_SESSION['data_date'] = $row_data['date'];
$_SESSION['data_time'] = $row_data['time'];

How about this kkeith, what if I create a new column in the database. Before the query, I check the database for the value of that field. If the field contains a 1 the query does not run, if it contains nothing the query runs. When the query runs, it puts a 1 in this field. Then when the user logs out it clears the field and the process starts all over again. Do you think this would work? I don't need the data refreshing every time the page reloads, just occasionally.

That sounds like it would work.

Can you explain what you are doing exactly? Like the reason for the random data, ect.

That sounds like it would work.

Can you explain what you are doing exactly? Like the reason for the random data, ect.

The random data is due to a recommendation area. When the user goes to that page there are recommendations of other places on the site to visit. But it needs to be dynamic.

Here is what I came up with:

<?php
//Connect to user database
include_once("connect_user.php");
$query = "SELECT rec_check FROM members WHERE uid='".$_SESSION['uid']."'";
if(!$rec_check = mysql_query($query)){
echo mysql_error();

}elseif($rec_check = "1"){
header("location: index.php");

}elseif(empty($rec_check)){

//Connect to website database
include("connect_web.php");

$results = mysql_query("SELECT column1, column2, date, time FROM data ORDER BY RAND() LIMIT 1");
$row_animals = mysql_fetch_assoc($results);
}
?>

But it appears to be an endless loop, no page is displayed then I get a firefox error that the page isn't redirecting properly. Could it be because the query is encapsulated? Another suspicion I have is that I wrote the script to connect to 2 different databases because the website data is in a separate database than the user information. Any ideas?

Well after changing some of it around, I nearly got it working. But I realized something, the while loop will error out if I bypass the query and I won't get any data. Unfortunately the query HAS to run. Any other ideas out there? I need to get this working badly.

Sorry about the delay in my response.

What is the value of rec_check? Just a 0 or a 1?

Oh that's OK kkeith, at the time of testing it contained a 1. I can post that failed code if you want. One thing it was missing now that I think about it was dumping the data into a session then getting the data from there. But I just realized I accidentally deleted that code. Working on rebuilding it.

Can you post the code and some example data? Is the code online at all so I can see it working (or not working)?

Here is the code.

<?php
//Connect to user database
include("connect_user.php");

$qry = mysql_query("SELECT rec_check FROM members WHERE uid='".$_SESSION['id']."'");
if(!$rec_check = mysql_query($qry)){
echo mysql_error();
}elseif($rec_check == "1"){

header("location: index.php");
}

if($rec_check == "0"){

//Connect to website database
include("connect_web.php");

$resu = mysql_query("SELECT column1, column2, date, time FROM data ORDER BY RAND() LIMIT 1");
$row_data = mysql_fetch_assoc($resu);
$_SESSION['data'] = $row_data;

include("connect_user.php");

$qy = "UPDATE members SET rec_check = 1 WHERE uid='".$_SESSION['id']."'";
if(!$rec_c = mysql_query($qy)){
echo mysql_error();
}else{
echo "added 1 to database"; (I just added this to check the progress)
}
}

?>

It appears to be running the query every time however, even though I had that part working before. I just have been working on this all day and am starting to go crosseyed :)

After looking through your code, I am still confused as to what you are trying to accomplish. Sorry, but can you try to explain it better.

I got it taken care of. Thanks for trying to help kkeith.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.