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>";
      }
}
LVL 1
pingeyegAsked:
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.

lucki_lukeCommented:
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...
0
pingeyegAuthor Commented:
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>";
      }
}
0
pingeyegAuthor Commented:
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;
}
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

pingeyegAuthor Commented:
Any suggestions?
0
Mark GilbertSenior Performance EngineerCommented:
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:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_21697147.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.
0
Jeff DarlingDeveloper AnalystCommented:
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";
0
lucki_lukeCommented:
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>';
?>

0
pingeyegAuthor Commented:
I'm going crazy here.  It is still going row per row.
0
pingeyegAuthor Commented:
By the way lucki luke, how will your code display two basic ads per row and only one premium ad per row?
0
lucki_lukeCommented:
Well, as I said I wasn't sure what you were trying to do, so my code makes always two rows.

I slightly modified my code and I assure you that it is working:

<?php
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>';
      }
     
      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>
                  ';
      }
}
echo '</table>';
?>
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
lucki_lukeCommented:
Sorry, forgot these 2 lines before the while loop:

<?php
echo '<table>';
$newRow = true;
?>
0
pingeyegAuthor Commented:
I don't know, but right now the basic ads just tile downward like they normally would.
0
pingeyegAuthor Commented:
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>
                  ';
      }
}
0
lucki_lukeCommented:
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.
0
lucki_lukeCommented:
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);
0
pingeyegAuthor Commented:
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.
0
lucki_lukeCommented:
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>';
?>
0
pingeyegAuthor Commented:
Finally!!!  Your right, I missed that little bit.  And I like the extract(row) function, pretty cool.
0
lucki_lukeCommented:
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.
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.