Solved

Checking for last result of  $offset

Posted on 2012-03-21
4
140 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 108

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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
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.

930 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

10 Experts available now in Live!

Get 1:1 Help Now