The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> PHP Assigning URL's to Variables., If URL is Homepage, then do this...
Dante Monaldo
post Jan 15 2010, 08:26 PM
Post #1


Advanced Member
****

Group: Members
Posts: 124
Joined: 22-March 09
From: California, USA
Member No.: 8,132



Hi everyone. I have been trying to set up a small portion of code so that if the viewer is on the homepage or the search page, the site will show a portion of PHP code. If the viewer is on any other page, then they will see another portion of code. Below is what I have gotten so far.
CODE

<?
$full=array("/index.php","/","/about.php","/contact_us.php");
$currentpage = $_SERVER['REQUEST_URI'];

if($full==$currentpage) {
<marquee scrollamount="1" scrolldelay="10" direction="up" width="50" height="375" style="font-family: Verdana; font-size: 8pt">
<?php foreach($cust_menu as $item): ?>
        <h1><?php echo $item; ?></h1>
            <ul class="sidemenu">
            <?php foreach ($cust_menu_items as $i): ?>
            <?php echo @$i[$item]; ?>
            <?php endforeach; ?>
            </ul>
        <?php endforeach; ?>
         </marquee>
}

else {
<?php foreach($cust_menu as $item): ?>
        <h1><?php echo $item; ?></h1>
            <ul class="sidemenu">
            <?php foreach ($cust_menu_items as $i): ?>
            <?php echo @$i[$item]; ?>
            <?php endforeach; ?>
            </ul>
        <?php endforeach; ?>
}
?>

I keep getting "Parse error: syntax error, unexpected '<'." when I try to view any of my site. I am fairly new to PHP, so any help would be great.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Jan 15 2010, 11:18 PM
Post #2


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



CODE
if($full==$currentpage) {<marquee scrollamount="1" scrolldelay="10" direction="up" width="50" height="375" style="font-family: Verdana; font-size: 8pt">



This looks like the first problem. (Doesn't the error message say "on line xxx of file yyy.php" ?)

There is a style issue: do you write the web page so it looks like an html page, with snipets of php inserted, or do you write it to look like a php program, that is generating a web page? I think the answer is normally "a slightly messy compromise". Parts of the page are almost entirely straight html; other parts are really "program"...

Above you are in PHP -- if (condition) -- then you write '<', but '<' isn't valid PHP to follow if (condition)!

More generally: you should be able to use simplified brackets, so <? is enough to mean <?php (unless you are really using two scripting languages simultaneously!). I think the two following examples would be neater ways of writing the last part:

(Note: I use the conventional syntax, { }, and I align the { and } under each other. )

"Program style"

CODE

else {
   foreach($cust_menu as $item)
   {  echo "<h1>$item</h1>\n<ul class=\"sidemenu\">\n";
      foreach ($cust_menu_items as $i)
         echo @$i[$item];
      echo '</ul>';
   }
}


"HTML style"

CODE

else {
   foreach($cust_menu as $item)
   {
?>

<h1><?=$item?></h1>
<ul class="sidemenu">

<?
      foreach ($cust_menu_items as $i)
         echo @$i[$item];
?>

</ul>

<?
   }
}


(Not sure I would ever actually jump out ?> and <? in to php just to echo one line; I'd write echo '<ul>'...)

What does the @ in front of an array in echo do? (I don't know, and the php manual doesn't seem to have an index for symbols!)

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dante Monaldo
post Jan 16 2010, 11:58 AM
Post #3


Advanced Member
****

Group: Members
Posts: 124
Joined: 22-March 09
From: California, USA
Member No.: 8,132



I did what you told me to do and cleaned up the code a bunch, but now I get an error saying "Parse error: syntax error, unexpected T_ENDFOREACH in /home/content/34/4806034/html/templates/Simple/master.tpl on line 56". Below is the updated code:

CODE
<?
$full=array("/index.php","/","/about.php","/contact_us.php");
$currentpage = $_SERVER['REQUEST_URI'];

if($full==$currentpage) {
         {  foreach($cust_menu as $item):
            echo "<h1>$item</h1>/n<ul class=\"sidemenu\">\n";
            foreach ($cust_menu_items as $i)
            echo @$i[$item];
            endforeach;
            echo "</ul>";
            endforeach;
          }
}

else {
         {  echo "<marquee scrollamount="1" scrolldelay="10" direction="up" width="50" height="375" style="font-family: Verdana; font-size: 8pt">\n";
            foreach($cust_menu as $item):
            echo "<h1>$item</h1>/n<ul class=\"sidemenu\">\n";
            foreach ($cust_menu_items as $i)
            echo @$i[$item];
            endforeach;
            echo "</ul>";
            endforeach;
            echo "</marquee>"\n;
          }
}
?>


Again, I appreciate any help.

This post has been edited by Dante Monaldo: Jan 16 2010, 12:09 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Jan 16 2010, 02:31 PM
Post #4


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE
I did what you told me to do and cleaned up the code a bunch, but now I get an error saying "Parse error: syntax error, unexpected T_ENDFOREACH in /home/content/34/4806034/html/templates/Simple/master.tpl on line 56". Below is the updated code:


Ah, well now this is debugging. Your editor should show you which is line 56 (which the pasted code doesn't).

The error message means there is an unmatched "endforeach" -- if you use this "alternative syntax" you need exactly one colon to match each "endforeach". Indenting helps to make the program readable, btw.

Also you might not have noticed, but when the "body" of a for loop is a single statement there are actually _three_ ways of writing it:

CODE

foreach($things as $wotsit)
{  echo "<p>Hey, $wotsit!\n";
}


{ matches }

CODE

foreach($things as $wotsit):
   echo "<p>Hey, $wotsit!\n";
endforeach;


: matches endforeach;

CODE

foreach($things as $wotsit)
   echo "<p>Hey, $wotsit!\n";


No bracketing needed!

You didn't answer my question about the @ sign... ?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dante Monaldo
post Jan 16 2010, 05:35 PM
Post #5


Advanced Member
****

Group: Members
Posts: 124
Joined: 22-March 09
From: California, USA
Member No.: 8,132



I have debugged about 4 or 5 errors that came up, but then I hit a wall. Now I get an error saying "Parse error: syntax error, unexpected $end in /home/content/34/4806034/html/templates/Simple/master.tpl on line 114". Line 114 is the last line in the file. Below is the updated code for the whole page. I have researched a little into this, and have tried multiple solutions including checking the curly brackets, and have tried using <?php instead of <?.

CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta name="Description" content="Price Prospector.com is a price comparison site designed to compare prices on top products to help you save money." />
<meta name="Keywords" content="Price Prospector.com, priceprospector.com, price comparison, price, comparison, products, money, savings, shopping" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="templates/<?php echo $template; ?>/images/SimpleBlog.css" type="text/css" />
<link rel="stylesheet" href="myform/myform.css" type="text/css" />
<title>Price Prospector.com</title>
<script src="<strong>http://www.priceprospector.com/peel/peel.js</strong>" type="text/javascript"></script>  
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
<meta name="google-site-verification" content="5IOd9lgu02TzDl0535UJGSxf7gx2xtUX-XlBzmrpEuA" />
</head>

<body>

<!-- Wrap -->
<div id="wrap">
  <!-- Header -->
  <div id="header">

  
    <div id="searchform">
      <form method="get" class="search" action="search.php">
        <p><input name="search" style="width:275px; height:20px; -moz-border-radius:100px; -webkit-border-radius: 100px;" class="textbox" type="text" size="20" />
        <input name="s2" type="submit" value="Compare!" /></p>
      </form>
    </div>
  </div>
  <!--Content Wrap -->
  <!-- menu -->
<div id="menu">
    <ul>
      <li><a href="index.php"><span>Home</span></a></li>
      <li><a href="about.php"><span>About</span></a></li>
      <li><a href="partners.php"><span>Partners</span></a></li>
      <li><a href="contact_us.php"><span>Contact Us</span></a></li>
    </ul>
  </div>
  <div id="content-wrap">
    <div id="sidebar">
      <h1>Sponsors</h1>

<?php
$full=array("/index.php","/","/about.php","/contact_us.php");
$currentpage = $_SERVER['REQUEST_URI'];

if($full==$currentpage):
            echo '<marquee scrollamount="1" scrolldelay="10" direction="up" width="50" height="375" style="font-family: Verdana; font-size: 8pt">\n';
            foreach($cust_menu as $item):
            echo "<h1>$item</h1>/n<ul class=\"sidemenu\">\n";
                 foreach ($cust_menu_items as $i):
                 echo @$i[$item];
                 endforeach;
            echo "</ul>";
            endforeach;
            echo '</marquee>\n';
            
else:
            foreach($cust_menu as $item):
            echo "<h1>$item</h1>/n<ul class=\"sidemenu\">\n";
                 foreach ($cust_menu_items as $i):
                 echo @$i[$item];
                 endforeach;
            echo "</ul>";
            endforeach;
?>

<div align="center">
<script type="text/javascript"><!--
google_ad_client = "pub-4828388096208010";
/* Price Prospector, 160x600 Left Sidebar */
google_ad_slot = "9216889591";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

</div>

    </div>
    <div id="main">
      <?php echo $main_content; ?>
    </div>
    <!--End content-wrap-->
  </div>
  <!-- Footer -->
  <div id="footer">
    <p>©  
    <?php echo date("Y"); ?><strong>
    <?php echo $site_name; ?></strong></p>
  </div>
  <!-- END Wrap -->
</div>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10511612-1");
pageTracker._trackPageview();
} catch(err) {}</script>

    <script type="text/javascript">         var infolink_pid = 21322;         var infolink_wsid = 0;     </script>     <script type="text/javascript" src="http://resources.infolinks.com/js/infolinks_main.js"></script>

</body>

</html>


I usually just design simple web pages with CSS and HTML, so PHP is a fairly big step for me. As for @, I hired someone to build the basis of this code, so I don't know what the point of it is. I appreciate the help.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Jan 16 2010, 08:47 PM
Post #6


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



CODE
if($full==$currentpage):
            ...
else:
            ...
?>
Shouldn't there be an
CODE
endif;
here?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dante Monaldo
post Jan 16 2010, 09:50 PM
Post #7


Advanced Member
****

Group: Members
Posts: 124
Joined: 22-March 09
From: California, USA
Member No.: 8,132



Okay, so now that I have added the endif; I get another error message. "Parse error: syntax error, unexpected T_ELSE" The line that it is on just says "else:"
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Jan 17 2010, 11:35 PM
Post #8


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE(Dante Monaldo @ Jan 17 2010, 11:50 AM) *

Okay, so now that I have added the endif; I get another error message. "Parse error: syntax error, unexpected T_ELSE" The line that it is on just says "else:"


"Ah, well now this is debugging." The "else" is unexpected, which means that somehow the bracketing doesn't match. It's really your job to work out how it doesn't match... but it must mean that the 'else' is not preceded by if(_condition_) _single_thing_to_do_ Indenting will help, as will finding an editor with syntax highlighting.

Incidentally, I knew that @ suppresses error messages in function calls, but apparently it works the same for any expression. While you are degugging a program I would remove it, because if there is an error in the array evaluation somehow, you will not know about it.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dante Monaldo
post Jan 18 2010, 11:19 AM
Post #9


Advanced Member
****

Group: Members
Posts: 124
Joined: 22-March 09
From: California, USA
Member No.: 8,132



Ok. Well thanks for all the help. I really appreciate it.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 23rd April 2024 - 07:43 PM