Solved

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

Posted on 2007-03-27
19
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

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

Expert Comment

by:Mark Gilbert
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

749 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