#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Array contents - confusion


    Hi all

    I am pretty new to all this so sorry if my question is simple but I have tried playing around quite a bit with this and cannot understand what is happening inside the array.

    I have some very basic code to query a MySQL database and fill an array with the results (the variable is named result). I am having trouble understanding how to use the information from that array, or at least why one method gives the result it does.

    Using this first piece of code it succesfully populates the drop down with the 7 items from the table, this kind of proves to me my code loading the array is ok....:

    PHP Code:
    <select name="TypeSelection">
        <?php   
        
    echo '<option value="">- Please select -</option>';
        
    $cycle 0;
        while (
    $line $result[$cycle]) 
            {
            
    ?> 
            
            <option select name="typeselect" value="<?php echo $line['Type'];?>"> <?php echo $line['Type'];?> </option> 
            <?php
            $cycle
    ++;
            }
         
    ?>
    </select>
    However I need to print some of the lines from the array on a spearate part of the page also, I have tried the statements below but all of them just print the word 'Array' to the screen:

    PHP Code:
    <?php
    printf
    ($result[3]);
    $temp = ($result[3]);    
    printf ($temp);
    echo 
    $temp;
    ?>
    Please could anyone explain why I get the word array printed and not the item from the table like I get in the first code? I was thinking perhaps it is something to do with selecting the right column in the array or something but I am not sure how to achieve this

    Many thanks
    James
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    Because in the first code you are using 'echo' and in the second 'printf'. The two functions are not identical. See the documentation. Also, we have no way to know how you initialized '$result' since you didn't show it but variable typing may also be an issue.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,660
    Rep Power
    4123
    $result is a 2 dimensional array.

    This means that $result[3] is, it's self, an array and echo-ing it will produce 'Array'

    In your first example you are assigning this array value to $line, and then using $line['Type'] (not echo $line)

    If you want to use the values in $result[3] you will have to use notation like

    PHP Code:
    echo $result[3]['Type']; 
    by doing
    PHP Code:
    print_r($result);
    //and
    print_r($result[3]); 
    you will see the differences and structures of these two arrays

    {hint - when using print_r look in the source or wrap in <pre> tags to see the plain text formatting and easy to read structure}

    {hint - print_r and var_dump are debugging functions - not intended for use as production or display tools}
    Last edited by Northie; April 18th, 2013 at 07:21 AM.
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Hi Northie

    Thank you so much for taking the time to reply in such detail, certainly cleared it up for me!

    I have just tried a couple of tests after your tips and all seems to work now!

    Much appreciated
    James

IMN logo majestic logo threadwatch logo seochat tools logo