Epd Posted July 8, 2006 Share Posted July 8, 2006 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!";} elseecho "You haven't got enough money!";}[/code]Also if anything looks a bit wrong to anyone please let me know!Thanks!EPD Quote Link to comment https://forums.phpfreaks.com/topic/14018-running-code-after-a-link-within-the-page-has-been-clicked/ Share on other sites More sharing options...
hvle Posted July 8, 2006 Share Posted July 8, 2006 save your code to a file, say myfile.phpdon'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. Quote Link to comment https://forums.phpfreaks.com/topic/14018-running-code-after-a-link-within-the-page-has-been-clicked/#findComment-54738 Share on other sites More sharing options...
Daniel0 Posted July 8, 2006 Share Posted July 8, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/14018-running-code-after-a-link-within-the-page-has-been-clicked/#findComment-54795 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.