• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

Three Random Stories one page, weird table layout

I have a query that pulls three random news stories from a table, and displays them on the page.  The random order works perfectly if i have one per row in a table, or put a line break between each one.  The problem is that i want two of the stories to be side by side with the third story being underneath them in a new row that spans across both columns.

When I do this, however, all three stories are the same, which obviously is not what I'm trying to do.

Screenshot of page and code is attached. Screenshot
<?php 
//News Index page
require_once($_SERVER['DOCUMENT_ROOT']."/includes/verifyaccess.php");
if ($permgrpid == '0' or $permgrpid == '')
	{
	header("Location: login.php?error=unauthaccess");
	die;
	}

require_once($_SERVER['DOCUMENT_ROOT']."/includes/header.php");

$interval = 240;

$compquery = "";
$stickysql = "";
if (isset($sticky)) $stickysql = " and newsid != " . $sticky;
$randhour = (date("j") * 100 + date("g"));

$query = "SELECT DISTINCT(news_stories.newsid), news_stories.title, news_stories.story, news_stories.date, news_stories.author, news_stories.image, news_stories.apprauth, 
			news_stories.apprmod, news_comments.date, news_comments.sentby, news_comments.newsitem, news_comments.comment FROM news_stories	LEFT JOIN news_comments ON news_stories.newsid = news_comments.newsitem WHERE DATE_SUB(CURDATE(),INTERVAL $interval DAY) <= news_stories.date AND image = '1' AND apprmod = '1' AND apprauth = '1' group by news_stories.newsid order by rand() limit 0,1";

$res = mysql_query($query) or die (mysql_error());

//echo $query . "<br />";

echo "<table width='95%' align='center' cellspacing = '3' cellpadding='3'>";

while ($row = mysql_fetch_array($res))
	{
	$newsid = $row['newsid'];
	$newsid2 = $row['newsid'];
	$newsid3 = $row['newsid'];
	$title = $row['title'];
	$story = $row['story'];
	$date = $row['date'];
	$author = $row['author'];
	$image = $row['image'];
	$commentdate = $row['date'];
	$sentby = $row['sentby'];
	$commentnewsitem = $row['newsitem'];
	$comment = $row['comment'];
	
	echo "<tr>";
	echo "<td><span class='newsheadline'>" . fixQuotes($title) . "</span><br />" . fixQuotes($story) . "</td>";
	echo "<td><span class='newsheadline'>" . fixQuotes($title) . "</span><br />" . fixQuotes($story) . "</td>";
	echo "</tr>";
	echo "<tr><td> &nbsp; </td></tr>";
	echo "<tr><td colspan = '2'><span class='newsheadline'>" . fixQuotes($title) . "</span><br />" . fixQuotes($story) . "</td></tr>";
	}

echo "</table>";
?>

Open in new window

0
t3chguy
Asked:
t3chguy
1 Solution
 
JayDiabloCommented:
If you know you'll always have 3 stories, and always want the one on the bottom to have a colspan=2 then you should be able to modify your PHP code to deal with that.

First, change your SQL query's limit to "0,3" (I assume you've done that already, and it's now "0,1" for testing).

Now, keep a counter variable available so we can tell how many rows it's already displayed:

// counter variable
$i = 0;

while ($row = mysql_fetch_array($res))

Open in new window


We'll increment this variable on each iteration of the while loop, which you'll see later.

Now that we have this counter, it's easy to tell which news story it's on, we just need to look at the counter, if it is equal to "2" we know it's on the third story (since it started on 0, the third iteration makes it 2).  We can also use this counter to know whether we should be displaying a new <tr> or not.  Like so:

while ($row = mysql_fetch_array($res)) {
        $newsid = $row['newsid'];
        $newsid2 = $row['newsid'];
        $newsid3 = $row['newsid'];
        $title = $row['title'];
        $story = $row['story'];
        $date = $row['date'];
        $author = $row['author'];
        $image = $row['image'];
        $commentdate = $row['date'];
        $sentby = $row['sentby'];
        $commentnewsitem = $row['newsitem'];
        $comment = $row['comment'];
        
        // Only display the <tr> on first and third stories
        if ($i == 0 || $i == 2) {
          echo "<tr>";
        }
        
        // If we're on the third story, set the colspan to 2, otherwise leave alone
        if ($i == 2) {
          $colspan = 'colspan="2"';
        } else {
          $colspan = '';
        }
        echo "<td $colspan><span class='newsheadline'>" . fixQuotes($title) . "</span><br />" . fixQuotes($story) . "</td>";

        //  Only close the TR on 2nd or 3rd stories
        if ($i == 1 || $i == 2) {
          echo "</tr>";
        }
        // Only insert this spacer after story 2
        if ($i == 1) {
          echo "<tr><td colspan='2'> &nbsp; </td></tr>";
        }

        // Increment the counter
        $i++;
}

Open in new window


That should do what you're looking for.

If you're feeling ambitious, you could lay all of this out with pure CSS, and not have to worry about all of this, but that can be tricky as well, I'll leave that for you to explore on your own. ;)
0
 
Lukasz ChmielewskiCommented:
This

      echo "<tr><td> &nbsp; </td></tr>";
      echo "<tr><td colspan = '2'>

the first td cell needs to be colspan = 2 also

      echo "<tr><td colspan = '2'> &nbsp; </td></tr>";
      echo "<tr><td colspan = '2'>
0
 
t3chguyAuthor Commented:
Jay, thank you so much for the help.  This worked like a charm!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now