Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    data stred as string, even thought it should be an int....:(

    right in the below code, i am taking data in from a table (its data type in table is int). but when i put the data in the var, its stored as a string. i need it to be an int as i need to do calculations with it. why is it oding this?

    $production_cost, $user_gold are the culprits. im taking data in from the same table, which are f same datatype in the table and they will be set as ints, but not these ones........any ideas?



    PHP Code:
    <?
    (!defined('')) ? die : '';
    (!isset(
    $_SESSION['user_id'])) ? redirect('index.php?mode=login') : '';
    if (!@isset(
    $_GET['upgrading']))
    {
      
    $user_id=$_SESSION['user_id'];
      
    $tresultmysql_query("SELECT * FROM `user_training`
                          WHERE user_id = $user_id"
    );
    if (
    $train=mysql_fetch_assoc($tresult)){
    $cost=0;
    is_int($cost);
    if (
    is_int($cost)){echo' cost is an int 11 ';}else{echo'cost isnt an int 11';}
    $cost=$train["production_cost"];
    is_string($cost);
    if (
    is_string($cost)){echo' cost is string ';}else{echo'cost isnt astring';}
    is_int($cost);
    if (
    is_int($cost)){echo' cost is an int';}else{echo'cost isnt an int';}
    echo 
    " produciton cost gold is $cost /";print_r($cost);echo' / ';settype($cost"int");
    }else{
    echo 
    'cant set user gfold';
    }
    $uuser_gold=intval($user_gold);
    is_int($uuser_gold);if(is_int($uuser_gold)){echo'uuuuser gold is an int';}else{echo'uuuser gold isnt an int';}
    echo
    ' ++ ';print_r($uuser_gold);echo" +$uuser_gold+ ";
    if (
    $tresult)
    {
      echo 
    'tresult successful';
    }else{
      echo 
    'tresult failed';
    }
      
    $result mysql_query("SELECT * FROM `users`
                          WHERE user_id = '"
    .$user_id."'");
    if (
    $user=mysql_fetch_assoc($result)){
    $user_gold=0;
    is_int($user_gold);
    if (
    is_int($user_gold)){echo' user gold  is an int 1122 ';}else{echo'user gold isnt an int 1122';}
    $user_gold=$user["user_gold"];
    is_string($user_gold);
    if (
    is_string($user_gold)){echo' user gold is string ';}else{echo'user gold isnt astring';}
    is_int($user_gold);
    if (
    is_int($user_gold)){echo' gold is an int';}else{echo'gold isnt an int';}
    echo 
    " user gold is $user_gold / ";print_r($user_gold);echo' / ';settype($user_gold"int");
    }else{
    echo 
    'cant set user gfold';
    }
    if (
    $result)
    {
      echo 
    'result successful';
    }else{
      echo 
    'result failed';
    }
    echo 
    '<table width="90%" cellpadding="0" cellspacing="0" border="0">
           <tr>
                 <td align="left"><b>Unit Production</b></td>
                 <td align="left"><b>Next Level</b></td>
                 <td align="left"><b>Cost</b></td>
           </tr>'
    ;
    while (
    $tmember mysql_fetch_assoc($tresult))
    {
            echo 
    '<tr>
                         <td align="left">'
    .$tmember['unit_production'].'</td>
                 <td align="left">'
    .$tmember['next_lvl'].'</td>
                     <td align="left">'
    .$tmember['production_cost'].'</td>
                </tr>'
    ;
    }
    echo 
    '</table>';
       echo 
    '
    <form action="index.php?mode=training&amp;upgrading=trueamp;" method="POST">
                       Upgrade:  <input type="submit" value="Upgrade" />
                         <input type="hidden" name="Upgrade" value="true" />
    </form>'
    ;
    } else if (@isset(
    $_GET['upgrading'])) {
    $tresultmysql_query("SELECT * FROM `user_training`
                          WHERE user_id = '"
    .$user_id."'");
    $tmember mysql_fetch_assoc($tresult)or die('an error occured in this query:'.$tresult.'. The error is: '.mysql_error()) ;
    echo
    ' // ';;print_r($user_gold);echo' // ';
    echo
    ' // ';print_r($cost);echo' // ';
      if (
    $user_gold>=$cost)
      {
    $new_user_gold=0;
        
    $new_user_gold=$user["user_gold"]-$train["production_$cost"];
    settype ($new_user_gold"int");
    is_int($new_ser_gold);
    if (
    is_int($new_user_gold)){echo' new user gold  is an int';}else{echo'new user gold isnt an int';}
    echo 
    "new gold is  $new_user_gold / ";
    print_r($new_user_gold);echo'/';
         
    $nproduction_lvl=$tmember['unit_production']+1;
    is_int($nproduction_lvl);
    if (
    is_int($nproduction_lvl)){echo' $nproduciton_lvl is an int';}else{echo'$nproduciton_lvl isnt an int';}
    echo 
    " produciton cost gold is $cost /";print_r($cost);echo' / ';settype($cost"int");
         
    $nnext_lvl=$tmember['next_lvl']+1;  
    $addition=0;
    $addition=$nproduction_lvl+($nproduction_lvl*82);
    settype ($addition"int");
         
    $ncost=addition+($cost*2);
    echo 
    "new cost is $ncost / ";
    is_int($ncost);
    if (
    is_int($ncost)){echo' new cost  is an int';}else{echo'new cost isnt an int';}
    settype($ncost"int");print_r(ncost); echo'/';
         
    $user_result mysql_query("UPDATE `users`
                                             SET user_gold = '"
    .$new_user_gold."'
                                            WHERE user_id = '"
    .$_SESSION['user_id']."'");

         
    $tuser_result=mysql_query("UPDATE `user_training`
                                            SET unit_production = '"
    .$nproduction_lvl."',
                                            next_lvl = '"
    .$nnext_lvl."',
                                             production_cost =$ncost
                                            WHERE user_id = '"
    .$_SESSION['user_id']."'");

       }else{
         echo 
    'Not enough gold';
         
    timed_redirect('index.php?mode=training'5);
    }

    if (
    $user_result)
    {
      echo 
    'Upgrade Successful!';
      
    timed_redirect('index.php?mode=training'5);
    }else{
      echo 
    'Error Occurred1';
      
    timed_redirect('index.php?mode=training'5);
    }

    if (
    $tuser_result)
    {
      echo 
    'Upgrade Successfu22l!';
      
    timed_redirect('index.php?mode=training'5);
    }else{
      echo 
    'Error Occurre2d: '.$tuser_result.'. The error is: '.mysql_error();
      
    timed_redirect('index.php?mode=training'5);
    }
    }
    ?>

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    use this, it's called typecasting

    PHP Code:
    $cost=(int)$train["production_cost"]; 

  • #3
    New Coder
    Join Date
    Jun 2005
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts
    right dude cheers that has set em to ints. sadly...when i runy the code

    PHP Code:
      if ($user_gold>=$cost)
      {
    $new_user_gold=0;
        
    $new_user_gold=$user["user_gold"]-$train["production_$cost"];
    settype ($new_user_gold"int");
    is_int($new_ser_gold);
    if (
    is_int($new_user_gold)){echo' new user gold  is an int';}else{echo'new user gold isnt an int';}
    echo 
    "new gold is  $new_user_gold / ";
    print_r($new_user_gold);echo'/';
         
    $nproduction_lvl=$tmember['unit_production']+1;
    is_int($nproduction_lvl);
    if (
    is_int($nproduction_lvl)){echo' $nproduciton_lvl is an int';}else{echo'$nproduciton_lvl isnt an int';}
    echo 
    " produciton cost gold is $cost /";print_r($cost);echo' / ';settype($cost"int");
         
    $nnext_lvl=$tmember['next_lvl']+1;  
    $addition=0;
    $addition=$nproduction_lvl+($nproduction_lvl*82);
    settype ($addition"int");
         
    $ncost=addition+($cost*2);
    echo 
    "new cost is $ncost / ";
    is_int($ncost);
    if (
    is_int($ncost)){echo' new cost  is an int';}else{echo'new cost isnt an int';} 
    excuse the debugs. it is setting $user_gold and $cost to 0 it seems...it shndt be doing that.

  • #4
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    PHP Code:
    if ($user_gold>=$cost
      { 
    $new_user_gold=0
        
    $new_user_gold=((int)$user["user_gold"]-(int)$train["production_$cost"]); 
    if (
    is_int($new_user_gold)){echo' new user gold  is an int';}else{echo'new user gold isnt an int';} 
    echo 
    "new gold is  $new_user_gold / "
    print_r($new_user_gold);echo'/'
         
    $nproduction_lvl=((int)$tmember['unit_production']+1); 
    if (
    is_int($nproduction_lvl)){echo' $nproduciton_lvl is an int';}else{echo'$nproduciton_lvl isnt an int';} 
    echo 
    " produciton cost gold is $cost /";print_r($cost);echo' / ';
    $nnext_lvl=((int)$tmember['next_lvl']+1);   
    $addition=0
    $addition=$nproduction_lvl+($nproduction_lvl*82); 
         
    $ncost=((int)$addition+($cost*2)); 
    echo 
    "new cost is $ncost / "
    if (
    is_int($ncost)){echo' new cost  is an int';}else{echo'new cost isnt an int';} 

  • #5
    New Coder
    Join Date
    Jun 2005
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts
    sorta worked.

    $ncost is sorta getting set. but it isnt the correct value. it still not using $user_gold or $cost in the equations. they getting ignored totally it seems.
    any ideas as to why they are doing this?

    here is all the code, it might make it easier to spot what i am doing wrong. dang..its annoying.

    PHP Code:
    <?
    (!defined('')) ? die : '';
    (!isset(
    $_SESSION['user_id'])) ? redirect('index.php?mode=login') : '';
    if (!@isset(
    $_GET['upgrading']))
    {
      
    $user_id=$_SESSION['user_id'];
      
    $tresultmysql_query("SELECT * FROM `user_training`
                          WHERE user_id = $user_id"
    );
    if (
    $train=mysql_fetch_assoc($tresult)){
    $cost=0;
    is_int($cost);
    if (
    is_int($cost)){echo' cost is an int 11 ';}else{echo'cost isnt an int 11';}
    $cost=(int)$train["production_cost"];
    is_string($cost);
    if (
    is_string($cost)){echo' cost is string ';}else{echo'cost isnt astring';}
    is_int($cost);
    if (
    is_int($cost)){echo' cost is an int';}else{echo'cost isnt an int';}
    echo 
    " produciton cost gold is $cost /";print_r($cost);echo' / ';settype($cost"int");
    }else{
    echo 
    'cant set user gfold';
    }
    $uuser_gold=intval($user_gold);
    is_int($uuser_gold);if(is_int($uuser_gold)){echo'uuuuser gold is an int';}else{echo'uuuser gold isnt an int';}
    echo
    ' ++ ';print_r($uuser_gold);echo" +$uuser_gold+ ";
    if (
    $tresult)
    {
      echo 
    'tresult successful';
    }else{
      echo 
    'tresult failed';
    }
      
    $result mysql_query("SELECT * FROM `users`
                          WHERE user_id = '"
    .$user_id."'");
    if (
    $user=mysql_fetch_assoc($result)){
    $user_gold=0;
    is_int($user_gold);
    if (
    is_int($user_gold)){echo' user gold  is an int 1122 ';}else{echo'user gold isnt an int 1122';}
    $user_gold=(int)$user["user_gold"];
    is_string($user_gold);
    if (
    is_string($user_gold)){echo' user gold is string ';}else{echo'user gold isnt astring';}
    is_int($user_gold);
    if (
    is_int($user_gold)){echo' gold is an int';}else{echo'gold isnt an int';}
    echo 
    " user gold is $user_gold / ";print_r($user_gold);echo' / ';settype($user_gold"int");
    }else{
    echo 
    'cant set user gfold';
    }
    if (
    $result)
    {
      echo 
    'result successful';
    }else{
      echo 
    'result failed';
    }
    echo 
    '<table width="90%" cellpadding="0" cellspacing="0" border="0">
           <tr>
                 <td align="left"><b>Unit Production</b></td>
                 <td align="left"><b>Next Level</b></td>
                 <td align="left"><b>Cost</b></td>
           </tr>'
    ;
    while (
    $tmember mysql_fetch_assoc($tresult))
    {
            echo 
    '<tr>
                         <td align="left">'
    .$tmember['unit_production'].'</td>
                 <td align="left">'
    .$tmember['next_lvl'].'</td>
                     <td align="left">'
    .$tmember['production_cost'].'</td>
                </tr>'
    ;
    }
    echo 
    '</table>';
       echo 
    '
    <form action="index.php?mode=training&amp;upgrading=trueamp;" method="POST">
                       Upgrade:  <input type="submit" value="Upgrade" />
                         <input type="hidden" name="Upgrade" value="true" />
    </form>'
    ;
    } else if (@isset(
    $_GET['upgrading'])) {
    $tresultmysql_query("SELECT * FROM `user_training`
                          WHERE user_id = '"
    .$user_id."'");
    $tmember mysql_fetch_assoc($tresult)or die('an error occured in this query:'.$tresult.'. The error is: '.mysql_error()) ;
    echo
    ' // ';;print_r($user_gold);echo' // ';
    echo
    ' // ';print_r($cost);echo' // ';
      if (
    $user_gold>=$cost)  
      {  
    $new_user_gold=0;  
        
    $new_user_gold=((int)$user["user_gold"]-(int)$train["production_$cost"]);  
    if (
    is_int($new_user_gold)){echo' new user gold  is an int';}else{echo'new user gold isnt an int';}  
    echo 
    "new gold is  $new_user_gold / ";  
    print_r($new_user_gold);echo'/';  
         
    $nproduction_lvl=((int)$tmember['unit_production']+1);  
    if (
    is_int($nproduction_lvl)){echo' $nproduciton_lvl is an int';}else{echo'$nproduciton_lvl isnt an int';}  
    echo 
    " produciton cost gold is $cost /";print_r($cost);echo' / '
    $nnext_lvl=((int)$tmember['next_lvl']+1);    
    $addition=0;  
    $addition=$nproduction_lvl+($nproduction_lvl*82);  
         
    $ncost=((int)$addition+($cost*2));  
    echo 
    "new cost is $ncost / ";  
    if (
    is_int($ncost)){echo' new cost  is an int';}else{echo'new cost isnt an int';}  
    settype($ncost"int");print_r(ncost); echo'/';
         
    $user_result mysql_query("UPDATE `users`
                                             SET user_gold = '"
    .$new_user_gold."'
                                            WHERE user_id = '"
    .$_SESSION['user_id']."'");

         
    $tuser_result=mysql_query("UPDATE `user_training`
                                            SET unit_production = '"
    .$nproduction_lvl."',
                                            next_lvl = '"
    .$nnext_lvl."',
                                             production_cost =$ncost
                                            WHERE user_id = '"
    .$_SESSION['user_id']."'");

       }else{
         echo 
    'Not enough gold';
         
    timed_redirect('index.php?mode=training'5);
    }

    if (
    $user_result)
    {
      echo 
    'Upgrade Successful!';
      
    timed_redirect('index.php?mode=training'5);
    }else{
      echo 
    'Error Occurred1';
      
    timed_redirect('index.php?mode=training'5);
    }

    if (
    $tuser_result)
    {
      echo 
    'Upgrade Successfu22l!';
      
    timed_redirect('index.php?mode=training'5);
    }else{
      echo 
    'Error Occurre2d: '.$tuser_result.'. The error is: '.mysql_error();
      
    timed_redirect('index.php?mode=training'5);
    }
    }
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •