Getting buttons and textboxes within loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

iankent wrote:
Goofan wrote:well i have learned by "speaking to co-workers" that $_session got alot of faults...
for exampel if i remember correct:
If u close by (go by Vtrl + Alt + Delete) and close the webbpage it will keep u loged in and u might never get back into ure login...
it shouldn't. if you just use a normal session it will end when the browser window closes (note window, not tab in most browsers). if you use ctrl+alt+del or restart the computer or shut down the browser the session should end. if it doesn't thats a browser fault and not a PHP session fault. as far as I know this works perfectly in almost all browsers (certainly does in IE5/6/7/8, Opera, Safari, Chrome and Firefox).

The exception to this is if you manually change the session expiry date to something in the future. If you do, the session becomes persistent even when the browser window is closed/computer crashes etc. but thats only if you tell it to, otherwise it shouldn't.

lots of websites use sessions to store data, its pretty much the only straightforward way to manage data during a users time on a website. without sessions it's much more difficult and can get very messy

when a user logs in you retrieve the saved_id from the database? if so, what do you do with it? it needs to be passed to the menu file as a $_GET variable, i.e. yourfile.php?saved_id=x - that way the menu file can output the saved_id in the link, which will then pass it to the other script for the SQL query. a bit of a messy way of doing it I must say.

Yes, you need to change it to <?php echo $id ?> - at the top of the menu file you get the saved_id from the URL and put it in $id NOT $row['saved_id']. Also, please read my posts carefully, I've said most of the last two paragraphs before!
well ure "suspision on what my code does is pritty much all" and about the session...
i dont wanna use the session:
firstly "the rumors ive heard"
secondly im allmost finished with the code i belieave this to be my final problem (and a big one 2 ;) ).
third "difficulty is the way u learn" am i not correct? :D
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Getting buttons and textboxes within loop

Post by iankent »

So its now displaying the correct number of each on the page? I.e., the numbers on the page reflect the numbers in swordmen,macemen and pikemen in the database?

if so, now thats working we know saved_id is getting through. post your full code for that page again and we'll see why its not updating!

yes, difficulty certainly is the way you learn lol, and finding answers to problems and learning new stuff is the best part of programming :P

edit: and yes, its your choice as to whether you use sessions, but bear in mind that using your alternative has its own risks. once the user has logged in, whats to stop them changing the URL to another saved_id? how does each page know who the user is other than the saved_id? if you retrieve the user_id from saved_id then any user can access any saved_id just by changing the URL. a much bigger security risk than sessions would be
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

i think ure a very good tutor :D thought of being a teacher? ;)

the code:

Code: Select all

 
<?php
    // Get the database connector stuff
    include "../login/database.php";
     
    // Build our query
    $sql1 = 'SELECT * FROM `infantries`';
     
    // Get the result if there is one
   // DO NOT die() IN PRODUCTION!!!
   if (!$result1 = mysql_query($sql1)) 
    {
     die('The query<br /><strong>' . $sql1 . '</strong><br />failed:<br />' . mysql_error());
    }
?>
<?php
 
 
  $id =(isset($_GET['saved_id'])) ? (int)$_GET['saved_id'] : false;
    if($id !== false) 
 {
    $sql="SELECT * FROM konto WHERE saved_id=$id";      //selecting all from DB "Konto" where saved_id is the same as in the array $id
 } 
    else 
 {
    echo "NO saved_id!";
 }
    
 
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tall. //hämtar all info från tabell
                 
while($row = mysql_fetch_array( $result )) //hämtar info från tabell.
 
{
    $my['Swordmen'] = $row['swordmen'];
    $my['Macemen'] = $row['macemen'];
    $my['Pikemen'] = $row['pikemen'];
}
if(isset($_POST['Swordmen']))
{
        // the Swordmen Buy button was clicked
        $unit = "swordmen";
        $qty = $_POST['qty_swordmen'];
}
if(isset($_POST['Macemen']))
{
        // the Macemen Buy button was clicked
        $unit = "macemen";
        $qty = $_POST['qty_macemen'];
}   
if(isset($_POST['Pikeman']))
{
        // the Pikeman Buy button was clicked - so you need to use qty_Pikemen
        $unit = "pikeman";
        $qty = $_POST['qty_pikemen'];
}
if(isset($unit) && is_numeric($qty) && $qty > 0) 
{ // if isset($unit) then one of the three buttons was clicked
    $sql="UPDATE konto SET $unit=$unit + $qty WHERE saved_id=$id";//Sätt upp SQL fråga.
    $result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
}
?>
<html>
<head>
<link href="../css/style.css" rel="stylesheet" type="text/css">
<title>Infantrie</title>
</head>
<body bgcolor="#A4A8B0">
    <style type="text/css">     <!--selecting the style type-->
        <!--
        a:link {text-decoration: none; color: black}
        a:visited {text-decoration: none; color: black}
        a:active {text-decoration: none; color: black} 
        a:visited {text-decoration: none; color: black}
        .textbox { background-color: #DEB887; }
        .submit { background-color: #DEB887; }
        -->
    </style>        <!--ending the style type-->
    <p>                     
        Är du villig att köpa soldater? Tror du att du är man nog att göra det?
    </p>
 <table border="1">
   <tr>
    <th>Units</th>
        <th>AtkArm</th>
    <th>Buyfor</th>
        <th>Sellfor</th>
    <th>You Own</th>
        <th>Buy</th>
   </tr>
<form action="#" method="post">
 <?php while ($row1 = mysql_fetch_assoc($result1)): ?>
 <tr>
        <th><?php echo $row1['Units'] ;?></th>
    <th><?php echo $row1['AtkHp'] ;?></th>
        <th><?php echo $row1['Buyfor'] ;?></th>
    <th><?php echo $row1['Sellfor'] ;?></th>
        <th><?php echo $my[$row1['Units']]; ?></th>
    <th>
        <font color="black" valign= "top"><input style='width:45;height:20' type="text" name="qty_<?php echo $row1['Units']?>" size="250" class="textbox" value=""></font>
        <input style='width:45;height:30;font-weight:bold' name="<?php echo $row1['Units']?>" type="submit" class="submit" value="Buy!">
    </th>
   </tr>
 <?php endwhile; ?>
 </form>
 </table>
</html>
 
tell me if u want me to strip the code down :D

echo "<p>$sql</p>";
getting: SELECT * FROM konto WHERE saved_id=2

echo "Results: ".mysql_num_rows($result); //to see if u get any
getting: Results: 1
Last edited by Goofan on Thu Nov 19, 2009 3:33 pm, edited 1 time in total.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

when the code is running i get this code at "first text/button":

<th>
<font color="black" valign= "top"><input style='width:45;height:20' type="text" name="15" size="250" class="textbox" value=""></font>
<input style='width:45;height:30;font-weight:bold' name="15" type="submit" class="submit" value="Buy!">
</th>

name is the amount i got in the datatable same is the name of the button.
Dont think it should be like that: what if i got 2 datatables with the same value.
for example:
swordmen at (saved_id=2) and it got the value 2000 then the name of the textbox and the button will be name: 2000
however what if i got macemen with the value of 2 aswell...?
will it uppdate both of them if 1 button is pressed?
:D
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Getting buttons and textboxes within loop

Post by iankent »

echo "<p>$sql</p>";
getting: SELECT * FROM konto WHERE saved_id=2

echo "Results: ".mysql_num_rows($result); //to see if u get any
getting: Results: 1
yep, thats great - you can remove those two lines now, it's just showing that saved_id is working as you want it to

the problem is you don't have a <form> tag to tell the input and submit button what to do.

after line 92 (before the while() loop) add something like this:

Code: Select all

<form action="#" method="post">
and after line 105 (after the end of the while() loop) add this:

Code: Select all

</form>
now when you click the submit button it submits the form to '#' (i.e., the current page) using the method POST (i.e., variables are in the content not the URL, and are available via $_POST)

edit:
you need to change this bit:

Code: Select all

       <font color="black" valign= "top"><input style='width:45;height:20' type="text" name="<?php echo $my[$row1['Units']]; ?>" size="250" class="textbox" value=""></font>
        <input style='width:45;height:30;font-weight:bold' name="<?php echo $my[$row1['Units']]; ?>" type="submit" class="submit" value="Buy!">
In the first <input> (the quantity textbox) the name bit should be name="qty_<?php echo $row1['Units']?>" and in the second <input> (the submit button) the name bit should be name="<?php echo $row1['Units']?>". You want to set it to the name of the unit and not the quantity of that unit (i.e., $my)
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

ok i got work tomorrow so i need to go to sleep so ill be back tomorrow :D hope u find the problem.


-HTGH "Happy To Get Help"
-Thomas
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

error getting:

Notice: Undefined index: qty_swordmen in C:\Program Files\wamp\www\www\Projektarbete\sidor\Infanteries.php on line 45


ive reloaded the code above "complete code" to what it is now...
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Getting buttons and textboxes within loop

Post by iankent »

Goofan wrote:error getting:

Notice: Undefined index: qty_swordmen in C:\Program Files\wamp\www\www\Projektarbete\sidor\Infanteries.php on line 45


ive reloaded the code above "complete code" to what it is now...
could you copy and paste the output html between the <form></form> tags (i.e. from view source in the browser)

edit: including the <form></form> tag plz :)
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

code in use! :D
when the code is used it looks like this

Code: Select all

 
<form action="#" method="post">
  <tr>
 
        <th>Swordmen</th>
    <th>15/5</th>
        <th>$1500</th>
    <th>$500</th>
        <th>15</th>
    <th>
 
        <font color="black" valign= "top"><input style='width:45;height:20' type="text" name="qty_Swordmen" size="250" class="textbox" value=""></font>
        <input style='width:45;height:30;font-weight:bold' name="Swordmen" type="submit" class="submit" value="Buy!">
    </th>
   </tr>
  <tr>
        <th>Macemen</th>
    <th>25/2</th>
        <th>$1000</th>
 
    <th>$333</th>
        <th>4</th>
    <th>
        <font color="black" valign= "top"><input style='width:45;height:20' type="text" name="qty_Macemen" size="250" class="textbox" value=""></font>
        <input style='width:45;height:30;font-weight:bold' name="Macemen" type="submit" class="submit" value="Buy!">
    </th>
   </tr>
  <tr>
 
        <th>Pikemen</th>
    <th>20/10</th>
        <th>$3000</th>
    <th>$1000</th>
        <th>4563</th>
    <th>
 
        <font color="black" valign= "top"><input style='width:45;height:20' type="text" name="qty_Pikemen" size="250" class="textbox" value=""></font>
        <input style='width:45;height:30;font-weight:bold' name="Pikemen" type="submit" class="submit" value="Buy!">
    </th>
   </tr>
  </form>
 
 
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Getting buttons and textboxes within loop

Post by iankent »

Change line 45, 51 and 57 (the ones where we set $qty = $_POST['whatever']) to this:

Code: Select all

$qty = $_POST['qty_Swordmen'];

Code: Select all

$qty = $_POST['qty_Macemen'];

Code: Select all

$qty = $_POST['qty_Pikemen'];
i.e., capitalize them

its looking for the lowercase version when the html form is using the uppercase version!
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

THANKS NOW THE CODE WORKS =)
however can u basicly tell me where the code is telling the textbox to add the new numbers + the old and then show them?


