Click to See Complete Forum and Search --> : extremely slow web site


wwonder
01-07-2008, 02:15 PM
hi

the site i am developing has recently started to be very slow to load (and sometimes it is ok) and sometimes it will not even load. the host is fasthost,

two things that i have done since it has become slow is put google anayltics code this is placed at the bottom of the page just before the </body>

i have also attached a link counter the code as follows:

<?php
include("linkhits.php");

//function linkcount (){

$query ="SELECT * from links order by sitename";
$result = mysql_query($query) or die(mysql_error());

while ($row=mysql_fetch_array($result))
{
$id = $row["id"];
$sitename = $row["sitename"];
$siteurl = $row["siteurl"];
$hitsout =$row["hitsout"];
}

//echo "<li><a href=\"linkhits.php?id=$id\" target=\"_blank\">$sitename</a></li>";
//}
?>



<?php
echo "<li class=\"homead\">
<h4><a href=\"includes/linkhits.php?id=8\" target=\"_blank\" title=\"Topa\"> <img src=\"adverts/topa.gif\" width=\"100\" height=\"60\" alt=\"Topa logo\" />Topa</a></h4>
<p>Overseas property specialists.</p>
<p class=\"more\"><a href=\"includes/linkhits.php?id=8\" target=\"_blank\" title=\"Ca Nicola\">More Info&raquo;</a></p>
</li>";?>

i have created a textonly file without any coding and this also has trouble loading

the site address is

www.boavistaexperience.com

if anyone can help im very new at php and don't know if i have done the code incorrect that is causing the site to become slow.

thanks

scragar
01-07-2008, 02:22 PM
$query ="SELECT * from links order by sitename";
$result = mysql_query($query) or die(mysql_error());

while ($row=mysql_fetch_array($result))
{
$id = $row["id"];
$sitename = $row["sitename"];
$siteurl = $row["siteurl"];
$hitsout =$row["hitsout"];
}
sorry, but that's just silly.

replace it with:
$query ="SELECT id, sitename, siteurl, hitsout FROM links ORDER BY sitename DESC LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result)){
$row=mysql_fetch_assoc($result);
extract($row);
};

edit: sorry, spelt extract wrong. Edited again to improve safety of results.
And again to use assoc to save on DB calls. :P

wwonder
01-07-2008, 02:52 PM
hi scragar

i am quite new at php and this was how i was advised to do it and it seemed to work, i have used your code which also works, at the moment i am having to enter the id no on the output

ie a href=\"../includes/linkhits.php?id=4

is there a way i can code it so i can use the same code id=$id for example or is the way im doing the best way??

thanks

scragar
01-07-2008, 02:59 PM
my code should be considerably faster than the code you had origionaly, since your orrigional code loads all the information from the database, then loops through it all overwriting itself untill it finishes with the last row it pulled. my method skips right to the last row only, and assigns the information without the need for a loop.

what exactly do you want the code to do/is it doing?
Also, since you have a plain text file also taking a long time to load I suspect you may have a slow server, or something eating up it's resources.

wwonder
01-08-2008, 03:30 AM
hi scragar

the server has been upgraded and now the site is loading faster so it must have been the server.

what im trying to create is a linkcounter, i have adverts on the website and wish to know how many times the advert has been clicked on. in the database i have the website address of each link with a unique id no attached to it.

at the moment i have to specify each id no like

href=\"../includes/linkhits.php?id=4

but was wondering if i could change to code so that i can use the same code on each link rather than having to change the id no to corresponde to the link id no.

thanks for you help

scragar
01-08-2008, 04:34 AM
that's fairly easy:

<?php
include("linkhits.php");
function GenLinks($uri=false, $hits=false){
$query ="SELECT * from links order by sitename";
$result = mysql_query($query) or die(mysql_error());
while (($row=mysql_fetch_array($result))!== false){
echo "<li>
<a href='linkhits.php?id=${row['id']}'
target='_blank'>${row['sitename']}</a>";
if($uri)
echo " is located at <strong>${row['siteurl']}</strong>";
if($hits)
echo " - ${row['hitsout']} hits";
echo " </li>\n";
};
};
// then to put the links anywhere just do:
GenLinks();
// if you want more functionality you can show their end locations as well:
GenLinks(true);
// hide the locations and show click numbers:
GenLinks(false, true);
// or even show both:
GenLinks(true, true);
?>

wwonder
01-08-2008, 04:42 AM
im quite new at this so hope i don't sound to stupid

i have nine different web site addresses in my table id 1 to 9

how does the code work as to know what link i am refering to link no 5 or link no 2 etc.

i don't understand how it will pull the ${row['id'] corresponding to the correct advert. say i want id no 5.

