Newbiephper Posted July 12, 2006 Share Posted July 12, 2006 hello again i was trying to write my first code with no aids and hit a dead end.the code basically consists of a display feature to display values (total land, free land and the 3 building types); then the form action(build new buildings), then when build submit it should send data to server for storage and also update page.my code at the moment looks like:[code]<?php//buildings.php//open database connectionsinclude 'db.php'//define planet sizedefine("TLAND, 200")//get current number of buildings$nfarms="select nfarms from buildings";$nhomes="select nhomes from buildings";$nmines="select nmines from buildings";$nf = mysql_query($nfarms)$nh = mysql_query($nhomes)$nm = mysql_query($nmines)//check for free land sapce$fland = ((TLAND) - ($nfarms+$nhomes+$nmines))//output current dataecho "Total land: " . TLAND;echo "Free land: " . $fland;echo "Number of Farms: " . $nf;echo "Number of Homes: " . $nh;echo "Number of Mines: " . $nm;//allow building formif ($fland>0){<form action="<?=$_SERVER['PHP_SELF']?>" method="post">echo "<input size=4 type=text name='farm'>"echo "<input size=4 type=text name='house'>"echo "<input size=4 type=text name='mine'>"<input type="submit" name="submit" value="Build">}else{"echo "No free land to build upon";}if ($_POST['farm'] || $_POST['house'] || $_POST['mine']){ if ($_POST['farm']) { $update1 = "update buildings set nfarms = nfarms + 1 ";[/code]my problem is how to proceed because i dont wanna just add a fixed value [quote]$update1 = "update buildings set nfarms = nfarms + 1 ";[/quote]where that 1 is i need the value that was submitted by the user. this is why im stuck at moment. any help greatly appreciated. also if ur good u might just wanna check rest of code since its first try and probably has a zillion errors.thx a lot Quote Link to comment https://forums.phpfreaks.com/topic/14368-lil-stuck-with-my-code/ Share on other sites More sharing options...
ToonMariner Posted July 12, 2006 Share Posted July 12, 2006 OK couple of fixes first....<?php//buildings.php//open database connectionsrequire_once('db.php');if ($_POST['farm'] || $_POST['house'] || $_POST['mine']){ $farm = $_POST['farm'] > 0 ? $_POST['farm'] : 0; $house = $_POST['house'] > 0 ? $_POST['house'] : 0; $mine = $_POST['mine'] > 0 ? $_POST['mine'] : 0; $qry = "update buildings set nfarms = nfarms + " . $farm . ", nhouse = nhouse + " . $house . ", nfarm = nfarm + " . $farm . ""; $qry = mysql_query($qry);}//define planet sizedefine("TLAND", 200);//get current number of buildings$qry="select nfarms,nhomes,nmines from buildings";$qry = mysql_query($qry);$row = mysql_fetch_row($nf);$nf = $row[0];$nh = $row[1];$nm = $[2];//check for free land sapce$fland = ((TLAND) - ($nf+$nh+$nm));//output current dataecho "Total land: " . TLAND;echo "Free land: " . $fland;echo "Number of Farms: " . $nf;echo "Number of Homes: " . $nh;echo "Number of Mines: " . $nm;//allow building formif ($fland>0){?><form action="<?=$_SERVER['PHP_SELF']?>" method="post"><input size="4" type="text" name="farm"><input size="4" type="text" name="house"><input size="4" type="text" name='mine'><input type="submit" name="submit" value="Build"><?php}else{echo "No free land to build upon";}?>try that... Quote Link to comment https://forums.phpfreaks.com/topic/14368-lil-stuck-with-my-code/#findComment-56679 Share on other sites More sharing options...
GingerRobot Posted July 12, 2006 Share Posted July 12, 2006 Toonmariner, shouldn't this:$qry = mysql_query($qry);$row = mysql_fetch_row($nf);$nf = $row[0];$nh = $row[1];$nm = $[2];be this:$qry = mysql_query($qry);$row = mysql_fetch_assoc($qry); //you need to use the variable that did the query$nf = $row[nfarms];$nh = $row[nhomes];$nm = $row[nmines];Because we dont know if the fields are in that order, it would be better to use an associative array. Quote Link to comment https://forums.phpfreaks.com/topic/14368-lil-stuck-with-my-code/#findComment-56681 Share on other sites More sharing options...
Newbiephper Posted July 12, 2006 Author Share Posted July 12, 2006 reply thx ill try that with the little fix that GR made too. just a few things:why is that if statement relating to the form at the top?the '?' is shorthand for an if statement? what does teh colon mean (:)?[quote]$farm = $_POST['farm'] > 0 ? $_POST['farm'] : 0; $qry = "update buildings set nfarms = nfarms + " . $farm . "[/quote]so in english what happens here is that if user submits a value posted to variable $farm then $qry transfers this value to databse. im guessing " . $variable . " is standard convention for posted variables.thx for help, and it would be grate if you could answer my questions as whilst the script is great and im sure works, im trying to learn as i make so need to try and understand stuff as well. everything else i understand, a little rusty on arrays but understand basic principle. thx a lot again. Quote Link to comment https://forums.phpfreaks.com/topic/14368-lil-stuck-with-my-code/#findComment-56689 Share on other sites More sharing options...
ToonMariner Posted July 12, 2006 Share Posted July 12, 2006 Stop highlighting my inadequacies!!!!!!and yes i normally use fetch_assoc but could be arsed guessing if the field names were correct or not so just went on the old numeric index. Quote Link to comment https://forums.phpfreaks.com/topic/14368-lil-stuck-with-my-code/#findComment-56690 Share on other sites More sharing options...
ToonMariner Posted July 12, 2006 Share Posted July 12, 2006 the if statement is at the top so that if someone has put some data in, it is updated in the database BEFORE you go and check if there is any free space. If they filled it up and you did the update AFTER the query to check if there is any space then they would still be given the form to buld more stuff..Correct ? : is shorthand for if else statement so that line saysif $_POST['farm'] is greater than 0 $farm = $_POST['farm'] else $farm = 0and your last one is correct - you simply insert variables into your string to update the database. Quote Link to comment https://forums.phpfreaks.com/topic/14368-lil-stuck-with-my-code/#findComment-56691 Share on other sites More sharing options...
GingerRobot Posted July 12, 2006 Share Posted July 12, 2006 Umm, i was only trying to make the script work! Quote Link to comment https://forums.phpfreaks.com/topic/14368-lil-stuck-with-my-code/#findComment-56692 Share on other sites More sharing options...
Newbiephper Posted July 12, 2006 Author Share Posted July 12, 2006 thx toonmariner for the script changes and explanations ^^ off i go to code more now, hopefully ill manage a little longer before getting stuck :) Quote Link to comment https://forums.phpfreaks.com/topic/14368-lil-stuck-with-my-code/#findComment-56697 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.