Solved

Three Random Stories one page, weird table layout

Posted on 2011-02-17
3
309 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simplest php form 3 61
get domain with php 7 20
updating the date data 12 21
How to set php.ini variables in web.config file to make contact form work? 6 26
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…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

911 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

19 Experts available now in Live!

Get 1:1 Help Now