Some looping issues.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PHPstarter
    New Member
    • Jun 2009
    • 77

    Some looping issues.

    Heya.

    I have a loop which generates a table code for a specific number of times.
    What I'm having problems with is to echo a variable inside the loop.

    The loop runs 10 times, and there are 10 text messages sent to the page, so my problem is how do I get each of the looped tables to echo one of the text messages each time?

    Like loop 0 echoes text message 0, loop 1 echoes text message 1 and so on.

    This is the code for the table loops.
    Code:
    for ($fields=0; $fields<=9; $fields++)
    {
    echo "$titlesArr[titles]";
    echo	"
    	<tr>
        	<td height='35' class='boe_menu_raquo'>
    	<span class='boe_menu_raquo_label'> &raquo; </span>
    	</td> <td class='boe_link_text'>
    	<a href='$url1$number$part$number$number$fields'>$number.$fields — $title </a>
    	</td> <td>&nbsp;</td>
      	</tr>
    	";
    }
    $title is the text messages, though at this point, nothing appears there.
  • Mayur2007
    New Member
    • Aug 2007
    • 67

    #2
    Hello,

    You have to take two dimensional array for title. And need to change this line
    echo "$titlesArr[titles]"; by echo $titlesArr[$fields][titles];

    Hope this will help you..

    Thanks & Regards,
    Mayur Bhayani

    Comment

    • PHPstarter
      New Member
      • Jun 2009
      • 77

      #3
      Thank you, Mayur Bhayani .

      This works perfectly for me!
      I'm glad, now I learned something new ^^.


      Topic SOLVED

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Hello, PHPstarter.

        A quick side-note: when you're accessing an index of an array that has a string for its key, be sure to wrap that string in quotes! Turn on error_reporting () to see why.

        For example:
        Code:
        <?php
        
        $members = array(
            array(
                'name' => 'Markus',
                'role' => 'Moderator'
            ),
            array(
                'name' => 'Atli',
                'role' => 'Moderator'
            )
        );
        $mem_count = count($members);
        
        for ($i = 0; $i < $mem_count; ++$i) {
            printf('Member <em>%s</em> has role <em>%s</em><br />',
                   $members[$i]['name'], $members[$i]['role']); 
                   // Notice "name" and "role" here are quoted.
        }

        Comment

        • PHPstarter
          New Member
          • Jun 2009
          • 77

          #5
          Thanks for clearing that up.
          I've seen examples with and without single quotes inside the string, so I was not really sure about it.

          :)

          Comment

          Working...