Jump to content

lil stuck with my code


Newbiephper

Recommended Posts

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 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 ";
[/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
Link to comment
Share on other sites

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





Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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