View Full Version : if and else
I need an if statement that says...
if thispage.php use this <?=$row["boo"]?>
all other pages use <? echo($var); ?>
A always, all help is greatly appreciated...
bustamelon
03-27-2006, 04:57 PM
if ( $_SERVER['PHP_SELF'] == "thispage.php" ) {
echo $row["boo"];
} else {
echo $var;
}
No..
It doesnt seem to work
I'm getting a
parse error, unexpected '{' in on the first line
bustamelon
03-27-2006, 05:20 PM
No..
It doesnt seem to work
I'm getting a
parse error, unexpected '{' in on the first line
Oh. Sorry. I left out a parenthese. See edit above.
The else part works.. but the first part is not pulling the info from the db.
Any clues here?
if ( $_SERVER['PHP_SELF'] == "any.php" ) {
echo $row["boo"];
bustamelon
03-27-2006, 06:41 PM
OK. What is the name of the script? If it's not the same as the name you're checking against in the if() statement, then only the else{ will apply.
Do an echo on $_SERVER['PHP_SELF'] and see what comes up.
Oh, and this whole IF needs to live inside of the query result output, or else $row['boo'] = nothing. You probably know this but I thought I'd make sure.
I know something is not correct.
This should be asking for the content in the field "boo" for the record any.php?id=blah
if ( $_SERVER['PHP_SELF'] == "any.php" ) {
echo $row["boo"];
I think you're going to have to give us more code...
I have a page.php that calls records from a db like this page.php?id=idfieldnumber
I want the content in the boo field to echo when that particular page is called.
This should be asking for the content in the field "boo" for the record any.php?id=idfieldnumber
if ( $_SERVER['PHP_SELF'] == "any.php?id=$id" ) {
echo $row["boo"];
Perhaps posting the relevant code would help?
felgall
03-27-2006, 07:13 PM
What code do you have for the retrieval from the database?
Heres another try at this that's not working.
I should have listened to my mother when she had said she would buy me a Corvette if I finished programming school.
<?php
if ( $_SERVER['PHP_SELF'] == "page.php?id=$id" ) {
echo $row["boo"];
} else {
echo $var;
} ?>
Again , the else part works.
If that is indeed the entirety of your code, then where do you expect $row to be populated?
You need to connect to, and extract the data from a database.
bustamelon
03-27-2006, 07:25 PM
:rolleyes:
Is this what you are reffering to?
<?php
include "db.php";
if ( $_SERVER['PHP_SELF'] == "page.php?id=$id" ) {
echo $row["boo"];
} else {
echo $var;
} ?>
bustamelon
03-27-2006, 07:39 PM
Hard as I might try, my remote viewing abilities are just not working today. Maybe we can see what's INSIDE db.php ????
What we're saying is you need code that does the actual database fetching. Here's the basics:
$dbhost = "your db server here, might just be localhost";
$dbuser = "your db username here";
$dbpass = "your db password here";
$dbname = "your db name here";
$dbtable = "your table name here";
$link = mysql_connect( $dbhost, $dbuser, $dbpass );
mysql_select_db( $dbname, $link );
$sql = mysql_query("SELECT * FROM $dbtable WHERE row_id = $_GET['id']");
while( $row = mysql_fetch_assoc( $sql )) {
if ( $_SERVER['PHP_SELF'] == "page.php?id=$id" ) {
echo $row["your field name here"];
} else {
echo $var;
}
}
mysql_close();
*bangs head against nearest hard object*
I'm getting a
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in
from this line
$sql = mysql_query("SELECT * FROM $dbtable WHERE row_id = $_GET['id']");
bustamelon
03-27-2006, 08:04 PM
I'm getting a
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in
from this line
$sql = mysql_query("SELECT * FROM $dbtable WHERE row_id = $_GET['id']");
OK. First, you probably have to edit this to suit. Do you actually have a field called "row_id" ? Find out. We're looking for the primary key here, most likely, that the $id in the URL query string refers to. Once you figure that out, apply my *corrected* snippet:
$sql = mysql_query("SELECT * FROM $dbtable WHERE row_id = $_GET[id]");
(just take out the single quotes around the "id")
Going back to this...
The iftest.php page keeps printing a 0
<?php
if ( $_SERVER['PHP_SELF'] == "iftest.php" ) {
echo "1";
} else {
echo "0";
} ?>
bustamelon
03-27-2006, 09:17 PM
Going back to this...
<?php
echo $_SERVER['PHP_SELF'];
?>
What happens when you do that?
I don't think this is what you want anyway. What are you REALLY trying to accomplish? Are you trying to do X *only* if a certain query string value exists?
If that's the case, your IF should be something like:
<?php
if ( isset( $_GET['id'] ) && $_GET['id'] != "" ) {
echo "1";
} else {
echo "0";
} ?>
...where $_GET['id'] is what reads after the equals sign in your URL:
iftest.php?id=1
The slash was missing ...duh
This works.
<?php
if ( $_SERVER['PHP_SELF'] == "/iftest.php" ) {
echo "1";
} else {
echo "0";
} ?>
This is also working now. Good thing because all that code attached from the example above was getting me thinking.
<?php
if ( $_SERVER['PHP_SELF'] == "/page.php" ) {
echo $row["boo"];
} else {
echo $var;
} ?>
Thank you very, very much.
I am looking to if page1.php and page2.php to this.. any suggestions?
<?php
if ( $_SERVER['PHP_SELF'] == "/page1.php") {
echo $row["boo"];
} else {
echo $var;
} ?>
bustamelon
03-29-2006, 08:21 PM
I am looking to if page1.php and page2.php to this.. any suggestions?
Seems liike you left a verb out of that sentence, so I'm going to guess again. You want to echo $row['boo'] if the user is requesting either one of those 2 scripts?
<?php
if ( $_SERVER['PHP_SELF'] == "/page1.php" || $_SERVER['PHP_SELF'] == "/page2.php" ) {
echo $row["boo"];
} else {
echo $var;
} ?>
I thought so..
Does the $_SERVER['PHP_SELF'] == have to be repeated?
Also, what is the difference between ' and "? Do some systems take one and others the other?
<?php
if ( $_SERVER['PHP_SELF'] == "/page1.php" || $_SERVER['PHP_SELF'] == "/page2.php" ) {
echo $row["boo"];
} else {
echo $var;
} ?>
bustamelon
03-29-2006, 08:46 PM
I thought so..
Does the $_SERVER['PHP_SELF'] == have to be repeated?
Also, what is the difference between ' and "? Do some systems take one and others the other?
<?php
if ( $_SERVER['PHP_SELF'] == "/page1.php" || $_SERVER['PHP_SELF'] == "/page2.php" ) {
echo $row["boo"];
} else {
echo $var;
} ?>
When dealing with associative arrays in PHP ( $array['indexname'] ), single quotes are recommended. Double quotes work too but I understand they are being deprecated here.
As I understand it, in a conditional, you need to state the complete condition between '&&' (and) or '||' (or) separators.
Thanks bustamelon.
Should I be using single quotes throughout, even for "page1.php" ?
bustamelon
03-29-2006, 10:05 PM
double quotes around string values, except sometimes single quotes work better if the string in question actually contains single quotes. Otherwise, you would escape those quotes:
$string = 'This here\'s a string with quotes.';
If you're using a variable inside a srting value, you must use double quotes:
$newstring "Here's an example of a string: $string";
How do I include different content, in different pages...
if page1.html include page1.inc.php
if page2.html include page2.inc.php
if page3.html include page3.inc.php
if page4.html include page4.inc.php
All help a is greatly appreciated.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.