FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Checking if it is last entry...





Diablosblizz
Hey, I have a code that gets the game categories for my site. I got it so if there is one entry in the table then it doesn't display a '-', but if there is more than one then it will display the '-'.

The problem is, I can't get them to stop.

Code:
<?php
         error_reporting(0);
         include("../members/config.php");
         $select1 = mysql_query("SELECT * FROM `cats`");
         while ($select = mysql_fetch_array($select1)) {
         $id = $select['id'];
         $total = $select['tp'];
         $name = $select['name'];
         $howmany = mysql_num_rows($select1);
         if($howmany == 1) {
      echo "<a href='games.php?id=$id' alt='Total Games in Category: $total' title='Total Games in Category: $total'>$name</a> - $howmany";   
      } else if($howmany >= 2) {
         echo "<a href='games.php?id=$id' alt='Total Games in Category: $total' title='Total Games in Category: $total'>$name</a> - ";
         }
         }
         ?>


The above code currently displays:

Web wrote:
Action - Arcade -


Arcade is the last entry in the table. As you can see, after Arcade there is a '-'. Is it possible to have the last '-' removed if Arcade (or any other entry) is the last entry in the table?

I hope you understand, many thanks.
ThomasDesigns
You could check how long the returned array is, and have an if statement that checks if the current item in the loop is the last in the array if information. If it isn't, then display the '-'.
Code:


<?php
         error_reporting(0);
         include("../members/config.php");
         $select1 = mysql_query("SELECT * FROM `cats`");
         while ($select = mysql_fetch_array($select1)) {
         $id = $select['id'];
         $total = $select['tp'];
         $name = $select['name'];
         $howmany = mysql_num_rows($select1);
         if($howmany == 1) {
      echo "<a href='games.php?id=$id' alt='Total Games in Category: $total' title='Total Games in Category: $total'>$name</a> - $howmany";   
      } else if($howmany >= 2) {
         echo "<a href='games.php?id=$id' alt='Total Games in Category: $total' title='Total Games in Category: $total'>$name</a>";
         if(($id-1) != $howmany){
         echo "-";
       }
        }
        }
         ?>

This is assuming the $id field is unique, and auto increments. I put $id-1 because I don't know if the mysql_num_rows() function starts counting at 0 or 1.

Another way you could do it is to have a variable that starts at 0 (or 1 depending on how the mysql_num_rows() function acts) and increases by one for every loop. That way you can check the $howmany against that variable to determine if it's the last entry or not. This will work if for some reason you cannot rely on your $id value.
Diablosblizz
Thanks Thomas, It worked. All I had to do was change the ($id-1) to ($id-0) and it worked.

Many thanks.
SlowWalkere
Here's a simple solution. Instead of outputting the 'dash' after the entry, output the dash first. Then, include a variable $first, which starts as true. As long as $first is true, don't output a dash. During the first iteration, set $first to false, and every other iteration of the loop will output a dash.

Something like this...
Code:
$first = true;

while ($looping)
{
    if ($first == true)
        $first = false;
    else
        echo " - ";

    echo "Content";
}


That would produce something like "Content - Content - Content - Content"

Hope that helps,
- Walkere
Related topics
General FAQ forum
Webpage Design Programs
How do I set-up Mambo for my new site here?
Help with PHP + SQL problem.
Display Errors on Frihost.com Front Page
How to fwrite or remove /n at the last entry
Update the information on the front page --- 10 minute job
Frihost contests: suggestions & admin sign-up
Frihost Games! Round 1
Auto updating/refreshing
Menu[1] is Undefined [SOLVED]
Contests?
Why can't i accesss this page?
E3 2012
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.