Solved

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

Posted on 2007-03-27
19
190 Views
Last Modified: 2010-05-18
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>";
      }
}
0
Comment
Question by:pingeyeg
19 Comments
 
LVL 9

Expert Comment

by:lucki_luke
ID: 18803978
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 18804068
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 18804118
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 18804190
Any suggestions?
0
 
LVL 18

Expert Comment

by:ingwa
ID: 18804887
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
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 18806246
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
 
LVL 9

Expert Comment

by:lucki_luke
ID: 18807273
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 18808024
I'm going crazy here.  It is still going row per row.
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18808065
By the way lucki luke, how will your code display two basic ads per row and only one premium ad per row?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Accepted Solution

by:
lucki_luke earned 500 total points
ID: 18808201
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
 
LVL 9

Expert Comment

by:lucki_luke
ID: 18808209
Sorry, forgot these 2 lines before the while loop:

<?php
echo '<table>';
$newRow = true;
?>
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18808222
I don't know, but right now the basic ads just tile downward like they normally would.
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18808248
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
 
LVL 9

Expert Comment

by:lucki_luke
ID: 18808253
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
 
LVL 9

Expert Comment

by:lucki_luke
ID: 18808275
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 18808279
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
 
LVL 9

Expert Comment

by:lucki_luke
ID: 18808318
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 18808321
Finally!!!  Your right, I missed that little bit.  And I like the extract(row) function, pretty cool.
0
 
LVL 9

Expert Comment

by:lucki_luke
ID: 18808334
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now