Solved

Three Random Stories one page, weird table layout

Posted on 2011-02-17
3
303 Views
Last Modified: 2012-05-11
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
Comment
Question by:t3chguy
3 Comments
 
LVL 4

Accepted Solution

by:
JayDiablo earned 500 total points
ID: 34922552
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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34924563
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
 
LVL 1

Author Closing Comment

by:t3chguy
ID: 34925732
Jay, thank you so much for the help.  This worked like a charm!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now