Jump to content

Loops and Variable Column Names


AltarofScience

Recommended Posts

The Code Below is for my Colony Building page for my game.

That particular code is supposed to display the number of buildings built, which it does, and also allow me to change the number of buildings built, which it doesn't.

The code is part of a loop that displays all the possible buildings of a given type, in this case Iron Mines, that can be built on the colony.

Basically what I need to know is, how do I change the name of the column that is being updated as the code loops. Currently it is setting all the forms to alter the number of Basic iron mines on a colony.

I am hoping I can somehow call the names of all the columns into an array and use the same code that picks which column value i am selecting, the $?(an array)[$b](an incrementing number to call the correct array value):

$?[$b].

Then I would replace Basic, in the column part of the UPDATE code with the array and result selector so that the first time i include() the code it creates the form and sets it to alter Basic, but the second time it sets the code to alter the value in the next column.

 

<?php 
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'aosdb';
$conn = mysql_connect($dbhost,$dbuser,$dbpass)
or die ('Error connecting to mysql');
mysql_select_db($dbname);
$b=1;
$queryp = "SELECT * FROM IMBuilt WHERE idcol = $idcol";
$resultp = mysql_query($queryp);
$Base = mysql_fetch_row($resultp);
if(isset($_POST["built"])) {
if($_POST) {
$Badd = $Base[$b]+$_POST["built"];
$queryi="UPDATE IMBuilt SET Basic=$Badd WHERE idcol = $idcol";
mysql_query($queryi);
}
}
$queryf = "SELECT * FROM IMBuilt WHERE idcol = $idcol";
$resultf = mysql_query($queryf);
$Basic = mysql_fetch_row($resultf);
?>
<p class="bd" id="built">| Built: <?php echo $Basic[$b] ?> |</p>
<form class="bd" method="post" action="IronMineList.php"><input type="varchar" name="built"><input type="submit" value="Build"></form>
</div>

Link to comment
Share on other sites

So you are trying to update the IMBuilt table with all the different elements inside a loop?

You can update all of them at once.. Can you show us the form that you are using to update the table.. and perhaps the structure of the IMBuilt table?

 

Also just a few things.. <input type="varchar" /> isnt a valid type.. just use type="text", most browers default to this when a type is missing or invalid.. and is your $queryf really needed when you $Badd set to the new value? Just saves an extra query..

 

Link to comment
Share on other sites

So you are trying to update the IMBuilt table with all the different elements inside a loop?

You can update all of them at once.. Can you show us the form that you are using to update the table.. and perhaps the structure of the IMBuilt table?

 

Also just a few things.. <input type="varchar" /> isnt a valid type.. just use type="text", most browers default to this when a type is missing or invalid.. and is your $queryf really needed when you $Badd set to the new value? Just saves an extra query..

 

well i figured out how to set the column i want to change as a loop. i created an array from another table where the values of a column are the same as the column names and then used ++ so that each new copy of the code selected the next value as the column to edit.

 

as for varchar, the columns i am inputting data to are varchar(45), if i set the type as text that can input into varchar(45) columns?

 

The first query calls the original value so that i can add it to the post input value, say its 10 already, and post is 3, new value after post is 13. the last query calls the new value so that the player can see that they built something without refreshing the page.

 

 

Link to comment
Share on other sites

Glad you got that part sorted..

HTML doesnt care about MySQL data types. You can use type="text" to insert into most field data types

(ie. <input type="text" value="2345" /> will happily go into an int) Just use the maxlength attribute to limit the amount people can type into the input box.

 

With the last query it is unneeded

$queryf = "SELECT * FROM IMBuilt WHERE idcol = $idcol";

This query fetches the exact same information as what $Badd already contains..

You can change the display code to pick up either

<p class="bd" id="built">| Built: <?php echo (isset($Badd) ? $Badd : $Basic[$b]); ?> |</p>

If there is no POST information, thus meaning $Badd isnt set, it will use the original value from $queryp.

Link to comment
Share on other sites

The code below is supposed to be included in a loop. Currently it loops 4 times. That means that in each form the post value should be 1 more than previous. built0-built-3.

However, when I actually use a form to make 1 building, it adds 1 to each of the columns in the database. It is only supposed to add 1 to a single column, based on which of the 4 forms I click. How do I get it to work properly?

 

<?php 
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'aosdb';
$conn = mysql_connect($dbhost,$dbuser,$dbpass)
or die ('Error connecting to mysql');
mysql_select_db($dbname);
$queryj = "SELECT name FROM IMPrints";
$resultj = mysql_query($queryj);
$Column = mysql_result($resultj, $j);
$queryp = "SELECT * FROM IMBuilt WHERE idcol = $idcol";
$resultp = mysql_query($queryp);
$Base = mysql_fetch_row($resultp);
if(isset($_POST['built$i'])) {
if($_POST) {
$Badd = $Base[$b]+$_POST['built$i'];
$queryi="UPDATE IMBuilt SET $Column=$Badd WHERE idcol = $idcol";
mysql_query($queryi);
}
}
$queryf = "SELECT * FROM IMBuilt WHERE idcol = $idcol";
$resultf = mysql_query($queryf);
$Basic = mysql_fetch_row($resultf);
echo $Column;
echo $Base;
?>
<p class="bd" id="built">| Built: <?php echo $Basic[$n]; ?> |</p>
<form class="bd" method="post" action="IronMineList.php"><input type="varchar" name="built$i"><input type="submit" value="Build"></form>
</div>

Link to comment
Share on other sites

Still need help on the above post.

Why doesn't the built$i value increment properly on the forms?

also why does clicking the button on one make the code for all 4 forms activate?

 

it should have the built$i input create:

$_POST['built0']

$_POST['built1']

$_POST['built2']

$_POST['built3']

 

each form's built$i being 1 higher so that each button only affects its own post code.

 

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.