Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Three Random Stories one page, weird table layout

Posted on 2011-02-17
3
Medium Priority
?
328 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

972 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