-1

I want a simple non-MySQL PHP script that allows viewers to vote up or vote down a page. If it has to be MySQL database driven, then that's just how it has to be. Ideally, not though.

So basically if you were to view source of this page in a web browser it would look something like this:

<h2>Stack Overflow Is Cool</h2>
<span id="author-info-etc">Written by Ben Dover on 01-01-2012 (+12) | (-3) Total Rank: +9</span>
<p>Once upon a time there was this really cool website called Stack Overflow where amazing people would help answer questions asked by people who destroyed their keyboards by throwing them against their computer screen in an act of utter frustration and despair.</p>

And the source file would look like this:

<span id="author-info-etc">Written by <?php echo $authorname; ?> on <?php echo $pubDate; ?> <?php include ('pagevotescript.php'); ?></span>

Could someone help me to create this PHP script?

tadman
  • 208,517
  • 23
  • 234
  • 262
Garry
  • 251
  • 2
  • 13
  • @deifwud I don't know, I'm just a static file type of guy. I know it's lame. And what you say is obvious. I just prefer everything to by physically written to a file on the .php page rather than dynamically generated or called from a database. But I understand that in this case, doing so might not be possible. – Garry Oct 04 '12 at 16:39
  • @tadman I added the mysql tag because I am not opposed to the idea of using mysql. It may appear that way because my original question was edited already by someone else making it now appear that way. – Garry Oct 04 '12 at 17:56

5 Answers5

2

If you're wanting to have a reliable voting system, you'll need to use a database such as MySQL to store votes in. Otherwise, how will your web app know how many votes a page has received?

Your database structure could look something like this:

Pages

page_id | page_name
-------------------
1       | My Page
2       | Test

Users

user_id | user_name
-------------------
1       | John     
2       | Sara
3       | Tom

Votes

vote_id | user_id | page_id | vote_weight
-----------------------------------------
1       | 1       | 1       |  1
2       | 1       | 2       | -1
3       | 2       | 2       |  1
4       | 3       | 1       |  1

When a user votes:

  1. Check to see if they've already voted on the page.
  2. If so, either deny them another vote or update their existing vote.
  3. If they haven't voted on the page in question, insert a new vote.
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
  • I like that idea Wayne. How could I tie all that in together in the php file, also rather than having usernames, could the voting be anonymous? Perhaps logging the IP address would be a way to keep people from double voting instead? – Garry Oct 04 '12 at 17:16
  • IP address and Sessions would be another alternative. However, it'll never ensure that the same person hasn't voted twice. – Wayne Whitty Oct 05 '12 at 08:08
1

It would be easiest to store page data in MySQL and just have a column called votes. This way you would display the page and pull in the votes at the same time, and could easily up/down the votes when the user interacts.

Without MySQL, you would need to store the value in a file somewhere. You could have about-us.php and then about-us.php.txt where the txt file would be a simple # that could be retrieved and then changed and reset. Something like this:

// Open file for reading and writing
$fp = fopen('about-us.php.txt', 'w+');

//Retrieve contents of file as integar
$count = (int) fread($fp, filesize('about-us.php.txt'));

// Take the count and up it by 1
$count++;

// Write the changes
fwrite($fp, $count);

// Close the file
fclose($fp);
Jason
  • 1,192
  • 7
  • 8
  • I like that idea. How would I go about creating the HTML that would allow viewers to click to vote up or down? And is there a way to make the code above do the math: sum of UP votes plus DOWN votes to display current ranking? – Garry Oct 04 '12 at 17:10
  • This is a terrible idea. Please don't use the file-system as a database. This will not account for simultaneous requests that will cause the counter to slip. – tadman Oct 04 '12 at 17:46
  • @tadman I value your input. Could you please explain why it's a terrible idea to store the vote data in a .txt file? – Garry Oct 04 '12 at 17:52
  • For one, it can't handle URLs with slashes in them. Second, it suffers from race conditions. Third, you'll end up with an unmanageable mess of files all too quickly. This is how web counters were done in the early 1990 and everyone soon realized that it was a bad idea. – tadman Oct 04 '12 at 22:54
0

You need a way to persist your data between sessions, so you need a database, if you don't want to install mysql, I think last versions of PHP come with sqlite built-in support.

Nelson
  • 49,283
  • 8
  • 68
  • 81
0

You want to have a database with users and posts. Users should be able to vote up posts, increasing or decreasing the 'reputation' count of a user. To store all this information, you are already going to need a database.

If you still want to store the 'vote' information in something else then a mysql database, there are plenty of options. text file, NoSQL, Couchdb and many other options.

It is going to be much easier for you to learn how to communicate with a database then doing something like storing the info in a file. And since mysql is the most common used database, it would be a rather intelligent choice.

PS: If you are starting to learn mysql, you should definately not start using the PHP mysql_ functions, but rather something like PDO, to circumenvent SQL-Injections.

Zulakis
  • 7,859
  • 10
  • 42
  • 67
0

The low-tech approach is to use a key-value store with PHP to store your values. An example is the dba_* functions within PHP like dba_open which creates a single-file database that behaves like any regular file, no server required.

Example:

$db = dba_open("stats", "r", "gdbm");
$count = dba_fetch("/page/path", $db);
dba_insert("/page/path", $count + 1, $db);

As a note this would be subject to subtle race conditions where two or more simultaneous requests would fetch and save the same incremented value. Using a proper database, even the embedded SQLite, would be a better idea.

tadman
  • 208,517
  • 23
  • 234
  • 262