widget Posted August 10, 2008 Share Posted August 10, 2008 Hi, I am trying to setup a random event type situation where an image appears and if the user clicks on the image it is inserted into their inventory. I have the part where it randomly calls the image working fine but not sure how to do the image clicking bit. $randplay = rand(1,50); if ($randplay == 27) { $randring = rand(1,5); if($randring == 1) { $ring = 33331; } if($randring == 2) { $ring = 33332; } if($randring == 3) { $ring = 33333; } if($randring == 4) { $ring = 33334; } if($randring == 5) { $ring = 33335; } $code = mysql_query("INSERT INTO usersitems2 (owner,item_id,parts_left,game) VALUES ('$userid','$ring','1','$game')"); Any help is much appreciated. After clicking, the user can either stay on the page they were on or be directed to another URL - either way would work fine. Also this piece of code will be added using a include function. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 10, 2008 Share Posted August 10, 2008 Well it rather depends on how you want it to work. If you want it all to be done without a page reload, you'll ned some AJAX. If you're happy to have the page reloaded, then you'll need to make the image a link. Quote Link to comment Share on other sites More sharing options...
widget Posted August 10, 2008 Author Share Posted August 10, 2008 I dont mind the page reloading. i know I can make the image a link that refers to another page with the insert the item into the inventory code but I was hoping to do it all on the one page. I know theres a way to do it as a form and add... if ($act=click) { perform the code here } but I dont know how to do it exactly, just know that its possible Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 10, 2008 Share Posted August 10, 2008 Indeed. Make the link refer to the current page. You'll also want to allow for anything that might already be in the URL. Something like this: <?php $page = $_SERVER['PHP_SELF']; $act = "act=click&imageid=1"; if(empty($_SERVER['QUERY_STRING'])){ $page = $page.'?'.$act; }else{ $page = $page.'?'.$_SERVER['QUERY_STRING'].'&'.$act; } echo '<a href="'.$page.'">Clicky Clicky</a>"; ?> You'd then use something like this to check to see if the action is click: if(isset($_GET['act']) && $_GET['act']=='click'){ //add to inventory } Quote Link to comment Share on other sites More sharing options...
widget Posted August 10, 2008 Author Share Posted August 10, 2008 ok this is what i have and here is the error Parse error: syntax error, unexpected $end in /home/chicka/public_html/ring_event.php on line 51 <?php $page = $_SERVER['PHP_SELF']; $act = "act=click"; $randplay = rand(1,2); if ($randplay == 2) { $randring = rand(1,5); if($randring == 1) { $ring = 33331; } if($randring == 2) { $ring = 33332; } if($randring == 3) { $ring = 33333; } if($randring == 4) { $ring = 33334; } if($randring == 5) { $ring = 33335; } if(empty($_SERVER['QUERY_STRING'])) { $page = $page.'?'.$act; } else{ $page = $page.'?'.$_SERVER['QUERY_STRING'].'&'.$act; } echo '<a href="'.$page.">Clicky Clicky</a>"; if(isset($_GET['act']) && $_GET['act']=='click') { mysql_query("INSERT INTO usersitems2 (owner,item_id,parts_left,game) VALUES ('$userid','$ring','1','$game')"); } ?> Quote Link to comment Share on other sites More sharing options...
widget Posted August 10, 2008 Author Share Posted August 10, 2008 NVM found the error Thank you for your help!! Quote Link to comment 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.