A PHP if statement question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tharoah
Forum Newbie
Posts: 6
Joined: Sat Jan 12, 2008 2:40 am

A PHP if statement question

Post by Tharoah »

First of all I have only been playing with PHP for a few months now. I was wondering if it is possible to put a link in an Then section of the If statement....Also I have one page that I am trying to fix. There is too much code to put here. If anyoen has the experence please help :) Thanks so much...

Just PM here and Ill give you my MSN
User avatar
hannnndy
Forum Contributor
Posts: 131
Joined: Sat Jan 12, 2008 2:09 am
Location: Iran>Tehran
Contact:

Re: A PHP if statement question

Post by hannnndy »

:wink: there are two ways :
1. using echo

Code: Select all

<?php
if($statement)
   echo('<a href='url'>your text here</a>');
else
  echo('<a href='url'>your sec text here</a>');
?>
2. using pure html in code

Code: Select all

<?php
if($statement)
{
?>
   <a href='url'>your text here</a>
<?php
}
else
{
?>
   <a href='url'>your sec text here</a>
<?php
}
?>
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: A PHP if statement question

Post by RobertGonzalez »

I just have to ask, what have you tried so far? This is a fairly elementary practice (echoing data to the screen). Did you try this at all?
Tharoah
Forum Newbie
Posts: 6
Joined: Sat Jan 12, 2008 2:40 am

Re: A PHP if statement question

Post by Tharoah »

Everah | Please use the [code] or [code=php] tag when posting code in the forums.
i did, the problem is that I cant get it to display correctly without erroring out. Here is what I have.

Code: Select all

if($owner == ""){ echo "<p class='warning'>This has no owner click <a href="?action=takeover">here</a> to take it over</p>";} else { echo " This is already owned";} 
But seeing from above I can see why is messed up. I have a game that we are working on and there are properties on the game. I want to have it to where if there is no owner, I want then to be able to "purchase" it, but im not sure.
Everah | Please use the [code] or [code=php] tag when posting code in the forums.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: A PHP if statement question

Post by RobertGonzalez »

You need to read up on handling strings in PHP. It will teach you how to properly escape quotes in quoted strings.
Tharoah
Forum Newbie
Posts: 6
Joined: Sat Jan 12, 2008 2:40 am

Re: A PHP if statement question

Post by Tharoah »

Ok ill check it out.
Like I said im new to PHP, but am trying to learn what it is all about.
Tharoah
Forum Newbie
Posts: 6
Joined: Sat Jan 12, 2008 2:40 am

Re: A PHP if statement question

Post by Tharoah »

I dunno if this will work, but do you think that I would be able to set it up to where if no owner then it displays a link that redirects to a new page, and then the new page would run the script for them?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: A PHP if statement question

Post by RobertGonzalez »

Yes, you can do just about anything you want as long as you use the correct logic and syntax.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: A PHP if statement question

Post by Jonah Bron »

Tharoah wrote:Erroring out
Doesn't mean anything. Errors are there to tell you what's wrong.

Strings don't read minds. I strongly suggest downloading SciTE or some similar program for coding. If you are going to have a string that contains "s, then either surround the string with 's instead of "s, or escape all "s with the \ character. It works the other way around, too.

Code: Select all

if ($owner==null){
   echo "<p class=\"warning\">This has no owner click <a href=\"?action=takeover\">here</a> to take it over</p>";
}else{
   echo " This is already owned";
}
 
Tharoah
Forum Newbie
Posts: 6
Joined: Sat Jan 12, 2008 2:40 am

Re: A PHP if statement question

Post by Tharoah »

Gotcha,

I did it that way and it seems to work now. I also found out that the DB didn't have enough tables to set the post correctly so thats what was going on with it. I have been reading the manual on the PHP site and I saw that on the 's. The story behind it is that I bought these couple of scripts because i did not know how to add casino like games to my site. The new one that got got has it set up that if you own a casino, then someone is supposed to be able to offer you money to buy it. I see the $ offer in there, but I still think it is jacked up. I'm not going to mess with it until I can either become more efficient at PHP and MySql or have someone help me.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: A PHP if statement question

Post by RobertGonzalez »

We are here to help. Don't give up.
Tharoah
Forum Newbie
Posts: 6
Joined: Sat Jan 12, 2008 2:40 am

Re: A PHP if statement question

Post by Tharoah »

thanks so much for all of your help. I am starting to learn that its actually worse to buy framework scripts then to write from scratch.
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: A PHP if statement question

Post by jimthunderbird »

Tharoah wrote:thanks so much for all of your help. I am starting to learn that its actually worse to buy framework scripts then to write from scratch.
Yes, I agree, it's the developer who determine which framework to use based on the requirement of a project, not the other way around.
Post Reply