Jump to content


Photo

lil stuck with my code


  • Please log in to reply
7 replies to this topic

#1 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 12 July 2006 - 09:40 AM

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:
<?php

//buildings.php

//open database connections
include 'db.php'
//define planet size
define("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 data
echo "Total land: " . TLAND;
echo "Free land: " . $fland;
echo "Number of Farms: " . $nf;
echo "Number of Homes: " . $nh;
echo "Number of Mines: " . $nm;

//allow building form
if ($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 ";

my problem is how to proceed because i dont wanna just add a fixed value

$update1 = "update buildings set nfarms = nfarms + 1 ";


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

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 12 July 2006 - 10:00 AM

OK couple of fixes first....

<?php

//buildings.php

//open database connections
require_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 size
define("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 data
echo "Total land: " . TLAND;
echo "Free land: " . $fland;
echo "Number of Farms: " . $nf;
echo "Number of Homes: " . $nh;
echo "Number of Mines: " . $nm;

//allow building form
if ($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...






follow me on twitter @PHPsycho

#3 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 12 July 2006 - 10:03 AM

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.

#4 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 12 July 2006 - 10:18 AM

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 (:)?

$farm = $_POST['farm'] > 0 ? $_POST['farm'] : 0;
$qry = "update buildings set nfarms = nfarms + " . $farm . "


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.

#5 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 12 July 2006 - 10:19 AM

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.
follow me on twitter @PHPsycho

#6 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 12 July 2006 - 10:24 AM

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 says

if $_POST['farm'] is greater than 0 $farm = $_POST['farm'] else $farm = 0

and your last one is correct - you simply insert variables into your string to update the database.
follow me on twitter @PHPsycho

#7 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 12 July 2006 - 10:25 AM

Umm, i was only trying to make the script work!

#8 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 12 July 2006 - 10:36 AM

thx toonmariner for the script changes and explanations ^^  off i go to code more now, hopefully ill manage a little longer before getting stuck :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users