Jump to content

Running code after a link within the page has been clicked


Epd

Recommended Posts

Hi, I am starting to learn php my helping a friend of mine develop a game (Yeah a bit high up for a starting point but I learn fast). I wrote the following code and was wondering how I would make it execute only after a link on the page has been clicked.

[code]if(isset($_GET['repair'])) {
$dbres = mysql_query("SELECT * FROM `[garage]` WHERE `id`='{$_GET['repair']}'");
$car = mysql_fetch_object($dbres);
if($car->car == "Renault Clio Sport") { $waard = "2400"; }
elseif($car->car == "Audi A3") { $waard = "4250"; }
elseif($car->car == "Bmw m3") { $waard = "6800"; }
elseif($car->car == "Cadilac Escelade") { $waard = "8250"; }
elseif($car->car == "Nissan Skyline") { $waard = "10000"; }
elseif($car->car == "Porsche 911") { $waard = "12500"; }
elseif($car->car == "GT 40") { $waard = "15000"; }
elseif($car->car == "Lamborghini Murcielago") { $waard = "25000"; }
elseif($car->car == "Ferrari Enzo") {$waard = "26000"; }
elseif($car->car == "TVR Speed 12") {$waard = "27000"; }
elseif($car->car == "Mclaren f1") {$waard = "29000"; }
elseif($car->car == "Bugatti Veyron") {$waard = "30000"; }
$schadelost = round($waard-($waard-((($car->damage/100)*2)*$waard)));
if ($data->cash >= $schadelost) {
        mysql_query("UPDATE `[garage]` SET `damage`='0' WHERE `id`='{$_GET['repair']}' AND `login`='{$data->login}' AND `mission`='0'");
mysql_query("UPDATE `[users]` SET `cash`=`cash`-$schadelost WHERE `login`='{$data->login}'");
$data->cash -= $schadelost;
echo "You repaired the car!";
} else
echo "You haven't got enough money!";
}[/code]

Also if anything looks a bit wrong to anyone please let me know!

Thanks!
EPD
Link to comment
Share on other sites

save your code to a file, say myfile.php
don't forget to wrap your code with <?php ?>

create a html page with a link like this:
<a href="myfile.php?repair=7">run my script</a>

obviously, 7 is the id.

when user click that link, your script executed.
Link to comment
Share on other sites

Instead of using all those if's and else if's I would use a switch: [code]switch($car->car)
{
case 'Renault Clio Sport':
$waard = 2400;
break;
case 'Audi A3':
$waard = 4250;
break;
case 'Bmw m3':
$waard = 6800;
break;
case 'Cadilac Escelade':
$waard = 8250;
break;
case 'Porsche 911':
$waard = 10000;
break;
case 'Porsche 911':
$waard = 12500;
break;
case 'GT 40':
$waard = 15000;
break;
case 'Lamborghini Murcielago':
$waard = 25000;
break;
case 'Ferrari Enzo':
$waard = 26000;
break;
case 'TVR Speed 12':
$waard = 27000;
break;
case 'Mclaren f1':
$waard = 29000;
break;
case 'Bugatti Veyron':
$waard = 30000;
break;
}[/code]

It takes up more space in the script, but I just think it looks nicer. The other way works perfectly too though.

You can read more about switches here: http://php.net/switch
Link to comment
Share on other sites

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.