· 8 years ago · Jun 28, 2018, 09:50 AM
1<?php
2
3 $uTable = "users"; //user table
4 $pTable = "pokemon2"; //pokemon table
5 $trTable = "trades_created"; //new trade table
6 $trTable2 = "trade_pokemon"; //pokemon trade table
7
8function checkUser($pid,$user){
9 $s = mysql_query("SELECT * FROM `{$pTable}` WHERE `pokemon_id` = '{$pid}'");
10 if(mysql_num_rows($s) == 1){
11 $r = mysql_fetch_array($s);
12 $x = ($r["user_id"] == $user) ? true:false;
13 return $x;
14 } else {
15 return false;
16 }
17 }
18
19 if(isset($_POST["user"]) && $_POST["user"] == "Submit"){ //first form processing
20 if(!empty($_POST["uid"])){ //checks to see if they actually inputted something
21 if(is_numeric($_POST["uid"])){ //checks to see if input is a number (numeric)
22 $s = mysql_query("SELECT * FROM `{$uTable}` WHERE `id` = '{$_POST["uid"]}'");
23 if(mysql_num_rows($s) == 1){ //checks to see if user exists
24 $s2 = mysql_query("SELECT * FROM `{$pTable}` WHERE `user_id` = '{$_SESSION["user_id"]}' AND `roster` = '0'");
25 $s3 = mysql_query("SELECT * FROM `{$pTable}` WHERE `user_id` = '{$_POST["uid"]}' AND `roster` = '0'");
26 $mypokemon = "";
27 $theirpokemon = "";
28 if(mysql_num_rows($s2) != 0){
29 while($row = mysql_fetch_array($s2)){
30 $mypokemon .= "<option value='{$row["pokemon_id"]}'>{$row["type"]}{$row["pokemon"]}</option>";
31 }
32 } else {
33 $mypokemon = "<option>No Pokemon in Box</option>";
34 }
35 if(mysql_num_rows($s3) != 0){
36 while($row = mysql_fetch_array($s3)){
37 $theirpokemon .= "<option value='{$row["pokemon_id"]}'>{$row["type"]}{$row["pokemon"]}</option>";
38 }
39 } else {
40 $theirpokemon = "<option>No Pokemon in Box</option>";
41 }
42 echo "<form action='' method='POST'><input type='hidden' name='theirid' value='{$_POST["uid"]}' />";
43 echo "<table><tr><td width='50%'>My Roster: <select name='my[]' multiple='multiple'><option value='none'>None</option>{$mypokemon}</select></td><td width='50%'>Their Roster: <select name='their[]' multiple='multiple'><option value='none'>None</option>{$theirpokemon}</select></td></tr></table>";
44 echo "<input type='submit' name='create' value='Create Trade' />";
45 echo "</form>";
46 } else {
47 echo "This user doesn't exist.";
48 }
49 } else {
50 echo "Your inputted user id was not a number.";
51 }
52 } else {
53 echo "You left the user id field blank!";
54 }
55 } elseif(isset($_POST["create"]) && $_POST["create"] == "Create Trade"){
56 if(!empty($_POST["theirid"]) && is_numeric($_POST["theirid"])){
57 if(!empty($_POST["my"]) && !empty($_POST["their"])){
58 if((count($_POST["my"]) < 7 && count($_POST["my"]) > 0) && (count($_POST["their"]) < 7 && count($_POST["their"]) > 0)){
59 $ok = true;
60 $lineup = array("my"=>array(),"their"=>array());
61 foreach($_POST["my"] as $val){
62 if($val == "none"){
63 $lineup["my"][] = 0;
64 } else {
65 if(is_numeric($val)){
66 if(checkUser($val,$_SESSION["user_id"])){
67 $lineup["my"][] = $val;
68 } else {
69 break;
70 $ok = false;
71 echo "A pokemon chosen was not yours to be trading.";
72 }
73 }
74 }
75 }
76 foreach($_POST["their"] as $val){
77 if($val == "none"){
78 $lineup["their"][] = 0;
79 } else {
80 if(is_numeric($val)){
81 if(checkUser($val,$_POST["theirid"])){
82 $lineup["their"][] = $val;
83 } else {
84 break;
85 $ok = false;
86 echo "A pokemon chosen was not theirs to be trading.";
87 }
88 }
89 }
90 }
91 if($ok){
92 if(count($lineup["my"]) > count($lineup["their"])){
93 $z = $lineup["my"];
94 } else {
95 $z = $lineup["their"];
96 }
97 mysql_query("INSERT INTO `{$trTable}` (`user_to`, `user_from`, `trade_id`) VALUES ('{$_POST["theirid"]}', '{$_SESSION["user_id"]}', '')");
98 $tid = mysql_insert_id();
99 for($x=0;$x<count($z);$x++){
100 mysql_query("INSERT INTO `{$trTable2}` (`trade_id`, `pokemon_mine`, `pokemon_theirs`) VALUES ('{$tid}', '{$lineup["my"][$x]}', '{$lineup["their"][$x]}')");
101 }
102 echo "Successfully created trade.";
103 }
104 } else {
105 echo "You can only choose one pokemon to trade!";
106 }
107 } else {
108 echo "You didn't choose anything in the trade!";
109 }
110 } else {
111 echo "You encountered this form incorrectly!";
112 }
113 } else {
114 echo "<form action='' method='POST'>";
115 echo "Enter User ID: <input type='text' name='uid' />";
116 echo " <input type='submit' name='user' value='Submit' />";
117 echo "</form>";
118 }
119?>