Jump to content

if,elseif, and form


ghqwerty

Recommended Posts

ok so ive got a piece of code that im working on where the user can buy there own items

 

ive got this set up however my first 'buy' button only says 'inventory.php' at bottom of the screen when it should be 'inventory.php?buy=yes' the rest all say this

 

also whenever i click on it it takes me to my whoops page but i cant see why

 

if you see anything out of place please say :)

 

<?php  
					if($buy == "yes"){

						echo "
							<table id='buy' height='25%' width='45%' border='1'>
								<tr>
									<td colspan='3'>
										<center>
											sell
										</center>
									</td>
								</tr>
								<tr>									
									<td>
										item
									</td>
									<td>
										cost 
									</td>
									<td>
										buy
									</td>
								</tr>
								";
						$items = mysql_query("select item, moneybad from itemstats where item != 'scout tactical' and item != 'spas 12'") or die(mysql_error());								
							echo "";						
							while($itemresults = mysql_fetch_array($items)){
								$item = $itemresults['item'];
								$cost = $itemresults['moneybad'];
									echo "
										<tr>
											<td>

												".$item."
											</td>
											<td>
												".$cost."
											</td>
											<td>													
												<form action='inventory.php?buy=yes' method='post'>
												<input type='submit' name='buy' value='buy!'>
												</form>
											</td>
										</tr>";
							}
							echo "</table>";
								if(isset($_post['buy'])){						
									mysql_query("update members set money = money - '".$cost."' where id = '".$_SESSION['id']."'") or die ("item can not be bought " . mysql_error());
									$name = mysql_query("select username from members where id = '".$_SESSION['id']."'") or die(mysql_error());
										$whatname = mysql_fetch_arry($name);
											$user = $whatname['username'];
									mysql_query("insert into items(item, userid, user) values('".$item."', ".$_SESSION['id'].", '".$user."' ") or die (mysql_error());
								}					
					}elseif($view == "yes"){							
					//You need the delete above the select to show your changes instantly
						if(isset($_POST['sell'])){
							/*
							Now we change it from this:
								Array (1,2,5,8,12)
							To this:
								1,2,5,8,12
							So it becomes a string, then we can use IN
							*/
							$delete2 = $_POST['delete_check'];
							$delete = implode(",", $delete2);

							//Now run query
							$qry1 = mysql_query("DELETE FROM items WHERE itemid IN($delete)") or die(mysql_error());
							$qryq = mysql_query("select * from itemstats where item = '".$item."'") or die(mysql_error());
							while($qryqresult = mysql_fetch_array($qryq)){
								$money = $qryqresult['moneyexcellent'];
								echo $money;
							}

						}


						$max = 10; //amount of articles per page. change to what you want
							$p = $_GET['p'];
								if(empty($p))
								{
									$p = 1;
								}
						$limits = ($p - 1) * $max; 
						//view the items
						if(isset($_GET['items']) && $_GET['items'] == "view"){
							$id = $_GET['id'];
							$sql = mysql_query("SELECT * FROM items WHERE userid = ".(int)$_SESSION['id']);
								while($r = mysql_fetch_array($sql)){
									$id = $r['itemid'];
									$user = $r['user'];
									$userid = $r['userid'];
										echo "<div><p>$id</p><p>$user</p><p>$userid</p></div>";
								}	
						}else{
							//view all the items
							$sql = mysql_query("SELECT * FROM items WHERE userid = ".(int)$_SESSION['id']." LIMIT ".$limits.",$max") or die(mysql_error());
							//the total rows in the table
							$totalres = mysql_result(mysql_query("SELECT COUNT(itemid) AS tot FROM items where userid = '".$_session['id']."'"),0);	
							//the total number of pages (calculated result), math stuff...
							$totalpages = ceil($totalres / $max); 
							//the table
								echo "
								<table id='inventory' height='20%' width='35%' border='1'>
									<tr>
										<td colspan='3'>
											<center>
												Inventory
											</center>
										</td>
									</tr>
									<tr>
										<td width='50%'>
										<center>
											item
										</center>
										</td>
										<td width='50%' colspan='2'>
										<center>
											item id
										</center>
										</td>
									</tr>";
							if($totalres < 1){
								echo "<tr><td colspan=\"3\"><center>You currently have no items.<center></td></tr></table>";
							}else{
								while($r = mysql_fetch_array($sql)){
									$id = $r['itemid'];
									$item = $r['item'];										
									$user = $r['user'];
									$userid = $r['userid'];
									echo "
											<tr>
												<td>
													<center>
														".$item."
													</center>
												</td>
												<td width=\"40%\">
													<center>
														".$id."
													</center>
												</td>
												<td width=\"10$\">  
													<input type=\"checkbox\" name=\"delete_check[]\" value=".$id.">   
										        </td>
											</tr> ";
								}
								$prev_page = $p - 1;


							echo "<tr><td colspan='3'>";

									if($prev_page >= 1){
										echo "<b><<</b> <a href=inventory.php?p=$prev_page><b>Prev.</b></a>"; 
									}
								for($i = 1; $i <= $totalpages; $i++){ 
									//this is the pagination link

								echo "<a href='inventory.php?p=$i'> $i </a>| ";

								}
								$next_page = $p + 1;
									if($next_page <= $totalpages) {
									   echo"<a href=inventory.php?p=$next_page><b>Next</b></a> <b>>></b>";										   
									}
								echo "<p align='right' valign='top'><input type='submit' name='sell' id='sell' value='sell'</p></td></tr>";
							//close up the table
							echo "</tr></table>";

							}
								echo "<a href='inventory.php?buy=yes'>buy!</a>";
							}

					}else{
						echo "whoops";
					}


?>

Link to comment
https://forums.phpfreaks.com/topic/127271-ifelseif-and-form/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.