Solved

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

Posted on 2007-03-27
19
191 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

786 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