global variables

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 2268 Views - Last Post: 03 September 2008 - 02:33 PM Rate Topic: -----

#1 ghqwerty   User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

global variables

Posted 02 September 2008 - 04:13 AM

so ive got a variable that gets defined inside of a function but how can i use that variable outside of the function .. in know ive got to make it global but when i put


global $winning;


and echo it it doesnt display :S
and if i do

$winning=0;

before the function then do


global $winning;

inside the function $winning always = 0

This post has been edited by ghqwerty: 02 September 2008 - 04:16 AM


Is This A Good Question/Topic? 0
  • +

Replies To: global variables

#2 chili5   User is offline

  • D.I.C Lover

Reputation: 20
  • View blog
  • Posts: 1,146
  • Joined: 28-December 07

Re: global variables

Posted 02 September 2008 - 04:19 AM

Post your entire function code. :)
Was This Post Helpful? 0
  • +
  • -

#3 ghqwerty   User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: global variables

Posted 02 September 2008 - 04:21 AM

fuction code

<?php 
function check($bet)
{
global $one;
global $two;
global $three;
global $peach;
global $apple;
global $cherries;
global $coin;
global $winning;

if($one == $peach && $two == $peach && $three == $peach) {
$winning = $bet*3;
echo $winning;
}elseif(($one == $peach && $two == $peach) || ($one == $peach && $three == $peach) || ($two == $peach && $three == $peach)){
$winning = $bet*2;
echo $winning;
}elseif($one == $apple && $two == $apple && $three == $apple){
$winning = $bet*10;
echo $winning;
}elseif($one == $cherries && $two == $cherries && $three == $cherries){
$winning = $bet*20;
echo $winning;
}elseif($one == $coin && $two == $coin && $three == $coin){
$winning = $bet*50;
echo $winning;
}elseif(($one == $coin && $two == $coin) || ($one == $coin && $three == $coin) || ($two == $coin && $three == $coin)){
$winning = $bet*5;
echo $winning;
}else{
$winning = 0;
echo $winning;
}

}
?>



and this is where i need the variable

<?php
$winning = 0;
require("conn.php"); 
$playerid = $_SESSION['id']; 
echo $winning; 
echo "hello";            

if($winning > 0){ 

  $sql = ("SELECT * FROM members WHERE id = '.$playerid.' LIMIT 1");   
$result = mysql_query($sql) or die(mysql_error());  

$row = mysql_fetch_array($result);   
$updatedplayermoney = ($row['money'] + $winning);// adds winnings to player

$result = mysql_query("UPDATE members SET  money = '.$updatedplayermoney.' WHERE id='.$playerid.'") 
or die(mysql_error());
}else{
  $sql = ("SELECT * FROM members WHERE id = '.$playerid.' LIMIT 1");
$result = mysql_query($sql) or die(mysql_error());  

$row = mysql_fetch_array($result);   
$updatedplayermoney = ($row['money'] - $bet);// adds winnings to player

$result = mysql_query("UPDATE members SET  money = '.$updatedplayermoney.' WHERE id='.$playerid.'") 
or die(mysql_error());
    
}
?> 


Was This Post Helpful? 0
  • +
  • -

#4 JBrace1990   User is offline

  • D.I.C Addict
  • member icon

Reputation: 112
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: global variables

Posted 02 September 2008 - 08:18 AM

inside the function, add return $winning;
Was This Post Helpful? 0
  • +
  • -

#5 ghqwerty   User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: global variables

Posted 02 September 2008 - 10:42 AM

where abouts should i add that .??

and should i keep everything else the same ???
Was This Post Helpful? 0
  • +
  • -

#6 JBrace1990   User is offline

  • D.I.C Addict
  • member icon

Reputation: 112
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: global variables

Posted 02 September 2008 - 10:46 AM

yes, just add the return $winning at the end of the function, riught before the closing }
Was This Post Helpful? 0
  • +
  • -

#7 Ridikule   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 68
  • Joined: 29-September 05

Re: global variables

Posted 02 September 2008 - 11:43 AM

I think you're confusion Global scope with Session variables. $winning is not going to be set to anything other than 0 in your script example, because you don't set it to anything other than 0. The check() function isn't called in your script example.

Variables in the global scope are available everywhere, to other functions, to the script, to objects, ect. As long as they use the global keyword (as you have in your function) to explicitly indicate that the global scope is being referenced for this function.

If you want the value to live on past the execution of the script, you'll have to store the value in a session, database, or cookie. Note that session and database are server side and cookie is client side, so cookies are the least safe from tampering.

Hope that helps, and I apologize if I've just completely misread what you're trying to do :)
Was This Post Helpful? 0
  • +
  • -

#8 ghqwerty   User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: global variables

Posted 02 September 2008 - 12:18 PM

