
May 11th, 2008, 05:05 PM
|
|
Registered User
|
|
Join Date: Jul 2007
Posts: 3
Time spent in forums: 1 h 24 m 44 sec
Reputation Power: 0
|
|
|
logic - Repeating problem
Hi, I am querying a database about information and the information is displaying perfectly. I'm using a while loop to hit all of the rows that this pertains to. At the end, I have a form that does certain things when pressed, such as updating the information pertaining to that particular row. But when I press the button, it does it for ALL of the rows, not just that one. Could someone look the code over and help me restructure this to make it work? Thanks.
PHP Code:
$datac = mysql_query("SELECT DISTINCT name FROM buildings WHERE name IS NOT NULL and building_id between 1 and '".$j."'") or die(mysql_error());
//Begin Table
//Opening table. The while loops the data. the $i junk is formatting for the table. Skip until you see THIS IS THE SPOT!!, because these are just repetitions.
$i = 0;
$open = false;
while ($infoc = mysql_fetch_array( $datac ))
{
if ( $i % 2 == 0 )
{
// Left column
echo "<tr><td>";
echo "<b>Name:</b> ".$infoc['name']."<br>";
echo "<b>Cost:</b> ";
//COSTS
$query = mysql_query("select * from buildings where name='" . $infoc['name'] . "'") or die(mysql_query());
$fetch = mysql_fetch_array($query);
$query = mysql_query("select * from buildings where building_id='".$fetch['building_id']."' and cost_type=0");
while ($fetch = mysql_fetch_array($query)) {
// Get the resource and the cost for this id.
$rquery = mysql_query("select name from resources where id='" . $fetch['cost_restype'] . "'");
$rfetch = mysql_fetch_array($rquery) or die(mysql_error());
echo $fetch['cost_amount']." ".$rfetch['name']."; ";}
echo "<br>";
echo "<b>Input: </b>";
//INPUTS
$query = mysql_query("select building_id from buildings where name='" . $infoc['name'] . "'") or die(mysql_query());
while ($fetch = mysql_fetch_array($query)) {
$query2 = mysql_query("select cost_amount, cost_restype from buildings where building_id='" . $fetch['building_id'] . "' and cost_type='1'");
$fetch2 = mysql_fetch_array($query2);
if($fetch2['cost_amount'] == NULL){
echo "No input per hour.";
}
else{
$query2 = mysql_query("select cost_amount, cost_restype from buildings where building_id='" . $fetch['building_id'] . "' and cost_type='1'");
while($fetch2 = mysql_fetch_array($query2)){
// Get the resource and the cost for this id.
$rquery = mysql_query("select name from resources where id='".$fetch2['cost_restype']."'");
$rfetch = mysql_fetch_array($rquery) or die(mysql_error());
echo $fetch2['cost_amount']." ".$rfetch['name']." per hour; ";}}}
echo "<br>";
echo "<b>Output: </b>";
//outputs
$query = mysql_query("select building_id from buildings where name='" . $infoc['name'] . "'") or die(mysql_query());
while ($fetch = mysql_fetch_array($query)) {
$query2 = mysql_query("select cost_amount, cost_restype from buildings where building_id='" . $fetch['building_id'] . "' and cost_type='2'");
$fetch2 = mysql_fetch_array($query2);
if($fetch2['cost_amount'] == NULL){
echo "No output per hour.";
}
else{
$query2 = mysql_query("select cost_amount, cost_restype from buildings where building_id='" . $fetch['building_id'] . "' and cost_type='2'");
while($fetch2 = mysql_fetch_array($query2)){
// Get the resource and the cost for this id.
$rquery = mysql_query("select name from resources where id='".$fetch2['cost_restype']."'");
$rfetch = mysql_fetch_array($rquery) or die(mysql_error());
echo $fetch2['cost_amount']." ".$rfetch['name']." per hour; ";}}}
//PURCHASE
echo "<form method='post' action=''>Amount: <input type='text' name='amount' size='3'><input type='submit' name='purchase' value='Purchase'></form>";
if($_POST['purchase'] == 'Purchase'){
//Check user buildings
$query = mysql_query("select building_id from buildings where name='" . $infoc['name'] . "'");
$fetch = mysql_fetch_array($query);
$buildings = mysql_query("select amount from user_buildings where user_id='".$userid."' and building_id='".$fetch['building_id']."'");
$buildingsfetch = mysql_fetch_array($buildings);
if($buildingsfetch['amount'] == NULL){
$buildings = 0;
}
else{
$buildings = $buildingsfetch['amount'];
}
//Establishes amount
$amount = $_POST['amount'];
$insert = $amount+$buildings;
// Get the resource and the cost for this id.
$query2 = mysql_query("select cost_amount, cost_restype from buildings where building_id='" . $fetch['building_id'] . "' and cost_type='0'");
while($fetch2 = mysql_fetch_array($query2)){
//Check if has enough resources
$uquery = mysql_query("select amount from user_resources where user_id='".$userid."' and resource_id='" . $fetch2['cost_restype'] . "'");
$ufetch = mysql_fetch_array($uquery);
if($ufetch['amount'] == NULL){
$resourceamount = 0;
}
else{
$resourceamount = $ufetch['amount'];}
$netamount = $amount*$fetch2['cost_amount'];
$subtract = $resourceamount-$netamount;
if($subtract<0){
$x = -1;}
else{
$x = 0;}
$checker += $x; }
if($amount == NULL){
echo "You need to enter an amount.";}
elseif($checker<0){
echo "Sorry, you don't have enough.";}
else{
//mysql_query("UPDATE user_buildings SET amount='".$insert."' where user_id='".$userid."'");
//mysql_query("UPDATE user_resources SET amount='".$subtract."' where user_id='".$userid."'");
echo "Thank you for purchasing.";}}
echo "<br><br></td>";
$open = true;
}
else
{
// Right column
echo "<td>";
echo "<b>Name:</b> ".$infoc['name']."<br>";
echo "<b>Cost:</b> ";
//COSTS
$query = mysql_query("select * from buildings where name='" . $infoc['name'] . "'") or die(mysql_query());
$fetch = mysql_fetch_array($query);
$query = mysql_query("select * from buildings where building_id='".$fetch['building_id']."' and cost_type=0");
while ($fetch = mysql_fetch_array($query)) {
// Get the resource and the cost for this id.
$rquery = mysql_query("select name from resources where id='" . $fetch['cost_restype'] . "'");
$rfetch = mysql_fetch_array($rquery) or die(mysql_error());
echo $fetch['cost_amount']." ".$rfetch['name']."; ";}
echo "<br>";
echo "<b>Input: </b>";
//INPUTS
$query = mysql_query("select building_id from buildings where name='" . $infoc['name'] . "'") or die(mysql_query());
while ($fetch = mysql_fetch_array($query)) {
$query2 = mysql_query("select cost_amount, cost_restype from buildings where building_id='" . $fetch['building_id'] . "' and cost_type='1'");
$fetch2 = mysql_fetch_array($query2);
if($fetch2['cost_amount'] == NULL){
echo "No input per hour.";
}
else{
$query2 = mysql_query("select cost_amount, cost_restype from buildings where building_id='" . $fetch['building_id'] . "' and cost_type='1'");
while($fetch2 = mysql_fetch_array($query2)){
// Get the resource and the cost for this id.
$rquery = mysql_query("select name from resources where id='".$fetch2['cost_restype']."'");
$rfetch = mysql_fetch_array($rquery) or die(mysql_error());
echo $fetch2['cost_amount']." ".$rfetch['name']." per hour; ";}}}
echo "<br>";
echo "<b>Output: </b>";
//outputs
$query = mysql_query("select building_id from buildings where name='" . $infoc['name'] . "'") or die(mysql_query());
while ($fetch = mysql_fetch_array($query)) {
$query2 = mysql_query("select cost_amount, cost_restype from buildings where building_id='" . $fetch['building_id'] . "' and cost_type='2'");
$fetch2 = mysql_fetch_array($query2);
if($fetch2['cost_amount'] == NULL){
echo "No output per hour.";
}
else{
$query2 = mysql_query("select cost_amount, cost_restype from buildings where building_id='" . $fetch['building_id'] . "' and cost_type='2'");
while($fetch2 = mysql_fetch_array($query2)){
// Get the resource and the cost for this id.
$rquery = mysql_query("select name from resources where id='".$fetch2['cost_restype']."'");
$rfetch = mysql_fetch_array($rquery) or die(mysql_error());
echo $fetch2['cost_amount']." ".$rfetch['name']." per hour; ";}}}
//THIS IS THE SPOT.!!!!!!!!
//PURCHASE
echo "<form method='post' action=''>Amount: <input type='text' name='amount' size='3'><input type='submit' name='purchase' value='Purchase'></form>";
if($_POST['purchase'] == 'Purchase'){
//Check user buildings
$query = mysql_query("select building_id from buildings where name='" . $infoc['name'] . "'");
$fetch = mysql_fetch_array($query);
$buildings = mysql_query("select amount from user_buildings where user_id='".$userid."' and building_id='".$fetch['building_id']."'");
$buildingsfetch = mysql_fetch_array($buildings);
if($buildingsfetch['amount'] == NULL){
$buildings = 0;
}
else{
$buildings = $buildingsfetch['amount'];
}
//Establishes amount
$amount = $_POST['amount'];
$insert = $amount+$buildings;
// Get the resource and the cost for this id.
$query2 = mysql_query("select cost_amount, cost_restype from buildings where building_id='" . $fetch['building_id'] . "' and cost_type='0'");
while($fetch2 = mysql_fetch_array($query2)){
//Check if has enough resources
$uquery = mysql_query("select amount from user_resources where user_id='".$userid."' and resource_id='" . $fetch2['cost_restype'] . "'");
$ufetch = mysql_fetch_array($uquery);
if($ufetch['amount'] == NULL){
$resourceamount = 0;
}
else{
$resourceamount = $ufetch['amount'];}
$netamount = $amount*$fetch2['cost_amount'];
$subtract = $resourceamount-$netamount;
if($subtract<0){
$x = -1;}
else{
$x = 0;}
$checker += $x; }
if($amount == NULL){
echo "You need to enter an amount.";}
elseif($checker<0){
echo "Sorry, you don't have enough.";}
else{
//mysql_query("UPDATE user_buildings SET amount='".$insert."' where user_id='".$userid."'");
//mysql_query("UPDATE user_resources SET amount='".$subtract."' where user_id='".$userid."'");
echo "Thank you for purchasing.";}}
echo "<br><br></td></tr>";
$open = false;
}
$i++;
}
echo "</TD></TR></TABLE>";
|