-thanks for all the help :D

and if i wanna refresh the page after the button is used... what code can i use to do this?
ive tried:

Code: Select all

 
if(isset($unit) && is_numeric($qty) && $qty > 0) 
{ // if isset($unit) then one of the three buttons was clicked
    $sql="UPDATE konto SET $unit=$unit + $qty WHERE saved_id=$id";//Sätt upp SQL fråga.
    $result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
    
     header('Refresh: 0; url=Infanteries.php?saved_id='.$row[saved_id])
}
 
if u cant or dont "wanna try to solve this area(problem) then tell me and ill post a new thread :D for someone else to hlep me :D


-Thomas
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Getting buttons and textboxes within loop

Post by iankent »

Goofan wrote:THANKS NOW THE CODE WORKS =)
however can u basicly tell me where the code is telling the textbox to add the new numbers + the old and then show them?


-thanks for all the help :D
no prob, glad it works and hope you understand most of it :)

the bit displaying the current values is line 99:

Code: Select all

<th><?php echo $my[$row1['Units']]; ?></th>
this is displaying the value of $my[$row1['Units']] which you set at line 37-39 using the SQL query result

to add a new one, you enter the quantity in the text box (named qty_Unit where Unit is Swordmen,etc). you then click a button (where the buttons are named Swordmen, etc). the PHP script then checks if $_POST['Swordmen'] etc are set (lines 41-58), and if any of them are, assigns the value of the quantity box in the HTML form to $qty.

