Jump to content

Php Question


chadf

Recommended Posts

I am trying to find a way to assign to a variable the root file name from within a PHP included file. I use a header file (header.php) for all pages on my site. I want to assign a variable in header.php to refer to the file that actually included it (ie. /index.php, /feedback.php, etc). When I use $PHP_SELF or $REQUEST_URI, I do not always get the correct file name. These only work for files that are located in my public_html folder.

 

Does this make sense?

 

- Chad

Link to comment
Share on other sites

I've just setup a dummy file where I set the document title equal to the variable PHP_SELF

$page_title=$_SERVER['PHP_SELF'];

and it works for me including the directory it is in.

 

Can you give an example of when it doesn't work?

 

Andy

Link to comment
Share on other sites

When it works, I'm including the header (/header.php) from a file in the root directory. When it doesn't work, I'm including the header from another included file located in a subdirectory. In the latter case, $PHP_SELF always returns "/header.php". Is the subnesting my problem, or is it because of the file location?

 

- Chad

Link to comment
Share on other sites

I've just tried with nested included header files, and I still find that php_self works giving the right file name.

 

Are you happy to post the code here (if not to long), or PM me and I will have a quick look at it.

 

Andy.

 

PS. The code I have just used to test this is;

 

main file

<.?php

$page_title=$_SERVER['PHP_SELF'];

include $_SERVER['DOCUMENT_ROOT']."/inc/headtst.php";

?>

 

<.?php

include $_SERVER['DOCUMENT_ROOT']."/inc/footer.php";

?>

 

 

header file

<.html>

<.head>

<.title><.?php echo $page_title; ?><./title>

<./head>

 

<.body>

  a bit of body info

<.br>

<.p>from within the header<.?php echo $_SERVER['PHP_SELF']; ?><./p>

<.br>

<.?php  include $_SERVER['DOCUMENT_ROOT']."/inc/tst2.php"; ?>

 

the nested include file

<.br>

<.p>from within the nested file<.?php echo $_SERVER['PHP_SELF']; ?><./p>

<.br>

 

Please excuse all the "." in my code - it didn't want to display all the code...

 

Andy

Link to comment
Share on other sites

I'll post some of the code later, if I have time. But where I'm running into trouble is using Gallery. Gallery is located in a subfolder (/gallery). In /gallery, index.php includes albums.php, which includes gallery.header (located in /gallery/html_wrap), which is where I am including my header.php (located in /). It works just fine, but when I use $PHP_SELF in header.php, it always returns "/header.php" from the gallery index page.

 

- Chad

Link to comment
Share on other sites

Sorry I didn't jump in ealier... I was working on a client's website all day yesterday.

 

Here's another idea that should be foolproof (although I think Andy's method should work too). It takes a little re-coding.

 

Rather than having the included header file try to figure out which page included it, maybe you could try assigning the current page url to a variable, and then including the header. The header can access that variable.

 

I do something similar when I assign the page title and meta tags before including the header file. The header takes the variables and produces my meta tags for me and sets the page h1 tag.

 

More than one way to skin a cat.

 

Andy, kudos to you for your hands-on work here! I'll buy you a pint the next time you're in Florida.

Link to comment
Share on other sites

surefire,

 

Thanks, it's about 2 years since I've been over to Florida (thoroughly enjoyed it), but you've just given me another good reason to think about going back :D

 

I'm starting to get the hang of PHP and it's good practice for me to delve in occasionally B)

 

I must admit I do it the way you have suggested, set a variable for the page title, and pass that to the header, then use it for meta tabs and the h1 tab - makes search engine optimisation much easier.

 

Andy

Link to comment
Share on other sites

Rather than having the included header file try to figure out which page included it, maybe you could try assigning the current page url to a variable, and then including the header. The header can access that variable.

 

Thanks for the help. I ended up using the variable method, since I could never get $PHP_SELF to work consistently. I was trying to avoid having to go into each file to assign a variable, but at least my site is not TOO big so it didn't take that long.

 

In case you're wondering, I was just trying to code my own site navigation link bar, which would change the look of the link for the current page. I'm sure there are some great scripts out there that already do this, but I've been enjoying learning PHP (and CSS) by doing my own coding. Although I'm not quite ready yet to write my own photo gallery script - GALLERY already performs magnificently for this!

 

I sure don't miss FrontPage!!!

 

- Chad

Link to comment
Share on other sites

Hello everybody,

 

I stuck with UPDATE function. Script to edit/update links that are being exchanged with another websites.

Here is the coding of two scripts:

1-script Actual link modification form

2-script Updating record in the links table.

 

1 script

><?php
include "../config.php";
echo "<p><b>Edit Link Information</b></p>";
$ql=mysql_query("select * from $t_links where id=$_POST[id]");
$qlrow=mysql_fetch_array($ql);
$id=$qlrow['id'];
$title=$qlrow['title'];
$url=$qlrow['url'];
$descr=$qlrow['descr'];
$check=$qlrow['check'];
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="update_links.php" method="post">
 <p>Title 
   <input name="title" type="text" id="title" value="<?php echo "$title"; ?>" size="100">
 </p>
 <p> URL 
   <input name="url" type="text" id="url" value="<?php echo "$url";?>" size="100">
 </p>
 <p>Description 
   <textarea name="descr" cols="60" rows="4" id="descr"><?php echo "$descr";?></textarea>
 </p>
 <p>Backlink checked 
   <input name="check" type="checkbox" id="check" value="<?php echo "$check"; ?>" <?php if (!(strcmp("$check","YES"))) {echo "checked";} ?>>
 </p>
 <p>
   <input name="id" type="hidden" value="<?php echo "$id"; ?>"><input name="Update" type="submit" id="Update" value="Submit">
 </p>
</form>
</body>
</html>

 

2-script

<?

include "../config.php";

 

$sql="UPDATE $t_links SET title='$_POST[title]', url='$_POST

 

looks%20like%20stuck%20with%20the%20checkbox.%20Here%20is%20the%20error%20that%20I%20get:

You%20have%20an%20error%20in%20your%20SQL%20syntax.%20Check%20the%20manual%20that%20corresponds%20to%20your%20MySQL%20server%20version%20for%20the%20right%20syntax%20to%20use%20near%20'check='YES'%20where%20id='1''%20at%20line%201

 

For%20experiments%20please%20choose%20Real%20Estate%20category,%20then%20South%20Caroline%20subcategory%20and%20then%20you%20may%20play%20with%20the%20first%20link%20in%20that%20category.%20The%20website%20is%20actually%20just%20opened%20and%20the%20ino%20is%20kinda%20for%20real%20:)

 

Here%20is%20the%20link%20to%20actual%20pages:

%5burl=http://www.c21myrtlebeachhomes.com/admin/cat_links.php%5dhttp://www.c21myrtlebeachhomes.com/admin/cat_links.php" target="_blank">', descr='$_POST[descr]', check='$_POST[check]' where id='$_POST[id]'";

$result=mysql_query($sql, $dbh) or die(mysql_error());

#header ("Location: show_links.php");

#exit;

?>

 

looks like stuck with the checkbox. Here is the error that I get:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'check='YES' where id='1'' at line 1

 