scragar
01-08-2008, 04:49 AM
oh, you want to specify an ID and have it pull just that advert?

that's just as easy to write:
<?php
include("linkhits.php");
function GenLinks($num = -1, $uri=false, $hits=false){
if($num == -1)
$query ="SELECT * FROM links ORDER BY sitename";
else
$query ="SELECT * FROM links WHERE id=$num ORDER BY sitename";
$result = mysql_query($query) or die(mysql_error());
while (($row=mysql_fetch_array($result))!== false){
echo "<li>
<a href='linkhits.php?id=${row['id']}'
target='_blank'>${row['sitename']}</a>";
if($uri)
echo " is located at <strong>${row['siteurl']}</strong>";
if($hits)
echo " - ${row['hitsout']} hits";
echo " </li>\n";
};
};
// function calls in exactly the same way to list all results you just do:
GenLinks();
// to list only the 5th link do:
GenLinks(5);
// to list addition info works as before:
GenLinks(5, true);// show url
GenLinks(5, false, true); // hide URL, show clicks
GenLinks(5, true, true);// show URL and clicks.
// you can also still access the old features using -1 as the number to pull:
GenLinks(-1, true);//show URL to all files.
GenLinks(-1, false, true);//hide URL to all files, show clicks.
GenLinks(-1, true, true);//show URL and clicks to all files.
?>

wwonder
01-08-2008, 05:28 AM
thank you scragar

the code you have given is more straight forward than i was using.

wwonder
01-08-2008, 09:35 AM
sorry to trouble you again but you have been such help im having trouble altering the code for the updating of the table at the moment im using

<?php
include("dbconnect.inc.php");
// Database connection stuff here

//changed id to linksid

if (isset($_GET['id']))
$_GET['linksid'] = (int)$_GET['id'];

else
$_GET['id'] = null;

//using on own creates an errer
$id=(int)$_GET['id'];


header('Location: '.$siteurl);
$query ="UPDATE links SET hitsout=hitsout+1 where id='$id'";

//$result = mysql_query($query) or die(mysql_error());
//mysql_close($connect);
}

?>

im starting to get myself confused now. :-(

scragar
01-08-2008, 09:41 AM
that doesn't cause any problems for me, so odds are you'll want to make sure it's a number first. use is_numeric() to test it like so:
if(isset($_GET['id']) && is_numeric($_GET['id']))
$_GET['linksid'] = (int)$_GET['id'];
else
$_GET['id'] = null;

having said that however, (int)"abc" returns 0, not an error, so that's unlikly to be the problem...

wwonder
01-08-2008, 02:07 PM
scragar i have used the following code and got the site to work i then decided i would like the image to be a link to so i put the images into a database, this is the first time i have ever used images and do not know how to pull the image out of the database, the following code just produces lots of charactors i believe i need to use a header but tried this and i get an error.

thanks

<?php
include("includes/linkhits.php");
function GenLinks($num = -1, $uri=false, $hits=false)
{
if($num == -1)
$query ="SELECT * FROM links ORDER BY sitename";
else
$query ="SELECT * FROM links WHERE id=$num ORDER BY sitename";
$result = mysql_query($query) or die(mysql_error());

while (($row=mysql_fetch_array($result))!== false){
echo "<a href='includes/linkhits.php?id=${row['id']}' target='_blank'><img src='${row['image']}' width='100' height='60'>${row['sitename']}</a>\n";
}
}
?>

wwonder
01-08-2008, 02:10 PM
i also forgot to ask why do you put the curly brackets in

${row['siteurl']} rather than $row['siteure'] cant find in my book as to why

scragar
01-08-2008, 02:20 PM
that won't work, what you will need to do it link to another page that will server the images.

images.php<?
header("Content-Type: image/png;charset=utf8");
include("includes/linkhits.php");
$_GET['id'] = preg_replace("/[^0-9]/", "", $_GET['id']);
$rs =mysql_query("SELECT image FROM links WHERE id=$num ORDER BY sitename");
echo mysql_result($rs, 0, "image");
mysql_close();
exit;
?>then replace your current image with:
<img src='images.php?id=${row['id']}' width='100' height='60'>
everything untested...



the {} are so PHP knows you have a variable in them, it's ok to do normal variables without them(say $x or $imdex for example), but PHP may need to know when to start or stop with some special values(anything that's an array for example.)
try this for yourself:

$foo = "_foo_";
$foobar = "=foobar=";
define("bar", "-bar-");
$x = array(0, 1, 2, 3, 4);
echo "{$foo}{bar}
$foobar
${foobar}
$x[0]
${x[0]}";