tried putting it into a session variable but it is like one behind so if i bet 10 and then pressed spin and won it would still echo 0 but the next time i spun it it would give me the amount i won

also my database isnt updating when i do any of this

can you see why it isnt working ???
Was This Post Helpful? 0
  • +
  • -

#9 Ridikule   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 68
  • Joined: 29-September 05

Re: global variables

Posted 02 September 2008 - 12:23 PM

If you'd like, you can post your code using the session, and I'll take a look at it.
Was This Post Helpful? 0
  • +
  • -

#10 ghqwerty   User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: global variables

Posted 02 September 2008 - 12:27 PM

all of it ??? .. my ide (whatever that is im using nusphere recommended by jbrace) doesnt find anything wrong with it but it doesnt update my database and my variable winning seems to be 1 turn behind .. i know php runs the whole page before submitting its data but could repositioning my function have anything to do with it ??

well here goes
<?php
session_start();
if ($_SESSION['loggedIn'] != "true") {
     header("Location: checklogin.php");
}

?>



<html>

<head>
<link rel="stylesheet" type="text/css" href="globalwars.css" />
</head>
<body bgcolor="#FFFFCC">





<table cellspacing="1" cellpadding="0" border="1" bgcolor="#FFFFCC" id="layout" height="98%" width="98%">

<!--header-->
<tr height="15%">  
<td colspan="3"> 
<center>
<h1>
Global wars
</h1>
</center>
</td>
</tr>
<!--header-->

<!--links-->
<tr height="85%">
<td width="12%">
<!--links page-->
<?php require("links.php"); ?>
<!--links page-->
</td>
<!--links-->

<!--content-->
<td width="76%">
<p id="content" align="center">


<?php
//puts the pictures into an array
$peach="<p><img src=\"peach.jpeg\" alt=\"Peach\" /></p>";
$apple="<p><img src=\"apple.jpeg\" alt=\"Apples\" /></p>";
$cherries="<p><img src=\"cherries.jpeg\" alt=\"Cherry\" /></p>";
$coin="<p><img src=\"coin.jpeg\" alt=\"Coin\" /></p>";
$slots = array("$peach","$apple","$cherries","$coin");

//makes the pictures be chosen at random
$one_t=rand(0,3);
$two_t=rand(0,3);
$three_t=rand(0,3);

$one=$slots[$one_t];
$two=$slots[$two_t];
$three=$slots[$three_t];

