Jump to content

Archived

This topic is now archived and is closed to further replies.

cameeob2003

Using the while statment

Recommended Posts

I am trying to use the following code and get 2 different posts from my MySQL database:

[code]<?php
switch ($_GET['page']){
default:
case "home":
$sql = "SELECT * FROM `news` order by `id` DESC LIMIT 0,3";
$result    = mysql_query($sql) or die(mysql_error());
$num    = mysql_num_rows($result);
while ($text = mysql_Fetch_array($result)) {
    $id = $text['id'];
    $title = $text['title'];
    $date = $text['date'];
    $author = $text['author'];
    $content = $text['content'];

echo '<table border="0" cellpadding="0" cellspacing="0"><td background="images/news_1.jpg" height="19" align="left" valign="bottom" width="652">'. $title .'</td></tr>
<tr>
<td align="left" background="images/news_2.jpg">'. $content .'</td>
</tr>
<tr>
<td background="images/news_3.jpg" height="17"></td>
</tr>';
echo '</table>';
echo '<table border="0" cellpadding="0" cellspacing="0"><td background="images/news_4.jpg" height="19" align="left" valign="bottom" width="652">'. $title .'</td></tr>
<tr>
<td align="left" background="images/news_2.jpg">'. $content .'</td>
</tr>
<tr>
<td><img src="images/news_6.jpg" width="652" height="17" alt=""></td>
</tr>';
echo '</table>';}
break;}
?>[/code]

But when it outputs it displays 2 of each post from my news table. Is there a way to make this only display 1 of each news post while using the following html output?

Share this post


Link to post
Share on other sites
It seems that you currently have 2 news items, it could be solved by changing your SQL statement:
$sql = "SELECT * FROM `news` order by `id` DESC LIMIT 1";

Share this post


Link to post
Share on other sites
Well, you are getting two of each because you're displaying each record twice in two different tables.

From your question, I'm not exactly sure how you want to display the records.

Ken

Share this post


Link to post
Share on other sites
Not really sure what your trying to accomplish here...what you're asking and what your code does are not really the same.

I did clean it up some for you:

[code]switch ($_GET['page']){
default:
case "home":
$sql = "SELECT * FROM `news` order by `id` DESC LIMIT 3";
$result = mysql_query($sql) or die(mysql_error());

while ($text = mysql_Fetch_array($result)) {
$id = $text['id'];
$title = $text['title'];
$date = $text['date'];
$author = $text['author'];
$content = $text['content'];

echo '
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td background="images/news_1.jpg" height="19" align="left" valign="bottom" width="652">'. $title .'</td>
</tr>
<tr>
<td align="left" background="images/news_2.jpg">'. $content .'</td>
</tr>
<tr>
<td background="images/news_3.jpg" height="17"></td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td background="images/news_4.jpg" height="19" align="left" valign="bottom" width="652">'. $title .'</td>
</tr>
<tr>
<td align="left" background="images/news_2.jpg">'. $content .'</td>
</tr>
<tr>
<td><img src="images/news_6.jpg" width="652" height="17" alt=""></td>
</tr>
</table>';
}

break;
}[/code]

Share this post


Link to post
Share on other sites
this is an example of what i am doing:

[url=http://n2p.ventgaming.com/for%20ref/image%20layout/index.php]http://n2p.ventgaming.com/for%20ref/image%20layout/index.php[/url]

I want the first one in the newsection to be
"Blue and Silver"
the next one
"Orange and Silver"
the one after that
"Blue and Silver"
Ect...
I hope this helps you understand my question :)

Share this post


Link to post
Share on other sites
You want alternating backgounds for your news. Is that correct?

Try this:
[code]<?php
switch ($_GET['page']){
    default:
    case "home":
        $sql = "SELECT * FROM `news` order by `id` DESC LIMIT 0,3";
        $result    = mysql_query($sql) or die("Problem with the query: $sql<br>" . mysql_error());
        $num    = mysql_num_rows($result);
        $table_background = 1;
        $td_background = 3;
        while ($rw = mysql_Fetch_assoc($result)) {
                echo '<table border="0" cellpadding="0" cellspacing="0"><td background="images/news_' . $table_background . '.jpg" height="19" align="left" valign="bottom" width="652">'. $rw['title'] .'</td></tr>
                <tr>
                <td align="left" background="images/news_2.jpg">'. $rw['content'] .'</td>
                </tr>
                <tr>
                <td background="images/news_' . $td_background . '.jpg" height="17"></td>
                </tr>';
                echo '</table>';
                $table_background = ($table_background == 1)?4:1; // if it's 1 set it to 4, if it's not 1 set it to 1
                $td_background = ($td_background == 3)?6:3; // same idea as above
        }
        break;
}?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.