PDA

View Full Version : Parse php $var to javascript and redirect?


hugol
12-26-2009, 02:47 AM
Hi!
First post here on the forum, and i hope im posting in the right section!
:thumbsup:

To the problem:

Im currently running a php script who is parsing information from another url and caches it. A cronjob is set up to remove cache after a certain amount of time.
From what I understand, theres no way to update the cache from my sites url via php, I have to refresh the site via client side.

So, what im trying to do is to make Update script, wich is looping trough all my links on my site to update the cache.

Iv tryed with Javascript but I have never worked with it before, whereas I need your help.

This is what iv tryed:

<?php

$year = $_GET['view'];
$type = $_GET['type'];
$chart = $_GET['chart'];
$menu = $_GET['menu'];
$chartDate = $_GET['chartDate'];

$url = "home.php?menu=$menu&chart=$chart&type=$type&chartDate=$chartDate&order=position";
<script type="text/javascript">
<!--
function delayer(){
window.location = "<?php echo $url ?>"
}
//-->
</script>
</head>
<body onLoad="setTimeout('delayer()', 5000)">
<h2>Updating</h2>
<p>Redirecting...</p>

The problem here is that I want it to redirect to next chartDate.
Imagine that the link it starts from is:

home.php?menu=archive&chart=charts&type=europe&chartDate=1965-01-28&order=position

The next chartDate is 1965-02-28, 1965-03-28 => 2009-12-28

Is there any way I can make this happend and automatically?
Hope you understand my question!
Thanks in advance!
:D

godofreality
12-26-2009, 03:29 AM
put the char dates into a php array and use php to get the next chart date out of the array

hugol
12-26-2009, 04:40 AM
put the char dates into a php array and use php to get the next chart date out of the array

Thanks for your reply!

How do I use the output in the javascript? As I did in my post or in a diffrent way?

godofreality
12-26-2009, 05:27 AM
the same way u did in your original post and guessing from your original post i assumed u knew enough about php to be able to do the array method

hugol
12-26-2009, 07:40 PM
Well... I had some problems with putting out the dates to an array on the fly.
It outputted like this
[0]=>1965-01-28
[0]=>1965-02-28
and so forth, so i ended up doing a function for inserting the dates in the database automatically and when you run the function it checks if theres any new dates to insert.
After that I just call this code as an specific user to reload all the urls and save the cache.

I figure if i post a q I can aswell post the (my) solution!:p

$date = $_GET['chartDate'];
$result = mysql_query("SELECT * FROM table WHERE date ='$date'");

if (mysql_num_rows($result) == 0) {
die("Date does not exist");
}
if (!$result) {
echo("<b>Error performing query: " . mysql_error() . "</b>");
exit();
}
while ($row = mysql_fetch_array($result)) {
$dateid = $row['date'];
}

$nextResult= mysql_query("SELECT * FROM table WHERE date>'$dateid' ORDER BY date ASC LIMIT 1");
$nextRow=@mysql_fetch_array($nextResult);
$nextDate=$nextRow['date'];
@mysql_free_result($nextResult);


$url = "home.php?menu=archive&chart=chart&type=europe&chartDate=$nextDate&order=position";
?>
<script type="text/javascript">
<!--
function delayer(){
window.location = "<?php echo $url ?>"
}
//-->
</script>
</head>
<body onLoad="setTimeout('delayer()', 5000)">
<h2>Updating</h2>
<p>Redirecting...</p>


If you have any suggestions please feel free.
Thanks for your time!