GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   quick php help required (https://gfy.com/showthread.php?t=1067943)

roly 05-13-2012 06:22 AM

quick php help required
 
i'm not a programmer so this might seem fairly basic stuff but here goes. i've got a dynamic page that displays random data from a mysql database. it currently displays new data with every page refresh. i'm sure google won't like this, so i want to generate the dynamic part of the page once per day (with cron?) and save it as either a txt file that i can include or use the whole original page as a template.

i know what i want to do i'm just not sure how, any suggestions of the best way of achieving this?

thanks in advance

V_RocKs 05-13-2012 06:25 AM

Best to do exactly what you already suggested.

Babaganoosh 05-13-2012 06:26 AM

Set up a cron job and have it output to a static file.

Or just let the script run once normally when someone visits the page but output the content to a static file and use filemtime() to find when the file was last modified. The first solution seems easier to me but it's your call.

EddyTheDog 05-13-2012 06:34 AM

You seem to have it sorted - The php just needs to get the timestamp from the txt file and if its over 24hrs old create a new one.

asdasd 05-13-2012 06:52 AM

Call Microsoft !!!

SmokeyTheBear 05-13-2012 08:26 AM

im not sure why you think this would help. Google does check your site more than once per day but i dont think google will see much of a difference in having new data every 6 hours or every 24. It will still appear much the same to google.

Might want to set the cron a bit longer like every week or month even

potter 05-13-2012 09:50 AM

Yeah, I don't google is going to care because it's probably not checking your site more than once every 24 hours anyway. So whether you reload dynamic content every page refresh or once every 24 hours it'll likely not make a difference.

But, if you do want to continue to do what you're asking. Add a table/column/something to the database and just make it a simple unix timestamp. Everytime your page is loaded - prior to running the function that replaces the content - check whether or not time() is greater than 24 hours of the timestamp in the DB. If it isn't, you don't do anything - if it is you update the timestamp to the current time and replace the content.

Babaganoosh 05-13-2012 09:53 AM

Quote:

Originally Posted by potter (Post 18944562)
Yeah, I don't google is going to care because it's probably not checking your site more than once every 24 hours anyway. So whether you reload dynamic content every page refresh or once every 24 hours it'll likely not make a difference.

But, if you do want to continue to do what you're asking. Add a table/column/something to the database and just make it a simple unix timestamp. Everytime your page is loaded - prior to running the function that replaces the content - check whether or not time() is greater than 24 hours of the timestamp in the DB. If it isn't, you don't do anything - if it is you update the timestamp to the current time and replace the content.

Unnecessary db call is unnecessary.

HomerSimpson 05-13-2012 10:27 AM

http://www.awmzone.com/services

potter 05-13-2012 10:31 AM

Quote:

Originally Posted by Babaganoosh (Post 18944564)
Unnecessary db call is unnecessary.

Well, as stated already a cron job be the best way.

But please don't try to make it out like a single db query to get a single field value is bad. :2 cents:

Babaganoosh 05-13-2012 10:54 AM

Quote:

Originally Posted by potter (Post 18944602)
Well, as stated already a cron job be the best way.

But please don't try to make it out like a single db query to get a single field value is bad. :2 cents:

Not trying to make a big deal out of it but if a file is written there's already a timestamp. There's no reason to make an entry in a db and query the db every time we want to see when the file was created. That step is completely redundant.

fris 05-13-2012 11:13 AM

Quote:

Originally Posted by roly (Post 18944387)
i'm not a programmer so this might seem fairly basic stuff but here goes. i've got a dynamic page that displays random data from a mysql database. it currently displays new data with every page refresh. i'm sure google won't like this, so i want to generate the dynamic part of the page once per day (with cron?) and save it as either a txt file that i can include or use the whole original page as a template.

i know what i want to do i'm just not sure how, any suggestions of the best way of achieving this?

thanks in advance

i did something like this, i had 30 links, which would rotate one spot per day, so everyone got the same exposure.

not sure if that interests you.

roly 05-13-2012 11:24 AM

thanks for all the replys guys, this is roughly the php code i've got

Code:

$sql = "select foo from bar where catid = $cat limit 20";
      $result = mysql_query($sql ,$db);

 while ($myrow = mysql_fetch_array($result))
{
echo $myrow['something'];
}

i want to copy the output of that into a text file as discussed so that i can include it in a file, how do i do that? fopen? any idea how i would write it?

Babaganoosh 05-13-2012 11:26 AM

Quote:

Originally Posted by roly (Post 18944679)
thanks for all the replys guys, this is roughly the php code i've got

Code:

$sql = "select foo from bar where catid = $cat limit 20";
      $result = mysql_query($sql ,$db);

 while ($myrow = mysql_fetch_array($result))
{
echo $myrow['something'];
}

i want to copy the output of that into a text file as discussed so that i can include it in a file, how do i do that? fopen? any idea how i would write it?

http://php.net/manual/en/function.fwrite.php

roly 05-13-2012 11:27 AM

Quote:

Originally Posted by SmokeyTheBear (Post 18944489)
im not sure why you think this would help. Google does check your site more than once per day but i dont think google will see much of a difference in having new data every 6 hours or every 24. It will still appear much the same to google.

Might want to set the cron a bit longer like every week or month even

well the bulk of the page is generated on the fly and with almost the whole page content changing with every refresh i think that's likely to look spammy with google. i would rather google think it was a static page that was updated daily.

roly 05-13-2012 11:28 AM

Quote:

Originally Posted by fris (Post 18944665)
i did something like this, i had 30 links, which would rotate one spot per day, so everyone got the same exposure.

not sure if that interests you.

yes that's the sort of thing.

roly 05-13-2012 11:29 AM

Quote:

Originally Posted by Babaganoosh (Post 18944681)

that looks like what i'm looking for thanks

fris 05-13-2012 11:41 AM

Quote:

Originally Posted by roly (Post 18944685)
yes that's the sort of thing.

links.txt

Code:

http://www.bing.com|very unpopular search engine
http://www.google.com|search engine
http://www.gfy.com|gfy forum

to show the links

Code:

<?

$show = 30;
$linkfile = dirname(__FILE__). "/links.txt";
$links = file($linkfile);
$result = array();
for ($i=0; (($i<$show) && ($i<count($links))); $i++) {
  $link = explode("|", $links[$i]);
  $result[] = '<a href="' . $link[0] . '">' . $link[1] . '</a><br/>';
  echo $result[$i];
}
       
?>

script to run in crontab once every 24 hours

Code:

<?

$linkfile = dirname(__FILE__). "/links.txt";
$tmp = file($linkfile);
$result = array();
$result[] = trim($tmp[(count($tmp)-1)]);
unset($tmp[(count($tmp)-1)]);

foreach ($tmp as $idx => $t) {
  $result[] = trim($t);
}

$s = implode("\n", $result);
$fp = fopen($linkfile, "w+");
fwrite($fp, $s);
fclose($fp);

?>


roly 05-13-2012 01:39 PM

thanks fris that's a big help :thumbsup

roly 05-14-2012 10:03 AM

hi

i've got the fwrite to work and my script works ok, so i've got both bits working seperately, but i'm not sure how i call all of this:

Code:

$sql = "select foo from bar where catid = $cat limit 20";
      $result = mysql_query($sql ,$db);

 while ($myrow = mysql_fetch_array($result))
{
echo $myrow['something'];
}

into this $scriptoutput:

Code:

$myfile= fopen ("/home/domains/domain.com/public_html/mytxt.txt", "w+");
fwrite($myfile, $scriptoutput);
fclose($myfile;

how do i get that first little script to output into $scriptoutput, that's what i'm stuck on, anyone got any advice?

thanks in advance

Brujah 05-14-2012 11:03 AM

Code:

$sql = "select foo from bar where catid = $cat limit 20";
$result = mysql_query($sql ,$db);

$scriptoutput = '';
while ($myrow = mysql_fetch_array($result))
{
    $scriptoutput .= '<li>' . $myrow['something'];
}

$myfile= fopen ("/home/domains/domain.com/public_html/mytxt.txt", "w+");
fwrite($myfile, $scriptoutput);
fclose($myfile;

If you're interested in using prepared statements, you can do this:
Code:

$mysqli = new mysql( 'localhost', 'username', 'password', 'db' );

$content = '';
if( $stmt = $mysqli->prepare("SELECT foo FROM bar WHERE catid = ? LIMIT 20") )
{
  $stmt->bind_param( 'i', $cat );
  $stmt->execute();
  $stmt->bind_result( $foo );
  while ( $stmt->fetch() )
  {
    $content .= '<li>' . $foo;
  }
  $stmt->close();
}

$mysqli->close();

file_put_contents( '/path/to/saved.txt', $content );

set up a cron to execute this every 24 hours
0 3 * * * /usr/bin/php /path/to/cron.php

roly 05-14-2012 11:47 AM

Quote:

Originally Posted by Brujah (Post 18946043)
Code:

$sql = "select foo from bar where catid = $cat limit 20";
$result = mysql_query($sql ,$db);

$scriptoutput = '';
while ($myrow = mysql_fetch_array($result))
{
    $scriptoutput .= '<li>' . $myrow['something'];
}

$myfile= fopen ("/home/domains/domain.com/public_html/mytxt.txt", "w+");
fwrite($myfile, $scriptoutput);
fclose($myfile;

If you're interested in using prepared statements, you can do this:
Code:

$mysqli = new mysql( 'localhost', 'username', 'password', 'db' );

$content = '';
if( $stmt = $mysqli->prepare("SELECT foo FROM bar WHERE catid = ? LIMIT 20") )
{
  $stmt->bind_param( 'i', $cat );
  $stmt->execute();
  $stmt->bind_result( $foo );
  while ( $stmt->fetch() )
  {
    $content .= '<li>' . $foo;
  }
  $stmt->close();
}

$mysqli->close();

file_put_contents( '/path/to/saved.txt', $content );

set up a cron to execute this every 24 hours
0 3 * * * /usr/bin/php /path/to/cron.php

Thanks Brujah, that's a big help, very much appreciated. :thumbsup

Brujah 05-14-2012 11:54 AM

oops, that first line in the prepared statement should be:
$mysqli = new mysqli, forgot the i

if you cron using the php /path/to/script.php example, then wherever you want it in your files you can just

Code:

<?php echo file_get_contents( '/path/to/saved.txt' ); ?>

xJerk 05-14-2012 12:28 PM

You could do

ORDER BY RAND(CURDATE())

But there are some major performance implications with doing this with a large table (unless the query results are cached).

roly 05-14-2012 01:18 PM

Quote:

Originally Posted by Brujah (Post 18946141)
oops, that first line in the prepared statement should be:
$mysqli = new mysqli, forgot the i

if you cron using the php /path/to/script.php example, then wherever you want it in your files you can just

Code:

<?php echo file_get_contents( '/path/to/saved.txt' ); ?>

i'm using
Quote:

<?php include ("/home/domain.com/public_html/saved.txt"); ?>
is that just as efficient?

it's all working perfectly thanks

roly 05-14-2012 01:19 PM

Quote:

Originally Posted by xJerk (Post 18946219)
You could do

ORDER BY RAND(CURDATE())

But there are some major performance implications with doing this with a large table (unless the query results are cached).

got it sorted thanks, but that's a new one i've never heard of which i'm sure will have its uses.

Brujah 05-14-2012 01:53 PM

Quote:

Originally Posted by roly (Post 18946361)
i'm using


is that just as efficient?

it's all working perfectly thanks

I prefer file_get_contents() in this case, and it's probably safer just in case, since it won't attempt to execute code or anything.

roly 05-15-2012 05:05 AM

Quote:

Originally Posted by Brujah (Post 18946424)
I prefer file_get_contents() in this case, and it's probably safer just in case, since it won't attempt to execute code or anything.

that's great, thanks again

potter 05-16-2012 10:20 AM

Quote:

Originally Posted by Babaganoosh (Post 18944630)
Not trying to make a big deal out of it but if a file is written there's already a timestamp. There's no reason to make an entry in a db and query the db every time we want to see when the file was created. That step is completely redundant.

Your idea breaks down if the file is ever manually updated, or if there isn't actually a static file to update and it's a dynamic page.

I would go as far to say a file's timestamp is akin to user input - not something I would base a script off of.


All times are GMT -7. The time now is 07:21 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123