12 Replies - 1278 Views - Last Post: 03 April 2012 - 05:28 AM Rate Topic: -----

#1 alienhunt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-November 07

PHP+Wordpress Counter

Posted 31 March 2012 - 05:20 AM

<?php
$start = "July 1, 2008";
$now = "March 16, 2011";

$now = strtotime ("$now");
$then = strtotime ("$start");
$difference = $now - $then ;
$num = $difference/86400;
$days = intval($num);
$num2 = ($num - $days)*24;
$hours = intval($num2);
$num3 = ($num2 - $hours)*60;
$mins = intval($num3);
$num4 = ($num3 - $mins)*60;
$secs = intval($num4);
?>

<div>
I lived in China for <? echo $days ?> days.
</ul>
</div>


Basically as you can see here, I have a wordpress website. In my former theme, I had a perfectly working counter for the number of days I lived in China. It worked by placing the php code in the header.php and then calling it out on the top right corner of my page. I recently switched my theme, and wished to move the place where it is called up to the middle of a page. I have tried placing the code in different parts of the website, but for the life of me have no idea why it won't display on the page. Any ideas? The page I wish to call the code up on is here.

THANKS!

This post has been edited by alienhunt: 31 March 2012 - 05:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: PHP+Wordpress Counter

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: PHP+Wordpress Counter

Posted 31 March 2012 - 05:50 AM

This is an HTML/CSS issue, not PHP. When called your script is going to produce:
<div>
I lived in China for XX days.
</ul>
</div>

where XX is the number of days. Whether or not this gets displayed depends on the HTML/CSS it accompanies.

P.S. What's the </ul> tag for?

Oh, and I see from your link that the PHP code is sent to the browser, so it does't get executed. Does the file have a .php extension?
Was This Post Helpful? 0
  • +
  • -

#3 alienhunt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-November 07

Re: PHP+Wordpress Counter

Posted 31 March 2012 - 05:51 AM

ok, so I've been messing around with the code more on my website.. so this is what I've done.

<ul>
    <li>< ?php
$start = "July 1, 2008";
$now = "March 16, 2011";
$now = strtotime ("$now");
$then = strtotime ("$start");
$difference = $now - $then ;
$num = $difference/86400;
$days = intval($num);
$num2 = ($num - $days)*24;
$hours = intval($num2);
$num3 = ($num2 - $hours)*60;
$mins = intval($num3);
$num4 = ($num3 - $mins)*60;
$secs = intval($num4);
echo "Days I've lived in China: $days;"
?></li>
	<li>Train rides: 21</li>



Basically, I'm trying to call this stuff up inside of an unlined table.. sorry for the confusion from before

it's Wordpress, so it's just a page.. when I try to post the php code on a php file like page.php, it never gets executed either and the code ends up being displayed on the page..
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: PHP+Wordpress Counter

Posted 31 March 2012 - 06:40 AM

Wordpress can execute PHP files on the server before sending the HTML to the browser. Browsers cannot execute PHP files at all.
Start here:
http://codex.wordpre...rg/Include_Tags
Was This Post Helpful? 0
  • +
  • -

#5 alienhunt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-November 07

Re: PHP+Wordpress Counter

Posted 31 March 2012 - 06:50 AM

I'm sorry, but my PHP understanding isn't so good.. which is why I'm having issues here.. but what I don't understand is how the code worked before on my old theme, but not now.
Was This Post Helpful? 0
  • +
  • -

#6 jonesa01   User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 125
  • Joined: 18-February 07

Re: PHP+Wordpress Counter

Posted 02 April 2012 - 02:52 AM

As you have trouble with this I suggest you consider creating a plugin for this. Which would enable you to fairly easily change templates.

so in the wp-contents/plugins/ create a new folder called travelcounter and inside there make a file called travelcounter.php and save the code in it:

<?php

	/*
	Plugin Name: TravelCounter
	Plugin URI: 
	Description: 
	Version: 1.0
	Author: 
	Author URI: 
	*/
	
	function displayCounter(){
		// your code goes here
	}

?>



Then activate the plugin in the wordpress plugin section.

And in the template wherever you want it to go just place this code <?=displayCounter();?>. So if you change template all you need is to put that bit of code in the place you want it.
Was This Post Helpful? 0
  • +
  • -

#7 alienhunt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-November 07

Re: PHP+Wordpress Counter

Posted 02 April 2012 - 05:16 AM

<?php
 
    /*
Plugin Name: TravelCounter
Plugin URI: I have no idea what this is
Description: This is a way for me to display the number of days
Version: 1.0
Author: Sean Weisbrot
Author URI: I still don't know what this is
    */
	     
    function displayCounter()
    {
$start = "July 1, 2008";
$now = "March 16, 2011";

$now = strtotime ("$now");
$then = strtotime ("$start");
$difference = $now - $then ;
$num = $difference/86400;
$days = intval($num);
$num2 = ($num - $days)*24;
$hours = intval($num2);
$num3 = ($num2 - $hours)*60;
$mins = intval($num3);
$num4 = ($num3 - $mins)*60;
$secs = intval($num4);
    }
 
?>


I did everything you said, what you see above is travelcounter.php. But when I put the displaycounter function on my page, it does this;


Days Iíve lived in China: < ?=displayCounter();?>

So now what?
Was This Post Helpful? 0
  • +
  • -

#8 jonesa01   User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 125
  • Joined: 18-February 07

Re: PHP+Wordpress Counter

Posted 02 April 2012 - 05:28 AM

Firstly make sure there is no spaces in the <?=displayCounter();?> code, and make sure the template where you are adding it to is PHP like header.php
Was This Post Helpful? 0
  • +
  • -

#9 alienhunt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-November 07

Re: PHP+Wordpress Counter

Posted 02 April 2012 - 05:34 PM

so let me get this straight.. I made the plugin, called it travelcounter.php, uploaded and activated it in Wordpress. Then, I went to the page I wanted to display the code on and placed the code in it. Well, it's not working.. so what now??
Was This Post Helpful? 0
  • +
  • -

#10 jonesa01   User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 125
  • Joined: 18-February 07

Re: PHP+Wordpress Counter

Posted 03 April 2012 - 01:10 AM

Ah, if it in a page added through wordpress rather than the template add this code to just above the displayCounter function

add_shortcode('displayCounter','displayCounter');



And just put this on the wordpress page: [displayCounter]
Was This Post Helpful? 0
  • +
  • -

#11 alienhunt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-November 07

Re: PHP+Wordpress Counter

Posted 03 April 2012 - 03:10 AM

My friend wrote a counter from scratch and it works now. Thanks though!
Was This Post Helpful? 0
  • +
  • -

#12 leacamille662   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-April 12

Re: PHP+Wordpress Counter

Posted 03 April 2012 - 04:45 AM

I request you to check the codings you made for a minor issue like tags.

Alexandrite Jewelry
Was This Post Helpful? 0
  • +
  • -

#13 alienhunt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-November 07

Re: PHP+Wordpress Counter

Posted 03 April 2012 - 05:28 AM

RESOLVED. please close commenting, thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1