php table output (last closing tr is missing)

I'm using this code to show businesses from database. It works fine, the only problem is if there are 3,5,7,9 ... businesses the last closing </tr> before the closing </table> is missing in the output.

With 2,4,6,8 ... businesses the last </tr> appears fine.

This is the code:

-----------------------------------------------------------------
<table style="width:100%;">
            <?php
 $counter=0;
            $result = mysql_query("SELECT bizID,bizName,bizAddr,bizCity,bizState,bizZip,bizPhone FROM biz_cars4 WHERE bizState='$bizState' AND bizCity='$bizCity' GROUP BY bizName ASC LIMIT $offset,100");
            while($r=mysql_fetch_array($result))
            {
                  $bizID = $r['bizID'];
                  $bizName = $r['bizName'];
                  $bizAddr = $r['bizAddr'];
                  $bizZip = $r['bizZip'];
                  $bizPhone = $r['bizPhone'];
                  $bizCity = $r['bizCity'];
                  
                       $bizCityLink = str_replace(" ","-",$bizCity);
                  $bizCityLink = strtolower($bizCityLink);
                  $bizStateLink = strtolower($bizState);
                  $bizNamev = replaceForLink($bizName);


                  $bizNameLink = replaceForText($bizName);
             if($counter==0)
                  {
                                        echo "<tr>";
                                                             }

                              echo '<td style="width:180px;" class="bold"><a href="'. $urlPrefix . $bizStateLink . '/' . $bizCityLink . '/' . $bizID . '-' .  $bizNamev . '.html">' . $bizNameLink .' '  .$bizCity.  '</a><br>
      
            

                '.$bizAddr.'<br> '.'Zip'.' '.$bizZip.'<br>
                   '.'Phone'.'  '.  $bizPhone.'
                  
                        
                        <br>
                        
                        </td>';
if($counter==1)
                  {
                        echo "</tr>";
                        $counter = 0;
                        continue;
                  }
                                  $counter++;
            }
            ?>
</table>
-----------------------------------
seoptiAsked:
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.

Ray PaseurCommented:
What is the purpose of the $counter variable?  Why test it at all?  Just unconditionally create a <tr> and a </tr> tag for every row of the results set.
0
SStoryCommented:
The problem seems to be that you seem to be trying to put two rows of data within one HTML table row. That can be a problem because if the counter=0 on the last record it will never get to the time where counter=1 and write out the <TR>
I agree with Ray_Paseur. What is the purpose of doing it this way instead of just writing one row at a time surrounded by the <tr> </tr>?
0
seoptiAuthor Commented:
This was the best amateur code I was able to write to get 2 rows. I am still not sure how to get this output in php with the code above.

<table>
      <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
      </tr>
      <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
      </tr>
      <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
      </tr>
      <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
      </tr>

.......
</table>
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Ray PaseurCommented:
Maybe something like this...
<table style="width:100%;">
<?php

// KEEP THE OUTPUT STRING HERE
$out = NULL;

// CREATE QUERY
$sql = "SELECT bizID,bizName,bizAddr,bizCity,bizState,bizZip,bizPhone FROM biz_cars4 WHERE bizState='$bizState' AND bizCity='$bizCity' GROUP BY bizName ASC LIMIT $offset,100";

// RUN QUERY AND TEST FOR SUCCESS
$res = mysql_query($sql);
if (!$res)
{
    $msg = "FAIL: $sql <br/>" . mysql_error();
    die($msg);
}

// USE AN ITERATOR TO GET ALL ROWS OF THE RESULTS SET
while($r=mysql_fetch_array($result))
{
    $bizID = $r['bizID'];
    $bizName = $r['bizName'];
    $bizAddr = $r['bizAddr'];
    $bizZip = $r['bizZip'];
    $bizPhone = $r['bizPhone'];
    $bizCity = $r['bizCity'];
    $bizCityLink = str_replace(" ","-",$bizCity);
    $bizCityLink = strtolower($bizCityLink);
    $bizStateLink = strtolower($bizState);
    $bizNamev = replaceForLink($bizName);
    $bizNameLink = replaceForText($bizName);

    // ADD THE DATA TO THE OUTPUT STRING
    $out .= "<tr>";
    $out .= '<td style="width:180px;" class="bold"><a href="'
         . $urlPrefix 
         . $bizStateLink 
         . '/' 
         . $bizCityLink 
         . '/' 
         . $bizID 
         . '-' 
         .  $bizNamev 
         . '.html">' 
         . $bizNameLink 
         . ' '  
         . $bizCity
         .  '</a><br>'
         . $bizAddr
         . '<br> '
         . 'Zip'
         . ' '
         . $bizZip
         . '<br>'
         . 'Phone'
         . '  '
         .  $bizPhone
         . '<br>'
         . '</td>'
         ;
    $out .= '</tr>'
         . PHP_EOL
         ;
}

// WRITE THE ROWS TO THE CLIENT OUTPUT STREAM
echo $out;
?>
</table>

Open in new window

0
seoptiAuthor Commented:
Thanks, but this is not what I'm looking for and additionally does not work when tested.

I'm simply looking for a solution for the end </tr>, I don't have the php skills to rewrite the whole code section.
0
Ray PaseurCommented:
not what I'm looking for
does not work when tested
Sorry.  As you must know this programming can only be speculative from our end of things.  Any scripts we might try to amend or write for you can only be tested in your exact installation including your data base and your surrounding PHP programming.  Maybe if you can give us a description of what you're looking for and why, and some clue about the "does not work" part we could help.  What happened when you tried it?  Parse error?  Loop?  Incorrect output?  

Failing that, if you believe as I do that time is money, it might be a good idea to hire a professional PHP programmer to write the code for you.  It would not cost you very much money at all and it would get you results in a matter of, at most, a few minutes or hours.
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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.