#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7

    Print array within array


    Sorry all. I feel I must be over-tired if I'm overlooking this one...

    I select all with my query below. When I print out my array, only row 1 is printing. Am I missing something to print continued rows? This query DOES select both rows when ran in MySQL...

    PHP Code:
    $query mysqli_query($con,"SELECT * FROM `invoices` WHERE 1;");
    $list mysqli_fetch_array($query);
    $rows mysqli_num_rows($query);
    print_r($list); 
    Output:
    Code:
    Array ( [0] => 1 [id] => 1 [1] => 1 [category] => 1 [2] => 59.49 [amount] => 59.49 [3] => 2012-12-20 [due] => 2012-12-20 [4] => [comment] => )
    Last edited by Triple_Nothing; December 3rd, 2012 at 01:15 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    You are only processing the first row. You need a loop to get all the rows.
    PHP Code:
    $query mysqli_query($con,"SELECT * FROM `invoices` WHERE 1;");
    $rows mysqli_num_rows($query);
    while (
    $list mysqli_fetch_array($query)) {
       
    print_r($list);

    I may be missing something because I don't see and array within an array.
    Last edited by gw1500se; December 3rd, 2012 at 01:09 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Ya. That array within doesn't seem to represent, which is why I figured I am overlooking something. The rowcount is fine, n this is my loop. Should foreach() maybe be used instead? I'm used to for() and just increment row with the $i variable. *shrugz*

    PHP Code:
    for($i 1$i <= $rows$i++) {
      echo 
    "<tr>
      <td></td>
      <td>
    $list[$i][due]</td>
    </tr>"
    ;

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    I don't understand. Where is the array within? Based on what you have posted, '$list' is the only array and its elements are the columns of the row. What are you expecting to be in the table?

    P.S. When you use print_r it is much more readable if you wrap it in <pre> tags.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Well, when I run the below line, $rows comes to 2, which is correct.

    $rows = mysqli_num_rows($query);

    My current table I am selecting all from has 2 rows. The row posting for all is the first row, and posting fine. If the return value holds multiple rows. Wouldn't that return as an array within an array? First array being the entire return, and each row being its own array? So, my reference would be similar to $result[row_id][col_id] ?


    P.S. Thanks for <pre> note.
    Last edited by Triple_Nothing; December 3rd, 2012 at 01:27 PM.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    mysql_fetch_array returns an array representing the next row in your result set. You use loops to fetch all the rows. It does not return a matrix/table array.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    I think I see your confusion. '$result', as I said, contains only 1 row. Each call to 'mysqli_fetch_array' returns a single row. There is no array within array. Each additional call returns the next row and it returns null when there are no more rows to fetch. That is why I put in the while loop. In your case you need something like:
    PHP Code:
    while ($list mysqli_fetch_array($query)) {
      echo 
    "<tr>
      <td></td>
      <td>
    $list['due']</td>
    </tr>"
    ;

    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Yep. Sorry. The whole while() loop finally clicked in my brain to fetch each row. Wow. Hope I'm more awake once it's time for work...

    Thanks all.

IMN logo majestic logo threadwatch logo seochat tools logo