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
https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/
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++;

}

?>  

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

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

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

}

?>

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.