PDA

View Full Version : Could somebody possibly help.



AdamJ
June 26th, 2008, 10:06
Hello, I am currently attempting to code a website up similar to the site www.hugthetrees.com, but without using a database.

I have been told that I can do it with fwrite and fread, but I know nothing about php, tried learning it and it just doesn't help me at all. Would somebody possibly be able to code something up for me, or at least help me code it so its unique.

The sources I've been learning from are:
http://uk3.php.net/fwrite and http://uk2.php.net/fread

Thanks,
Adz :-)

JonnyH
June 26th, 2008, 11:03
If your using PHP 5 you can use file_get_contents and file_put_contents. Much easier for a "noob" like yourself ;)
Example:

<?php
$file = "clicks.txt";
$data = file_get_contents($file);
$data++;
file_put_contents($file, $data);
?>
That'll get the file, make sure it's a number in the file, up it by one and then put it back in the file.

AdamJ
June 26th, 2008, 11:32
Thanks Jonny, +rep awarded.


A question, can that be attached to a form? So for example as you click a big "Click Me" button, the data goes through that code in say a file called "click.php" and then adds the number up. Plus, how do I show the number of clicks on the page where the button is?

JohnN
June 26th, 2008, 11:37
no really, don't do this. flat-file writing is horribly inefficient. Use databases instead.

AdamJ
June 26th, 2008, 11:41
Ok then JohnN, if you know it all :lol: how should this be done? ;)


Adam.

JonnyH
June 26th, 2008, 11:59
Thanks for the rep. Don't say I never do anything for you.



<?php
//mySQL Details
$mysql['user'] = "user"; # mySQL username
$mysql['pass'] = "pass"; # mySQL password
$mysql['host'] = "localhost"; # mySQL hostname
$mysql['db'] = "database"; # mySQL database

//Check connection
$mysql['con'] = @mysql_connect($mysql['host'], $mysql['user'], $mysql['pass']);
if(!$mysql['con']) {
echo "Your mySQL details are incorrect!";
}
else {
$mysql['sdb'] = @mysql_select_db($mysql['db'], $mysql['con']);
if(!$mysql['sdb']) {
echo "Your mySQL Database is incorrect!";
}
else {
$carryon = 1;
}
}

//If mySQL was good.
if(isset($carryon)) {
//If button is clicked
if($_POST) {
$select = mysql_query("SELECT * FROM `clicks`", $mysql['con']);
$data = mysql_fetch_array($select);
$number = $data['num'] + 1;
mysql_query("UPDATE `clicks` SET `num` = '{$number}' WHERE `num` = '{$data['num']}'", $mysql['con']);
}
}
//HTML CODE BELOW
?>
Put the action on the form as blank, leave it as "". It'll post to the same file and that php code above will be executed and will notch the number up by one.

AdamJ
June 26th, 2008, 12:32
Wow thanks Jonny! I just add that to say the index.php after the <body> tag or before the <html> tag at the start?


Also, do I just setup a blank MySQL database? Or do I need to add anything to it?


Thanks again,
Adam

krakjoe
June 26th, 2008, 12:56
no really, don't do this. flat-file writing is horribly inefficient. Use databases instead.

You know databases are made up of flat files as well, what else would they be.

Invoking a process like a MySQL transaction to manipulate a lone number is a horribly inefficient way of working.

If it really is just one number you want to work with, like the SQL examples here imply then using a database is tragic ...

JohnN
June 26th, 2008, 13:44
You know databases are made up of flat files as well, what else would they be.

Invoking a process like a MySQL transaction to manipulate a lone number is a horribly inefficient way of working.

If it really is just one number you want to work with, like the SQL examples here imply then using a database is tragic ...

yes i was aware of this, however mysql is a highly optimized way compared to phps.

personally joe, i'd use memcache for this then run a cron job hourly to collect, but I think that might be a bit complex for the user here.

AdamJ
June 26th, 2008, 13:45
I've added that php code to my page. Is this correct?



