Closed Thread
Results 1 to 4 of 4

Thread: How to Only Echo something Once In a Loop?

  1. #1
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Question How to Only Echo something Once In a Loop?

    Hi Guys,

    Sorry I have another question for you pro's.

    Basically I am using this code to create a basic sidebar:

    Code:
     <?php 
        	$result = mysql_query("SELECT * FROM tbprofo");
    		echo "<ul>";
    		while($row=mysql_fetch_array($result))
    		{
    			echo "<li>".$row['category']."</li>";
    		}
    		echo "</ul>";
    	?>
    So what this does is basically echo out all the categories of all the posts in my database.

    The only problem is that some posts use the same categories (i.e. I have say 5 posts all using category "News") - this means that my code echo's News 5 times.

    For the life of me I cant figure out how to only display a category once

    Any ideas?

    Thanks very much, this forum is so helpful and all these members are great, I really do appreciate all the help that is given on here.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: How to Only Echo something Once In a Loop?

    it depends a little on your database table structure, either you could
    Code:
    select distinct category from tbprofo
    or you could do
    Code:
    select * from tbprofo group by category
    to get a better list to output.

    if you don't use the sql query for more than this list, I'd go with the first one.
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  4. #3
    zeroradius's Avatar
    zeroradius is offline Speaks fluent binary
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    1,403
    Rep Power
    25

    Re: How to Only Echo something Once In a Loop?

    you could also use a while loop inside of that while loop. The first while loop will return the catagories, the second have it return the board that belong in that catagory. so it would be like:


    Code:
    <?php
    // return each catagory by oreder number
    $getCatagory="Select * from forumCatagory Order by returnOrder DESC";
    $runCatagoryQuerry=mysql_query($getCatagory)or die("Sorry The Forum is not working corectly at the moment. Please e-mail support about this : rpgsupport@dynamicfantasies.com <br />" mysql_error());

    while(
    $returnCatagory=mysql_fetch_array($runCatagoryQuerry)){
    ?>
       
       <tr>
          <td class="tableHead">

    <?php
    echo $returnCatagory['catagory'];
    ?>
          </td>
       </tr>

    <?php
    //return each board by its catagory name
    $getBoard="Select * from forumBoard where catagory='$returnCatagory[0]'"// $returnCatagory['catagory'] would not work, had to use array #
    $runBoardQuerry=mysql_query($getBoard)or die("Sorry The Forum is not working corectly at the moment. Please e-mail support about this : rpgsupport@dynamicfantasies.com <br />" mysql_error());

    while(
    $returnBoard=mysql_fetch_array($runBoardQuerry)){
    ?>

       <tr>
          <td class="contentOther">
             <a href="show_threads.php?boardName=<?php echo $returnBoard['boardName']; ?>&catagory=<?php echo $returnCatagory[0]; ?>"> <?php
            
    echo $returnBoard['boardName'];
    ?>
             </a>
          </td>
       </tr>
       <tr>
          <td class="contentOther">
    <?php
            
    echo $returnBoard['description'];
    ?>
             <hr width="400px" size="2" />
          </td>
       </tr>
    <?php
    //end catagory loop and board loop
    }
    }
    ?>

    thats how i did it in my forum at anyrate and it works withought error

  5. #4
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Re: How to Only Echo something Once In a Loop?

    Thanks guys

    Orjan - select distinct works great - thanks very much matey.... zeroradius - you may be able to clean up your code with this.

    cheers guys

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. echo <a href>
    By newphpcoder in forum PHP Development
    Replies: 1
    Last Post: 05-12-2011, 02:23 PM
  2. Comparison printf to echo
    By xXAlphaXx in forum PHP Development
    Replies: 6
    Last Post: 02-05-2011, 02:40 PM
  3. How to Echo to a specific DIV?
    By ductiletoaster in forum PHP Development
    Replies: 1
    Last Post: 03-24-2010, 01:31 AM
  4. ECHO to DIV
    By ductiletoaster in forum PHP Development
    Replies: 3
    Last Post: 03-19-2010, 07:36 AM
  5. loop:{ if(true) {goto:loop;}}//PHP 5.3
    By BlaineSch in forum PHP Development
    Replies: 22
    Last Post: 09-12-2009, 06:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts