/    Sign up×
Community /Pin to ProfileBookmark

if statement question

Hello! This concerns a php website. I am attempting to call a specific page (home_text.php) from within my index.php. Since this is the page content for the homepage, I would like it to appear there and only there.

I am attempting to do this using an “if” statement. Here is my partially successful attempt:

[code=php]<?php
if ($page = “?”) include(“home_text.php”);
} ?>[/code]

This seems to do what I want but places the home_text.php content on [I]every[/I] page of the site. How can I restrict the home_text to just the home page?

Thank you!!

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@kurbyJun 17.2010 — You got a few problems here.

[code=php]<?php
if ($page = "?") include("home_text.php");
} ?>[/code]


1) You have a closing curly but not an opening one.

2) Your comparison of $page to a string uses an assignment operator(=) and not a comparison operator (==).

You want something more like:

[code=php]<?php
if ($page == "?")
{
include("home_text.php");
}
?>[/code]


If you only want it on a certain page, why not put the include on only one page?
Copy linkTweet thisAlerts:
@meltdown2authorJun 17.2010 — Thanks for your response.

As may well be quite obvious, I'm not overly versed in php usage ?

I am modifying an existing site for a friend. It uses one main page (index.php), from which all the other pages get their foundation. For this reason if I just put add an [I]echo[/I] or [I]include[/I] to that main page, the home_text.php then it will appear on each page. I thought that the way to deal with this was to specify the page by using an if statement.

Does this make much sense?
Copy linkTweet thisAlerts:
@meltdown2authorJun 17.2010 — Thanks for your response.

As may well be quite obvious, I'm not overly versed in php usage ?

I am modifying an existing site for a friend. It uses one main page (index.php), from which all the other pages get their foundation. For this reason if I just put add an [I]echo[/I] or [I]include[/I] to that main page, the home_text.php then it will appear on each page. I thought that the way to deal with this was to specify the page by using an if statement.

Does this make much sense?[/QUOTE]


Hmmm... It looks as though your corrected bit of code didn't work out that well. Abondoning my lame attempt at the if statement, could you tell me how to best call home_text.php?

Thanks
Copy linkTweet thisAlerts:
@kurbyJun 17.2010 — Theres nothing wrong with the syntax of my if statement, it just depends on how you are using it.

[code=php]<?php
if ($page == "?")
{
include("home_text.php");
}
?>[/code]


I would need more information on the structure of the page and how the includes are occuring. It seems like if you want home_text.php on just one page, you should put it in just that one page...
Copy linkTweet thisAlerts:
@meltdown2authorJun 17.2010 — For what it is worth, here is the index.php (which includes my questionable if statement near the bottom):

[code=php]
<?php require_once('Connections/santi.php'); ?>
<?php

//Connection statement
require_once('Connections/santi_ado.php');

$pages = explode("|",$_GET['p']);




?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;

case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_sections = 1;
$pageNum_sections = 0;
if (isset($_GET['pageNum_sections'])) {
$pageNum_sections = $_GET['pageNum_sections'];
}
$startRow_sections = $pageNum_sections * $maxRows_sections;

$colname_sections = "-1";
if (isset($pages[0])) {
$colname_sections = $pages[0];
}
mysql_select_db($database_santiago, $santiago);
$query_sections = sprintf("SELECT * FROM pages WHERE section = %s ORDER BY number ASC", GetSQLValueString($colname_sections, "text"));
$query_limit_sections = sprintf("%s LIMIT %d, %d", $query_sections, $startRow_sections, $maxRows_sections);
$sections = mysql_query($query_limit_sections, $santiago) or die(mysql_error());
$row_sections = mysql_fetch_assoc($sections);

if (isset($_GET['totalRows_sections'])) {
$totalRows_sections = $_GET['totalRows_sections'];
} else {
$all_sections = mysql_query($query_sections);
$totalRows_sections = mysql_num_rows($all_sections);
}
$totalPages_sections = ceil($totalRows_sections/$maxRows_sections)-1;

