Foreach() Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Blaine HIlton

    Foreach() Question

    I've been trying for 2 days now to solve this problem, which should be
    pretty simple, but it is turning off to not be as simple.

    I am trying to pull data out from a form that was POSTed on a previous
    page. The code that I have that works is below:

    <?php
    for ($i = 2; $i <= $stoped; $i++) {
    echo '<tr>'
    .'<td>Step '."$i".'</td>'
    .'<td>Input X</td>'
    .'</tr>' ;
    }

    What I need to do is replace "Input X" with $step1, $step 2, ect..
    until $stoped is reached. Before this code I added lines such

    $step2 = $_POST["step2"] ;
    $step3 = $_POST["step3"] ;
    $step4 = $_POST["step4"] ;

    to try to isolate the problem. I would perfer to be able to put
    $_POST["step2"] directly in the for loop. All of my tries though
    ened up with me just printing the line and not pulling the data from
    the form.

    I was also looking at foreach() and thought this might be what I am
    looking for. Something that will loop through the $_POST array.
    However what I have tried so far has not worked.

    --
    Any help would be greatly appreciated
    Thanks
    Blaine Hilton
  • André Næss

    #2
    Re: Foreach() Question

    Filippov Alexander:
    [color=blue]
    > P.S.: Don't use "echo" - it's a bad style, use "print" - it's better.
    > Or, you can just close PHP tags and write html.[/color]

    Huh? What's bad style about echo?

    André Næss

    Comment

    • uws

      #3
      Re: Foreach() Question

      I <5b1f1930.03070 60229.4c90a64a@ posting.google. com>, Filippov Alexander skrev:[color=blue]
      > P.S.: Don't use "echo" - it's a bad style, use "print" - it's better.[/color]

      Why is `print' better? print() always returns true, echo doesn't return
      anything. AFAIK that's the only difference.

      mvrgr, Wouter

      --
      uws mail uws@xs4all.nl

      but i must be too dumb to be proud :: coz i waited -- our lady peace

      Comment

      • Blaine HIlton

        #4
        Re: Foreach() Question

        Thanks alot for that advice, it really worked well. I am using your
        code now and have come up with another problem that perhaps you can
        solve. The code I am using is:

        $i = -1 ;
        foreach ($_POST as $key => $value) {
        If (strlen($value) >0 AND $key != "title" AND $key != "author"
        AND $key != "submit" ) {
        $E->writerange("sh eet1","A1","$va lue");
        }
        $i++;
        }


        What I need to do is loop through all of the POST data and write it to
        the sheet in cells that go A1, then A2, then B1, B2, C1, C2, D1, D2,
        ect.... All the way to 100 possibilities. How would I even begin to
        replace A1 in the line:

        $E->writerange("sh eet1","A1","$va lue");

        with something that will change like that?

        --
        Thanks again
        Blaine Hilton

        PS. Also can you tell me more why you think print is better then echo
        ?




        On 6 Jul 2003 03:29:51 -0700, alexander@filip pov.net (Filippov
        Alexander) wrote:
        [color=blue]
        >Hi,
        >
        >Try this:
        >
        >$i = 2;
        >foreach ($_POST as $key => $value)
        >{
        > print '<tr>'.'<td>Ste p '.$i.'</td>'.'<td>'.$va lue.'</td>'.'</tr>';
        > $i++;
        >}
        >
        >This should work - if not, write me, we'll make it work :)
        >
        >P.S.: Don't use "echo" - it's a bad style, use "print" - it's better.
        >Or, you can just close PHP tags and write html.
        >
        >---
        >Kindest regards,
        >Filippov Alexander
        >
        >mailto: alexander@filip pov.net
        >http://www.filippov.net[/color]

        Comment

        • Ian.H [dS]

          #5
          Re: Foreach() Question

          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA1

          Whilst lounging around on Sun, 6 Jul 2003 13:42:20 +0200, uws
          <uws@xs4all.inv alid> amazingly managed to produce the following with
          their Etch-A-Sketch:
          [color=blue]
          > I <5b1f1930.03070 60229.4c90a64a@ posting.google. com>, Filippov
          > Alexander skrev:[color=green]
          > > P.S.: Don't use "echo" - it's a bad style, use "print" - it's
          > > better.[/color]
          >
          > Why is `print' better? print() always returns true, echo doesn't
          > return anything. AFAIK that's the only difference.
          >
          > mvrgr, Wouter[/color]


          And because if this, is actually faster (although you'll probably
          never physically see the difference without benchmarking).



          Regards,

          Ian

          -----BEGIN PGP SIGNATURE-----
          Version: PGP 8.0

          iQA/AwUBPwhVk2fqtj2 51CDhEQKhNQCgqX aly3paaZDMvRTT1 0TNqM+X7+AAni4h
          EQVEdGtMmpUJl5j J/asEtTsw
          =O7xs
          -----END PGP SIGNATURE-----

          --
          Ian.H [Design & Development]
          digiServ Network - Web solutions
          www.digiserv.net | irc.digiserv.ne t | forum.digiserv. net
          Programming, Web design, development & hosting.

          Comment

          • uws

            #6
            Re: Foreach() Question

            I <gblggvoedckafr loifgf1nn1u1nta m8tu4@4ax.com>, Ian.H [dS] skrev:[color=blue][color=green]
            >> Why is `print' better? print() always returns true, echo doesn't
            >> return anything. AFAIK that's the only difference.[/color]
            > And because if this, is actually faster (although you'll probably
            > never physically see the difference without benchmarking).[/color]

            I only use print() for debugging.

            <?php $R = mysql_query("SE LECT ..."); ?>

            becomes:

            <?php $R = mysql_query(pri nt "SELECT ..."); ?>

            or else the code will break.

            mvrgr, Wouter

            --
            uws mail uws@xs4all.nl

            no more kill :: no more kill i'm in love -- black rebel motorcycle club

            Comment

            Working...