[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

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.
0
Mark Gilbert
Asked:
Mark Gilbert
1 Solution
 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now