Results 1 to 5 of 5

Thread: [RESOLVED] Fast responce $_REQUESTS

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Resolved [RESOLVED] Fast responce $_REQUESTS

    I've writen this code to remove elements from the URL string but I'm finding that I have to press the delete button twice for it to be recorded. Is there any way the code can be eddited to remove this problem?

    PHP Code:
    <form name="delrowx" method="post" action="?lastname=<?php echo "$lastname";  
                  
              
    $boxes=$_REQUEST['deleterowMe'];
                      
    $nou=0;                   //worktype list
                      
    $nov=0;            
                      while(
    $nou<($wrktpeno)){
                      if(!
    in_array("$nou",$boxes)){
                      echo 
    "&worktype[$nov]=$wrktpe[$nou]";
                      
    $nov++;}
                      
    $nou++;
                      }
                      
    $noy=0;  
                      
    $noz=0;                             //tretype list
                      
    while($noy<($rows_in_tables)){                  
                      if(!
    in_array("$noy",$boxes)){
                      echo 
    "&items[$noz]=$Items_array[$noy]";
                      
    $noz++;}
                      
    $noy++;
                      }
                  
                  
                  
    ?>">
    HTML Code:
    -- this line is in a loop showing a different $number for each item in the URL string. --
    <input type="checkbox" name="deleterowMe[<?php echo "$number"; ?>]" value="<?php echo $number; ?>" />
    
    <input type="submit" name="Deluse" value="Del" />
    
    </form>
    Thanks!

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Fast responce $_REQUESTS

    does this script that you're submitting to already have the $_GET variable populated with items[] and worktype[]? if so, there may be an easier way to just remove the things you want to delete. what does the URL you're submitting from look like? if not, where do you get $wrktpe and $items_array from?

    in the future, you might want also to come up with better variable names. ;)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: Fast responce $_REQUESTS

    PHP Code:
    if(isset($_GET['worktype']))
    $wrktpe=$_GET['worktype'];
    if(isset(
    $_GET['items']))
    $Items_array=$_GET['items']; 
    URL String=
    PHP Code:
    ?items[0]=Apple&worktype[0]=pru25&items[1]=Ash&worktype[1]=Reduce%20canopy%2025%%20improve%20shape&items[2]=Magnolia&worktype[2]=&items[0]=Apple&worktype[0]=pru25&items[1]=Ash&worktype[1]=Reduce%20canopy%2025%%20improve%20shape&items[2]=Magnolia&worktype[2]=&items[0]=Appl 

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Fast responce $_REQUESTS

    well, I hate while() loops -- especially in the way that you're using them. you could accomplish this exact same routine using a foreach loop, and in my opinion it would be much easier as well. you already have your arrays of stuff. all you need to do is loop through that information and filter out what you don't want. then, you can create a string from it. this is how I might manipulate that URL that you have there into my form's action:
    PHP Code:
    <?php
      $query 
    urldecode(getenv("QUERY_STRING"));
      echo 
    "?{$query}\n\n\n";

      
    $_POST['delete'][0] = true//simulating checkbox

      //start of url
      
    $url "?";

      
    //$title = 'items' or 'worktype'
      //$array = contents of array
      
    foreach($_GET as $title => $array){

        
    //well, GET can carry all sorts of stuff. but we don't care about any of it if it's not an array. skip non-arrays.
        
    if(!is_array($array)) continue;

        
    //$id = 0 -> 2
        //$value = value of array key
        
    foreach($array as $id => $value){

          
    //if the checkbox was NOT checked, the following variable will not be set.
          //therefor, we add to our URL. otherwise, we do nothing.
          
    if(!isset($_POST['delete'][$id])){

            
    $url .= "{$title}[{$id}]={$value}&";

          }

        }
      }
      echo 
    $url;
    ?>
    you can see it in action by visiting the following URL:
    http://davidmiles.ca/php/form_arrays...ktype&#91;2]=&

    you can also manipulate that page's URI to see how changes would update things, but it will always only delete the 0th item because the delete part was just simulated. it would, of course, be easy to make it check your own "delete" variable to see which items should not be included.

    edit: was having a few problems posting that URL. should be fixed.
    Last edited by kows; Jun 24th, 2009 at 04:03 AM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: Fast responce $_REQUESTS

    foreach command is great thanks!

Posting Permissions

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



Click Here to Expand Forum to Full Width