SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Busch's Avatar
    Join Date
    Jan 2004
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    dealing with some arrays

    i am trying to alter some code but am stumped with an array problem.

    the original code:
    PHP Code:
    $direction = array("N""S""E""W"); 
    i want to give the user a choice of the 4 directions so i made some checkboxes:
    Code:
    <input type="checkbox" name="nsew[]" value="N">North<br />
    <input type="checkbox" name="nsew[]" value="S">South<br />
    <input type="checkbox" name="nsew[]" value="E">East<br />
    <input type="checkbox" name="nsew[]" value="W">West<br />
    and now here's where im stumped. if i do this:
    PHP Code:
    $nsew $_POST["nsew"];
    $direction $nsew
    the $direction is basically just being set to the first value of $nsew. all other things i do renders worse results.


    so how can i duplicate the origainl code functionality while still giving the user the ability to choose N, S, E and/or W?

    thanks
    eric

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what you want to do with the submitted directions, but $_POST['nswe'] will be an array (if some were checked). So here's an example which builds a $direction array and also echos/debugs the submitted directions:
    PHP Code:
    if(!empty($_POST['nsew']))
    {
      
    $direction = array();
      foreach(
    $_POST['nsew'] as $dir)
      {
        echo 
    'Debug: '.$dir.'<br />';
        
    $direction[] = $dir;  
      }


  3. #3
    SitePoint Wizard Busch's Avatar
    Join Date
    Jan 2004
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks. i think i see, from your example, what i was doing wrong. got to eat. test later.

  4. #4
    SitePoint Wizard Busch's Avatar
    Join Date
    Jan 2004
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, still no luck though i know i have the data i need stored in an array.

    here's the original code i am trying to emulate where the direction values are already set:
    PHP Code:
    function fillBoard(){
      
    //fill board with list by calling addWord() for each word
      //or return false if failed
      
      
    global $word;
      
    $direction = array("N""S""E""W");
      
    $itWorked TRUE;
      
    $counter 0;
      
    $keepGoing TRUE;
      while(
    $keepGoing){
        
    $dir rand(03);
        
    $result addWord($word[$counter], $direction[$dir]);
        if (
    $result == FALSE){
          
    //print "failed to place $word[$counter]";
          
    $keepGoing FALSE;
          
    $itWorked FALSE;
        } 
    // end if
        
    $counter++;
        if (
    $counter >= count($word)){
          
    $keepGoing FALSE;
        } 
    // end if
      
    // end while
      
    return $itWorked;
      
    // end fillBoard 
    my modified code to allow the user to select their own directions (BTW, this is for a word search game):
    PHP Code:
    if(!empty($_POST['nsew']))
    {
      
    $direction = array();
      foreach(
    $_POST['nsew'] as $get_direction)
      {
        
    //echo 'Debug: '.$get_direction.'<br />';
        
    $direction[] = $get_direction;  
      }


    $nsew $_POST["nsew"];
    $number_of_directions = (count($nsew) - 1);

    ...

    function 
    fillBoard(){
      
    //fill board with list by calling addWord() for each word
      //or return false if failed
      
      
    global $word$direction;
      
    //$direction = $nsew;
      
    $itWorked TRUE;
      
    $counter 0;
      
    $keepGoing TRUE;
      while(
    $keepGoing){
        
    $dir rand(0$number_of_directions);
        
    $result addWord($word[$counter], $direction[$dir]);
        if (
    $result == FALSE){
          
    //print "failed to place $word[$counter]";
          
    $keepGoing FALSE;
          
    $itWorked FALSE;
        } 
    // end if
        
    $counter++;
        if (
    $counter >= count($word)){
          
    $keepGoing FALSE;
        } 
    // end if
      
    // end while
      
    return $itWorked;
      
    // end fillBoard 
    maybe some fresh eyes can help me out because i can't see why this won't work.

  5. #5
    SitePoint Wizard Busch's Avatar
    Join Date
    Jan 2004
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nevermind, the problem was not in the array but i stpidly forgot to declare another important global variable.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •