Solved

Checking for last result of  $offset

Posted on 2012-03-21
4
142 Views
Last Modified: 2013-09-16
I'm having a script which creates tables from a MySQL query. Now it sometimes misses the last closing </tr> within a table after the last result. What I would like to do is to check for the last result of the $offset and  echo "</tr>";  in this case.

So this line should contain something like:

if($counterr==2 OR if is the last result of the $offset) echo "</tr>";


<table>
<?php
 $counterr=0;
            $result = mysql_query("SELECT bizID,bizName GROUP BY bizName ORDER BY bizName ASC LIMIT $offset,100");
while($r=mysql_fetch_array($result))
            {
....

 if($counterr==0)
                  {
                                        echo "<tr>";
                                                             }

      echo '<td>'
.............
                        <br></td>';
if($counterr==2)
                  {
                        echo "</tr>";
                        $counterr = 0;
                        continue;
                  }
                                  $counterr++;
            }
            ?>
</table>
0
Comment
Question by:seopti
  • 3
4 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37750848
In the code you have posted here I see this:
if($counterr==2)

Open in new window

But the only reference to the variable seems to be here
$counterr=0;

Open in new window

In other words, there is no chance that $counterr could ever be set equal to 2.  This kind of programming seems to assume that undefined variables have some inherent properties.  You might not want to make these assumptions.

I am wondering about this line:
$result = mysql_query("SELECT bizID,bizName GROUP BY bizName ORDER BY bizName ASC LIMIT $offset,100");

Open in new window

How do you know whether that query worked at all?  I do not see any test for the value in $result.  

Going forward, you might want to consider getting this excellent little book.  It will not make you a professional programmer, but it will help you get some kind of foundation in how PHP and MySQL work together.  A few weeks and you will be much better grounded in the basics, I promise!
http://www.sitepoint.com/books/phpmysql4/
0
 

Author Comment

by:seopti
ID: 37750869
Sorry but I don't need a book, I need a simple solution. Thank you for your time. Maybe someone else here is able to give a solution? Once again, thank you.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37750902
If there were any simple simple solution that I knew about I would have offered a simple solution.  You can choose to get an understanding and write your own solution or you can choose to hire someone who already has an understanding and they can write your solution for you.  I am trying to be respectful of your time.  

If I had the time to give a solution or sell a solution I would offer that, but I am involved in a complicated real estate transaction now.  What you want may require some investigation and detailed testing.  My recommendation is to hire a professional programmer and turn over all of the information -- URL, PHP scripts, data base CREATE TABLE scripts, login credentials, etc. -- everything they ask for.  Then relax and in a few minutes you will have a good solution.  When you get the bill, you will probably think it is expensive.  But as Red Adair, the great oil well firefighter, famously pointed out, "If you think it is expensive to hire a professional, just wait until you hire an amateur!"
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37760772
I have some free time this weekend, so I took another look at this question.  Rereading this code, I still cannot figure out what the purpose of $counterr could be.  Can you please post the rest of the code in the while() loop?  I would like to line up the control structures so we can see the conditional instruction paths.  If we do that, the issues may become obvious.  Please also explain what $counterr is used for.  Thanks, ~Ray
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

827 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