By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,044 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,044 IT Pros & Developers. It's quick & easy.

Some looping issues.

P: 77
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.
Expand|Select|Wrap|Line Numbers
  1. for ($fields=0; $fields<=9; $fields++)
  2. {
  3. echo "$titlesArr[titles]";
  4. echo    "
  5.     <tr>
  6.         <td height='35' class='boe_menu_raquo'>
  7.     <span class='boe_menu_raquo_label'> &raquo; </span>
  8.     </td> <td class='boe_link_text'>
  9.     <a href='$url1$number$part$number$number$fields'>$number.$fields — $title </a>
  10.     </td> <td>&nbsp;</td>
  11.       </tr>
  12.     ";
  13. }
$title is the text messages, though at this point, nothing appears there.
Apr 3 '10 #1
Share this Question
Share on Google+
4 Replies


P: 67
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
Apr 5 '10 #2

P: 77
Thank you, Mayur Bhayani .

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


Topic SOLVED
Apr 5 '10 #3

Markus
Expert 5K+
P: 6,050
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $members = array(
  4.     array(
  5.         'name' => 'Markus',
  6.         'role' => 'Moderator'
  7.     ),
  8.     array(
  9.         'name' => 'Atli',
  10.         'role' => 'Moderator'
  11.     )
  12. );
  13. $mem_count = count($members);
  14.  
  15. for ($i = 0; $i < $mem_count; ++$i) {
  16.     printf('Member <em>%s</em> has role <em>%s</em><br />',
  17.            $members[$i]['name'], $members[$i]['role']); 
  18.            // Notice "name" and "role" here are quoted.
  19. }
  20.  
Apr 6 '10 #4

P: 77
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.

:)
Apr 6 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.