$queryString_sections = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_sections") == false &&
stristr($param, "totalRows_sections") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_sections = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_sections = sprintf("&totalRows_sections=%d%s", $totalRows_sections, $queryString_sections);


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Olivia Santiago - Fine Artist</title>
<meta name="description" content="This is the website for Olivia Santiago, a hawaiian-born painter now living in Florence, Italy">
<link href="img/santiago.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
<!--
function oliviars_popup(oliviars) {

var w = 600;
var h = 600;
var l = Math.floor((screen.width-w)/2);
var t = Math.floor((screen.height-h)/2);
window.open(oliviars,"", "width=" + w + ",height=" + h + ",top=" + t + ",left=" + l + ", status=no, menubar=no, toolbar=no, scrollbar=no, directories=no" );
}
//-->
</script>

</head>

<body>
<?php
if ($pages[0] == "links"){ // visualizza i link
?>
<div id="os_body">
<div id="os_top1"><ul><li><a href="?">Home</a></li>
<li><a href="?p=portraits|1" <?php if ($pages[0]=="portraits") { echo "class="a_active""; } ?>>Portraits</a></li>
<li><a href="?p=landscapes|1" <?php if ($pages[0]=="landscapes") { echo "class="a_active""; } ?>>Landscapes</a></li>
<li><a href="?p=still-life|1" <?php if ($pages[0]=="still-life") { echo "class="a_active""; } ?>>Still life</a></li>
<li><a href="?p=bio|1" <?php if ($pages[0]=="bio") { echo "class="a_active""; } ?>>Bio</a></li>
<li><a href="?p=events|1" <?php if ($pages[0]=="events") { echo "class="a_active""; } ?>>Events</a></li>
<li><a href="?p=links|1" <?php if ($pages[0]=="links") { echo "class="a_active""; } ?>>Links</a></li>
</ul> <img src="img/tread.jpg" />
</div>
<div id="os_page1">
<div id="box_text"> <div id="text">
<?php
// begin Recordset
$query_links = sprintf("SELECT * FROM link ORDER BY ordine ASC");
$links = $santiago_ado->SelectLimit($query_links) or die($santiago_ado->ErrorMsg());
$totalRows_links = $links->RecordCount();
// end Recordset
?>

<table class="os_table_links">
<tr >
<td height="20" ></td></tr>
<?php
while (!$links->EOF) {
?>
<tr >
<td height="35" ><p><strong><?php echo $links->Fields('descrizione'); ?></strong><br/><?php if ($links->Fields('url') != "") { ?><a href="<?php echo $links->Fields('url'); ?>" target="_blank" ><?php }?><?php echo $links->Fields('url'); ?></a></p></td>
</tr>

<?php
$links->MoveNext();
}
?>
</table>


</div></div>
</div>
<div id="os_bottom">
<div id="pages">
<div id="page_previous"><?php if ($pageNum_sections > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_sections=%d%s", $currentPage, max(0, $pageNum_sections - 1), $queryString_sections); ?>">previous</a>
<?php } // Show if not first page ?></div>

<div id="message"><a href="mailto:[email protected]" class="credits" >Web Art - [email protected]</a></div>
<div id="page_next"><?php if ($pageNum_sections < $totalPages_sections) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_sections=%d%s", $currentPage, min($totalPages_sections, $pageNum_sections + 1), $queryString_sections); ?>">next</a><?php } // Show if not last page ?></div>
</div>

</div>
</div>
<?php
}
elseif($pages[0] != "links"){ ?>

<div id="os_body">
<div id="os_top1"><ul><li><a href="?">Home</a></li>
<li><a href="?p=portraits|1" <?php if ($pages[0]=="portraits") { echo "class="a_active""; } ?>>Portraits</a></li>
<li><a href="?p=landscapes|1" <?php if ($pages[0]=="landscapes") { echo "class="a_active""; } ?>>Landscapes</a></li>
<li><a href="?p=still-life|1" <?php if ($pages[0]=="still-life") { echo "class="a_active""; } ?>>Still life</a></li>
<li><a href="?p=bio|1" <?php if ($pages[0]=="bio") { echo "class="a_active""; } ?>>Bio</a></li>
<li><a href="?p=events|1" <?php if ($pages[0]=="events") { echo "class="a_active""; } ?>>Events</a></li>
<li><a href="?p=links|1" <?php if ($pages[0]=="links") { echo "class="a_active""; } ?>>Links</a></li>
</ul> <img src="img/tread.jpg" />
</div>
<div id="os_page1">
<?php if ($totalRows_sections > 0) { // Show if recordset not empty
include("modules.php");
} ?>
</div>
<div id="os_bottom">
<div id="pages">
<div id="page_previous"><?php if ($pageNum_sections > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_sections=%d%s", $currentPage, max(0, $pageNum_sections - 1), $queryString_sections); ?>">previous</a>
<?php } // Show if not first page ?></div>

<div id="page_next"><?php if ($pageNum_sections < $totalPages_sections) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_sections=%d%s", $currentPage, min($totalPages_sections, $pageNum_sections + 1), $queryString_sections); ?>">next</a><?php } // Show if not last page ?></div>
</div>

