SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Only show content wrapper if there's content

    Hi there,

    Now, I'm really new at PHP, so maybe you'll laugh at me. I can live with that.

    I'm in the middle of creating an info screen at to work show guests and news. I'm fetching the data from our intranet, which runs on Joomla. The example below fetches today's guests from the database based on the category.

    This works fine — however, I only want the entire section#todaysguests to show if there's actually anything to show. I've tried running the while just before the section, but it creates a new section for every guest of the day, so I'm really not sure how to fix this.

    Hope you guys can help — thanks!

    Code:
    <?php
    	$sql1 = "select * from flex_content where catid=186 and state=1 order by id desc";
    	$query1 = mysql_query($sql1);
    ?>
    <section id="todaysguests" class="panel">
    	<div class="panel-wrapper">
    		<h1>Dagens gæster & møder</h1>
    		<ul>
    			<?php
    				while(($row = mysql_fetch_array($query1)) && (strtotime($row[19]) > strtotime(strftime('%F %T'))) && (strtotime($row[18]) < strtotime(strftime('%F %T')))) {
    			?>
    				<li>
    					<h2><?php echo $row[5] ?></h2>
    					<p><?php echo utf8_encode($row[2]) ?></p>
    				</li>
    			<?php }  ?>
    		</ul>
    	</div>
    </section>

  2. #2
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    165
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    After executing the query, count the number of returned rows. If there are rows, print the div, otherwise, print something else (or nothing):

    PHP Code:
    <?php
        $sql1 
    "select * from flex_content where catid=186 and state=1 order by id desc";
        
    $query1 mysql_query($sql1);
    $rows mysql_num_rows($query1);
    if(
    $rows 0)
    {
    ?>
    <section id="todaysguests" class="panel">
        <div class="panel-wrapper">
            <h1>Dagens gæster & møder</h1>
            <ul>
                <?php
                    
    while(($row mysql_fetch_array($query1)) && (strtotime($row[19]) > strtotime(strftime('%F %T'))) && (strtotime($row[18]) < strtotime(strftime('%F %T')))) {
                
    ?>
                    <li>
                        <h2><?php echo $row[5?></h2>
                        <p><?php echo utf8_encode($row[2]) ?></p>
                    </li>
                <?php }  ?>
            </ul>
        </div>
    </section>
    <?php
    }
    else
    {
    echo 
    'Nothing to show...';
    }
    ?>
    Yours truely
    Mário Ramos

  3. #3
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the quick reply! Unfortunately, it doesn't work. I've tried printing mysql_num_rows, and it doesn't show anything!

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Establish who is to blame. Is this a PHP error or a mysql error?

    Is there any matching data in your database at all?

    If you can establish that then start var_dump() ing all your variables.
    PHP Code:
    var_dump($sql1);
    var_dump($query1);
    var_dump($rows); 

  5. #5
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dumping gives me this:

    Code:
    string(71) "select * from flex_content where catid=186 and state=1 order by id desc"
    resource(2) of type (mysql result)
    int(4)
    I have no idea what that means.

  6. #6
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Establish who is to blame. Is this a PHP error or a mysql error?
    How can I figure out if it's a PHP or MySQL error?

    Quote Originally Posted by Cups View Post
    Is there any matching data in your database at all?
    Yes. There's a column in the database called publish_down. Whenever the current time is lower than this, the content will be visible. My issue is that when the time passes publish_down, and no more content matches, the section still shows.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by pshoeg View Post
    Dumping gives me this:

    Code:
    string(71) "select * from flex_content where catid=186 and state=1 order by id desc"
    resource(2) of type (mysql result)
    int(4)
    I have no idea what that means.
    line 1 - the sql statement being sent to Mysql. Copy that text, paste it straight into your database (via PhoMyAdmin or whatever you use) - then you know you have some matching data.

    line 2 - looks like it found a mysql_result set

    line 3 - looks like it found 4 rows.

    So, PHP in this instance is hooked up to Mysql fine and you have some matching data.

    So, it seems this earlier statement you made is incorrect
    Thanks for the quick reply! Unfortunately, it doesn't work. I've tried printing mysql_num_rows, and it doesn't show anything!
    if you do:
    PHP Code:
    echo $rows
    It should give you 4.

    So, leave aside all your html layout for a moment and try and loop through the result set and echo out something so you know you are not losing your sanity.
    PHP Code:
    foreach(mysql_fetch_array($query1) as $db_row){

    var_dump($db_row);


    ... if that outputs as then slowly add complexity back in as you establish each piece of code is working as it should, else go back to the previous step.

  8. #8
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, I tried printing $rows. It gives me 6, and not 4. So the reason why it's not working is maybe that the query finds 6 rows but then the while loop says that only 4 of them should be shown — and thus still shows the section. Or am I wrong here?

    When I use the foreach loop you mentioned, I still get those rows that I don't want to include — the ones that the while removes. Should I not use while altogether in this?

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You are saying that you want to filter out 2 of the 6 rows because they do not meet a certain criteria.

    I have 2 responses:

    a) Filter these out in your database so you only bring back 4 rows in the first place ...

    (that will depend on your data of course, and may not be possible)

    b) if you are building in a condition, then try and do that inside the loop,

    in pseudocode that would be something like:

    foreach( results as row ){

    if(condition is met for this row){

    //display your row

    }

    }

    ... then put that condition back into your loop - if you feel you have something to gain from doing so.

    Either way, when trying to do anything like this, break the operation up into bite-size pieces and solve the issues one at a time.

    Create a simple script which you loop through an array containing hard-coded examples of real data. Prove to yourself it works, then re-assemble all the parts.


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
  •