Jump to content

[SOLVED] While condition problem


mark110384

Recommended Posts

I have a table that displays a news archive, however I wan't to display 6 news links at a time I have the code working perfectly to pop out the links the first time ($i <= 6) around but i'm not too sure how to the condition of links 7 and 12. The following code is what I have at the moment. Any suggestions would be appreciated, thanks

 

<?php
			$i = 1;


			$sql = "SELECT * FROM other_news ORDER BY news_date desc";

			$result = mysql_query($sql) or die  ("Data not found");

			while ($i <= 6)


			{

			$i++;
			}
				  
				  
					#This is where I am having difficulties with!
						while ($i >=7)
						{

					$myrow = mysql_fetch_array($result);

				$date = $myrow['news_date'];
				$title = $myrow['news_title'];
				$url = $myrow['news_url'];

				$formatdate = $date;
				$dateformated=date('d/m/Y',strtotime($formatdate));


					?> <table width="92%" border="0" cellspacing="0" cellpadding="0">
                      <tr> 
                        <td width="26%" height="93"><img src="../news_image.bmp" width = "50%"/></td>
                        <td width="74%"><font face="Lucida sans" size="1" color="#666666"> 
                          <? echo $dateformated; ?></font> - <font face="Lucida sans" size="1" color="#000000"> 
                          <?
					echo "<a class='other_news' href=$url>$title </a></br>"; ?>
                          </font></td>
                      </tr>
					<?
					$i++;	  
				}	  
		 ?>

Link to comment
Share on other sites

I've come up with this but the same result just gets pumped out

 

                      $i = 1;

 

$sql = "SELECT * FROM other_news ORDER BY news_date desc";

 

$result = mysql_query($sql) or die  ("Data not found");

 

while ($i <= 12)

 

 

{

 

if ($i >=7)

{

 

 

$myrow = mysql_fetch_array($result);

 

$date = $myrow['news_date'];

$title = $myrow['news_title'];

$url = $myrow['news_url'];

 

$formatdate = $date;

$dateformated=date('d/m/Y',strtotime($formatdate));

 

 

?> <table width="92%" border="0" cellspacing="0" cellpadding="0">

                      <tr>

                        <td width="26%" height="93"><img src="../news_image.bmp" width = "50%"/></td>

                        <td width="74%"><font face="Lucida sans" size="1" color="#666666">

                          <? echo $dateformated; ?></font> - <font face="Lucida sans" size="1" color="#000000">

                          <?

echo "<a class='other_news' href=$url>$title </a></br>"; ?>

                          </font></td>

                      </tr>

<?

}  

 

 

 

 

$i++;

}

?>  

Link to comment
Share on other sites

<?php
$i = 1;
$sql = "SELECT * FROM other_news ORDER BY news_date desc";
$result = mysql_query($sql) or die  ("Data not found");

while ($myrow = mysql_fetch_array($result) && $i <= 12)
{

if ($i >=7)
{

$date = $myrow['news_date'];
$title = $myrow['news_title'];
$url = $myrow['news_url'];

$formatdate = $date;
$dateformated=date('d/m/Y',strtotime($formatdate));
?>
<table width="92%" border="0" cellspacing="0" cellpadding="0">
<tr> 
<td width="26%" height="93"><img src="../news_image.bmp" width = "50%"/></td>
<td width="74%"><font face="Lucida sans" size="1" color="#666666"> 
<?php echo $dateformated; ?></font> - <font face="Lucida sans" size="1" color="#000000"> 
<?php echo "<a class='other_news' href=$url>$title </a></br>"; ?>
</font></td>
</tr>
<?php
}	  
$i++;
}
?>

 

That work???

Link to comment
Share on other sites

Infact, do you use previous and next links?

 

just add <a href="yoursite.com/script.php?limit1=1&limit2=6">Next</a>

Like:

<?php
if (empty($_GET['limit1'])){
$limit1 = 1;
}

if (empty($_GET['limit2'])){
$limit2 = 6;
}

$sql = "SELECT * FROM other_news ORDER BY news_date desc LIMIT $limit1 , $limit2";
$result = mysql_query($sql) or die  ("Data not found");

while ($myrow = mysql_fetch_array($result))
{

$date = $myrow['news_date'];
$title = $myrow['news_title'];
$url = $myrow['news_url'];

$formatdate = $date;
$dateformated=date('d/m/Y',strtotime($formatdate));
?>
<table width="92%" border="0" cellspacing="0" cellpadding="0">
<tr> 
<td width="26%" height="93"><img src="../news_image.bmp" width = "50%"/></td>
<td width="74%"><font face="Lucida sans" size="1" color="#666666"> 
<?php echo $dateformated; ?></font> - <font face="Lucida sans" size="1" color="#000000"> 
<?php echo "<a class='other_news' href=$url>$title </a></br>"; ?>
</font></td>
</tr>
<?php
}

echo "<a href=\"yoursite.com/page.php?limit1=".$limit1+6."&limit2=".$limit2+6."\">Next</a>";
?>

Link to comment
Share on other sites

Sweet thanks Andy, using your idea I made a couple of adjustments and I got the desired result that was looking for, heres the end coding that works!

 

$i = 1;

 

$sql = "SELECT * FROM other_news ORDER BY news_date desc";

 

$result = mysql_query($sql) or die  ("Data not found");

 

while ($myrow = mysql_fetch_array($result) && $i <= 12)

{

 

if ($i >=7)

{

 

while($myrow = mysql_fetch_array($result))

{

 

$date = $myrow['news_date'];

$title = $myrow['news_title'];

$url = $myrow['news_url'];

 

$formatdate = $date;

$dateformated=date('d/m/Y',strtotime($formatdate));

?>

<table width="92%" border="0" cellspacing="0" cellpadding="0">

<tr>

<td width="26%" height="93"><img src="../news_image.bmp" width = "50%"/></td>

<td width="74%"><font face="Lucida sans" size="1" color="#666666">

<?php echo $dateformated; ?></font> - <font face="Lucida sans" size="1" color="#000000">

<?php echo "<a class='other_news' href=$url>$title </a></br>"; ?>

</font></td>

</tr>

<?php

}

}  

$i++;

}

?>

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.