</div>
<div id="os_copyright"> Copyright &copy; 2008 by L. Olivia Santiago - All rights reserved</div>
</div>


<?php
if ($pages = "?") include("home_text.php");
} ?>

</body>
</html>
<?php
mysql_free_result($sections);
?>
[/code]
Copy linkTweet thisAlerts:
@kurbyJun 17.2010 — [code=php]<?php
if ($page == "?")
{
include("home_text.php");
}
?>[/code]


Clearly you need to set what you want $page equal to. Also, it looks like its $pages[0]['pagename']. So which pagename do you want it on? Do you want it if there are is no pages specified?

For the life of me I can't figure out why people want to slam all their code onto one page.... but thats for another time.
Copy linkTweet thisAlerts:
@meltdown2authorJun 18.2010 — This is what has been confusing me; I can see that all other pages are specificed by name. But by what is the home page called? What name will the script recognize? And how is the if statement properly written to call that page?

Since it seems that all other page names are specified, maybe it would be best to have the if statement be written for if there are is no pages specified.

Thanks again ?
Copy linkTweet thisAlerts:
@kurbyJun 18.2010 — put a print_r($pages); on that page. It will show what is set for each page.
Copy linkTweet thisAlerts:
@meltdown2authorJun 18.2010 — put a print_r($pages); on that page. It will show what is set for each page.[/QUOTE]

Hi, sorry but if I put that bit of code on the page, where will it list the page names? And should I put it somewhere specific? Within the html has just resulted in that code itself displaying. Thank you!!
Copy linkTweet thisAlerts:
@kurbyJun 18.2010 — right after $pages = explode("|",$_GET['p']);

within the PHP tags. It will tell you the content of $pages, which is what determines what is pulled onto the page. If you see that your "homepage" has a unique value of $pages then you know what to search for in your if statement that includes your home content.
Copy linkTweet thisAlerts:
@meltdown2authorJun 18.2010 — right after $pages = explode("|",$_GET['p']);

within the PHP tags. It will tell you the content of $pages, which is what determines what is pulled onto the page. If you see that your "homepage" has a unique value of $pages then you know what to search for in your if statement that includes your home content.[/QUOTE]


Here is what the print_r($pages); returns:

[code=php]Array
(
[0] =>
)
[/code]


And if I view other pages, it lists pages associated with that page. For example:

[code=php]Array
(
[0] => portraits
[1] => 1
)
[/code]


Honestly, I don't quite know how to use this info. Is the home page name [0] => ? How would I include that in an if statement, for example? ?

I am trying this but not having success:

<?php

if ($pages[0] == "[0] =>") include("home_text.php");

} ?>[/QUOTE]
Copy linkTweet thisAlerts:
@kurbyJun 18.2010 — [code=php]<?php
if (empty($pages[0]))
{
include("home_text.php");
}
?>[/code]
Copy linkTweet thisAlerts:
@meltdown2authorJun 18.2010 — [code=php]<?php
if (empty($pages[0]))
{
include("home_text.php");
}
?>[/code]
[/QUOTE]


Hmmm... that's throwing a syntax error:
[code=php]Parse error: syntax error, unexpected $end in C:xampp...index.php on line 214[/code]
Copy linkTweet thisAlerts:
@meltdown2authorJun 18.2010 — OK - Got it! Thank you both for your help. The syntax error was being thrown because of another modification I had made, now all is well (so it seems).

There is another item I need to address, if I may; I need to create a new page. I tried adding it to the list found within the SQL file and threw an error doing so. knowing this site configuration somewhat, how does one generate a new page?

Thanks again!
×

Success!

Help @meltdown2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...