For experiments please choose Real Estate category, then South Caroline subcategory and then you may play with the first link in that category. The website is actually just opened and the ino is kinda for real :(

 

Here is the link to actual pages:

http://www.c21myrtlebeachhomes.com/admin/cat_links.php

 

 

Thanks for any suggestions.

Link to comment
Share on other sites

Currently, I'm in a rush and don't have time to trouble shoot your code.

 

But here is the process I would use to figure out what the problem is.

 

I'd take out all the variable stuff in the UPDATE line of your code and replace them with static values just to test to see what's going on. The way I do it is to comment out the line you have by putting '//' in front of the line so that when you're done testing you can remove the comment slashes and you're back in business.

 

Then if that works, I'd start putting the variables back in one at a time to see what's causing the error.

 

One thing I'd try first would be to change 'where' to 'WHERE'

 

Don't know if that affects anything but as a practice, I always capitalize mysql syntax.

Link to comment
Share on other sites

The script actually works. I tryed like you Surefire said. And finally I put back all the variavles back except for "check" one. and it let me to make chamges on alll the foelds back and forth. I put "check" column back - and it's the same error. The error show that check column gets the data but it referres to SQL syntax error. Well, i'm thinking maybe I need to use another name for the column ensted of "check" - maybe there is a PHP command like that. I'll try that later - right now I can't log in into my cpanel office - have no idea what's wrong with that.

 

Anyway the script is working, just need to figure out only with this sertain variable.

Link to comment
Share on other sites

Hi everybody.

Have another question:

 

I'm trying to use a combobox with JumpMenu on change. For example its easy if you use just a combobox and submit button then you just do "post" and you can obtain your variable from the combobox as $_POST[variable]. But I'm trying to have two comboboxes with jump menu that i could choose a category in one and subcategories would appear in another one. I'm putting value in the JumpMnue as "index.php?category_id=$qcrow[id]" and it works - I can see that url path changes depending on your choice in the combobox, but I cant figure how to get that variable for query for the other combobox to show all the subcategories - I can't use $_POST and trying to use the same $qcrow[id] variable to make query for the other combobox to show the subcategores.

 

Here is the page

Here is the source - it's a mess, sorry

><script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
 eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
 if (restore) selObj.selectedIndex=0;
}
//-->
</script>
<?
include "../config.php";
?>
<p><b>Edit Links</b></p>
<p><a href=cat_links.php>Choose another category</a></p>
<?
if (!isset($id) && !isset($sid)) {
#choosing category
$qc=mysql_query("select * from $t_category order by cat");
while ($qcrow=mysql_fetch_array($qc)){
$id=$qcrow['id'];
$cat=$qcrow['cat'];
$option_block1.="<option value=check.php?cid=$qcrow[id]>$cat</option>";
}
?>
<form name="form1">
Category 
 <select name="cat" id="cat" onChange="MM_jumpMenu('parent',this,0)">
   <option value="#" selected>Choose category</option>
<?php echo "$option_block1";?>
 </select></form>
<?
#choosing subcategory
$qs=mysql_query("select * from $t_subcategory where cid=$qcrow[id] order by sub");
while ($qsrow=mysql_fetch_array($qs)){
$sub=$qsrow['sub'];
$sid=$qsrow['id'];
$option_block2.="<option value=check.php?sid=$sid>$sub</option>";
}
?>
 <form name="form2">
 Subcategory 
 <select name="menu2" onChange="MM_jumpMenu('parent',this,0)">
   <option value="#" selected>Choose subcategory</option>
<?php echo "$option_block2";?>
 </select>
</form>
<?
#showing links by chosen subcategory
}elseif (isset($id)) {
$ql=mysql_query("select * from $t_links where sid='$_POST[list]' order by id");
while ($qlrow=mysql_fetch_array($ql)) {
$id=$qlrow['id'];
$title=$qlrow['title'];
$url=$qlrow['url'];
$descr=$qlrow['descr'];
$check=$qlrow['backlink'];
?>
 <table width="100%" border="0" cellspacing="2" cellpadding="2">
 <tr> 
   <td width="30" bgcolor="#000099"><form name="form2" method="post" action="<?php "$_SERVER[PHP_SELF]"; ?>">
       <input type="submit" name="Submit" value="Ok">
       <input name="mark" type="checkbox" id="mark" value="YES" <?php if (!(strcmp("$check","YES"))) {echo "checked";} ?>>
     </form></td>
   <td width="50" bgcolor="#99CCFF"><? echo "$check"; ?></td>
   <td width="30%" bgcolor="#CCCCFF"><? echo "$title"; ?></td>
   <td width="25%" bgcolor="#CCCCFF"><? echo "$url"; ?></td>
   <td bgcolor="#CCCCFF"><? echo "$descr"; ?></td>
   <td bgcolor="#CCCCFF">
<form name="form3" method="post" action="">
       <input type="submit" name="Submit2" value="Edit">
     </form> </td>
 </tr>
</table>
 <?
}
}
echo "<a href=cat_links.php>Choose another category</a>";
?>
<?
$sql="UPDATE $t_links SET backlink='$_POST[mark]' where id='$id'";
$result=mysql_query($sql, $dbh) or die(mysql_error());
?>

 

