Solved

Checking for last result of  $offset

Posted on 2012-03-21
4
148 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
[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
4 Comments
 
LVL 110

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 110

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 110

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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.

730 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