Link to home
Start Free TrialLog in
Avatar of Mark Gilbert
Mark GilbertFlag for United States of America

asked on

Asp script to php (split db results into 2 columns then repeat in rows) - URGENT

Greetings experts,

I have the following asp script which works great for 4 columns then repeats in rows:

<table>
<%
intCounter = 4
while not rs.eof
   if intCounter = 4 then
   %>
     <TR>
   <%
   intCounter = 0
   end if
   %>  
       <TD><%=rs("field")%>
   <%
   intCounter = intCounter + 1
   rs.movenext
wend
%>
<%
for intCounter = intCounter to 3
Response.Write "<TD>.."
Next
%>
</table>

please could someone help me modify this code so that I can span over 2 columns then repeat in rows.  I really appreciate any assistance you can provide.

ingwa
Avatar of Harisha M G
Harisha M G
Flag of India image

Hi ingwa, do you want it in PHP or ASP ? Which DB are you using ? (I assume MySQL)


ASP
____
<table>
<%
intCounter = 2
while not rs.eof
   if intCounter = 2 then
   %>
     <TR>
   <%
   intCounter = 0
   end if
   %>  
       <TD><%=rs("field")%>
   <%
   intCounter = intCounter + 1
   rs.movenext
wend
%>
<%
for intCounter = intCounter to 1
Response.Write "<TD>.."
Next
%>
</table>
____

PHP
____
<table>
<?php
    $intCounter = 2;
    while(($row = mysql_fetch_array($rs))
    {
        if(intCounter == 2)
        {
?>
<TR>
<?php
    $intCounter = 0;
        }
?>  
    <TD><?=row("field")?>
<?php
    $intCounter++;
    }
   
    for($intCounter = intCounter;$intCounter<=1;$intCounter++)
        echo "<TD>..";
?>
</table>
____

---
Harish
Avatar of Mark Gilbert

ASKER

hi Harish,

I have tried your code but it seems to be going into an endless loop for some reason.

This is how I have modified it so far and I'm getting a correct result in the $myColumns field, but I can't figure out how to add a column to the table:
<?
mysql_select_db($database_bvdb, $bvdb);
$query_rsProjectList = "SELECT * FROM projects ORDER BY Title ASC";
$rsProjectList = mysql_query($query_rsProjectList, $bvdb) or die(mysql_error());
$row_rsProjectList = mysql_fetch_assoc($rsProjectList);
$totalRows_rsProjectList = mysql_num_rows($rsProjectList);
?>

              <table width="130" border="0" cellspacing="0" cellpadding="0">
                    <? $myColumns = 2; ?>
                    
              <?php do { ?>
                    <? if($myColumns == 2) { $myColumns = 0; } ?>
                <tr>
                  <td><p><img src="<?php echo $row_rsProjectList['ThumbImage']; ?>" /></p>
                    <p><a href="viewproject.php?id=<?php echo $row_rsProjectList['ID']; ?>" class="greytxt"><?php echo $row_rsProjectList['ShortTitle']; ?></a></p></td>
<?      $myColumns++;?>
                </tr>                <?php } while ($row_rsProjectList = mysql_fetch_assoc($rsProjectList)); ?>

              </table>
<? echo $myColumns; ?>

What I would like is for the link to be displayed in a table cell and span 2 columns.  After 2 columns, it then creates a new row and resumes it's spanning.  If there are 9 records, what would be nice is that an empty cell is inserted into the table just to keep things nice and tidy.

My brain is really hurting now, and it would be great if you can help me on this.

Ingwa.
I have finally managed to find some code that will do this for me on :

https://www.experts-exchange.com/questions/21411412/fetch-row-to-display-images.html?query=myPhoto+myName&clearTAFilter=true

The solution that I have managed to come up with is:

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

I will put a request into community support close this question without points (no delete, it was very difficult finding the solution and this question now has some nice keywords to find it easily -- one hopes).
ASKER CERTIFIED SOLUTION
Avatar of PashaMod
PashaMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial