Help - Search - Members - Calendar
Full Version: need php help
Movable Type Community Forum > Additional Resources > Tips and Tricks
LisaJill
Hi everyone.

I'm starting a weight loss blog.

What I want to do is put up at the top the "target amount to lose"

the way I'm setting this up is that the entry title will be the current day's weight. A number only.

So, what I want to do is take the current day and subtract it by my target weight (120). I'll be updating the site once a day so rebuilds aren't an issue, it'll rebuild when I make the entry....

I may not even need php to do this, I don't know.

But if someone can help me out, what I want to do is take the current days weight (the MTEntryTitle from the latest post) and subtract it by 120.

If anyone is willing to help me out with some code for this I'd really appreciate it. =)

Thank you!

-Lisa
maddy
Something like this ought to do it:
CODE
<?php
$current = <MTEntries lastn="1"><MTEntryTitle></MTEntries>;
$to_goal = $current - 120;
echo "Target Weight Loss: " . $to_goal;
?>


Is that the kind of thing you want? Just shout if you want an explanation or some more help. I like learning PHP. biggrin.gif

p.s. You can find a run down of the basic mathematical operators for PHP here at w3schools. smile.gif
LisaJill
You're a saint, thank you Maddy!!

Just one question, if I may?

I tried to change the order around, so it would say "blah lbs left to go"

but it wouldn't do it...

I tried like this



CODE
<?php
$current = <MTEntries lastn="1"><MTEntryTitle></MTEntries>;
$to_goal = $current - 120;
echo " . $to_goal; "lbs left to go"
?>


but it gave me a parse error.. so I'm guessing I missed something... I'll have to read through that link later, and I want to learn php but I'm really busy with college apps so I'm here looking for shortcuts *blushes*

thank you so much =)
maddy
And if you want to keep track of your total loss so far as well, you could use something like this:
CODE
<?php
//enter your start weight below
$start = 130;
$current = <MTEntries lastn="1"><MTEntryTitle></MTEntries>;

//where 120 is your goal
$to_goal = $current - 120;
$achieved = $start - $current;

echo "Still to go: " . $to_goal ."<br />";
echo "Lost so far: ". $achieved;
?>


I've no doubt the code could be tightened up and cut down to just a line or two, but I like to make it clear what I'm doing. smile.gif
maddy
CODE
<?php
$current = <MTEntries lastn="1"><MTEntryTitle></MTEntries>;
$to_goal = $current - 120;
echo  $to_goal ."lbs left to go";
?>


That should do it. It's all about concatenation. It throws me too, most days. wink.gif
LisaJill
Oh my god. That is brilliant. That one I'm putting in exactly as is, I love it.

Thank you =)
LisaJill
After you went and showed me how to fix the 'lbs left to go' part, I went and used your other php instead. Thank you so much.

I haven't done the design yet, but if you want to see what I've done is here. The menu stuff has no links yet, since I started this only an hour or so ago hehe.

Thank you so much, I really appreciate all the help!
maddy
Glad to help. smile.gif
kadyellebee
Just for fun, since Maddy mentioned concatenation, here's another way you can separate things. I tend to use the comma method instead of periods it because its what Brenna first showed me when I was learning. wink.gif

CODE
echo  $to_goal, "lbs left to go";


If you are putting multiple variables in an echo statement, its easy to see where they start and end:
CODE
echo "I only have ", $to_goal, " pounds until I reach my ideal weight of ", $goal, " and can fit into my size ", $size_goal, " swim suit!";

biggrin.gif

Sometimes, it even works to put
CODE
echo "I only have $to_goal pounds until I reach my ideal weight of $goal and can fit into my size $size_goal swim suit!";

But I'm not sure why its not as consistant about working. Maybe it depends on whether you use single or double quotes.

So there ya go, a little PHP lesson from someone who really doesn't know that much. wink.gif Back to you, Miss Maddy tongue.gif
LisaJill
That's neat, thank you for posting it. I've got the thing finally live, I had decided to keep it simple but ended up making it failry complicated on the back end. Lot of comparisons going on, bleh! Apparently, I'm addicted to MT or something. =)

I think as it goes on I can make a lot of use of that, maybe even something on an entry by entry basis.. thank you for posting it, it helps a lot. =)
maddy
QUOTE (kadyellebee @ Mar 18 2004, 07:37 PM)
So there ya go, a little PHP lesson from someone who really doesn't know that much. wink.gif  Back to you, Miss Maddy tongue.gif

Hehe. I think you know quite a bit more than me! Thank you for your lesson, Ms Kristine. wink.gif
kadyellebee
Yeah, but I've only been learning since I started using MT, so I'm still behind the smart people, like, say, my husband. ;-) I still use the PHP manual about every day. smile.gif
maddy
QUOTE (LisaJill @ Mar 18 2004, 07:39 PM)
Apparently, I'm addicted to MT or something. =)

Aren't we all? biggrin.gif
almuhajabah
QUOTE (maddy @ Mar 17 2004, 10:49 PM)
QUOTE (LisaJill @ Mar 18 2004, 07:39 PM)
Apparently, I'm addicted to MT or something. =)

Aren't we all? biggrin.gif

Yes wink.gif wink.gif wink.gif
LisaJill
If it's not very difficult (if it is complcicated, I'll live with it)... is it possible to round?

right now it's saying this:

Still to go: 106.6
Lost so far: 0.40000000000001

which may appear to be a little bit nit picky on my part *grins*

I was thinking of cutting it off after the 4 (wow, that's what, to the 10th rights? I can't even do math much less code for it huh.gif ) so, anyway.. one number to the right of the decimal point....

Like I said, if it's difficult, it's ok to leave it like that, I don't want to abuse your kindness =)

thank you!
imabug
QUOTE
If it's not very difficult (if it is complcicated, I'll live with it)... is it possible to round?

round()
LisaJill
thank you imabug, but I see here how to round via the examples, but not how to incorporate it or to round the generated variable from the code above

CODE
<?php
//enter your start weight below
$start = 227;
$current = <MTEntries category="Dailies" lastn="1"><MTEntryTitle></MTEntries>;

//where 120 is your goal
$to_goal = $current - 120;
$achieved = $start - $current;

echo "Still to go: " . $to_goal ."<br />";
echo "Lost so far: ". $achieved;
?>


being what i ended up going with. =)
kadyellebee
Try this for the center section:
CODE
//where 120 is your goal
$to_goal = round($current - 120, 1);
$achieved = round($start - $current, 1);


I think that should do it; the 1 means 1 decimal place.

Are you using whole numbers in your entry? I'm trying to figure out why it is showing all those decimal places anyhow!

Kristine
LisaJill
No, I'm going to a 10th - which is what my scale goes to. Today my weight was blahtoomuchblah.6 *whistles* so that's what I put.

I need all the encouragement I can get, I'll take .4 =)
LisaJill
worked purr-fectly, thank you thank you thank you so so so so much. =)
kadyellebee
You are welcome! smile.gif

Still, it seems weird that if its a number to the tenth to show .400000000000001 for any reason! Obviously, fixable by some code, but odd!! biggrin.gif

Kristine
LisaJill
I have no idea why, I assume that it's simply because computers are literal and do what you say. Still, the math seems very odd - to go THAT far to get to a 1?

Oh well, that's why programming drives me nuts when I try it. I used to get Access db's to throw fits at me - actually, thats kind of fun. biggrin.gif

But it's fixed now, thank you so much. It's all running really smoothly now. Got MTAdminLink in there so updating what I've done is a cinch.

Love MT, yes I do. =)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.