Jump to content

JonDecagon

New Members
  • Posts

    1
  • Joined

  • Last visited

JonDecagon's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I'm trying to create a code that moves a string array A B C D E around based on the amount specified entered into a field in a form and the direction which is also entered into the form. What I want to do is to make the $newarray equal $array when the page is refreshed and the submit button is hit again. That way A B C D E when moved LEFT by 1 will be B C D E A and then if RIGHT and 2 is entered, you will see E A B C D. I've been struggling to get session working and just ended up more lost. Here is the code I got so far. I would greatly appreciate any help with this. <html> <div style="border:1px solid black;font-weight:bold;"> <div style="border:none;">A B C D E</div> <form method="POST"> <input type="text" name="thebutton"/> <input type="text" name="offset"/> <input type="submit" value="SUBMIT"/> </form> </div> <?php session_start(); if (isset($_POST["offset"], $_POST["thebutton"])) { $shift=$_POST["offset"]; $direction=$_POST["thebutton"]; $counter=0; $array=array('A','B','C','D','E'); if ($direction=="LEFT"){ for($i=$shift; $counter+$shift<5; $i++,$counter++){ $newarray[$counter]=$array[$counter+$shift]; } for($j=0; $j<$shift; $j++){ $newarray[$counter + $j]=$array[$j]; } } if ($direction=="RIGHT"){ for($counter=0,$k=5-$shift; $counter<$shift; $counter++){ $newarray[$counter]=$array[$counter+$k]; } for($j=0; $j<5-$shift; $j++){ $newarray[$counter + $j]=$array[$j]; } } echo $newarray[0]; echo $newarray[1]; echo $newarray[2]; echo $newarray[3]; echo $newarray[4]; } ?> </html>
×
×
  • 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.