Jump to content

[SOLVED] using 2 loops with MAX()/MIN()


rbragg

Recommended Posts

I've managed to get the results that I would like for $queryLastReply:

 

<?php 
# this query groups subjects together and displays the last post of each thread to show the last reply
$queryLastReply = "
SELECT actionID, name, subjectID, subject, timestamp
FROM onetable
WHERE subjectID = '". $first['subjectID'] ."'
ORDER BY actionID DESC
LIMIT 1
";
?>

 

This successfully gives me the last post of each thread.

 

However, I'm having trouble with consistently getting the first post for each thread with $queryFirst. Most show the first post in the thread but a few will show the last post. I cannot find the common denominator between those threads:

 

<?php 
# this query groups subjects together and displays the first post of each thread
$queryFirst = "
SELECT MIN(actionID), name, subjectID, subject, timestamp
FROM onetable
GROUP BY subjectID
ORDER BY subjectID DESC 
";
?>

Link to comment
Share on other sites

how's it goin rbragg? see if this works for ya:

$queryFirst = 
                "SELECT
                        actionID,
                        name,
                        subjectID,
                        subject,
                        timestamp
                FROM
                        onetable
                WHERE
                        subjectID = '". $first['subjectID'] ."'
                ORDER BY
                        actionID
                ASC
                LIMIT 1
";

Link to comment
Share on other sites

None of the queries posted resemble the code I suggested to get first and last post

SELECT t.name, t.subject, t.msg  FROM mytablename t
INNER JOIN 
           (SELECT subject, MIN(timestamp ) as date FROM mytablename GROUP BY subject
            UNION 
            SELECT subject, MAX(timestamp ) as date FROM mytablename GROUP BY subject) as a
ON t.subject= a.subject and t.timestamp = a.date
ORDER BY t.subject, t.timestamp

Link to comment
Share on other sites

I did try this. ;D However, since the result was nowhere close to working I reverted back to what seemed to be very close to working.

 

ON t.subject= a.subject and t.timestamp = a.date

 

I have only one table so am not sure how to approach this part of the query. Also, I am finding the MIN/MAX actionID instead of timestamp/date.

Link to comment
Share on other sites

I'm sorry, I was assuming that the t. and a. were referencing separate tables. I was being stubborn about that. Here was my attempt at this join:

 

<?php
$queryPosts = "
SELECT t.name, t.subject, t.timestamp FROM onetable t
INNER JOIN
  (SELECT subjectID, MIN(actionID) as id FROM onetable GROUP BY subjectID
  UNION
  SELECT subjectID, MAX(actionID) as id FROM onetable GROUP BY subjectID ) as a
ON t.subjectID = a.subjectID and t.actionID = a.id
ORDER BY t.subjectID DESC
";
$postResults = mysql_query($queryPosts) or die('The get posts query failed: ' . mysql_error());

echo "<form name='form_display' method='POST' action='forumDetail.php'>";
echo "<table width='100%' border='0' cellspacing='2' cellpadding='2'>
<tr bgcolor='#616161'><td width='8%'><input name='view' type='submit' class='style3' value='view'></td>
<td class='style6'>Message</td><td class='style6'>Started by</td><td class='style6'>Date & Time Started</td></tr>"; 

while ($post = mysql_fetch_array($postResults))
{
  echo "<tr><td align='center' class='style3'>";
  ?>
  <input type='radio' name='selected' value='<?php echo $post['subjectID'];?>'>
  <?php
  echo "</td><td class='style8'>";
  echo $post['subject'];
  echo "</td><td class='style3'>";
  echo $post['name'];
  echo "</td><td class='style3'>";   
  echo $post['timestamp'];
  echo "</td></tr>";
}
echo "</table>";
echo "</form>";
?>

 

This gives the results that it is indeed asking for. This type of query is new to me so I'm still trying to wrap my brain around it.

 

I only have a small problem, now, of trying to think up a cosmetic trick. Of course, I would like first and last posts to be grouped together with an indention before the last post. Also, all of the posts have radiobuttons in front of them and I'd like for only the first post to have a radiobutton.  I'm not sure of how I can achieve these effects since they are being displayed within a loop. ???

Link to comment
Share on other sites

Fairly simple (pseudocode below)

prevSubject = '';