The query on line 61 is then executed:

Code: Select all

$sql="UPDATE konto SET $unit=$unit + $qty WHERE saved_id=$id"
This is telling it to take the current value of the $unit column (which will be swordmen, etc as set in lines 41-58) and add the value of $qty (which comes from the form).

That's all there is to it!

actually, one bug in your code now is that when you buy items the page doesn't show the update straight away, only the next time you refresh it. that's because the UPDATE sql query is called after you set the value of $my. what you should do is something like:
$my[$unit] += $qty;
after line 62 (the $result = mysql_query line). You probably want to actually do something like this, so it only updates if the query was successful:

Code: Select all

 
if(mysql_affected_rows()>0) {
    $my[$unit] += $qty;
}
 
edit: that bug solves the problem in your edit (with refreshing the page). if you add that code you shouldn't need to refresh as the numbers should update automatically

hth :)
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

ok thanks so much :D ill make everything work from now on =)
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

i inputted the extra code u gave me and i get the error
after i enter a value in textbox and press the button this shows:

Notice: Undefined index: swordmen in C:\Program Files\wamp\www\www\Projektarbete\sidor\Infanteries.php on line 61

Code: Select all

 
if(isset($unit) && is_numeric($qty) && $qty > 0) 
{ // if isset($unit) then one of the three buttons was clicked
    $sql="UPDATE konto SET $unit=$unit + $qty WHERE saved_id=$id";//Sätt upp SQL fråga.
    $result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
if(mysql_affected_rows()>0) {
 line 61 ----->    $my[$unit] += $qty;
 }
}
 
 
i guess this is where u wanted me to put it however incase u wanted me to put it like this:

Code: Select all

 
{ // if isset($unit) then one of the three buttons was clicked
    $sql="UPDATE konto SET $unit=$unit + $qty WHERE saved_id=$id";//Sätt upp SQL fråga.
    $result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
 
}
if(mysql_affected_rows()>0) {
 line 61------>    $my[$unit] += $qty;
 }
 
then i get the error message of:

Notice: Undefined variable: unit in C:\Program Files\wamp\www\www\Projektarbete\sidor\Infanteries.php on line 63

Notice: Undefined index: in C:\Program Files\wamp\www\www\Projektarbete\sidor\Infanteries.php on line 63

Notice: Undefined variable: qty in C:\Program Files\wamp\www\www\Projektarbete\sidor\Infanteries.php on line 63
Last edited by Goofan on Fri Nov 20, 2009 7:54 am, edited 1 time in total.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

and if i press the button with a value in textbox after second exampel i get this:

Notice: Undefined index: swordmen in C:\Program Files\wamp\www\www\Projektarbete\sidor\Infanteries.php on line 63

and it uppdates the datatable so it adds the value of the textbox but it dont uppdate the page...
Post Reply