Web Analytics Made Easy -
StatCounter variable name? - CodingForum

Announcement

Collapse
No announcement yet.

variable name?

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

  • variable name?

    This code has error: Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING'


    Code:
        for ($k=0; $k<2; $k++)
        {
            $m = $k+1;
            echo "$row["test_name{$m}"]";
         }
    What am i miising?
    I am expecting
    test_name1, test_name2, test_name3....

  • #2
    Try
    Code:
        for ($k=0; $k<2; $k++)
        {
            $m = $k+1;
            echo "\$row[\"test_name{$m}\"]";
         }
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

    Comment


    • #3
      Thank you for your kindness.

      Originally posted by hemebond
      Try
      Code:
          for ($k=0; $k<2; $k++)
          {
              $m = $k+1;
              echo "\$row[\"test_name{$m}\"]";
           }
      I've ommitted one line.
      code is
      Code:
          for ($k=0; $k<2; $k++)
          {
              $m = $k+1;
              $test_name = "gymnagis_kdf";
              echo "\$row[\"$test_name{$m}\"]";
           }
      Thus, my expecting is;
      gymnagis_kdf1, gymnagis_kdf2, gymnagis_kdf3....

      Comment


      • #4
        PHP Code:
        for ($k=0$k<2$k++)
            {
                
        $m $k+1;
                
        $test_name 'gymnagis_kdf' $m;
             } 
        Instead of escaping characters, besides its faster.

        Comment


        • #5
          Thanks Kurashu,

          But this loop is nested.

          Code:
          while ($row=mysql_fetch_array($result))
          {
          .....
              for ($k=0; $k<2; $k++)
              {
                  $m = $k+1;
                  echo "$row["$test_name{$m}"]";
               }
          ........
          }

          Comment


          • #6
            Code:
            while ($row=mysql_fetch_array($result))
            {
            ...
                foreach $k (1..3)
                {
                    echo $row[$test_name$k];
                }
            ...
            }

            /edit: oh crap. this is the php forum. sorry :-/

            Comment


            • #7
              joeframbach,

              Thanks,
              I will try.

              ^^

              Comment


              • #8
                sorry, i seem to mix perl and php a lot lately. i've been going back and forth with the languages at work and its driving my batty.

                Comment


                • #9
                  PHP Code:
                  while ($row=mysql_fetch_array($result))
                  {
                      for (
                  $k=0$k<2$k++)
                      {
                          
                  $m $k+1;
                          echo 
                  $row['test_name' $m];
                       }

                  That is what you really want, if I am understanding you correctly.

                  Comment


                  • #10
                    why not ditch the $m=$k+1 and make the loop iterate from 1 to 3?

                    Comment

                    Working...
                    X