Web Analytics Made Easy -
StatCounter Generating $strings or arrays () from a form submit - CodingForum

Announcement

Collapse
No announcement yet.

Generating $strings or arrays () from a form submit

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generating $strings or arrays () from a form submit

    Hi all
    wonder if you can help

    on my server I have a folder full of files. I read the files into an array

    $filename_array

    From this I create a form listing the filenames and place a checkbox to one side
    Code:
    $array_loop = count($filename_array);
    if ($array_loop == 0) {echo 'No files available';
                           } else {
    				echo '<form method="post" action="';
    				echo ($_SERVER['PHP_SELF']);
    				echo '">';
    				echo '<fieldset>';
    				echo '<label for="file list">File list:</label>';
    				echo '<br/><br/>';							 								
    				
    				do {
    					echo '<input type="radio" value="' . current($filename_array) . '" name="' . key($filename_array) . '": />';						echo current($filename_array);
    					echo '<br/><br/>';																			next($filename_array);							 						
    					$array_loop = ($array_loop - 1);							  									} while ($array_loop > 0);							 							 				echo '<input name="add" type="submit" id="add" value="Send" />';
    				echo '</fieldset>';
    				echo '</form>';							 							 				   }
    This generates me a form with a list of all the files in the folder

    How ever my experiance with forms and php is that I’d then have the following

    Code:
    $line_1 = $_POST[‘0’]; // 0 being the array key (the name="" in the form)
    $line_2 = $_POST[‘1’];
    etc

    checking a box and submitting would pass the filename to the appropriate string (according to the arrays key). How ever as I’m generating the form from an unknown number of files I also need to generage the -
    $line_1 = $_POST[‘0’];
    $line_2 = $_POST[‘1’];
    lines accordingly.

    So how do you do this? or is there a way of posting only the checked items to and array where there original keys and file names (from $filename_array) are passed.eg

    $submit_array ( [0] => File One,
    [3] => File Four;
    [6] => File Seven
    );

    If you see what I mean.

    Cheers

  • #2
    Generate the entire block in a single form using checkboxes instead of radio buttons. The advantage of checkboxes is they only appear in post if they are checked. Using an HTML array will let you group them together into an indexed array with their name.
    PHP Code:
    $iFileCount count($filename_array);
    // I assumed $filename_array was like so:
    // array(0 => 'Filename 1', 1 => 'Filename 2', ...);
    if ($iFileCount 0)
    {
        
    printf("<form method=\"%s\" action=\"%s\">\n"'post'$_SERVER['SCRIPT_NAME']);
        print(
    "<fieldset>\n");
        
    printf("<legend>%s</legend>\n"'File List');
        foreach (
    $filename_array AS $filename_key => $filename_val)
        {
            
    printf("<input type=\"checkbox\" name=\"file[]\" value=\"%s\" />\n"$filename_val);
        }
        print(
    "</fieldset>\n");
        print(
    "<input type=\"submit\" value=\"Delete?\" />\n");
        print(
    "</form>\n");
    }
    else
    {
        print(
    "<p>No files to view</p>");
    }

    // Processing
    foreach ($_POST['file'] AS $file)
    {
        
    // Each file lists what was checked, I assumed that its the name

    I see no reason in this situation to retain the key for the associated filename, but if you so desire you can add it into the checkbox name at the file[%s] and add the $filename_key argument before the $filename_val in the printf.

    That what you're looking for?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

    Comment


    • #3
      Hi thanks for the reply. Not tried it out yet as not at the programming machine however I have a couple of question (which show how much of a noob I'm!)

      First you use print instead of echo. Is there a reason for this (never used print) and why has every thing in the print command been escaped?

      In the printf() you have the %s which reading the php manual states that '%' is No argument is required and 's' is the argument is treated as and presented as a string. But were and what is the argument?

      While you are correct in saying I don't need to keep the original keys from $filename_array you mention I can add it into the checkbox name at the file[%s] and add the $filename_key argument before the $filename_val in the printf

      I'm I right in assuming that the 'file[]' bit is connected to the POST array and that [] pertains to the post arrays keys and changing to:
      name=\"file[%s]\" value=\"%s\" />\n", $filename_key, $filename_val); then inserts the key.

      Cheers

      Comment


      • #4
        Originally posted by Cacus View Post
        First you use print instead of echo. Is there a reason for this (never used print) and why has every thing in the print command been escaped?
        using print or echo is a question of a habit. if someone has programmed in let's say a C language a lot, then probably one is used to print. but there's no major difference between print and echo (although somewhere in my head is memory of someone writing a difference on trap17 forums, but i really cant remember)

        you need to escape "special characters" when you are using double quotes for printing.

        you can read more here
        http://hr.php.net/manual/en/function.echo.php

        Originally posted by Cacus View Post
        In the printf() you have the %s which reading the php manual states that '%' is No argument is required and 's' is the argument is treated as and presented as a string. But were and what is the argument?
        PHP Code:
         printf("<input type=\"checkbox\" name=\"file[]\" value=\"%s\" />\n"$filename_val); 
        in this piece of code, argument that will replace %s is $filename_val


        Originally posted by Cacus View Post
        Code:
        name=\"file[%s]\" value=\"%s\" />\n", $filename_key, $filename_val);
        above piece of code will replace first %s with $filename_key, and second %s with $filename_val
        PHP Idea Factory

        Comment


        • #5
          Originally posted by funnymoney View Post
          using print or echo is a question of a habit. if someone has programmed in let's say a C language a lot, then probably one is used to print. but there's no major difference between print and echo (although somewhere in my head is memory of someone writing a difference on trap17 forums, but i really cant remember)
          speed and print return a value.

          PHP Code:
           printf("<input type=\"checkbox\" name=\"file[]\" value=\"%s\" />\n"$filename_val); 
          you can avoid escaping " by using ' to quote the format:
          PHP Code:
           printf('<input type="checkbox" name="file[]" value="%s" />\n'$filename_val); 
          best regards

          Comment


          • #6
            Thanks for the input makes more sense now!

            Comment


            • #7
              Print vs Echo:
              Print is actually a little slower than echo. Echo returns no result, while print returns a result. Its legal to assign a print to variable: $result = print "Some Stuff";, but you cannot do this with echo (which is why its technically faster). Both echo and print are language constructs. Print can also be used in a ternary operation (as true / false parts instead of resultants). Because it has a resultant to it, print would be incorporated into the precedence table as well, meaning that it holds a higher value than a few operations, including AND, XOR, OR and ,. I can't actually see a use for this though.
              [[vs]|[v]|[s]]printf are formatters. It lets you provide replacement variables in declarative order:
              PHP Code:
              $string 'lots';
              $iCount 15;
              // %d = numeric representation, %s = string representation
              printf("%d is %s items\n"$iCount$string); // 15 is lots of items

              // More interesting approach:
              $phrase = array('lots'15);
              // Argument swapping with [vs]printf, notice the order is 0 => lots, 1 => 15
              vprintf("%2$d is %1$s items\n"$phrase); // 15 is lots of items 
              Argument swapping is sweet, its the only way to handle multiple usages of the same value without repeating it in the arguments list:
              printf("%1$d * %1$d = %1$d?", 1);

              As for the escaping, I used double quotations simply because I made use of a newline char.
              PHP Code:
              header('HTTP/1.1 420 Enhance Your Calm'); 
              Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

              Comment


              • #8
                Originally posted by Fou-Lu View Post
                As for the escaping, I used double quotations simply because I made use of a newline char.
                you probably talk in general about echo and print. As far as I know in printf and friends is no problem to use escaping in format.
                good point to mention vs/v/s version of printf, usualy people avoid to use them no matter if sometime is the simplest solution,

                best regards

                Comment

                Working...
                X