Page 1 of 1

[Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 10:58 am
by lunaticrabic
Objective: Make an array consist of about 8 value and a button in a single php page.
Echo each of the array value and put a </br> between them.
When the button is clicked, the arrangement of the array will be reversed so the display of array will be reversed.
Make the script repeatable, each time of click will reverse the array.
Eg: 1st click will make the array reverse but 2nd click will make the array arrangement normal again,
3rd click make the array reverse again...etc..


More Challenge: Put the whole code into only a single php page. (Input,process,output in the same page)

This was my homework back then. :D

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 11:11 am
by Weiry
Just completed it then.
Do i need to upload the code at all, or should i wait?

Edit:
Well this was my code :D

Code: Select all

$itemArray = array(0,1,2,3,4,5,6,7);
    print "<form method='post' action=''>";
    if(isset($_POST['changeOrder']) && $_POST['set']==0){
        foreach($itemArray as $item){
            print $item."<br/>";$set = 1;}
    }else{
        foreach(array_reverse($itemArray) as $item){
            print $item."<br/>";$set = 0;}
    }
    print " <input type='hidden' name='set' value='{$set}' />
            <input type='submit' name='changeOrder' value='Reverse!' />
            </form>";

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 11:20 am
by Mirge
This better NOT be homework :P.

Anyway, just threw this together real quick.

Code: Select all

 
<?php
 
$values = array(1,2,3,4,5,6,7,8);
$prevReverse = $_GET['prevReverse'];
$reverse = 0;
 
if(strlen($prevReverse) < 1 || $prevReverse == '1') { $reverse = 0; } else { $reverse = 1; }
 
print "<pre>";
$reverse != '1' ? print_r($values) : print_r(array_reverse($values));
print "</pre>";
?>
<br/><br/>
<form action="<?=$PHP_SELF;?>" method="GET">
<input type="hidden" name="prevReverse" value="<?=$reverse;?>"/>
<input type="submit" value="Submit"/>
</form>
 

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 11:25 am
by lunaticrabic
To:Weiry
Well done..I use about 60+ line of code to did this back then Lol.

To: Mirge

I copied your code and executed on my localhost and it turned out:
Notice: Undefined index: prevReverse in D:\wamp\www\arraymirge.php on line 4

After I click the button I get :
Forbidden
You don't have permission to access /< on this server.

O.o Anything wrong with the code or perhaps my localhost problem?

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 11:37 am
by SimonMayer

Code: Select all

<?
$a = 1;
$b = 2;
$c = 3;
$d = 4;
$e = 5;
$f = 6;
$g = 7;
$h = 8;
if($_POST["reverse"] == "Backward")
{
    $arr = array($h, $g, $f, $e, $d, $c, $b, $a);
    $rev = "Forward";
}
else
{
    $arr = array($a, $b, $c, $d, $e, $f, $g, $h);
    $rev = "Backward";
}
?>
<html>
<body>
<?
for($i = 0; $i <= 7; $i++)
{
    echo $arr[$i]."<br/>";
}
?>
<form method='post'>
    <input name='reverse' type='submit' value='<? echo $rev; ?>' />
</form>
</body>
</html>

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 12:12 pm
by pickle
Here's my go:

Code: Select all

<?PHP
$array = (isset($_POST['array'])) ? unserialize($_POST['array']) : array(0,1,2,3,4,5,6,7);
$array = (isset($_POST['reverse'])) ? array_reverse($array) : $array;
foreach($array as $item){
    echo $item.'<br />';
}
echo '<form method = "post" action = ""><input type = "hidden" name = "array" value = "'.serialize($array).'"><input type = "submit" name = "reverse" value = "Reverse" /></form>';
?>

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 12:15 pm
by jeffp
Just came across this randomly while searching for something else. Hello everyone. :)

Code: Select all

 
<?php
$myarray = array("cows", "chickens", "dogs", "goats", "sheep", "yaks", "monkeys", "koalas");
$x = (isset($_POST['x'])) ? $_POST['x'] + 1 : 0;
if($x%2==0) $myarray=array_reverse($myarray);
foreach($myarray as $element):
    echo $element."<br/>";
endforeach;
?>
<form method="post" action="">
<input type="hidden" name="x" value="<?php echo $x ?>" />
<input type='submit' name='Submitted' value='Get Jiggy With It' />
</form>
 

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 12:16 pm
by Mirge
lunaticrabic wrote:To:Weiry
Well done..I use about 60+ line of code to did this back then Lol.

To: Mirge

I copied your code and executed on my localhost and it turned out:
Notice: Undefined index: prevReverse in D:\wamp\www\arraymirge.php on line 4

After I click the button I get :
Forbidden
You don't have permission to access /< on this server.

O.o Anything wrong with the code or perhaps my localhost problem?
I didn't see any warnings/errors, but try this:

Code: Select all

 
<?php
 
