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
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]
Link to comment
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 :)
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.