Jump to content

Possible variable issue...


Go to solution Solved by Christian F.,

Recommended Posts

Hey all! I'm new to php and just ran into an issue.
Anyone know what could be going on here?
I've made a dialogue system, but the text I've echoed first never leaves the screen. The response text seems to update appropriately. I wrote a bit of code earlier and tested, the dialogue and branch variables seem to be updating appropriately. Just to make it clear, I only want the intro text to appear until a button is pressed, then display the response text instead. Thanks for any help you might be able to give me! smiley.gif
 

<?php
// set variables
$strength=25;
$constitution=25;
$dexterity=25;
$intelligence=25;
$willpower=25;
$charisma=25;
$speed=25;
//action info: 0 showstats 1 story
$action=1; 
$dialogue=1;
$branch=0;

if ($action =0)
    {
    //show stats
    print "<p><br><font color=\"red\">Your <big>stats</big> are as follows:</font></p>
    <br>
    <p>Strength: $strength <br>
    Constitution: $constitution <br>
    Dexterity: $dexterity <br>
    Intelligence: $intelligence <br>
    Willpower: $willpower <br>
    Charisma: $charisma <br>
    Speed: $speed <br>
    </p>\n";    
    }

if ($action=1)
    {
    if ($dialogue=1)
        {
print "We begin your story here, in the snowy hills of Alteberron, in the city of     

Winter's Hold. While on your way from the desert city of Shahazad, your caravan was     

attacked by marauders, the caravan guard slaughtered before your eyes. You were forced to 

watch as all of your goods you had worked so hard to acquire were ripped from you and taken 

to god knows where. Beaten and malnourished, you were taken as a     slave to be 

ransomed to your family for a high price. One morning you awaken to hear fighting, the 

clash of steel on steel. Jumping to your feet, you run to your cage door, screams and the 

smell of smoke taking hold of your senses. A moment later, a man stands at your cell, his 

armor glinting in the campfire light. <br><br>
    \"Well, well, well... What have we here?\"";


print"    <p> Your response: <br>
<form action='' method='post'>
<button type='submit' name='1'>Snarky Response</button><br>
<button type='submit' name='2'>Plead for help</button><br>  
<button type='submit' name='3'>Scared\aggressive response</button>
</p></form>";

if(isset($_POST['1'])) {$dialogue=2; $branch=1;  }
if(isset($_POST['2'])) {$dialogue=2; $branch=2;  }
if(isset($_POST['3'])) {$dialogue=2; $branch=3;  }
        }
        
if (($dialogue==2) && ($branch==1))
        { 
print "You reply sarcastically, \"Oh, look. If it isn't my knight in shining armor.\"";
        
}

if (($dialogue==2) && ($branch==2))
        {
print "Gripping the bars, you stare at him earnestly. <br>
\"Look, I need your help. Let me out, they might be back at any moment!\" 
<br>; Your voice comes out as a whispered hiss, afraid someone might overhear you.";
        }
        
if (($dialogue==2) && ($branch==3))
        {
print "You lunge forward, grabbing him by the collar. \"Ha! You're mine! I swore I'd make 

you scumsuckers pay if you ever touched me again!\" He chokes for a moment before swatting 

your hands away, sputtering. \"I'm not one of them, you imbecile!\" His eyes are wide as he 

tries to catch his breath.";
        }

    }


?>

I'm wondering why the script dependent on $dialogue=1 is firing even after I change it to 2. Is it looping my $dialogue=1 command? But if that is true, how can it equal BOTH 1 & 2? Unless it's looping, firing off my =1 command, printing, then seeing the button as pressed and changing it to 2, then printing that text too? If so, how do I get it to declare $dialogue as 1 only on first run instead of looping?

PS: I know I made two topics for this, but I didn't realize it was accidentally posted in the HTML help forum. 'DOH!

Edited by Cygna
Link to comment
https://forums.phpfreaks.com/topic/276845-possible-variable-issue/
Share on other sites

akphidelt2007 : I think I might have confused you somehow. I'm alright with their stats not showing up. That's a part of the script I want to embellish on later. Right now I'm having trouble with the dialogue portion.

For some reason this is what I'm getting:

-------------------------------------------------------

We begin your story here, blah blah blah....

 

Your response:

(Button 1)

(Button 2) 

(Button 3)

------------------------------------------------------

 

On clicking any of the buttons the page returns with this:

 

------------------------------------------------------

We begin your story here, blah blah blah....

Your response:

(Button 1)

(Button 2) 

(Button 3)

Response of whatever button has been clicked.

------------------------------------------------------

 

I'm trying to get this portion to disappear:

 

We begin your story here, blah blah blah....

 

Your response:

(Button 1)

(Button 2) 

(Button 3)

 

when a button has been clicked.

Edited by Cygna

akphidelt2007 is correct. You need to read the response again.

 

 

if ($action =0)

 

if the same as writing:

$action = 0;
if ($action)
A single equal-sign is assignment. To do a comparison, you have to use two equal-signs (or three for "exactly the same")

 

You are using single equal-signs in several IF tests, including the first $dialog test which is assigning 1 to that variable. Then you retrieve the value from $_POST, which makes it 2. So, yeah, it is equal to 1 and equal to 2, but at slightly different times in the script.

  • Solution

If you know you posted it in the wrong section, please ask a mod/guru to move it to the correct location. Do not make another thread with the same content. That's just wasting everyone's time.

 

Copy thread: http://forums.phpfreaks.com/topic/276843-need-help-possible-variable-issue

Because the other thread was removed, I thought I would apologize here. I did try to contact a moderator but was unable to find one due to the issue that I didn't realize that a Guru was a mod and have been appropriately chastised for it. I'm sorry, it won't happen again. (Thought Gurus was a username, didn't understand that it was a title. -double facepalm-)

And for those users who may be viewing this with the same issues, someone in the other thread mentioned that it was actually caused by the way my code handles submissions. When a user uses a submission button it basically reloads the script, therefore causing $dialogue to = 1 every time it ran through. Thank you all for your time and patience, and thank you for your tips about = and ==. Very helpful.

Edited by Cygna

I didn't realize that a Guru was a mod

They are not ;) As josh puts it, they are people "who know their shit", and are awarded a special title to stand out. Philip said they are his minions and CF makes him sandwiches, don't try to deny it CF.

Edited by ignace
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.