Solved

Three Random Stories one page, weird table layout

Posted on 2011-02-17
3
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

617 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