NOOB in trouble!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
TrojanMouse
Forum Newbie
Posts: 5
Joined: Fri Nov 19, 2010 7:01 pm
Location: Aberdeen

NOOB in trouble!!!

Post by TrojanMouse »

Hi,

I'm having a real problem that I hope someone can help me with. I wrote a graphical click-counter in PHP and had it working perfectly in one of my HTML web pages (4 counters were displayed in a table). As it was working (which impressed me as I'm a total noob and it's my first script!), I moved onto the next (totally independant) part of the site and started writing that.

However, now comes the problem...

While showing the site to a colleague, I noticed the counters were not being displayed, even though I've not changed the html page or php code. If I type the url of the php script into the browser's address bar, I get the appropriate value (in graphical format) displayed. However, the html page shows an empty table. This is the same on localhost and the website.

I even replaced the php script with one that just echo's 'Hello', with the same results.

I've basically been thrown in at the deep-end having had an unfinished web-site dumped in my lap and I don't know any php other than what I've seen in basic examples. It may be that I just 'got lucky' getting it to work the first time, but I'd sure like to know what happened to screw it up (and more importantly, how to fix it!). It's driving me nuts! :banghead:
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: NOOB in trouble!!!

Post by s992 »

Can you show us the code?
User avatar
TrojanMouse
Forum Newbie
Posts: 5
Joined: Fri Nov 19, 2010 7:01 pm
Location: Aberdeen

Re: NOOB in trouble!!!

Post by TrojanMouse »

Hi,

The code for the php works fine, however to try to locate the problem I reduced it to just the following:

Code: Select all

01//counter.php
02 <?php 
03 echo "Hello";
04 ?>
This worked when run directly from the browser (displaying 'Hello'), but produced nothing from within the webpage.

The web page is as follows;

count.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Pragma" content="no-cache" />
<title>Download Counter</title>
<link rel="stylesheet" type="text/css" href="files/mstyle.css" />
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="files/ie.css" />
<![endif]-->
<link rel="stylesheet" href="files/styles.css" type="text/css" />

</head>
<body>
<div id="wrap">
<div class="wrap_corner_right">
<div id="topcontent">
<div id="body" class="plain">
<h1>Download counts:</h1>
<table width="90%" border="0" cellspacing="0" cellpadding="5" align="center">
<tr>
<td width="50%"> Link 1: <script language="php" src="counter.php?id=1"><!-- //--></script></td>
<td width="50%"> Link 2: <script language="php" src="counter.php?id=2"><!-- //--></script></td>
</tr>
<tr>
<td width="50%"> Link 3: <script language="php" src="counter.php?id=3"><!-- //--></script></td>
<td width="50%"> Link 4: <script language="php" src="counter.php?id=4"><!-- //--></script></td>
</tr>
</table>
<br>
</div>
</div>
<div id="bottomcontent">
<div class="bottomcontent_right">
</div>
</div>
</div>
</div>
</body>
</html>
I confess to knowing nothing about headers, content-types and doctypes, those used were specified by the previous (former) employee who is no longer available to provide any answers. I'm assuming there is something wrong with the html as the php works on it's own.

In case it's necessary, and so everyone can see how bad my first script it, here is the proper code for counter.php as well:

Code: Select all

<?php 
	$pid = $_GET['id'];
	$lines = file('777/counters.txt');
	$value = '0';
	$donated = '0';

	foreach ($lines as $thisline)
	{
	    $thisline = trim($thisline);
	    list( $id, $count, $amount, $description )=explode('_',$thisline);
	    if ( $id == $pid )
			{
			$value = $count;
			$donated = $amount;			
			}
		}

	$formatted = number_format($donated, 2, '.', ',');
	$strg = "".$value."  (p".$formatted.")  ";

	$numbs = str_split ($strg);
	$html = "";
	foreach ($numbs as $key)
		{
		if ( $key == "(" ) { $key = "bl"; }
		if ( $key == ")" ) { $key = "br"; }
		if ( $key == " " ) { $key = "space"; }
		if ( $key == "." ) { $key = "dot"; }
		if ( $key == "," ) { $key = "comma"; }
		echo "<img src=\"images/$key.gif\">";
		}
	exit();
?>
The output is graphical representations of (combinations of) 0-9, left bracket, right bracket, space, period and comma.

Thanks for any help. In case it's relevant, this is running on W7 (x64), IIS7.5, Intel Core2 Quad 2.83GHz, 8Gb RAM, 16Tb HDD.

Oh, BTW, if there is a glaringly obvious error, please don't just point it out, tell me how to fix it too!
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: NOOB in trouble!!!

Post by Neilos »

You could turn the counter into a function;

Code: Select all

function counter($id) {
$string = "Hello " . $id;
return $string;
}
And then call them in the table like;

Code: Select all

<table width="90%" border="0" cellspacing="0" cellpadding="5" align="center">
<tr>
<td width="50%"> Link 1: <?php $string1 = counter(1); echo $string1; ?></td>
<td width="50%"> Link 2: <?php $string2 = counter(2); echo $string2; ?></td>
</tr>
<tr>
<td width="50%"> Link 3: <?php $string3 = counter(3); echo $string3; ?></td>
<td width="50%"> Link 4: <?php $string4 = counter(4); echo $string4;; ?></td>
</tr>
</table>
If you place the function counter in a file called functions.php then at the top of the webpage (change it to a php file not html) you can have

Code: Select all

<?php
include ("functions.php");
>
That should work, and it'd be the way I would do it.
User avatar
TrojanMouse
Forum Newbie
Posts: 5
Joined: Fri Nov 19, 2010 7:01 pm
Location: Aberdeen

Re: NOOB in trouble!!!

Post by TrojanMouse »

Hi Neilos,

Thanks for the info, I did manage to get it working using your suggestion.

Can you tell me though what the implications of renaming the .HTML files to .PHP are (if any) as the site has numerous .HTML files, some of which will also probably need to use php. I wouldn't want to rename them all until I know the effects it might have.

Thanks.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: NOOB in trouble!!!

Post by Neilos »

HI,

I don't think it matters too much, but it does make your code much easier to read.

Any existing redirects would no longer work!!! But you could keep the .html pages and use a redirect to .php.

You don't have to do it so perhaps I shouldn't have suggested it but if it were me I would do it. I don't know if there are any particular reasons why you would want to but changing to .php is easy as all html code stays as it is, if not in a <?php ?> tag it will just remain as is.

If you have loads of pages already up and running I would leave it as it is. But if you are in early-mid development I think that it wouldn't be a bad idea. I suppose it is preference. I suggest researching if there are any important specific differences.

I'm glad you managed to get it to work. How did you like the code? I didn't read your counter php code so I don't know how easily you managed to change it.
User avatar
TrojanMouse
Forum Newbie
Posts: 5
Joined: Fri Nov 19, 2010 7:01 pm
Location: Aberdeen

Re: NOOB in trouble!!!

Post by TrojanMouse »

Hi,

I didn't have to change the php code except adding the 'return $value' at the end. The only other changes were the 'include' at the start and changing the syntax of the calls to the php function.

I did notice now though that the popup tooltip I'm using on that page now goes slightly over the right-hand side of the page (causing the h-scrollbar to appear) whereas when the page had an .HTML extension it clipped properly just short of the edge.

Thanks for the help. I really must get hold of a php book!
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: NOOB in trouble!!!

Post by Neilos »

I didn't test any of the code so if it was me then sorry lol :wink:

I don't have a php book either, too expensive for students :lol: I use the w3schools.com and php.net websites like I'm their biggest fan!

Glad to be of service.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: NOOB in trouble!!!

Post by phphelpme »

Yeah, books can be limiting because you only have one persons ideas and skills where as the internet gives you plenty of resources to choose from. Plus sites like this can tell you if someone else is having a problem that you might be facing and you then can see how they fixed it. Well done on your first code by the way.

We are all noobs really cos we all are still learning new things and language itself is constantly changing to adapt to new technology.
User avatar
TrojanMouse
Forum Newbie
Posts: 5
Joined: Fri Nov 19, 2010 7:01 pm
Location: Aberdeen

Re: NOOB in trouble!!!

Post by TrojanMouse »

It's certainly very handy to be able to see other people's code and have access to people who have more experience than you do (and are willing to share it!).
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: NOOB in trouble!!!

Post by phphelpme »

I agree pal. I am new to this forum but I find google is my best friend really. But this forum seems to bring most og google together somehow.. One very large resource that is very valuable. :)
Post Reply