Jump to content

Recommended Posts

My MySQL server site is down so I couldn't make a highscores database in MySQL, which would be a lot easier. I am not that good at PHP since I just started learning the day before yesterday so just please help me.

 

I've been trying to create a PHP highscores database w/o the use of MySQL. It currently has 3 scripts to it, a create.php with parameters data and pass, a add.php with parameters data, pass, name, and score, and a display.php with parameter data. I have some trouble displaying the scores in a list from .dat files you create. It used to work. I also have some trouble adding new names tagged with their scores. Please help!

 

 

Below are the 3 scripts:

 

display.php
<html>
<body>

<?php
if (!isset($_REQUEST["data"]))
exit("Invalid highscores data name!");
if (file_exists($_REQUEST["data"].".dat"))
$file = fopen($_REQUEST["data"].".dat","r") or exit("Unable to load highscores!");
else
exit("Highscores data doesn't exist!");

if (file_exists($_REQUEST["data"].".dat2"))
$file2 = fopen($_REQUEST["data"].".dat2","r") or exit("Unable to load highscores!");
else
exit("Highscores data doesn't exist!");

$place=0;
$pss=fgets($file);
echo $pss;

while((!feof($file)) && (!feof($file2)))
  {
  $place++;
  $name=fgets($file);
  echo $name;
  $score=fgets($file2);
  echo $score;
  if(($score!="") and ($name!=""))
echo $place."-".$name." ".$score."<br>";
  }
  
fclose($file);
fclose($file2);
?>

</body>
</html>

 

create.php (This works fine)
<html>
<body>

<?php
if (!isset($_REQUEST["data"]))
exit("Invalid highscores data name!");
if (!isset($_REQUEST["pass"]))
exit("Invalid highscores data password!");

if (!file_exists($_REQUEST["data"].".dat"))
$file=fopen($_REQUEST["data"].".dat","x") or exit("Unable to create highscores data!");
else
exit("Highscores data already exists!");

if (!file_exists($_REQUEST["data"].".dat2"))
$file2=fopen($_REQUEST["data"].".dat2","x") or exit("Unable to create highscores data!");
else
exit("Highscores data already exists!");

fwrite($file,$_REQUEST["pass"]);

fclose($file);
fclose($file2);

echo "Highscores data with name, ".$_REQUEST["data"].", and password, ".$_REQUEST["pass"].", created successfully!"
?>

</body>
</html>

 

add.php
<html>
<body>

<?php
if (!isset($_REQUEST["data"]))
exit("Invalid highscores data name!");
if (!isset($_REQUEST["pass"]))
exit("Invalid highscores data password!");
if (!isset($_REQUEST["name"]))
exit("Invalid entry name!");
if (!isset($_REQUEST["score"]))
exit("Invalid entry score!");

if (file_exists($_REQUEST["data"].".dat"))
$file = fopen($_REQUEST["data"].".dat","r") or exit("Unable to load highscores!");
else
exit("Highscores data doesn't exist!");

if (file_exists($_REQUEST["data"].".dat2"))
$file2 = fopen($_REQUEST["data"].".dat2","r") or exit("Unable to load highscores!");
else
exit("Highscores data doesn't exist!");

if($_REQUEST["pass"]!=fgets($file))
{
fclose($file);
fclose($file2);
exit("Incorrect password for highscores data, ".$_REQUEST["data"]."!");
}

$place=0;

while((!feof($file)) && (!feof($file2)))
  {
  $place++;
  $scr=fgets($file2);
  if($scr<$_REQUEST["score"])
break;
  }
  
fclose($file);
fclose($file2);

$file = fopen($_REQUEST["data"].".dat","r+");
$file2 = fopen($_REQUEST["data"].".dat2","r+");

$read = file($_REQUEST["data"].".dat");
$read["Set"]=$_REQUEST["name"];
fwrite($file,$read[0]."
");
for($s=1;$s<place;$s++)
{
fwrite($file,$read[$s+1]);
}
fwrite($file,$read["Set"]."
");
for($s=place+1;$s<=count($read)-1;$s++)
{
fwrite($file,$read[$s+1]);
}
fclose($file);

$read = file($_REQUEST["data"].".dat2");
$read["Set"]=$_REQUEST["score"];
for($s=0;$s<place-1;$s++)
{
fwrite($file2,$read[$s]);
}
fwrite($file2,$read["Set"]."
");
for($s=place;$s<=count($read)-2;$s++)
{
fwrite($file2,$read[$s]);
}
fclose($file2);

include("display.php")
?>

</body>
</html>

 

If you have any better way to make this, PLEASE tell me, thank you!

Link to comment
https://forums.phpfreaks.com/topic/134448-php-highscorescan-you-solve-it/
Share on other sites

your sql server is down so you opt to make a flatfile solution instead of fixing the server?

 

Yea, but I can't fix the server since I don't have access to it. It is down for maintenance. So, uhh, can you solve the bug? I've been trying to fix it for hours already...

So...you just started learning php 2 days ago, and yet managed to master it enough to come up with all that?  And what about that "It used to work" statement?  Since you just started php 2 days ago, did it used to work, what, yesterday?

 

 

So...you just started learning php 2 days ago, and yet managed to master it enough to come up with all that?  And what about that "It used to work" statement?  Since you just started php 2 days ago, did it used to work, what, yesterday?

Like a couple hours ago, somehow, it was working, but that was b4 I created the other 2 scripts, then I edited it a little, and it went kaploosh... Plus, PHP isn't that hard, and I didn't master it, my creations don't work...

 

EDIT: @Maq

The bug is in the scripts, I've been using another server for executing my php files, so please, just help me fix the scripts.

Ok, I have another code that seems to not work, and somehow I can't find the edit topic post button, so ill post here:

if (!file_exists($REQUEST["data"]))
  exit("Highscores table, ".$REQUEST["data"]." doesn't exist!")

 

Please help me fix this, it says some error in the if part.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.