Solved

Checking for last result of  $offset

Posted on 2012-03-21
4
139 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.

762 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

6 Experts available now in Live!

Get 1:1 Help Now