Solved

php table output (last closing tr is missing)

Posted on 2012-03-20
6
204 Views
Last Modified: 2012-03-21
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
Comment
Question by:seopti
  • 3
  • 2
6 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 
LVL 25

Expert Comment

by:SStory
Comment Utility
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
 

Author Comment

by:seopti
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:seopti
Comment Utility
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

763 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

12 Experts available now in Live!

Get 1:1 Help Now