?
Solved

php table output (last closing tr is missing)

Posted on 2012-03-20
6
Medium Priority
?
212 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
[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
  • 3
  • 2
6 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37742197
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
ID: 37742241
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
ID: 37742391
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37742484
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
ID: 37744962
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 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 37747332
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 count occurrences of each item in an array.
Suggested Courses

752 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