PHP Question!!!

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
On my index page I have a news section that gets updated with every page refresh... since I maintain the index throughout the site and include the sections in a DIV this can be a problem because every news update queries the mysql server... so every time the user clicks any link or refreshes the page a new query is run to update the news...

the problem is that the news probably won't be updated that often so this is just a waste and if many people are accessing the site simultaneously it would create a lot of useless mysql traffic...

i was thinking of storing the news as an .xml file because then I wouldn't have to redesign my entire site because afaik it wouldn't be as bad to constantly check an .xml file for changes vs querying the server.

but then i need a way to update the .xml file say once per hour or something similar...

can anyone help me with suggestions or workarounds? thanks!
 

joshg

Golden Member
Jul 3, 2001
1,359
0
0
This is what AJAX is for (use AJAX to refresh everything else on your page and not the news section)
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
it's a good suggestion and i've actually been studying ajax recently but i just can't assume that everyone has javascript enabled for this one...
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
I did a ghetto hack once, where I used a timer file. If the date on the file was older then X, I re queried the database and rebuilt the xml file. I made sure the xml file was in rss format, then I had rss news and it was updated constantly every 10 minutes by checking the date of this file. Once the file is older then 10 minutes, delete, rebuild.
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
Originally posted by: sourceninja
I did a ghetto hack once, where I used a timer file. If the date on the file was older then X, I re queried the database and rebuilt the xml file. I made sure the xml file was in rss format, then I had rss news and it was updated constantly every 10 minutes by checking the date of this file. Once the file is older then 10 minutes, delete, rebuild.

that works for me... my question is where do I run the script from? can i just create a script that runs on the server independently of the website?

Originally posted by: Aikouka
Sounds like you'd have to make it a cron job.

a what now?
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
Originally posted by: jjones
How is the new news inserted?

in a nutshell: i call a function getNews() that returns a 'NewsList' object which is basically an ArrayList implementation that holds 'NewsItem' objects. I then use a loop to parse the NewsList and echo the text returned from a function that returns News Information.


in a bit more detail: the getNews(n) function queries the server and returns the 'n' most recent rows from the news table. it then loops through the results and creates 'NewsItem' objects for each, adding them into the 'NewsList' as it goes along.

I wrote the news classes a couple weeks ago when I got really excited about object-oriented PHP classes but it seems fine to me...



perhaps ideally, I'd parse an xml file and echo the contents into my news DIV and somewhere else I'd make sure the xml file gets updated regularly. come to think of it i think i just found the solution... i could just update the xml file every time news is posted. (which won't be that often).

this way instead of querying the database with every refresh all I'd do is parse xml... which isn't great but not so bad either... but actually... is there a way to check when the file has been last modified? that way I could save myself some unnecessary work....
 

jjones

Lifer
Oct 9, 2001
15,424
2
0
See that's what I was thinking. If you want to take this off the db, then when you're posting new news to the db, you can update a file when that happens. Either an include or xml would work fine, and save the db queries for viewing archived news. No need to even worry about file last modified because the action only happens when there's new news anyway, but if you need it for some reason, filemtime will tell you when a file was last modified.
 

mugs

Lifer
Apr 29, 2003
48,920
46
91
If I understand what you're asking, I think one option would be to store the news in a session variable along with a timestamp, and each time you hit a new page check for the session variable, check if the time is recent enough, and if so display the news from the session variable. Otherwise hit the DB and update the session variable.

But I have to ask - is performance actually a problem now, or do you anticipate it becoming a problem? Most small websites could be written pretty inefficiently without issues.

Edit: Of course that could require a bit of disk space if you have a lot of simultaneous users...

Your XML file idea is not a bad idea - or an alternative would be to serialize a PHP object and output that to a text file, then you don't have to worry about parsing it.

What you'd want to do is write a script that generates the file, then add it to crontab.

See examples: http://en.wikipedia.org/wiki/Crontab

You can run PHP code from the command line with "php script.php".

If you're in a shared hosting situation that may not be something you're allowed to do.
 

mugs

Lifer
Apr 29, 2003
48,920
46
91
Originally posted by: jjones
See that's what I was thinking. If you want to take this off the db, then when you're posting new news to the db, you can update a file when that happens. Either an include or xml would work fine, and save the db queries for viewing archived news. No need to even worry about file last modified because the action only happens when there's new news anyway, but if you need it for some reason, filemtime will tell you when a file was last modified.

Yeah that would work too.
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
Originally posted by: franguinho
Originally posted by: sourceninja
I did a ghetto hack once, where I used a timer file. If the date on the file was older then X, I re queried the database and rebuilt the xml file. I made sure the xml file was in rss format, then I had rss news and it was updated constantly every 10 minutes by checking the date of this file. Once the file is older then 10 minutes, delete, rebuild.

that works for me... my question is where do I run the script from? can i just create a script that runs on the server independently of the website?

Originally posted by: Aikouka
Sounds like you'd have to make it a cron job.

a what now?

my script ran every time the page loaded, it checked the date of the file, if it was older then 10 minutes, it re-ran the sql and generated a new xml file. However the suggestions below to serialze a php object is a better way to go. Mine was a dirty hack a few years old.

 

jjones

Lifer
Oct 9, 2001
15,424
2
0
Originally posted by: troytime
why not just use query caching?
That would probably be best in this situation. However, that depends on whether or not query cache is enabled. If it's a shared server, he may be out of luck.

 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
Originally posted by: jjones
See that's what I was thinking. If you want to take this off the db, then when you're posting new news to the db, you can update a file when that happens. Either an include or xml would work fine, and save the db queries for viewing archived news. No need to even worry about file last modified because the action only happens when there's new news anyway, but if you need it for some reason, filemtime will tell you when a file was last modified.

great! i already have it generating an xml file with the last 'n' entries every time news is posted (which won't be that often)

i'm working on a function to parse the xml and output it as text in the news div...

since i don't expect news to be posted more than once or twice a month maybe i could just read the xml at the start of the session and save the news in session variables. and if the news gets updated in the middle of a session that's just too bad.

it's really a kind of situation where i offered dynamic news functionality for an additional price and the person said "sure" but judging by the nature of the site i don't really think there will be much to add regularly, so it's all good!

Originally posted by: mugs
If I understand what you're asking, I think one option would be to store the news in a session variable along with a timestamp, and each time you hit a new page check for the session variable, check if the time is recent enough, and if so display the news from the session variable. Otherwise hit the DB and update the session variable.

But I have to ask - is performance actually a problem now, or do you anticipate it becoming a problem? Most small websites could be written pretty inefficiently without issues.

Edit: Of course that could require a bit of disk space if you have a lot of simultaneous users...

Your XML file idea is not a bad idea - or an alternative would be to serialize a PHP object and output that to a text file, then you don't have to worry about parsing it.

What you'd want to do is write a script that generates the file, then add it to crontab.

See examples: http://en.wikipedia.org/wiki/Crontab

You can run PHP code from the command line with "php script.php".

If you're in a shared hosting situation that may not be something you're allowed to do.

sup mugs!

no performance is definitely not a problem and it's a small site, but I want to build it from the ground up as efficiently as possible to avoid later headaches. there probably won't be much simultaneous traffic or that many visitors. and yea its shared hosting but i won't be needing a cron job after all because I will generate the xml whenever new news is posted.

i think all in all this is the best solution.

i definitely need to read up on caching queries though because that's a mystery to me
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
Originally posted by: jjones
Originally posted by: troytime
why not just use query caching?
That would probably be best in this situation. However, that depends on whether or not query cache is enabled. If it's a shared server, he may be out of luck.

it is a shared server
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
check to see if you can cache the query

if not, just stick with the query
i really don't think parsing xml is faster than a query.
 

joshg

Golden Member
Jul 3, 2001
1,359
0
0
Yes I was thinking while I was driving to work, you could do what you asked how to do with a cron job also, but normally (assuming you are leasing your hosting space from a provider or something) this is not an option!
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
Originally posted by: joshg
Yes I was thinking while I was driving to work, you could do what you asked how to do with a cron job also, but normally (assuming you are leasing your hosting space from a provider or something) this is not an option!

thx dude but i've already solved that problem!

 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |