Jump to content


Photo

Running code after a link within the page has been clicked


  • Please log in to reply
2 replies to this topic

#1 Epd

Epd
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 08 July 2006 - 11:31 AM

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.

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!";
}

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

Thanks!
EPD

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 08 July 2006 - 11:49 AM

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.
Life's too short for arguing.

#3 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 08 July 2006 - 02:54 PM

Instead of using all those if's and else if's I would use a switch:
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;
}

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users