Thanks for any ideas

 

P.S. I'm just trying to put all steps of choosing category, then subcategory and then showing links for editing on one page

Link to comment
Share on other sites

Hi Jack,

 

click on the link that I posted with the script and you'll get the idea if what I was asking.

 

In the subcategory query I set a constant that referres to the 1st subcategory. But I what I'm trying to do, instead the constatnt in the query I need variavle that picks up from choosing of category in the first combo box.

 

It still remain a problem for me.

 

Thanks

Link to comment
Share on other sites

First, you should strongly consider changing the form a tiny bit so that when the first category is selected, the chosen option is prefilled in the box or in some way indicate that the selection was received.

 

Currently, the url for your form changes, but for a while, I thought it wasn't working because I'd select a category and it looked like it would revert back to Home Related.

 

Now...

 

I don't have a firm grasp or the data you're trying to present for your subcategory.

 

If you have it in a database, then you just need to write your code to pull the subcategories based on the cid variable.

 

There is no reason why you can't pass both variables to another page, or the same page.

 

One way would be to use a hidden variable in the form and use a php print statement to get the cid into the form when the user posts the subcategory variable. That's usually the way I do it.

 

Hope that helps and best of luck with your script.

Link to comment
Share on other sites

Guest Guest

I'd like to pull info of db in reverse order. For example to sort everything by date and show it on the page from newer date to old. Is there a fuction that can reverse the order?

 

Thanks

Link to comment
Share on other sites

Received the following error on the webpage:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/xxxx/xxxx/test/auth_user.php on line 29

 

Here's the code in question:

>// define function auth_user()
function auth_user($username, $userpassword) {
// Set up global variables to connect to the database and "Referee" table
global $default_dbname, $user_tablename;

$link_id = db_connect($default_dbname);

/* Now that we're connected, create a query for a record whose "username"
   and "userpassword" entries correspond to the user-provided arguments,
   then select the Tempusername value for that record. 
   Note that $userpassword is encrypted BEFORE it is compared with the value
   in the Referee table.  This compares two encrypted values which have been
   derived by exactly the same process and will therefore (in theory anyway!)
   be identical.  */
$query = "SELECT Tempusername FROM $user_tablename
         WHERE username = '$username'
         AND userpassword = password('$userpassword')";
$result = mysql_query($query);
/* If no records match both criteria, we should return "False".  Otherwise,
   we fetch and return the "Tempusername" value for that record. */
if(!mysql_num_rows($result)) return 0;
else {
 $query_data = mysql_fetch_row($result);
 return $query_data[0];
}
}

 

I know I'm missing something with the mysql_num_rows function, but my mind is fried from looking at this all night last night and most of today.

 

Any thot's or helpful words would be MOST helpful!

Link to comment
Share on other sites

ok...answered my own question. The SELECT query was selecting a column name that wasn't in the table. That problem solved. Now, trying to figure out WHY no data was returned. I verified that there is data in the table and I was entering the correct information. Any PHP guru's think you can help by looking at the code?

 

(All part of the fun of learning PHP!)

Link to comment
Share on other sites

I don't know if it's the caffine kicking in or what, but I'm making headway. I've determined that for some reason, the password() server function was causing the problem. I solved this problem by setting my field type in my table to varchar(16) (it was set to a length of 15) and set the attributes to BINARY.

 

I'm assuming that the field type needed to be a length of 16 because of the BINARY attribute, but I really don't understand the PASSWORD() function all that well. Can anyone explain a little better than the book(s) I have so I can understand the why's and wherefore's? Is it really THAT secure or is it just a "warm fuzzy"?

Link to comment
Share on other sites

  • 2 weeks later...

Hello everybody.

 

This is probably a lame question, but I don't know the answer:

I'm using INCLUDE function in my pages and sometimes including pages from subfolders which makes pictures disappear form the header. When I look at the properties I can see that its trying to read the file from the folder where the included page is - of course I always can provide a full path with the domain name for each picture, but I think there is other way to do it right. Is there a way to include pathes somewhere that I just could use something like /images/aaaa.jpg and not entering *****/images/aaaaa.jpg ?

 

