Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Retrieving Data from array

    Hi, so I have been trying to come up with a way to do a mysql query and store the results so that I can then access them and create an html table.

    Here is the code for the query and array:

    $query = "SELECT * FROM table WHERE user_id = '" . $_SESSION['user_id'] . "'";
    $data = mysqli_query($dbc, $query);
    $array = array();
    while ($row = mysqli_fetch_array($data)) {
    array_push($array, $arr = array ($a => $row['a'], $b => $row['b'], $c => $row['c], $d => $row['d'], $e => $row['e], $f => $row['f']));
    }

    This should create a large array of arrays, depending on how many rows of data result from the query.

    Can someone help me with the code that will now take these arrays and create an html table with the results? Also needs to make sure if there are no results, nothing is produced.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Where are these $a through $f variables coming from? As is, you'd end up with a single field appended as you are trying to force it to null offset (making it irretrievable via ArrayAccess, iteration should still work).
    That's way too much work. Unless you need to actually force it on a different key, you can simply use:
    PHP Code:
    $array = array();
    while (
    $row mysql_fetch_assoc($data))
    {
        
    $array[] = $row;

    As for iterating it, a simple foreach can be done for that:
    PHP Code:
    print '<table>';
    foreach (
    $array AS $record)
    {
        print 
    '<tr>';
        foreach (
    $record AS $item)
        {
            
    printf('<td>%s</td>'$item);
        }
        print 
    '</tr>';
    }
    print 
    '</table>'
    Simple as that. Its impossible to have an empty entry from the database itself (as it would be abandoned as a non-matching entry), so there is no need to conditionally check it. Fields could be empty, but you'll need to make sure that the td's are still provided for it if you intend to use a table.

  • #3
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The variables were made just to keep track of the data, your right, it offsets it.

    I was trying to do this because there can be multiple rows of data, and wanted to be able to create a table with each data row matching with each html row.

    Thanks for your help.


  •  

    Tags for this Thread

    Posting Permissions

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