Jump to content
MakeWebGames

Have I converted this code properly?


-BRAIDZ-

Recommended Posts

<?php
include 'globals.php';
print "<h3>Car Yard</h3><hr />";
$car=abs((int) $_GET['car']);
if($car)
{
$q=$db->query("SELECT * FROM cars_types WHERE carID={$car} and carBUYABLE=1", $c);
if($db->num_rows($q) == 0)
{
die("Invalid Car");
}
$r=$db->fetch_row($q);
if($r['carCOST'] > $ir['money'])
{
die("You do not have enough money to buy this car.");
}
$db->query("UPDATE users SET money=money-{$r['carCOST']},cars_owned=cars_owned+1 where userid=$userid", $c);
$db->query("INSERT INTO cars_playercars VALUES('', $userid, $car, 1, 1, 1, 1)", $c);
print "You bought a {$r['carNAME']}!<br />
> <a href='caryard.php'>Back to Caryard</a><br />
> <a href='garage.php'>Goto Your Garage</a><br />";
}
else
{
print "<table width=100% border=6> <tr style='background:black'> <th>Car</th><th>Description</th><th>Base Acceleration</th><th>Base Handling</th><th>Base Speed</th><th>Base Shield</th><th>Price</th><th>Buy</th></tr>";
$q=$db->query("SELECT * FROM cars_types WHERE carBUYABLE=1 ORDER BY carCOST", $c);
while($r=$db->fetch_row($q))
{
$price='$'.number_format($r['carCOST']);
print "<tr><td>{$r['carNAME']}</td><td>{$r['carDESC']}</td> <td>{$r['carACC']}</td> <td>{$r['carHAN']}</td><td>{$r['carSPD']}</td> <td>{$r['carSHD']}</td> <td>$price</td> <td><a href='caryard.php?car={$r['carID']}'>Buy</a></td> </tr>";
}
print "</table>";
}
$h->endpage();
?>
Link to comment
Share on other sites

Looks right but word of advice you should always check whether an array key is set or exists. Depending on evironment setup you may see some errors for an undefined key which in your case would be 'car'. Ill go one step further for you and show you:

$car = (isset($_GET['car'])) ? abs((int)$_GET['car']) : NULL;

Which is equivalent to:

if(isset($_GET['car'])) {
   $car = abs((int)$_GET['car']);
}
else {
   $car = NULL;
}

Or you can use

array_key_exists('car',$_GET);

Instead of isset()

Link to comment
Share on other sites

Looks ok, does it work?

Yeah working perfectly fine mate.

But not sure about the other codes that go with this, they all work now that I've converted them, first "proper" conversion I've done, normally I'm lazy and only change

include 'global_func.php';

To:

include_once 'globals.php';

 

And remove the header parts and leave all the rest there

But going to start converting them properly now I know what I'm doing haha, it looks so much cleaner when you do it properly, looks like I'm going to have to edit a number of my codes to tidy then up :)

- - - Updated - - -

 

Looks right but word of advice you should always check whether an array key is set or exists. Depending on evironment setup you may see some errors for an undefined key which in your case would be 'car'. Ill go one step further for you and show you:
$car = (isset($_GET['car'])) ? abs((int)$_GET['car']) : NULL;

Which is equivalent to:

if(isset($_GET['car'])) {
   $car = abs((int)$_GET['car']);
}
else {
   $car = NULL;
}

Or you can use

array_key_exists('car',$_GET);

Instead of isset()

Thanks mate, hopefully I don't run into any problems

Link to comment
Share on other sites

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.   Paste as plain text instead

  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...