while (fetch next row) {

       IF subjectID != prevSubject
             output radio button in first cell
             prevSubject = subjectID
       else
            putput blank in first cell
      end if

      output rest of cells in row

end while
             
       

Link to comment
Share on other sites

Good morning, Barand. I get the general idea except for the $prevSubject variable. I'm not sure how I would set it:

 

<?php
$prevSubject = '';

while ($post = mysql_fetch_array($postResults))
{
  if ($post['subjectID'] != $prevSubject)
  {
    echo "<tr><td align='center' class='style3'>";
    ?>
    <input type='radio' name='selected' value='<?php echo $post['subjectID'];?>'>
    <?php
    echo "</td>";
  }
  else
  {
    echo "<tr><td align='center' class='style3'> </td>";
  }
  echo "<td class='style8'>";
  echo $post['subject'];
  echo "</td><td class='style3'>";
  echo $post['name'];
  echo "</td><td class='style3'>";   
  echo $post['timestamp'];
  echo "</td></tr>";
  } # close while
?>

Link to comment
Share on other sites

You can set it just about anywhere in the loop AFTER you have tested its value

 

while ($post = mysql_fetch_array($postResults))
{
  if ($post['subjectID'] != $prevSubject)
  {
    echo "<tr><td align='center' class='style3'>";
    ?>
    <input type='radio' name='selected' value='<?php echo $post['subjectID'];?>'>
    <?php
    echo "</td>";
  }
  else
  {
    echo "<tr><td align='center' class='style3'> </td>";
  }
  $prevSubject = $_POST['subjectID'];                 // such as here
  echo "<td class='style8'>";
  echo $post['subject'];
  echo "</td><td class='style3'>";
  echo $post['name'];
  echo "</td><td class='style3'>";   
  echo $post['timestamp'];
  echo "</td></tr>";
  } # close while
?>

Link to comment
Share on other sites

Ah. $subjectID is not yet stored in the POST array. This is the first page in the process. It just displays all of the current threads and a glimpse of the last reply to each thread. I have to find a way to set $prevSubject.

 

I hope I am following you.

Link to comment
Share on other sites

;) I thought that may have been the case. I tried that and I get rbs before every subject. How would $prevSubject know whether or not to get the subjectID from the MIN or MAX?

 

I've attached a screencap:

 

[attachment deleted by admin]

Link to comment
Share on other sites

Sure:

 

<?php
include 'dbConnect.php';

$queryPosts = "
SELECT t.name, t.subject, t.timestamp FROM onetable t
INNER JOIN
  (SELECT subjectID, MIN(actionID) as id FROM onetable GROUP BY subjectID
  UNION
  SELECT subjectID, MAX(actionID) as id FROM onetable GROUP BY subjectID ) as a
ON t.subjectID = a.subjectID and t.actionID = a.id
ORDER BY t.subjectID DESC
";
$postResults = mysql_query($queryPosts) or die('The post query failed: ' . mysql_error());

echo "<form name='form_display' method='POST' action='forumDetail.php'>";
echo "<table width='100%' border='0' cellspacing='2' cellpadding='2'>
<tr bgcolor='#616161'><td width='8%'><input name='view' type='submit' class='style3' value='view'></td>
<td class='style6'>Message</td><td class='style6'>Started by</td><td class='style6'>Date & Time Started</td></tr>"; 

while ($post = mysql_fetch_array($postResults))
{
  $prevSubject = $post['subjectID'];

  if ($post['subjectID'] != $prevSubject)
  {
    echo "<tr><td align='center' class='style3'>";
    ?>
    <input type='radio' name='selected' value='<?php echo $post['subjectID'];?>'>
    <?php
    echo "</td>";
  }
  else
  {
    echo "<tr><td align='center' class='style3'> </td>";
  }

  echo "<td class='style8'>";
  echo $post['subject'];
  echo "</td><td class='style3'>";
  echo $post['name'];
  echo "</td><td class='style3'>";   
  echo $post['timestamp'];
  echo "</td></tr>";
}
echo "</table>";
echo "</form>";
mysql_close;
?>

Link to comment
Share on other sites

Made a couple of changes, commented

<?php
include 'dbConnect.php';

$queryPosts = "
SELECT t.subjectID, t.name, t.subject, t.timestamp FROM onetable t      
INNER JOIN
  (SELECT subjectID, MIN(actionID) as id FROM onetable GROUP BY subjectID
  UNION
  SELECT subjectID, MAX(actionID) as id FROM onetable GROUP BY subjectID ) as a
ON t.subjectID = a.subjectID and t.actionID = a.id
ORDER BY t.subjectID DESC
";                                                                              //added subjectID to selection ***
$postResults = mysql_query($queryPosts) or die('The post query failed: ' . mysql_error());

echo "<form name='form_display' method='POST' action='forumDetail.php'>";
echo "<table width='100%' border='0' cellspacing='2' cellpadding='2'>
<tr bgcolor='#616161'><td width='8%'><input name='view' type='submit' class='style3' value='view'></td>
<td class='style6'>Message</td><td class='style6'>Started by</td><td class='style6'>Date & Time Started</td></tr>"; 

$prevSubject = '';						
while ($post = mysql_fetch_array($postResults))
{

  if ($post['subjectID'] != $prevSubject)
  {
    echo "<tr><td align='center' class='style3'>";
    ?>
    <input type='radio' name='selected' value='<?php echo $post['subjectID'];?>'>
    <?php
    echo "</td>";
  }
  else
  {
    echo "<tr><td align='center' class='style3'> </td>";
  }

  $prevSubject = $post['subjectID'];                          // moved to here   ***

  echo "<td class='style8'>";
  echo $post['subject'];
  echo "</td><td class='style3'>";
  echo $post['name'];
  echo "</td><td class='style3'>";   
  echo $post['timestamp'];
  echo "</td></tr>";
}
echo "</table>";
echo "</form>";
mysql_close;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.