Thanks

Link to comment
Share on other sites

  • 5 weeks later...

I am new at php and am having trouble getting a .php header to work. To make the header.php file, I took all of the code (see below) from top of my original page, placed it into a header.php file and then inserted the following php call into my remaining page:

 

><?php
include $_SERVER['DOCUMENT_ROOT']."/scripts/header.php"; 
?>

'plus remaining page code here

 

Unfortunately, all I get is a page that displays only the remaining page code and none of the code in the header.

 

Any thoughts on what I am doing wrong?

 

Thanks in advance!

 

- Webnovice

 

Oh, and the code in the header.php file is as follows:

 

>--------------------------------------------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Mentor Resources</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<STYLE type=text/css>
A:link {BACKGROUND: none transparent scroll repeat 0% 0%; COLOR: #0000ff; TEXT-DECORATION: underline}
A:visited {BACKGROUND: none transparent scroll repeat 0% 0%; COLOR: #bc0021; TEXT-DECORATION: underline}
A:hover {BACKGROUND: #bb0000; COLOR: #ffffff; TEXT-DECORATION: underline}

</STYLE>

<META content="Microsoft FrontPage 4.0" name=GENERATOR></HEAD>
<BODY bgColor=#bc0021>
<TABLE cellSpacing=2 cellPadding=2 width="96%" align=center border=0>
 <TBODY>
 <TR>
   <TD>
     <TABLE cellSpacing=2 cellPadding=2 width="96%" align=center border=0>
       <TBODY>
       <TR vAlign=bottom>
         <TD><IMG height=50 alt="" src="http://www.ihadepa.org/images/title6.gif" width=250 
           border=0></TD><Td></td></tr>
<tr>      <TABLE cellSpacing=0 cellPadding=0 width="95%" align=center 
     bgColor=#ffffff border=0><tbody><tr>
         <TD valign=bottom align=center><FONT face=Arial color=#bc0021 size=1> <b>  |  <A href="http://www.ihadepa.org/index.htm"><FONT 
     face=Arial size=1>Home</FONT></A>  |  <A 
     href="http://www.ihadepa.org/Programs.htm"><FONT 
     face=Arial size=1>School Programs</FONT></A>  |  <A 
     href="http://www.ihadepa.org/Partners.htm"><FONT 
     face=Arial size=1>Partners</FONT></A>  |  <A href="http://www.ihadepa.org/News.htm"><FONT 
     face=Arial size=1>News</FONT></A>  |  <A 
     href="http://www.ihadepa.org/Contact.htm"><FONT 
     face=Arial size=1>Contact Us</FONT></A>  |  <A href="http://www.ihadepa.org/Help.htm"><FONT
     face=Arial size=1>How to 
Help</FONT></A>  |  <A href="http://www.ihadepa.org/Mentors/index.htm"><FONT
     face=Arial size=1>Mentors</FONT></A>  |  <HR align=left valign=top width="100%" color=#bc0021 noShade SIZE=2 VSPACE="0"></b></td>
</tr></tbody></table>

Save file as:  

Link to comment
Share on other sites

webnovice sent me an email with some additional details, like the url. When I checked the url, it appeared that the script was working fine.

 

This leads me to believe that this was a site propogation issue. Some scripts won't work until the site has fully propogated... this is one of them.

 

Webnovic, if I'm wrong about this or I'm missing something, then please let us know and please give the url.

Link to comment
Share on other sites

Thanks for the fast response and sorry about the, er, missing link. The url where I tried to implement the php header is:

 

http://www.ihadepa.org/test.htm

 

This url is intended to look identical to this page:

 

http://www.ihadepa.org/sample.htm

 

As of this morning, the two pages (at least on my machine) do not look the same and I cannot figure out why.

 

As for propogation, my site has been up for a couple of weeks now, so it would be strange if that were the issue (though it is quite possible that I am misunderstanding what full propoagation is).

 

As always, any thoughts or insights would be greatly appreciated.

 

Thanks!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...