SitePoint Sponsor

User Tag List

Page 3 of 3 FirstFirst 123
Results 51 to 68 of 68

Thread: Count records and display

  1. #51
    Unobtrusively zen silver trophybronze trophy
    SitePoint Award Recipient paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,207
    Mentioned
    40 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by computerbarry View Post
    Thanks pmw57. If your way is more of a standard then I'll have to change a few things, 5 SELECT query's to be exact, but I'd like to get my code as good as it can be, what are the benefits of doing it your way?
    One benefit is that you can easily copy/paste from one place to another, but that'r eally should be done is we're concerned with DRY (Don't Repeat Yourself) so you can place the code in a function called queryAll, so that you can then feed it a SQL string and get back the data.

    If your server already has PEAR installed on it, you can make use of the MDB2 library which allows you to do:

    Code php:
    $data = $mdb2->queryAll('SELECT SomeFields FROM SomeTable WHERE SomeClause');

    But even without it, you can achieve a similar approximation and prevent large amounts of database code from being mixed in throughout the rest of your code.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  2. #52
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see Paul thanks, I'll look more at this when I get what I have working exactly how I want it, spent a lot of time past two days with this so... thanks for pointing this out, it can be a pain repeating similar code over and over, I know what you mean cheers

    Just wondering as well, is it quite common to have 5+ SELECT statements running at the same time with different bits of code on the same page?
    The more you learn.... the more you learn there is more to learn.

  3. #53
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    update

    I have this working like the other snippets Shrapnel but, it's adding a category_id for every venue, instead one category_id for a group of venues. What do I need to add from oddz snippet, if you have time cheers ?

    Code PHP:
    <? if($num_rows5): ?>
     
    		<? foreach($arrVenues as $row5): ?>
    				<ul>
    				<h2><?=$row5['category_id']?></h2>
    				<li><a href="/<?=$row5['category_id']?>"><?=$row5['venue_id']?></a></li>
    				</ul>
    		<? endforeach ?>
    		<? else: ?>
    		 <p>No data found</p>
    		<? endif ?>
    The more you learn.... the more you learn there is more to learn.

  4. #54
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ust wondering as well, is it quite common to have 5+ SELECT statements running at the same time with different bits of code on the same page?
    A programmer would make a function of this code. Just to shrink the code itself.
    As you not a programmer, yet, it is ok to leave these bits as is.

    I need to add from oddz snippet, if you have time
    I have time, but sorry, I don't like to waste it.

  5. #55
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A programmer would make a function of this code. Just to shrink the code itself.
    Yes I understand but each SELECT's will be making different query's, so I didn't see how you could use one function to cater for all, I'll do some research thinking maybe stored procedures might be a good solution, thanks.

    As you not a programmer, yet, it is ok to leave these bits as is.
    I'm a front-end developer Shrapnel, still learning PHP and other back-end languages, thanks for reminding me

    I have time, but sorry, I don't like to waste it.
    A don't see how your wasting your time Shrapnel, I've had a problem and you've been helping me (I'm learning), isn't that what these forums are for? Like I said before, I appreciate the help greatly, maybe I just don't understand things as easy as you do.
    The more you learn.... the more you learn there is more to learn.

  6. #56
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't see how you could use one function to cater for all
    functions has arguments
    you can pass your queries as an arguments to the function.
    A don't see how your wasting your time Shrapnel, I've had a problem and you've been helping me (I'm learning)
    I am sorry to mention it, but I can't see you learn a little bit.

    You have a goal very similar to ones you accomplished many times before: to have an array from the sql query. You even noticed that all these code bits are very similar. but you failed it. As little thing as different variable name spoiled you.

    Say, you have a query,
    $sql3 = "SELECT venue_id FROM tbl_venues";
    and want to have an array
    $DATA1
    so, you're using the code
    PHP Code:
        $sql3 "SELECT venue_id FROM tbl_venues";
        
    $result3 mysql_query($sql3) or die(mysql_error());
        
    $DATA1=array();
            while (
    $row3 mysql_fetch_array($result3MYSQL_ASSOC)) {
            
    $row3['title']=str_replace("-"," ",(ucwords($row3['venue_id'])));
            
    $row3['url']=str_replace(" ","-",(strtolower(ucwords($row3['venue_id']))));
            
    $DATA1[]=$row3;
            } 
    and get it.

    Now you have very similar conditions: a query
    $sql5 = "SELECT category_id, venue_id FROM tbl_venues ORDER BY category_id";
    and want to have an array
    $arrVenues
    ...and then fail
    I can't believe it

    Same for the HTML part.
    It consists of very few operators. Very easy to learn.
    But you never learn, you just copy.

  7. #57
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am sorry to mention it, but I can't see you learn a little bit.
    I've learned a lot Shrapnel, good use of foreach(), better and cleaner templates amongst other things...

    But you never learn, you just copy.
    Maybe a bit, you know how it is when you want to get something working

    I understand what your saying and I understand what we've been working on the past couple of nights, it's just when I'm trying to separate the venues into the different category_id's... my code does work just in the wrong way.

    example:

    bars
    venue 1
    bars
    venue 2
    clubs
    venue 3
    clubs
    venue 4

    it should be

    bars
    venue 1
    venue 2
    clubs
    venue 3
    ...

    I was having trouble understand what to take from oddz code, so we can get it to work without using echo, cleaning templates

    This is what I had.
    outputs what I want
    bars
    venue 1
    venue 2
    Code PHP:
    <?
            $intCategory = null;
            foreach($arrVenues as $arrVenue) {
     
            if($intCategory != $arrVenue['category_id']) {
                if($intCategory !== null) echo '</ul></div>';
                echo '<div><h2>',$arrVenue['category_id'],'</h2><ul>';
                $intCategory = $arrVenue['category_id'];
            }
     
                echo '<li>',$arrVenue['venue_id'],'</li>';
            }
            ?>

    but know replaced with (cleaner code)
    outputs
    bars
    venue 1
    bars
    venue 2
    Code PHP:
    <? if($num_rows5): ?>
     
            <? foreach($arrVenues as $row5): ?>
                    <ul>
                    <h2><?=$row5['category_id']?></h2>
                    <li><a href="/<?=$row5['category_id']?>"><?=$row5['venue_id']?></a></li>
                    </ul>
            <? endforeach ?>
            <? else: ?>
             <p>No data found</p>
            <? endif ?>

    Can you see what I mean?
    I need to fit something like...

    ($intCategory != $arrVenue['category_id']) {
    if($intCategory !== null)

    ...into this new code so it only creates the new category_id after its shown all the venues from that category, like the first example.
    The more you learn.... the more you learn there is more to learn.

  8. #58
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have syntax of all these operators
    As you can see from the code you posted above,
    PHP Code:
     if(confition) { echo "text"; } 
    can be replaced with
    PHP Code:
    <? if (condition): ?>text<? endif ?>
    hope you got the idea

  9. #59
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hope you got the idea
    Yes I got the idea... cracking up, been trying for the past hour, just not sure what goes where?

    What I have:
    Code PHP:
    <? if($num_rows5): ?>
     
    		<? foreach($arrVenues as $row5): ?>
    		<? if($intCategory != $arrVenues['category_id']) 
    			if ($intCategory !== null): ?>
    			</ul><h2>
    			<?=$row5['category_id']?>
    			</h2><ul>
     
    		<? $intCategory = $arrVenues['category_id'];  ?>
     
    		<? endif ?>
     
    			<li><a href="/<?=$row5['venue_id']?>"><?=$row5['venue_id']?></a></li>
     
    		<? endforeach ?>

    It outputs

    bars
    venue 1
    venue 2
    venue 3 ( this is a club venue)

    it should be
    clubs
    venue 3

    Something simple?
    The more you learn.... the more you learn there is more to learn.

  10. #60
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you have two if statements here
    both must have <? if (condition): ?>text<? endif ?> format

  11. #61
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Shrapnel, still no change?
    Code PHP:
    <? if($num_rows5): ?>
     
    		<? foreach($arrVenues as $row5): ?>
    			<? if($intCategory != $arrVenues['category_id']): ?> </ul><h2> <? endif ?>
    			<? if ($intCategory !== null): ?>
     
    				<?=$row5['category_id']?>
    				</h2><ul>
     
    		<? $intCategory = $arrVenues['category_id'];  ?>
     
    		<? endif ?>
    		<li><a href="/<?=$row5['venue_id']?>"><?=$row5['venue_id']?></a></li>
    		<? endforeach ?>
     
    		<? else: ?>
    		 <p>No data found</p>
    		<? endif ?>
    ???
    The more you learn.... the more you learn there is more to learn.

  12. #62
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll just keep oddz snippet Shrapnel, it does work how I want it to, I was just hoping to keep the code in the same format... without the echo etc, clean templates as you pointed out on the first page, turning into a bad headache thanks again for the time
    The more you learn.... the more you learn there is more to learn.

  13. #63
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    3,845
    Mentioned
    11 Post(s)
    Tagged
    3 Thread(s)
    Glad you figured it out.

    Just to let you know though the index category_id doesn't exist in $arrVenues because it is a numeric array of each row of the result set. So this code for instance $intCategory = $arrVenues['category_id'] should be $intCategory = $row5['category_id'].

  14. #64
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <? endif ?> in this syntax represents closing }

  15. #65
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...should be $intCategory = $row5['category_id'].
    thanks for pointing it out oddz, your code has helped a lot
    <? endif ?> in this syntax represents closing }
    Cheers Shrapnel, making things that little bit clearly

    ok. Two questions.

    1. What does : mean at the end? If I remove it, I need to remove <? endif ?>, what does it do?
    Code PHP:
    <? if($intCategory != $row5['category_id']): ?>

    2. Assuming my code is right - The last category output has no closing </ul>, how do i fix it? Tried everything?

    Code PHP:
    <? if($num_rows5): ?>
    		<? foreach($arrVenues as $row5):?>
     
    		<? if($intCategory != $row5['category_id']):?>
    			<? if ($intCategory !== null)?></ul><h3>
    			<?=$row5['category_id']?></h3><ul>
    			<? $intCategory = $row5['category_id'];?>
    		<? endif ?>
     
    			<li><a href="/<?=$row5['venue_id']?>"><?=$row5['venue_id']?></a></li>
     
    		<? endforeach ?>
    		<? else: ?>
    		<p>No data found</p>
    		<? endif ?>
    output
    <h3>bars</h3>
    <ul>
    <li>ven1</li>
    </ul>
    <h3>clubs</h3>
    <ul>
    <li>ven2</li>
    <li>ven3</li>

    Thank you
    The more you learn.... the more you learn there is more to learn.

  16. #66
    Unobtrusively zen silver trophybronze trophy
    SitePoint Award Recipient paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,207
    Mentioned
    40 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by computerbarry View Post
    1. What does : mean at the end? If I remove it, I need to remove <? endif ?>, what does it do?
    There are two types of syntax for the if statement.

    Code php:
    if (condition) {
        ...
    }
    if (condition):
        ...
    endif;

    When sprinkled throughout HTML code, here is how they would look

    PHP Code:
    <?php if (condition) { ?>
        <!-- HTML code -->
    <?php ?>
    <?php 
    if (condition): ?>
        <!-- HTML code -->
    <?php endif; ?>
    Quote Originally Posted by computerbarry View Post
    2. Assuming my code is right - The last category output has no closing </ul>, how do i fix it? Tried everything?
    The </ul> tag needs to be shown when one of either two situations occur. One is before <h3> is shown, but only when a category has already been shown.

    The other is at the end of the last row, so we should have one too after the end of the foreach statement.

    Code PHP:
    <? if($num_rows5): ?>
        <? foreach($arrVenues as $row5):?>
     
            <? if($intCategory != $row5['category_id']):?>
                <? if ($intCategory !== null):?>
                </ul>
                <? endif ?>
                <h3><?=$row5['category_id']?></h3>
                <? $intCategory = $row5['category_id'];?>
                <ul>
            <? endif ?>
     
                    <li><a href="/<?=$row5['venue_id']?>"><?=$row5['venue_id']?></a></li>
        <? endforeach ?>
                </ul>
    <? else: ?>
        <p>No data found</p>
    <? endif ?>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  17. #67
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Paul, working great now and well explained thank you.

    There are two types of syntax for the if statement.
    So its just a matter of personal preference and how you want to output your html, or is one way better than the other?

    My next problem will be to tackle the multiple SELECT statements, like you pointed out in the early post, starting to get a bit messy I'll do a bit of research and start a new thread for this if I have any problems.

    Cheers guys
    The more you learn.... the more you learn there is more to learn.

  18. #68
    Unobtrusively zen silver trophybronze trophy
    SitePoint Award Recipient paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,207
    Mentioned
    40 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by computerbarry View Post
    Thanks Paul, working great now and well explained thank you.

    So its just a matter of personal preference and how you want to output your html, or is one way better than the other?
    if (cond) {...} is the normal standard way that is used in most situations.

    if (cond): ... endif; is primarily used for templating, where you have mostly raw HTML code that is interspersed with occasional php control code.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

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
  •