Link to home
Start Free TrialLog in
Avatar of pingeyeg
pingeyeg

asked on

Can't seem to get two tables per row to show up

Can anyone tell me what I am doing wrong here?  I am trying to display two tables per row, but only one table is showing right now and I know there are currently two html tables in the db table right now.

while ($row = mysql_fetch_assoc($SQLStrings[0])) {

            $strProviderservice = $row['strProviderservice'];
            $strCompanyname = $row['strCompanyname'];
            $strOwner = $row['strOwner'];
            $strAddress = $row['strAddress'];
            $strTown = $row['strTown'];
            $strZipcode = $row['strZipcode'];
            $strPhone = $row['strPhone'];
            $str2ndphone = $row['str2ndphone'];
            $strMobile = $row['strMobile'];
            $strPager = $row['strPager'];
            $strFax = $row['strFax'];
            $strEmail = $row['strEmail'];
            $strWebsite = $row['strWebsite'];
            $strLicense = $row['strLicense'];
            $strInsured = $row['strInsured'];
            $strBonded = $row['strBonded'];
            $strHours = $row['strHours'];
            $str24houremerg = $row['str24houremerg'];
            $strOtherservices = $row['strOtherservices'];
            $strServicearea = $row['strServicearea'];
            $strInbusiness_since = $row['strInbusiness_since'];
            $strServicesoffered = $row['strServicesoffered'];
            $strFreeestimate = $row['strFreeestimate'];
            $strWorkguaranteed = $row['strWorkguaranteed'];
            $strProvidertagline = $row['strProvidertagline'];
            $strAd_size = $row['strAd_size'];
            $strImage = $row['strImage'];
      
      if ($row['strAd_size'] == "Premium") {
            require("premium_ad.php");
      }
      
      if ($row['strAd_size'] == "Basic") {
            $counter = 0;
            $row = mysql_fetch_assoc($SQLStrings[0]);
                  while ($row)
                        {
                        
      // Start the row
                              echo "<tr><td>";
                              require("basic_ad.php");
                                echo "</td>";
                        }
      $row = mysql_fetch_assoc($SQLStrings[0]);
    if( $row ) {
                             echo "<td>";
                              require("basic_ad.php");
                                 echo "</td>";
      $row = mysql_fetch_assoc($SQLStrings[0]);      // make ready for next loop test
      } else {
           echo "<td>&nbsp;</td>";            // empty cell
      }
       // End the row
       echo "</tr>";
      }
}
Avatar of lucki_luke
lucki_luke
Flag of Germany image

I don't really get your code.

First you are making a while-loop that is supposed to fetch all rows, but then in the loop you are fetching new rows again, thus mixing data rows. Is it like that on purpose?

Also I don't see what you mean with the second table...
Avatar of pingeyeg
pingeyeg

ASKER

Does this look better.  After making the change I am now getting the tables that should be shown, but they are one per row:

while ($row = mysql_fetch_assoc($SQLStrings[0])) {

            $strProviderservice = $row['strProviderservice'];
            $strCompanyname = $row['strCompanyname'];
            $strOwner = $row['strOwner'];
            $strAddress = $row['strAddress'];
            $strTown = $row['strTown'];
            $strZipcode = $row['strZipcode'];
            $strPhone = $row['strPhone'];
            $str2ndphone = $row['str2ndphone'];
            $strMobile = $row['strMobile'];
            $strPager = $row['strPager'];
            $strFax = $row['strFax'];
            $strEmail = $row['strEmail'];
            $strWebsite = $row['strWebsite'];
            $strLicense = $row['strLicense'];
            $strInsured = $row['strInsured'];
            $strBonded = $row['strBonded'];
            $strHours = $row['strHours'];
            $str24houremerg = $row['str24houremerg'];
            $strOtherservices = $row['strOtherservices'];
            $strServicearea = $row['strServicearea'];
            $strInbusiness_since = $row['strInbusiness_since'];
            $strServicesoffered = $row['strServicesoffered'];
            $strFreeestimate = $row['strFreeestimate'];
            $strWorkguaranteed = $row['strWorkguaranteed'];
            $strProvidertagline = $row['strProvidertagline'];
            $strAd_size = $row['strAd_size'];
            $strImage = $row['strImage'];
      
      if ($row['strAd_size'] == "Premium") {
            require("premium_ad.php");
      }
      
      if ($row['strAd_size'] == "Basic") {
            $counter = 0;
                  while ($row)
                        {
                        
      // Start the row
                              echo "<tr><td>";
                              require("basic_ad.php");
                                echo "</td>";
                        }
    if( $row ) {
                             echo "<td>";
                              require("basic_ad.php");
                                 echo "</td>";
      } else {
           echo "<td>&nbsp;</td>";            // empty cell
      }
       // End the row
       echo "</tr>";
      }
}
I changed it even more to try and make it simple:

