Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

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>
-----------------------------------
0
seopti
Asked:
seopti
  • 3
  • 2
1 Solution
 
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
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!

 
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

Featured Post

Get your problem seen by more experts

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

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now