//shows the user what pictures were chosen and gives the option to spin again 
function slotpicture()
{
global $one;
global $two;
global $three;
print("<td width=\"100px\"><center>$one</td>");
print("<td width=\"100px\"><center>$two</td>");
print("<td width=\"100px\"><center>$three</td>");
print("<tr><td colspan=\"3\" bgcolor=\"#FF9966\"><center>take a spin!<br>
<form action=\"page1.php\" method\"page1.php\">
<input type=\"submit\" name=\"submit\" value=\"spin!\" />
</form>");
}
?>



  <p id="money"> bank : <?php echo $bank ; ?> </p>


<?php
echo $bet;
?>



<div id="control"><center>
<form action="page1.php" method="POST">
<div id="betSelector"> 
<p id="bet"> bet : <input type="text" name="bet" />





<?php
$bet = (int) $_POST["bet"];
?>

</p>

</center></div>


<?php  
// update the database with winnings 
require("conn.php");   
$playerid = $_SESSION['id']; 
$newwin = $_SESSION['win'];  
echo $newwin;   
echo "hello";              
  
if($winning > 0){   
  
  $sql = ("SELECT * FROM members WHERE id = '.$playerid.' LIMIT 1");     
$result = mysql_query($sql) or die(mysql_error());    
  
$row = mysql_fetch_array($result);     
$updatedplayermoney = ($row['money'] + $newwin);// adds winnings to player  
  
$result = mysql_query("UPDATE members SET  money = '.$updatedplayermoney.' WHERE id='.$playerid.'")   
or die(mysql_error());  
}else{  
  $sql = ("SELECT * FROM members WHERE id = '.$playerid.' LIMIT 1");  
$result = mysql_query($sql) or die(mysql_error());    
  
$row = mysql_fetch_array($result);     
$updatedplayermoney = ($row['money'] - $bet);// adds winnings to player  
  
$result = mysql_query("UPDATE members SET  money = '.$updatedplayermoney.' WHERE id='.$playerid.'")   
or die(mysql_error());  
      
}  
?>   


<div align="center">
you bet : <?php echo $bet; ?>
<br>
and you won : <?php check($bet); ?>


<?php  
//work out the winnings 
function check($bet)  
{  
global $one;  
global $two;  
global $three;  
global $peach;  
global $apple;  
global $cherries;  
global $coin;  
global $winning;  
  
if($one == $peach && $two == $peach && $three == $peach) {  
$winning = $bet*3;  
echo $winning;  
}elseif(($one == $peach && $two == $peach) || ($one == $peach && $three == $peach) || ($two == $peach && $three == $peach)){  
$winning = $bet*2;  
echo $winning;  
}elseif($one == $apple && $two == $apple && $three == $apple){  
$winning = $bet*10;  
echo $winning;  
}elseif($one == $cherries && $two == $cherries && $three == $cherries){  
$winning = $bet*20;  
echo $winning;  
}elseif($one == $coin && $two == $coin && $three == $coin){  
$winning = $bet*50;  
echo $winning;  
}elseif(($one == $coin && $two == $coin) || ($one == $coin && $three == $coin) || ($two == $coin && $three == $coin)){  
$winning = $bet*5;  
echo $winning;  
}else{  
$winning = 0;  
echo $winning;  
}  
return $winning; 
$_SESSION['win'] = $winning ;
}  
?> 




<table id="machine" border="1" width="300px">
  <tr>
<?
slotpicture();
?>
</center>
</td>
</tr>
  </tr>
</table>

<br>
<br>

<center>
<?php //payouts table ?>
<table id="odds" width="40%" border="1">
<tr bgcolor="#FF9966">
<td>number of item</td>
<td>item</td>
<td>payout</td>
</tr>
<tr>
<td>2</td>
<td>peachs</td>
<td>2 x bet</td>
</tr>
<tr>
<td>3</td>
<td>peachs</td>
<td>3 x bet</td>
</tr>
<tr>
<td>2</td>
<td>coins</td>
<td>5 x bet</td>
</tr>
<tr>
<td>3</td>
<td>apples</td>
<td>10 x bet</td>
</tr>
<tr>
<td>3</td>
<td>cherries</td>
<td>20 x bet</td>
</tr>
<tr>
<td>3</td>
<td>coins</td>
<td>50 x bet</td>
</tr>









</table>
</center>







<td width="12%">
<center>
<?php require("links.php");?>
</center>
</td>
</tr>
</table>




</body>
</html>


This post has been edited by ghqwerty: 02 September 2008 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#11 JBrace1990   User is offline

  • D.I.C Addict
  • member icon

Reputation: 112
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: global variables

Posted 02 September 2008 - 01:06 PM

right here:
$result = mysql_query("UPDATE members SET  money = '.$updatedplayermoney.' WHERE id='.$playerid.'")or die(mysql_error());    


that's where your problem is. You assign the Query to a variable, meaning the query is never activated. change it to:

mysql_query("UPDATE members SET  money = '$updatedplayermoney' WHERE id='$playerid'")or die(mysql_error()); 


and you should be fine.
Was This Post Helpful? 0
  • +
  • -

#12 ghqwerty   User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: global variables

Posted 02 September 2008 - 01:22 PM

will that just fx the 'database' problems or will it also fix the 'lag' problems
Was This Post Helpful? 0
  • +
  • -

#13 ghqwerty   User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: global variables

Posted 02 September 2008 - 01:31 PM

haha hate to disenhearten you but it still doesnt work .. always me that it doesnt work for :(
Was This Post Helpful? 0
  • +
  • -

#14 Ridikule   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 68
  • Joined: 29-September 05

Re: global variables

Posted 02 September 2008 - 01:32 PM

View PostJBrace1990, on 2 Sep, 2008 - 01:06 PM, said:

right here:
$result = mysql_query("UPDATE members SET  money = '.$updatedplayermoney.' WHERE id='.$playerid.'")or die(mysql_error());    


that's where your problem is. You assign the Query to a variable, meaning the query is never activated. change it to:

mysql_query("UPDATE members SET  money = '$updatedplayermoney' WHERE id='$playerid'")or die(mysql_error()); 


and you should be fine.


There is nothing wrong with assigning a query result to a variable. For select statements, this return value is a resource used by other functions. For an update statement, it simply returns true/false indicating if it succeeded or not. In any event, removing the assignment will not change the behavior of your code and will not fix your problem.



You are calling check() at the end of the script. Is this the intent? The problem I see is that you are checking the the variable $winning before you have computed it's value with check(). You might benefit from calling check() earlier in the script.
Try moving the call to check to right after these lines:
echo $newwin;     
echo "hello";
$winningsDisplay = check($bet);



This will update winnings and the session before they are used, so that winnings will have the value as you expect, rather than it always being zero.

Of course, if you do that, you'll also have to change this line:

// old line
and you won : <?php check($bet); ?>

//new line
and you won : <?php echo $winningsDisplay; ?>


Was This Post Helpful? 0
  • +
  • -

#15 ghqwerty   User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: global variables

Posted 02 September 2008 - 01:44 PM

ok ive got the 'lagging' issues sorted but no matter what i try it just refuses to update the database
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2