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 2 of 2
  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    141
    Thanks
    64
    Thanked 1 Time in 1 Post

    Create new table for each unique id

    Hi guys, have a problem I am struggling with. I have a database table which contains the fields

    id - PK
    alert_id - FK
    letter - Char
    number - int(11)
    last_updated - Timestamp

    Sql query to select the data is just a basic select
    Code:
    $sql = "SELECT * FROM `alert_availability`;
    The problem is, for each unique alert_id, I need the data displayed in a new html table. Each table should take the form of
    Code:
    table 1 <---alert_id
                                        A       B        C      <----letter
    01:00am  <--last_updated            3       2        5      <----number
    02:00am                             5       4        0
    
    
    table 2 <---alert_id
                                        F       T       R      <----letter
    01:00<--last_updated                2       5       6      <----number
    ...
    ...
    How can something like this be achieved? My other script is running on a cron job, so each alert will be updated basically every hour. The letter will always stay the same, but the numbers can change. I need to show this change, which is why I give it a new row. I can get information displaying in one table, but not to sure how to do a new table for each unique alert_id, and how to display the data as I need it.

    Any advice appreciated as always.

    Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You simply keep track of the last alert id and break the table whenever it changes. You need to ensure the query is sorted by the alert_id as well.
    PHP Code:
    $lastid null;
    printf('<table>');
    while (
    $row $qry->fetch_assoc())
    {
        if (
    $lastid != $row['alert_id'])
        {
            if (
    $lastid != null)
            {
                
    printf('</table><table>');
            }
            
    $lastid $row['alert_id'];
        }
        
    //. . .
    }
    printf('</table>'); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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