1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

need help to make a php code works

Discussion in 'PHP' started by greenworld, Jun 9, 2006.

  1. #1
    Hi;
    I am not sure if this can be done or not, I don't know much about php, and trying to get something else from the following word count script which is available free from mtdewvirus.com.
    the script is a wp plugin which simply count the words of every post on the blog, and print it at the end of every post,
    I want to have a condition expresion that when the words reaches a certain numbers say 100 then add a picture there, and so on until the end of post. lets say the post is 550 words then it would insert 5 pictures within the post.

    I appreciate your help,
    it does not have to be this script, if you have a simpler word count that works with what i want, I appreciate your help.

    here is the script:
    <?php 
    function mdv_post_word_count() { 
        global $wpdb; 
            $now = gmdate("Y-m-d H:i:s",time()); 
            $words = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE post_status = 'publish' AND post_date < '$now'"); 
            if ($words) { 
                    foreach ($words as $word) { 
                            $post = strip_tags($word->post_content); 
                            $post = explode(' ', $post); 
                            $count = count($post); 
                            $totalcount = $count + $oldcount; 
                            $oldcount = $totalcount; 
                    } 
            } else { 
                    $totalcount=0; 
            } 
            echo number_format($totalcount); 
    } 
    ?>
    
    Code (markup):
    and this is what i have done, which does not work:

    <?php 
    $filepath = 'images/';
    $file="pic_1";
    
    function mdv_post_word_count() { 
        global $wpdb; 
            $now = gmdate("Y-m-d H:i:s",time()); 
            $words = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE post_status = 'publish' AND post_date < '$now'"); 
            if ($words) { 
                    foreach ($words as $word) { 
                            $post = strip_tags($word->post_content); 
                            $post = explode(' ', $post); 
                            $count = count($post); 
                            $totalcount = $count + $oldcount; 
                            $oldcount = $totalcount; 
                    } 
            } 
    	  elseif   ($totalcount == 100) {
    	echo "\n<img src='".$filepath.$file."'       
            } 
    	else { 
                    $totalcount=0; 
            } 
    ?>
    Code (markup):


    the image path could be just a ddress to a html file which I could have pic in it.

    thanks.
     
    greenworld, Jun 9, 2006 IP
  2. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You need to tally each word as it is being counted. When the tally reaches 100, you insert the picture and reset the count to zero. But, in order to insert pictures into the text stream, you need to be streaming the text while in the loop.

    You need to do the picture insertion inside the loop in which you are counting words . . . not outside the loop.

    As you know, your code will not work. First, the logic is wrong.

    You are testing to see if there are $words. If there are no words than you ask if the total word count equals 100 and if that is not true then you say the total word count must equal zero.

    Secondly, within the foreach loop, you are not counting individual words, but adding toether the total number of words in arrays. Most of the time totalcount will never equal zero, which means that you would hardly ever display the picture.

    Here is an example of how I would approach the problem:

    
    <?php
    
    $words = array("this is a string that    is a bunch of 11 words", "this has four words", "and this is now five");
    $count = $oldcount = $totalcount = 0;
    $tally = 0; 
            if ($words) { 
                    foreach ($words as $word) { 
                            $post = strip_tags($word);
    			$post = preg_replace( '/ +/', " ", $post);
                            $post = explode(' ', $post); 
                            $count = count($post);
    			for($x=0; $x < $count; $x++)
    				{
    				$tally++;
    				print $post[$x];
    				if($tally == 7 ) { print "PICTURE\n"; $tally = 0; }
    				}
                            $totalcount += $count;
                    } 
            }
    
    echo "total words are ", $totalcount, "\n";
    
    exit;
    ?>
    
    Code (markup):
    I am using the preg_replace function to deal with the problem of excess spaces in a line. I replace multiple with single spaces. If there are multiple spaces, the original would treat spaces as words, instead of spaces.

    I hope this helps you out.
     
    clancey, Jun 10, 2006 IP
  3. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks a lot for your help,
    let me check it out;
     
    greenworld, Jun 10, 2006 IP
  4. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I did run the script,
    I am confused over using the array here,
    in this case every post I have, it should be pasted inside the array.
    is it supposed to be?
    I feel something is not right, or I am completly out of focus.

    sorry if this is a newbie question,

    thanks
     
    greenworld, Jun 10, 2006 IP
  5. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    As a personal preference I do not echo from within a function but rather return a string.

    Bobby
     
    Chemo, Jun 10, 2006 IP
  6. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The array is just used to make his example work outside of the WP context. Your script can set $words like it does in your very first code sample.

    If I'm understanding your question though you have a much bigger problem. Are you trying to output a pic in the middle of the post after each 100 words? Or just put a number of pics at the end based on the number of words?

    Assume * is your pic and we're doing every 3 instead of every 100. Do you want:
    The quick brown * fox jumps over * the lazy dog *
    or
    The quick brown fox jumps over the lazy dog ***

    If you want the former you're going to need to modify the WP routine that outputs the post. If you want the later you're pretty close. :)
     
    jnestor, Jun 10, 2006 IP
  7. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks,
    ahha;

    If I'm understanding your question though you have a 
    much bigger problem. Are you trying to output a pic in the middle 
    of the post after each 100 words? 
    Code (markup):
    Yes, i want to add a pic (or a html file/text file which has a pic inside), right after every certain amount of words( say 100) until the end of post,
    that's it .

    I don't want the output of total number of words or total number of pics what so ever,

    now to just make this script works in wp,
    do I have to just replace the variable $words array with the $word that was in the first script?
    you mean get rid of array?
    whatever I do, I mess it up, and does not work.


    please I need more help here.

    thanks.
     
    greenworld, Jun 10, 2006 IP
  8. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks Chemo for the help,

    would you mind give me some example?
    I am not good with all the php functions.

    keep going forth and back to php.net to see what is what.

    thanks
     
    greenworld, Jun 10, 2006 IP
  9. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #9
    The string returned from the function you are calling, just gives you the word count. It does not print the entry. It prints the word count.

    The function could be modified to return a string with the picture code included. But, you would need to rewrite the section of code which is calling this function to print that result instead of simply appending the number.

    It could be rewritten along the lines:

    
    <?php 
    function mdv_post_word_count() { 
        global $wpdb; 
        $count = $oldcount = $totalcount = $tally = 0;
        $printme = "";
        $filepath = 'images/';
        $file="pic_1";
            $now = gmdate("Y-m-d H:i:s",time()); 
            $words = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE post_status = 'publish' AND post_date < '$now'"); 
            if ($words) { 
                    foreach ($words as $word) { 
                            $post = strip_tags($word->post_content);
    			$post = preg_replace( '/ +/', " ", $post);
                            $post = explode(' ', $post);
                            $count = count($post);
    			for($x=0; $x < $count; $x++)
    				{
    				$tally++;
    				$printme += $post[$x];
    				if($tally == 100 ) { $printme .= '\n<img src='".$filepath.$file."'; $tally = 0; }
    				}
                            $totalcount += $count;
                    } 
            } else { 
                    $totalcount=0; 
            }
    	echo $printme;
            echo number_format($totalcount); 
    } 
    ?>
    
    Code (markup):
    If this works, the post will be displayed twice. You now need to determine where this function is being called and rewrite that bit of code to not otherwise display the entry. However, if it is also doing a bunch of formatting to text, that needs to be taken into account in rewriting this.
    Probably the best solution may be to call a new function and give it three pieces of information -- the text to be displayed, and the filepath and file name for the image file to be displayed. This allows you to change those pieces of information in a central location.
     
    clancey, Jun 11, 2006 IP
  10. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    thanks a lot clancey;

    I did run the script on my local machine on a php file with some text in it,
    I get the following error, I can not see anything wrong with the line, any idea?
    here is the error:

    on line 53 we have:

    if($tally == 100 ) { $printme .= '\n<img src='".$filepath.$file."'; $tally = 0; }
    PHP:
    the only thing I did change was to give an .gif extension to the file in $file variable. ( $file="pic_1.gif"; )

    thanks for your help
     
    greenworld, Jun 11, 2006 IP
  11. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Just updating what I have done,



    I did test the script which clancey posted, and there is something wrong in this line and after as I mentioned above,

    
    if($tally == 100 ) { $printme .= '\n<img src='".$filepath.$file."'; 
    $tally = 0; }
    				}
                            $totalcount += $count;
                    } 
            } else { 
                    $totalcount=0; 
            }
    	echo $printme;
            echo number_format($totalcount); 
    } 
    ?>
    PHP:
    I did remove the " " and did not get any more error:

    if($tally == 100 ) { $printme .= '\n<img src='.$filepath.$file.'; 
    $tally = 0; } 
    PHP:
    then I get other errors, ( unexpected $end error at the end of doc).


    the thing is the codes after end of the line above remain red,
    I use Dreamweaver, and usually codes has different color, (black, blue red),


    I tried and upload the file ( script that clancey posted) into the plugin area, as I did with the word count, but it did not appear in the plugin area, so I did try and paste the script into the index file of default theme, it gave me error, I tried to put it after article area and before comment area,
    but it just gives me error. changing the place gives different errors.




    thanks
     
    greenworld, Jun 11, 2006 IP
  12. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Sorry, my mistake. I was just cutting and pasting the material you coded. To display the html bit properly, the single quotes need to be moved:

    
    if($tally == 100 )
         {
         $printme .= "\n" . '<img src="' . $filepath . $file . '" />';
         $tally = 0;
         }
    
    Code (markup):
    The newline needs to be in double quotes, otherwise it is treated as a literal. You will notice the double quote marks are treated as literals -- display a "slash n" NOT a newline.

    This looks better. I added a bit more whitespace to the line. It does not affect the code, but it makes it easier to see what is going on. I tested it and it now displays a proper link on the screen.

    In your version, you had three single quotes in the line. The last one was incorporating material after it into a printable line. That caused an error when the program was running. These tiny bugs can be hard to see.

    For the image source, make sure the file path is correct. It is normally "/dir/dir/file"

    You can be absolutely sure by changing $filepath to the first part of the URL and $file to the full name of the image in the directory specified by the URL.
     
    clancey, Jun 12, 2006 IP
  13. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    thanks a lot I am trying it now, and trying to figure it out where the curly bracket has not been closed,

    Parse error: parse error, unexpected T_ELSE in c:\Inetpub\wwwroot\insert_pic_test\test3.php on line 46

    this error is where the else is ,
    thanks.
     
    greenworld, Jun 12, 2006 IP
  14. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    OK, no error now,
    but nothing works, I guess I should not expect to work as stand alone script,
    this has to work as plugin,
    I save the script under new name and upload it into plugin folder of wp,
    but it does not show up there to activate,
    any idea?
    thanks.
     
    greenworld, Jun 12, 2006 IP
  15. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    OK,
    plugin showup problem solved,
    I did activate it,
    but does not work, nothing, no error,

    I did upload the images folder into default theme folder where the index page of theme exsists, so the filepath should be correct as "images/"

    and then upload the image folder into the plugin folder area as well, either way the picture is there.

    here is the whole script,

    Do you think that the curely brackets are OK?

    
    <?php
    function mdv_post_word_count() 
    { 
        global $wpdb; 
        $count = $oldcount = $totalcount = $tally = 0;
        $printme = "";
        $filepath = 'images/';
        $file="pic_1.gif";
            $now = gmdate("Y-m-d H:i:s",time()); 
            $words = $wpdb->get_results("SELECT post_content FROM $wpdb->
    posts WHERE post_status = 'publish' AND post_date < '$now'"); 
            if ($words) 
            { 
                    foreach ($words as $word) 
            { 
                            $post = strip_tags($word->post_content);
    	           $post = preg_replace( '/ +/', " ", $post);
                            $post = explode(' ', $post);
                            $count = count($post);
    			for($x=0; $x < $count; $x++)
           {
    				$tally++;
    				$printme += $post[$x];
                             if($tally == 20 )
                             {
                        $printme .= "\n" . '<img src="' . $filepath . $file . '" />';
                         $tally = 0;
                             }
                             }
                                   $totalcount += $count;
                             } 
                             } else 
    	            { 
                                   $totalcount=0; 
                             }
    	                echo $printme;
                                 echo number_format($totalcount); 
    		} 
    ?>
    
    PHP:


    thank you for your help.
     
    greenworld, Jun 12, 2006 IP
  16. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Change line 23 to:

    $printme .= $post[$x] . " ";
    Code (markup):
    This time I tested it on my computer. I added in the space because we took that out in the explode statement. It was my mistake to use the addition sign instead of the dot for the text. Both perl and php add text to text with a dot-equal and use plus-equal for numbers.

    Welcome to the wonderful world of code debug code debug code debug!

    If this change fixes it and the image does not print change the directory to: /images/ or ./images/
     
    clancey, Jun 12, 2006 IP
  17. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #17
    thanks
    Now, it works !

    there are some problems:

    1- it does repeat the post, so you post one into your blog you have two posts there, and the second one has pic in it.

    2- the actual pictures not showing, I have tried both /images/ and ./images/
    still does not show, I am trying to see what is the problem.

    3- when you click on the title of the post, the post that opens up has no pictures it. it shows the usual post.

    4- this only works on the front page, not posts on categories.

    thanks a lot, I did learn alot.
     
    greenworld, Jun 12, 2006 IP
  18. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #18
    OK, some progress,

    image works now,
    but repeating the post, is like this:

    the first post, it will be two of them,
    the lower one has pic in it and no title,

    when you post again, the new post will be on top as it should, but no pic in it,
    and it will repeat the first post below it with pic in it and no title.

    so every time you post, it will post the new one once but it will repeat the very first one again with pic it right below it.

    I am trying to find out why.

    thanks
     
    greenworld, Jun 12, 2006 IP
  19. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #19
    actually it is more messier than I thought,
    every time you post, it will add one more time of the last post on the previous posts as well.
    the first post you have 2 posts
    the second post you have 6 posts ( 3 times first post, and 3 times second post)
    the third post you have 12 posts ( 4 times first post, 4 tims second post, 4 times third post)

    I did not try more than three post, but i guess the counting goes up.
     
    greenworld, Jun 12, 2006 IP
  20. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #20
    The problems you are having were anticipated in this thread. As I mentioned earlier:

    I have helped you hack the function which was used to tally the number of words in a post. But, that function does not display the post. That is your problem. You need to find the code which displays then entry and rewrite that section.


    By URL I mean, <img src="http://www.mydomain/images/pic.gif" /> That ensures you pick up the image you want. When you use file paths, you need to remember they are always relative to the directory in which the page is being created.
     
    clancey, Jun 13, 2006 IP