Jump to content

Using the while statment


cameeob2003

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?
Link to comment
https://forums.phpfreaks.com/topic/16162-using-the-while-statment/
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]
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 :)
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

Archived

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

×
×
  • Create New...

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.