if ($row['strAd_size'] == "Basic") {
            $counter = 0;
                  if ($counter == 0) {
                        echo "<tr>";
                        }
                        echo "<td>";
                        require("basic_ad.php");
                         echo "</td>";
                        if ($counter == 2) {
                        echo "</tr>";
                        }
                        $counter = $counter + 1;
}
Any suggestions?
Can you please clarify exactly what you are looking for.  If I understand correctly, perhaps you are looking for 2 rows per column, and span from left to right per row, in a zig zag way, going all the way to the bottom as the following example shows:

1                           2
3                           4
5                           6
etc

If this is correct, then I believe I have asked a similar question in the past and found an excellent solution through trawling EE questions/answers:

https://www.experts-exchange.com/questions/21697147/Asp-script-to-php-split-db-results-into-2-columns-then-repeat-in-rows-URGENT.html

This code is easliy modifyable.  You can increase the number of rows it processes and decrease, on the fly if you so choose.  Through some modification, you can get each row to do something different based on certain criteria too, such as build certain table layouts within the row/column.

Hope this helps.
Avatar of Jeff Darling
I would recommend that you build the html code first manually, then after seeing the html in action, then craft the necessary php code to build the html code.

I think you are on the right track using $row to keep track of the row.

Here is a simple table built using php.

<table border="1">
<tr>
<td><img src="1.jpg" /></td>
<td><img src="2.jpg" /></td>
<td><img src="3.jpg" /></td>
<td><img src="4.jpg" /></td>
</tr>
<tr>
<td><img src="5.jpg" /></td>
<td><img src="6.jpg" /></td>
<td><img src="7.jpg" /></td>
<td><img src="8.jpg" /></td>
</tr>
</table>

Here is the php code that created it.

echo "<table border=\"1\">\n";
echo "<tr>\n";

$rowcnt = mysql_num_rows($results);
$row = 0;
$col = 0;

for($u=0;$u<$rowcnt;$u++) {

$col = $col + 1;

$ProdPic=mysql_result($results,$u,'ProdPic');
$ProdTitle=mysql_result($results,$u,'ProdTitle');

// start new row
if($col > 4) {
 $row = row + 1;
 $col = 0;
 echo "</tr>\n";
 echo "<tr>\n";
}

echo "<td><img src=\"$ProdPic\" /></td>\n";
}

echo "</tr>\n";
echo "</table>\n";
Let me guess what you are looking for:

<?php

echo '<table><tr>';
$newRow = false;
while($row = mysql_fetch_assoc($SQLStrings[0])) {
                  
      // extract($row);      // this line if enabled does the same as all the lines you put at the beginning:
                              // like $strProviderservice = $row['strProviderservice']; but with all vars from $row
   
      if($newRow === true){
            echo '</tr><tr>';
      }
      
      echo '<td>';
      
      if($row['strAd_size'] == "Premium"){
            require("premium_ad.php");
      }else if($row['strAd_size'] == "Basic"){
            require("basic_ad.php");
      }
            
      echo '</td>';
      
      $newRow = (false === $newRow);
      
}
echo '</tr></table>';
?>

I'm going crazy here.  It is still going row per row.
By the way lucki luke, how will your code display two basic ads per row and only one premium ad per row?
ASKER CERTIFIED SOLUTION
Avatar of lucki_luke
lucki_luke
Flag of Germany image

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
Sorry, forgot these 2 lines before the while loop:

<?php
echo '<table>';
$newRow = true;
?>
I don't know, but right now the basic ads just tile downward like they normally would.
This is what I have so far, but like I said it still does one basic ad per row:

echo '<table><tr>';

$newRow = false;
while ($row = mysql_fetch_assoc($SQLStrings[0])) {

            $strProviderservice = $row['strProviderservice'];
            $strCompanyname = $row['strCompanyname'];
            $strOwner = $row['strOwner'];
            $strAddress = $row['strAddress'];
            $strTown = $row['strTown'];
            $strZipcode = $row['strZipcode'];
            $strPhone = $row['strPhone'];
            $str2ndphone = $row['str2ndphone'];
            $strMobile = $row['strMobile'];
            $strPager = $row['strPager'];
            $strFax = $row['strFax'];
            $strEmail = $row['strEmail'];
            $strWebsite = $row['strWebsite'];
            $strLicense = $row['strLicense'];
            $strInsured = $row['strInsured'];
            $strBonded = $row['strBonded'];
            $strHours = $row['strHours'];
            $str24houremerg = $row['str24houremerg'];
            $strOtherservices = $row['strOtherservices'];
            $strServicearea = $row['strServicearea'];
            $strInbusiness_since = $row['strInbusiness_since'];
            $strServicesoffered = $row['strServicesoffered'];
            $strFreeestimate = $row['strFreeestimate'];
            $strWorkguaranteed = $row['strWorkguaranteed'];
            $strProvidertagline = $row['strProvidertagline'];
            $strAd_size = $row['strAd_size'];
            $strImage = $row['strImage'];
      
       if($newRow === true){
            echo '</tr><tr>';
      }
     
      echo '<td>';
     
      if($row['strAd_size'] == "Premium"){
                  echo '<td colspan="2">';
            require("premium_ad.php");
                  $newRow = true;
      }else if($row['strAd_size'] == "Basic"){
                  echo '<td>';
            require("basic_ad.php");
            $newRow = (false === $newRow);
      }
      echo '</td>';
     
      if($newRow === true){
            echo '</tr>
                  ';
      }
}
Maybe its caused by the content in the included files?

I set up a testing script, filling an array with 50 values, every 5th declared as Premium. Then I loop through them using the code I posted (except that I just echo basic or premium instead of including some file) and the output is

basic      basic
basic      basic
Premium
basic      basic
basic      basic
Premium
basic      basic
basic      basic
Premium

And so on.
You apparently missed my second reply:

Change:
<?php
echo '<table><tr>';

$newRow = false;
while ($row = mysql_fetch_assoc($SQLStrings[0])) {
?>
into
<?php
echo '<table>';
$newRow = true;
?>


Furthermore, you can replace all this code:

            $strProviderservice = $row['strProviderservice'];
            $strCompanyname = $row['strCompanyname'];
            $strOwner = $row['strOwner'];
            $strAddress = $row['strAddress'];
            $strTown = $row['strTown'];
            $strZipcode = $row['strZipcode'];
            $strPhone = $row['strPhone'];
            $str2ndphone = $row['str2ndphone'];
            $strMobile = $row['strMobile'];
            $strPager = $row['strPager'];
            $strFax = $row['strFax'];
[...]
simply with extract($row);
When I take out the files and replace them with the strings Premium and Basic I get:

Basic
Basic

Which is correct in the concept of how many should be shown, but still not two per row.
Did you update the code before the while loop?

This is the code I use to test and it works just fine:

<?php

for($i = 0; $i < 2; $i++)
      $results[] = array($i, 'strAd_size' => ($i % 5 ? "Basic" : "Premium"));

print_r($results);
echo '<table border=1>';
$newRow = true;
while($row = array_pop($results)) {
                 
      extract($row);      // this line if enabled does the same as all the lines you put at the beginning:
                              // like $strProviderservice = $row['strProviderservice']; but with all vars from $row
   
      if($newRow === true){
            echo '<tr>';
      }
     
      if($row['strAd_size'] == "Premium"){
                  if($newRow === false)
                        echo '<td>&nbsp;</td></tr><tr>';
                  echo '<td colspan="2">';
            echo "Premium";
                  $newRow = true;
      }else if($row['strAd_size'] == "Basic"){
                  echo '<td>';
            echo "basic";//require("basic_ad.php");
            $newRow = (false === $newRow);
      }
      echo '</td>';
     
      if($newRow === true){
            echo '</tr>
                  ';
      }
}
echo '</table>';
?>
Finally!!!  Your right, I missed that little bit.  And I like the extract(row) function, pretty cool.
Glad I was able to help.

In the last code I posted I also adjusted a little thing in the Premium if clause, starting a new row if the previous item was a basic ad as this wasn't taken into account before.