Log in

View Full Version : [PHP] Stcuk



loserWILL
07-03-2008, 03:31 AM
I'm stuck!

I'm just coding the frontend of my new site and I've hit my first error.

I want my news articles to alternate between three header colours. I can't do it, the colours alternate correct but three of each article comes out.

How would I make the colours alternate like that (green, blue, orange_ but only have one of each article come up?

Here's the code I'm using:

<?php
include "config.php";

$check = mysql_query("SELECT title, author, content, date FROM news ORDER BY id DESC LIMIT 0, 5") or die("nothing to display at the moment.");

while ($display = mysql_fetch_array($check)) {

$title = $display['title'];
$author = $display['author'];
$content = $display['content'];
$date = $display['date'];
echo "<h1>$title - by $author</h1>
$content<br />
<i>Posted on $date</i>

<h2>$title - by $author</h2>
$content<br />
<i>Posted on $date</i>

<h3>$title - by $author</h3>
$content<br />
<i>Posted on $date</i>";
}
?>

+rep to all help.

Verrou
07-03-2008, 06:45 AM
I'd just add an id column to the sql table and have it repeat 01, 02, 03 and then use PHP to check what id the article is. Eg: 01 = Green, 02 = Blue, 03 = Orange.

RYANNNNN
07-03-2008, 10:42 AM
It's not possible if you're gonna show 5 news items but only want 3 colours and dont want them to duplicate.

Mr Macro
07-03-2008, 11:47 AM
Yes it is ryan, Verrous idea was perfect...

Agnostic Bear
07-03-2008, 12:29 PM
I'm stuck!

I'm just coding the frontend of my new site and I've hit my first error.

I want my news articles to alternate between three header colours. I can't do it, the colours alternate correct but three of each article comes out.

How would I make the colours alternate like that (green, blue, orange_ but only have one of each article come up?

Here's the code I'm using:
+rep to all help.
Dunno if this is what you want but whatever.


<?php
include "config.php";

$check = mysql_query("SELECT title, author, content, date FROM news ORDER BY id DESC LIMIT 0, 5") or die("nothing to display at the moment.");
$i = 0;
while ($display = mysql_fetch_array($check)) {

$colour = ($i % 2) ? "background-color: #whatever" : "background-color: #a different whatever";
$title = $display['title'];
$author = $display['author'];
$content = $display['content'];
$date = $display['date'];
echo "<div style=\"$colour\"><h1>$title - by $author</h1>
$content<br />
<i>Posted on $date</i>

<h2>$title - by $author</h2>
$content<br />
<i>Posted on $date</i>

<h3>$title - by $author</h3>
$content<br />
<i>Posted on $date</i></div>";
$i++;
}
?>

loserWILL
07-03-2008, 02:13 PM
Okay, I tried adding a third colour, but it gave me an error:

Parse error: syntax error, unexpected ':' in /home/kolzy/public_html/index.php on line 220
Here's the PHP

$color = ($i % 3) ? "blue" : "green" : "orange";

Want to hide these adverts? Register an account for free!