PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 11th, 2008, 05:05 PM
Custer Custer is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 3 Custer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 == )
        {
            
// 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>"

Reply With Quote
  #2  
Old May 14th, 2008, 03:43 PM
icandothat's Avatar
icandothat icandothat is offline
Moderator
Click here for more information.
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 1,550 icandothat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 2 h 39 m 8 sec
Reputation Power: 3
are you saying it update all of the rows in the database?
__________________
There is no spoon.

Reply With Quote
  #3  
Old May 14th, 2008, 04:39 PM
RevRaz RevRaz is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 8 RevRaz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 16 m 20 sec
Reputation Power: 0
The only UPDATE statements I see in your code are the ones that are commented out

//mysql_query("UPDATE user_buildings SET amount='".$insert."' where user_id='".$userid."'");
//mysql_query("UPDATE user_resources SET amount='".$subtract."' where user_id='".$userid."'");

Are those the ones? If so, echo the query before you commit it and make sure your variables are right.

Also, always use mysql_error after a query so you can see if there are any errors.

Reply With Quote
  #4  
Old May 15th, 2008, 07:11 PM
dbemowsk dbemowsk is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 1 dbemowsk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 8 sec
Reputation Power: 0
Mixing PHP code with HTML

This reply is not to solve your issue, but to merely bring up a coding point. When writing code that has the HTML mixed in, it makes things hard to follow. I prefer to use templates. It makes the code a lot easier to follow. The basic idea is to create an HTML page the way you want it to look and then insert tags in the HTML to designate where data will be placed. Then use a template parser class or function that will call the template and replace the tagged areas with the data needed.

I have a little tutorial on creating and using templates at phpwebscripting dot com Hope this is useful for some.


dbemowsk
Comments on this post
icandothat disagrees: This is not helpful. Supurflous/ unsolicited posts should be avoided.

Reply With Quote
  #5  
Old May 15th, 2008, 08:20 PM
Custer Custer is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 3 Custer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 24 m 44 sec
Reputation Power: 0
I commented those out and replaced them with echoes to see what's going on. The mysql_errors are there, but I've rid the code of them for now as it's still being tested and I like to keep things slim.

The problem is, I have this huge loop that posts data corresponding to each "building" into a table (the table's just fine). The form accompanies each as well, but when the user posts, the data that should work with only that "building" does it to all of them.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > logic - Repeating problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump