SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Nov 2002
    Location
    Dubai
    Posts
    714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multidimensional array script not displaying any output

    I wrote the script for a multidimensional array as follows. But when I open my page to see the output, I can only see a blank page and cannot see any output produced by my php script. Why is this so? Can anyone help me?


    PHP Code:
    <?php
    $products 
    = array( array( 'TIR''Tires'100 ),
                array( 
    'OIL' 'Oil' 10 ),
                array( 
    'SPK''Spark Plugs') );
    for ( 
    $row 0$row 3$row++ )
     {
     for ( 
    $column 0$column 3$column++ )
     {
      echo 
    '|'.$products[$row][$column];
    }
    echo 
    '|<br />';
    }
    ?>

  2. #2
    SitePoint Addict
    Join Date
    Sep 2004
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because you forgot to comma separate the elements in the array

    array( 'OIL' 'Oil' 10 ),

    should be

    array( 'OIL', 'Oil', 10 ),

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also if this is merely for testing purposes, you can use:

    print_r($your_array);

    which prints the entire array nicely formatted for you.
    Can't really be used for display to your users though.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2002
    Location
    Dubai
    Posts
    714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because you forgot to comma separate the elements in the array
    Thanks for pointing it out. It was so careless of me. I made another newbie error. Instead of saving my file as a php file, I had saved it as an html file. But after having saved it as a php file, I am getting the following error.

    Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' on line 3
    My line 3 is the same one where I had forgotten to put the comma. I am unable to locate where I had missed the ) as mentioned in my error statement.

    I am sorry, I did not really understand how to use print_r($your_array);

  5. #5
    SitePoint Addict
    Join Date
    Sep 2004
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
         $products 
    = array( array( 'TIR''Tires'100 ),
                      array( 
    'OIL''Oil'10 ),
                      array( 
    'SPK''Spark Plugs') );
         for ( 
    $row 0$row 3$row++ )
          {
          for ( 
    $column 0$column 3$column++ )
          {
           echo 
    '|'.$products[$row][$column];
         }
         echo 
    '|<br />';
         }
         
    ?>
    The above code worked for me

    The print_r() as
    Laoujin said is used only for testing purposes (for example to see if the elements in the array are where they are supose to be) and to use it you pass an array variable as argument (in your case the usage would be
    PHP Code:
    print_r($products
    )

  6. #6
    SitePoint Guru
    Join Date
    Nov 2002
    Location
    Dubai
    Posts
    714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My code is working now. Thank you so much for taking the trouble to answer my newbie questions.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •