SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Comment "Plugging"

    In my blog script, I wish to have "plugs" to people who comment on my blog posts (a list of their names and if they post a url link their name to their website). I'm basing this off of a Wordpress plugin and customizing it to fit my blog. The only problem I have is with the foreach statement.

    I get the following error:
    Warning: Invalid argument supplied for foreach() in /PATHHIDDEN/blog.php on line 61
    Here's the just the "plugging" code:

    PHP Code:
    <?php 
            $before 
    '&hearts;';
            
    $plug_limit 10;
            
    $sep ', ';
            
    $request  "SELECT name, email, url, comment_ID FROM blog_comments";
            
    $request .= " WHERE blog_id='$id' GROUP BY name, url ORDER BY comment_ID DESC";
            
    $request .= ($limit 0) ? " LIMIT $plug_limit'';
            if(!
    $commenters mysql_query($request)) die("Query problem: $request." mysql_error());
            if (
    $commenters) {
                echo 
    'Thanks to:';
                
    $output '';
                foreach (
    $commenters as $commenter) {
                if (!empty(
    $commenter->url)) {
                    
    $output[] = '<a href="' $commenter->url '" title="' $commenter->name '" target="_blank">' $commenter->name '</a>';
                } else {
                    
    $output[] = $commenter->name;
                }
            }
               if (
    is_array($output)) {
                echo 
    $before.implode($sep$output);
            }
        } else {
         echo 
    '';
        }
            
    ?>
    If you need to, take a look at the script for the whole page.

    PHP Code:
    <?php
    error_reporting
    (E_ALL);

    //connection variables hidden

    $connection mysql_connect("$hostname"$user"$pass");
    $db mysql_select_db($dbase $connection);

    if (!empty(
    $_GET['page']) && is_numeric($_GET['page'])) {
      
    $page $_GET['page'];
    } else {
        
    $page 1;
    }
    $total_query "SELECT COUNT(*) FROM blog;";
    $total_result mysql_query($total_query) or die ("Could not execute query : $total_query." mysql_error());
    $limit 2;
    $total_records mysql_num_rows($total_result);
    $total_pages ceil($total_records $limit);
    $set_limit = ($page 1) * $limit;

    $query "SELECT id,title,DATE_FORMAT(date, '%W %M %d, %Y at %h:%i %p') as date,entry,cat FROM blog ORDER BY id DESC LIMIT $set_limit$limit";
    $result mysql_query($query) or die ("Could not execute query : $query." mysql_error());

    if (
    mysql_num_rows($result) == 0) {
      die(
    "No blogs met your criteria.");
    }

    while(
    $row mysql_fetch_array($result)) {
        
    $id $row["id"];
        
    $title $row["title"];
        
    $date $row["date"];
        
    $entry $row["entry"];
        
    $cat $row["cat"];
            
    $comq "select * from blog_comments where blog_id='$id' ";
            
    $comqresult mysql_query($comq$connection) or die ("Could not execute query : $comq ." mysql_error()); 
            
    $comqrows mysql_num_rows($comqresult);
            if (
    $comqrows=="0") { $coms "0";} else{ $coms $comqrows; }
            
    ?>
            <h5><?php echo "$title"?></h5>
            <small><?php echo "$date"?><br />
            <!-- Posted under <?php echo "$cat"?><br /> -->
            <a href="http://electricromance.org/post.php?id=<?php echo "$id"?>">Comments (<?php echo "$coms"?>)</a>
            </small><br />
            <br />
            <?php echo "$entry"?>
            <br /><br />
            <?php 
            $before 
    '&hearts;';
            
    $plug_limit 10;
            
    $sep ', ';
            
    $request  "SELECT name, email, url, comment_ID FROM blog_comments";
            
    $request .= " WHERE blog_id='$id' GROUP BY name, url ORDER BY comment_ID DESC";
            
    $request .= ($limit 0) ? " LIMIT $plug_limit'';
            if(!
    $commenters mysql_query($request)) die("Query problem: $request." mysql_error());
            if (
    $commenters) {
                echo 
    'Thanks to:';
                
    $output '';
                foreach (
    $commenters as $commenter) {
                if (!empty(
    $commenter->url)) {
                    
    $output[] = '<a href="' $commenter->url '" title="' $commenter->name '" target="_blank">' $commenter->name '</a>';
                } else {
                    
    $output[] = $commenter->name;
                }
            }
               if (
    is_array($output)) {
                echo 
    $before.implode($sep$output);
            }
        } else {
         echo 
    '';
        }
            
    ?>
            <br /><br />
            <?    
    }    

    if (
    $page >= 2) { 
        echo 
    "&laquo; <a href=\"http://electricromance.org/blog.php?";
        echo 
    "cat=" urlencode($cat) . "&limit=" $limit "&page=" . ($page 1) . "\">PREV</a> |";


    for(
    $a 1$a <= $total_pages$a++) {
        if(
    $a == $page) {
            echo 
    "<b>" $a "</b> | ";
        } else {
            echo 
    "<a href=\"http://electricromance.org/blog.php?page=" $a "\"> " $a " </a> | ";
        }
    }

    if(
    $total_pages >= ($page 1)) {
        echo 
    "<a href=\"http://electricromance.org/blog.php?page=" . ($page 1) . "\"><b>NEXT</b></a> &raquo;";

    ?>
    ~ Hannah

  2. #2
    SitePoint Member Graphic Master's Avatar
    Join Date
    Jun 2007
    Location
    Nottingham
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmmm..... i hate PHP, but i could help

  3. #3
    SitePoint Member Graphic Master's Avatar
    Join Date
    Jun 2007
    Location
    Nottingham
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo $before.implode($sep, $output); is that line 61 in your page geeklove?

  4. #4
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
                foreach ($commenters as $commenter) { 
    This is the line, the error says the something's wrong with the foreach argument.
    ~ Hannah

  5. #5
    SitePoint Member Graphic Master's Avatar
    Join Date
    Jun 2007
    Location
    Nottingham
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, i'll try and find something

  6. #6
    SitePoint Member Graphic Master's Avatar
    Join Date
    Jun 2007
    Location
    Nottingham
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there any other errors that are indicated by the browser?

  7. #7
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope. And I have all error reporting turned on.

    http://creativeburst.org/blog.php
    ~ Hannah

  8. #8
    SitePoint Member Graphic Master's Avatar
    Join Date
    Jun 2007
    Location
    Nottingham
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i can't quite put my finger on what's screwing this up... it could be anything with PHP, you could have missed out a semi colon in the line or something...

    I'm best with HTML, CSS, and Javascript, but can understand some PHP...

    Hold on -- the problem is with the commenter name, where do the commenter names come from? if it's MySQL it may be a problem registering the name.

  9. #9
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only error is on line 61, which I posted above. If there was an error with the MySQL then it would state so in the error.
    ~ Hannah

  10. #10
    SitePoint Member Graphic Master's Avatar
    Join Date
    Jun 2007
    Location
    Nottingham
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it a registed user prefrence, as there are a lot of errors in:

    http://creativeburst.org/post.php

    that could be the source of your problems, it says it doesn't like the code: include() in your post.php.
    Is that the line of code that includes the name into the page via MySQL?

  11. #11
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ^ Ignore that file.. I'm still working on it, and those are easily fixable errors. I'm focusint=g on blog.php right now.
    Is that the line of code that includes the name into the page via MySQL?
    What?
    ~ Hannah

  12. #12
    SitePoint Member Graphic Master's Avatar
    Join Date
    Jun 2007
    Location
    Nottingham
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your sql is fine, but the sql may source info from the "post.php" page and use them for: $commenter as commenters - the commenters may not exsist if the post action is unavalible

  13. #13
    SitePoint Member Graphic Master's Avatar
    Join Date
    Jun 2007
    Location
    Nottingham
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm confusing myself now - I loath PHP, there are allways errors included...
    i'm super pissed off at the moment anyway.
    It may be anything, i need to do something for a while, will have a look later though..

  14. #14
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Graphic Master View Post
    your sql is fine, but the sql may source info from the "post.php" page and use them for: $commenter as commenters - the commenters may not exsist if the post action is unavalible
    No... this has nothing to do with post.php.
    ~ Hannah

  15. #15
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    $before 
    ' &hearts; ';
    $plug_limit 10;
    $sep ', ';
    $request  "SELECT name, email, url, comment_ID FROM blog_comments";
    $request .= " WHERE blog_id='$id' GROUP BY name, url ORDER BY comment_ID DESC";
    $request .= ($limit 0) ? " LIMIT $plug_limit'';

    if (!
    $commenters mysql_query($request)) {
        die(
    "Query problem: $request." mysql_error());
    }

    if (
    $commenters) {
        
        echo 
    'Thanks to:';
        
        
    $output '';

        while (
    $commenter mysql_fetch_object($commenters)) {
            if (!empty(
    $commenter->url)) {
                
    $output[] = '<a href="' $commenter->url '" title="' $commenter->name '" target="_blank">' $commenter->name '</a>';
            } else {
                
    $output[] = $commenter->name;
            }
        }
        
        if (
    is_array($output)) {
            echo 
    $before implode($sep$output);
        }

    }
    You want to use a while with mysql_fetch_object not foreach.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  16. #16
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! But if there are no commenters, I don't want it to say "Thanks to:" and it still does =/
    ~ Hannah

  17. #17
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ($commenters) { 
    becomes
    PHP Code:
    if (mysql_num_rows($commenters) > 0) { 
    MySQL v5.1.58
    PHP v5.3.6

  18. #18
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you!
    ~ Hannah


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
  •