<?php
//mySQL Details
$mysql['user'] = "********"; # mySQL username
$mysql['pass'] = "********"; # mySQL password
$mysql['host'] = "********"; # mySQL hostname
$mysql['db'] = "********"; # mySQL database

//Check connection
$mysql['con'] = @mysql_connect($mysql['host'], $mysql['user'], $mysql['pass']);
if(!$mysql['con']) {
echo "Your mySQL details are incorrect!";
}
else {
$mysql['sdb'] = @mysql_select_db($mysql['db'], $mysql['con']);
if(!$mysql['sdb']) {
echo "Your mySQL Database is incorrect!";
}
else {
$carryon = 1;
}
}

//If mySQL was good.
if(isset($carryon)) {
//If button is clicked
if($_POST) {
$select = mysql_query("SELECT * FROM `clicks`", $mysql['con']);
$data = mysql_fetch_array($select);
$number = $data['num'] + 1;
mysql_query("UPDATE `clicks` SET `num` = '{$number}' WHERE `num` = '{$data['num']}'", $mysql['con']);
}
}
//HTML CODE BELOW
?>
<!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=utf-8" />
<title>Cut The Carbon - Home</title>
<style type="text/css">
<!--
body,td,th {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: small;
color: #FFFFFF;
}
body {
background-color: #333333;
text-align: center;
}
a:link {
color: #CCCCCC;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #FFFFFF;
}
a:hover {
text-decoration: none;
color: #99FF00;
}
a:active {
text-decoration: none;
color: #FFFFFF;
}
h1,h2,h3,h4,h5,h6 {
font-weight: bold;
}
h2 {
font-size: large;
color: #006699;
}
h3 {
font-size: medium;
color: #0099FF;
}
h4 {
font-size: small;
color: #99FF00;
}
#container {
margin: 0 auto;
margin-top: 50px;
width: 600px;
background: #666666;
}
#header {
margin: 0 auto;
margin-top: 50px;
width: 600px;
height: 100px;
background: url(images/logo.jpg);
background-repeat: no-repeat;
}
#content {
margin-top: 10px;
margin-bottom: 10px;
}
#form {
margin-top: 10px;
margin-bottom: 10px;
}
#random {
margin-top: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<!-- Header --><div id="header"></div>
<!-- Header -->

<!-- Start Container -->
<div id="container">
<div id="content">Content</div>

<div id="form">
<form id="carbon" name="carbon" method="post" action="">
<input type="image" name="submit" src="images/button.gif" width="200" height="80">
</form>
</div>

<div id="random">Random Info</div>
</div>
<!-- End Container -->
</body>
</html>



Thanks,
Adz

JohnN
June 26th, 2008, 14:30
use this:


mysql_query("UPDATE `clicks` SET `num` = num+1", $mysql['con']);

not that:

$select = mysql_query("SELECT * FROM `clicks`", $mysql['con']);
$data = mysql_fetch_array($select);
$number = $data['num'] + 1;
mysql_query("UPDATE `clicks` SET `num` = '{$number}' WHERE `num` = '{$data['num']}'", $mysql['con']);

AdamJ
June 26th, 2008, 14:39
Thanks JohnN.

How can I get the MySQL to show the number of clicks on the page?

Dynash
June 26th, 2008, 14:40
SELECT FROM `clicks` WHERE `amount`

that's if you have the amounts of clicks stored in clicks, in a table named amount.

AdamJ
June 26th, 2008, 14:43
I think thats how it is Stephen, how do I do that in a php code?

Dynash
June 26th, 2008, 14:45
Where you have your # displayed above the button;



$get_num = mysql_query("SELECT FROM `clicks` WHERE `amount`")

<div id="numberofclicks"><?php echo $get_num; ?></div>

AdamJ
June 26th, 2008, 14:59
Ah brilliant Stephen, thanks :D

AdamJ
June 27th, 2008, 07:58
Hi. I have uploaded the script to my VPS, and don't know what to add, or how to structure the MySQL so it collects the information. Can somebody give me some tips?


Thanks,
Adz