URGENT !!! Recordsets currently alternate td's but an urgent change is required

Greetings experts,

I have the following code which produces results like this:

News Story 1     News story 2
News story 3     News story 4
News story 5     News story 6


<? // beginning of dynamic column/row code ?>
<table width="473"  border="0" cellpadding="0" cellspacing="4" class="homecontent">
<?php
mysql_select_db($database_bvdb, $bvdb);

$ssql = "SELECT * FROM projects ORDER BY Title ASC";
$rs = mysql_query($ssql) or die("Sql error: " . mysql_error());

while($myrow = mysql_fetch_array($rs))
{
    $myPhoto[] = $myrow['ThumbImage'];
    $myName[]  = $myrow['Title'];
     $myID[] = $myrow['ID'];
}
//echo mysql_num_rows($rs);
?> <?
$i = 0;
while($i < mysql_num_rows($rs))
{
?><tr><?
     for($j = 0; $j < 2; $j++)
     {
          $newVal = $i + $j;?>
          <td align="left" valign="top" height="80" >
         <? if($newVal <  mysql_num_rows($rs)){ ?>
           <a href="viewproject.php?id=<?php echo $myID[$newVal]; ?>" class="greytxt"><img border="0" src='<?=$myPhoto[$newVal];?>' /></a>
         <p><?=$myName[$newVal];?></p><? } ?></td>
          <?
     }?>
     
     </tr>
<?     $i = $i + 2;
}
mysql_free_result($rs);

?>
</table>
<? // end of dynamic column/row code ?>


However, what I would like to do to it now is instead of it alternating td's I want it to look like this and then create a new td:

News Story 1     News story 5
News story 2     News story 6
News story 3     News story 7
News Story 4

Note that as there aren't 8 records, the script still creates an empty cell for it.

Your urgent assistance is greatly appreciated.
LVL 18
Mark GilbertSenior Performance EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
Hi,

You can use this code:

<table width="473"  border="0" cellpadding="0" cellspacing="4" class="homecontent">
<?php
mysql_select_db($database_bvdb, $bvdb);

$ssql = "SELECT * FROM projects ORDER BY Title ASC";
$rs = mysql_query($ssql) or die("Sql error: " . mysql_error());

$c = mysql_num_rows($rs);
$mid = ceil($c/2);
$rows = array();

$i = 0;
while($myrow = mysql_fetch_array($rs))
{
     $rows[$i++ % $mid][] = '<a href="viewproject.php?id'.$myrow['ID'].'" class="greytxt">'.
                                          '<img border="0" src="'.$myrow['ThumbImage'].'" /></a>'.
                                          '<p>'.$myrow['Title'].'</p>';
}

foreach($rows as $r) {
  printf('<tr><td>%s</td><td>%s</td></tr>', @$r[0], @$r[1]);
}

mysql_free_result($rs);
?>
</table>

-r-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
star_trekCommented:
try this

<? // beginning of dynamic column/row code ?>
<table width="473"  border="0" cellpadding="0" cellspacing="4" class="homecontent">
<?php
mysql_select_db($database_bvdb, $bvdb);

$ssql = "SELECT * FROM projects ORDER BY Title ASC";
$rs = mysql_query($ssql) or die("Sql error: " . mysql_error());
while($myrow = mysql_fetch_array($rs))
{
    $myPhoto[] = $myrow['ThumbImage'];
    $myName[]  = $myrow['Title'];
     $myID[] = $myrow['ID'];
}
$total_rows = mysql_num_rows($rs);
//echo mysql_num_rows($rs);
?> <?
$i = 0;
$j = ceil($total_rows/2);
while($i < $total_rows)
{
?><tr><?
     for($i = 0; $i < $j; $i++)
     {
          $newVal = $i + $j;?>
          <td align="left" valign="top" height="80" >
           <a href="viewproject.php?id=<?php echo $myID[$i]; ?>" class="greytxt"><img border="0" src='<?=$myPhoto[$i];?>' /></a>
         <p><?=$myName[$i];?></p></td>
         <td align="left" valign="top" height="80" >
           <a href="viewproject.php?id=<?php echo $myID[$newVal]; ?>" class="greytxt"><img border="0" src='<?=$myPhoto[$newVal];?>' /></a>
         <p><?=$myName[$newVal];?></p></td>
          <?
     }?>
     
     </tr>
<?
}
mysql_free_result($rs);
?>
</table>
<? // end of dynamic column/row code ?>
0
Mark GilbertSenior Performance EngineerAuthor Commented:
Roonan, thanks very much.  Tried the code and it works.  Great job!

Star_trek, gave your answer a bash but ended up crashing the server because it caught in a loop.  Seemed when a page eventually appeared that it was listing all the records in a long row (each in their own columns) but just looping.  

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.