Jump to content


Photo

Using the while statment


  • Please log in to reply
5 replies to this topic

#1 cameeob2003

cameeob2003
  • Members
  • PipPipPip
  • Advanced Member
  • 79 posts

Posted 01 August 2006 - 01:22 AM

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

<?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;}
?>

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?

#2 Eric_Ryk

Eric_Ryk
  • Members
  • PipPipPip
  • Advanced Member
  • 104 posts

Posted 01 August 2006 - 01:27 AM

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";

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 August 2006 - 01:30 AM

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

#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 01 August 2006 - 01:30 AM

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:

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;
}


#5 cameeob2003

cameeob2003
  • Members
  • PipPipPip
  • Advanced Member
  • 79 posts

Posted 01 August 2006 - 01:52 AM

this is an example of what i am doing:

http://n2p.ventgamin...ayout/index.php

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 :)

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 August 2006 - 04:10 AM

You want alternating backgounds for your news. Is that correct?

Try this:
<?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;
}?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users