$values = array(1,2,3,4,5,6,7,8);
$prevReverse = isset($_GET['prevReverse']) ? $_GET['prevReverse'] : '1';
$reverse = 0;
 
if(strlen($prevReverse) < 1 || $prevReverse == '1') { $reverse = 0; } else { $reverse = 1; }
 
print "<pre>";
$reverse != '1' ? print_r($values) : print_r(array_reverse($values));
print "</pre>";
?>
<br/><br/>
<form action="" method="GET">
<input type="hidden" name="prevReverse" value="<?=$reverse;?>"/>
<input type="submit" value="Submit"/>
</form>
 

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 12:27 pm
by Mirge
Here's my 2nd submission, a bit shorter..

Code: Select all

 
<?php
$array = isset($_GET['array']) ? array_reverse(unserialize($_GET['array'])) : array(1,2,3,4,5);
print "<pre>"; print_r(array_reverse($array)); print "</pre>";
?>
<form action="" method="get">
<input type="hidden" name="array" value="<?=serialize($array);?>"/><input type="submit" value="Submit"/>
</form>
 
7 lines of code

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 12:51 pm
by jeffp
Oh, is the challenge to use as few lines as possible? I'm new here - I don't know these things.

OK, here's my second attempt:

Code: Select all

 
<?php $myarray = array("cows", "chickens", "dogs", "goats", "sheep", "yaks", "monkeys", "koalas"); $x = (isset($_POST['x'])) ? $_POST['x'] + 1 : 0; if($x%2==0) $myarray=array_reverse($myarray); foreach($myarray as $element){ echo $element."<br/>";}?><form method="post" action="rev.php"><input type="hidden" name="x" value="<?php echo $x ?>" /><input type='submit' name='Submitted' value='Get Jiggy With It' /></form>
 
1 line :lol:

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 12:53 pm
by Mirge
jeffp wrote:Oh, is the challenge to use as few lines as possible? I'm new here - I don't know these things.

OK, here's my second attempt:

Code: Select all

 
<?php $myarray = array("cows", "chickens", "dogs", "goats", "sheep", "yaks", "monkeys", "koalas"); $x = (isset($_POST['x'])) ? $_POST['x'] + 1 : 0; if($x%2==0) $myarray=array_reverse($myarray); foreach($myarray as $element){ echo $element."<br/>";}?><form method="post" action="rev.php"><input type="hidden" name="x" value="<?php echo $x ?>" /><input type='submit' name='Submitted' value='Get Jiggy With It' /></form>
 
1 line :lol:
lol nah, but anybody can make a 1-liner :P lol

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 1:08 pm
by onion2k
No need to click a button, just refresh the page. Obviously you could add a button and control whether it reverses with an if() statement if you needed to.

Code: Select all

<?php
    
    session_start();
    
    $a = array("one", "two", "three");
    
    if ($_SESSION['d']=="f") {
    
        $_SESSION['d'] = "r";
        sort($a);
    
    } else {
 
        $_SESSION['d'] = "f";
        rsort($a);
    
    }
 
    echo implode(" ",$a);
 

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 2:23 pm
by jeffp
Right onion2k, but the parameters specified a button so using post variables from a form submission seems the logical way.
lunaticrabic wrote:Objective: Make an array consist of about 8 value and a button in a single php page.
Echo each of the array value and put a </br> between them.
When the button is clicked, the arrangement of the array will be reversed so the display of array will be reversed.

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 9:55 pm
by Griven
Probably not the most efficient means of doing this, but it works.

Code: Select all

    //Define the array
$array = array('1','2','3','4','5','6','7','8');
 
    //Check to see if the 'rev' is set, and set a default if it isn't
if(isset($_GET['rev'])) {
        $rev = $_GET['rev'];
    } else {
        $rev = 0;
    }
 
    //If the 'rev' variable is set to 1, reverse the array
if($rev == 1){
    $array = array_reverse($array);
}
 
    //Echo the array
foreach($array as $value){
    echo $value ,'<br />';
}
 
       //Create the form and set the correct 'rev' variable
echo '<form name="reverse" method="get" action="', $_SERVER['PHP_SELF'] ,'">';
    if($rev > 0) {
        $rev = 0;
    } else {
        $rev = 1;
    }
echo '<input type="hidden" name="rev" value="', $rev ,'" />';
echo '<input type="submit" value="Reverse It!" />';
echo '</form>';

Re: [Challenge] Array Reversing button

Posted: Fri Sep 11, 2009 10:41 pm
by McInfo

Code: Select all

<?php
$r = isset($_GET['r']) ? ((bool) $_GET['r']) : false;
$a = range('A', 'H');
echo implode('<br />', $r ? array_reverse($a) : $a);
?>
<form><input type="submit" name="r" value="<?php printf('%u', !$r); ?>" /></form>
Edit: This post was recovered from search engine cache.