Solved

php table output (last closing tr is missing)

Posted on 2012-03-20
6
208 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 110

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
Industry Leaders: 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 110

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 110

Accepted Solution

by:
Ray Paseur earned 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php56-php-mcrypt for rhel7 php56 1 104
JQuery Search Filter 2 69
Checking https returns 301 21 56
How to get this library to work load? 8 38
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.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 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