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
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts

    How to keep messages in one table?

    Having trouble keeping messages in one table. I understand why its printing multiple tables but do not know how to keep all the messages in one table.

    Code:
    <?php
    $getmessages = mysql_query("SELECT * FROM `messages` where `to`='".$_SESSION['userid']."'");
    while($message = mysql_fetch_array($getmessages))
      {
      $getname = mysql_query("SELECT * FROM `users` where `userid`='".$message['from']."'");
    while($name = mysql_fetch_array($getname))
    {
    if ($_GET['message'] == "")
    {
    print "
    <div class='bubble'>
    <table border='1'>
    <tr>
    <th>Mark</th>
    <th>Subject</th>
    <th>From</th>
    <th>Date</th>
    </tr>
    <tr>
    <td><input type='checkbox' name='mark' value='Mark' /></td>
    <td><a href=\"/home/messages?message={$message['id']}\">{$message['title']}</a></td>
    <td>{$name['full_name']}</td>
    <td>{$message['created']}</td>
    <td>{$message['category']}</td>
    </tr>
    </table>
    </div>";
    }
    Last edited by elitis; 07-23-2011 at 11:21 PM.
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps

  • #2
    New Coder
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    80
    Thanks
    1
    Thanked 8 Times in 8 Posts
    try moving

    PHP Code:
    <div class='bubble'>
    <
    table border='1'>
    <
    tr>
    <
    th>Mark</th>
    <
    th>Subject</th>
    <
    th>From</th>
    <
    th>Date</th>
    </
    tr
    out of the while loop and up to the top of the script.

    Leave
    PHP Code:
    <tr>
    <
    td><input type='checkbox' name='mark' value='Mark' /></td>
    <
    td><a href="/home/messages?message={$message['id']}\">{$message['title']}</a></td>
    <td>{$name['full_name']}</td>
    <td>{$message['created']}</td>
    <td>{$message['category']}</td>
    </tr> 
    Inside the while loop where it's at since it's the only code that needs repeating

    And then move

    PHP Code:
    </table>
    </
    div
    out of the while loop and to the bottom of the script.

    That should create one table row for each recordset you get from your query.
    Try that and then I also suggest looking up MySQL joins for what you are trying to do. This way you get all the data fields from multiple tables in one mysql query instead of having nested while loops.

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Truffle View Post
    try moving

    PHP Code:
    <div class='bubble'>
    <
    table border='1'>
    <
    tr>
    <
    th>Mark</th>
    <
    th>Subject</th>
    <
    th>From</th>
    <
    th>Date</th>
    </
    tr
    out of the while loop and up to the top of the script.

    Leave
    PHP Code:
    <tr>
    <
    td><input type='checkbox' name='mark' value='Mark' /></td>
    <
    td><a href="/home/messages?message={$message['id']}\">{$message['title']}</a></td>
    <td>{$name['full_name']}</td>
    <td>{$message['created']}</td>
    <td>{$message['category']}</td>
    </tr> 
    Inside the while loop where it's at since it's the only code that needs repeating

    And then move

    PHP Code:
    </table>
    </
    div
    out of the while loop and to the bottom of the script.

    That should create one table row for each recordset you get from your query.
    Try that and then I also suggest looking up MySQL joins for what you are trying to do. This way you get all the data fields from multiple tables in one mysql query instead of having nested while loops.
    thanks.
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps


  •  

    Posting Permissions

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