[Challenge] Array Reversing button

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lunaticrabic
Forum Newbie
Posts: 5
Joined: Thu Sep 10, 2009 5:19 am

[Challenge] Array Reversing button

Post 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
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: [Challenge] Array Reversing button

Post 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>";
Last edited by Weiry on Fri Sep 11, 2009 11:46 am, edited 2 times in total.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: [Challenge] Array Reversing button

Post 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>
 
lunaticrabic
Forum Newbie
Posts: 5
Joined: Thu Sep 10, 2009 5:19 am

Re: [Challenge] Array Reversing button

Post 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?
SimonMayer
Forum Commoner
Posts: 32
Joined: Wed Sep 09, 2009 6:40 pm

Re: [Challenge] Array Reversing button

Post 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>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: [Challenge] Array Reversing button

Post 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>';
?>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jeffp
Forum Newbie
Posts: 3
Joined: Fri Sep 11, 2009 12:14 pm

Re: [Challenge] Array Reversing button

Post 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>
 
Last edited by jeffp on Fri Sep 11, 2009 12:19 pm, edited 2 times in total.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: [Challenge] Array Reversing button

Post 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>
 
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: [Challenge] Array Reversing button

Post 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
jeffp
Forum Newbie
Posts: 3
Joined: Fri Sep 11, 2009 12:14 pm

Re: [Challenge] Array Reversing button

Post 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:
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: [Challenge] Array Reversing button

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: [Challenge] Array Reversing button

Post 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);
 
jeffp
Forum Newbie
Posts: 3
Joined: Fri Sep 11, 2009 12:14 pm

Re: [Challenge] Array Reversing button

Post 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.
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: [Challenge] Array Reversing button

Post 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>';
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: [Challenge] Array Reversing button

Post 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.
Post Reply