Forum Moderators: coopster
Any sample scripts?
Another question:
Here is the situation: I want to create an <A HREF> but the link is inside the database. For example, I have a link text XYZ with visible link library.php?db=acts&tid=a3815 (when you point to it on the browser). However, the actual link, referenced by a3815 is stored inside the database.
How can I "call" the link from inside the database? So that when I click on XYZ on the browser, it would go into the database, check for the reference and then go to the link without opening another browser?
I hope this is clear. Thanks again for the help....
What do you mean when you said: "Have the href link to the db identifier for the record and have the page that does the diplay look at the post var identifier, grab the id from it, grab the data from the db and output it."
I'm just new with PHP and still experimenting as I go along. Thanks for the patience.
The only possible way would be to do an href to a dynamic page that would grab the href from the db and do an immediate redirect.
So the first href would be something like href=mydynamicpage.php?id=1. Now mydamicpage.php would take the id and look it up in the db assuming that id was an id field in the db. Now if the db has an href field in the db that is in the same record that the id field is you would do an immediate redirect with the link that comes out of the db. The redirect would be nothing more than echoing a header() with the new link being from the db.
Hope that makes a little more sense. You basically call the same php page but do a redirect after you have found what the real href should be.
JAG
As for the links, you need to access the values through the $HTTP_GET_VARS or the $_GET array and then query your database using those values. Read about them in the section on variables in the PHP manual.
Cheers,
Tom
Hi. Thanks for the help. I know its very newbie on my part, but I wasn't able to fully comprehend your explanation. It's me. I know.
However, it gave me some idea to do this:
This is my reference: lib.php?db=main&tid=001
lib.php contains this:
<?php
$id = $_GET["tid"];
$db_to_use=$_GET["db"];
$dbcnx = mysql_connect("localhost")
or die("<br> <br>" . "Could not connect: " . mysql_error() . "<br> <br>");
mysql_select_db("site_db") or die(mysql_error());
$result = mysql_query("SELECT * FROM $db_to_use where id=\"$id\"");
if (!$result) {
echo("<P>Error performing query: " . mysql_error() . "</P>");
exit();
}
while ( $row = mysql_fetch_array($result) ) {
include($row["ref"]); }
?>
I know that this is not what you adviced but it worked like magic. However, when I type the URL from the browser, the graphics gets messed up. I know this is because the location of the graphics are inside a folder (one level down from the root), the location of the file "include"d are two levels down from the root, and the location of lib.php is on the root.
I was also able to resolve this by using absolute reference for all graphics, files, and menu items, for example, [localhost...] etc. But if I would have 1,000++ pages, that would sure drive me crazy changing the references before I upload it.(By the way, I'm using my workstation to do the site then I just upload it. I'm not even sure if I was able to set up PHP well, but its working, I think.)
At any rate, my questions are:
1. Am I just stupid or what? :-)
2. Really, any suggestions on how I can use relative references (for example, ../../graphics/image1.jpg)
3. Or if you have better ideas, can you help with at least a more detailed sample?
4. Would this affect the loading performance since it goes into the db first then, loads the page, as oppose to going directly to the page or going inside the database and retrieving and displaying the information from there?
I know that this is a lot already, but thanks for the help.
I was also able to resolve this by using absolute reference for all graphics, files, and menu items, for example, [localhost...] etc. But if I would have 1,000++ pages, that would sure drive me crazy changing the references before I upload it.
No problem really. What I do is simple. I have an include file called
C:\dir1\dir2\local\site-specific-settings.php
(okay, so it's not really called that, but you get the idea). Anyway, it contains a constant (acutally, lots of them)
define ("DB_NAME", "mydb");
define ("SITE_ROOT", "http://localhost/site1_root/");
Then I check to see whether that file exists and, if it does, I include it. That sets those constants. Since they are constants you can't override them. I set the same constants to different values in a file that is always included, and those constants define the same paths and such, but for the live server as in
define ("SITE_ROOT", "http://www.mycoolsite.com/");
Now the statement
echo "<img src=\"". SITE_ROOT . "images/myimage_lg.jpg\" />";
is always valid. Of course, absolut paths increase page weight from a little to a lot, depending on the number of links, but you can use the same technique for relative paths.
If you want to use relative paths on dynamic pages, I prefer to display all pages from templates that are always in the same directory and therefore the relative paths are always the same for every page. The page-specific data can be in files or the database, but the page that the URL names is one of a very few templates in a single dir.
T
1. Am I just stupid or what? :-)2. Really, any suggestions on how I can use relative references (for example, ../../graphics/image1.jpg)
3. Or if you have better ideas, can you help with at least a more detailed sample?
4. Would this affect the loading performance since it goes into the db first then, loads the page, as oppose to going directly to the page or going inside the database and retrieving and displaying the information from there?
ergophobe made a suggestion that certanly would work just fine for you.
1. Nope. We all started from scratch ourselves :-)
2. Yes you can do that.
3. I really just use an image directory so all my images are /images/myimage.whatever.
4. Not really. I have been extremely happy with MySql. Technically any extra calls to do anything will cause a slowdown but MySql does a query cache so it is extremely fast after the first query. Just make sure to optimize it everyway you can.
Your include works also and will continue to. Just my preference to do a header redirect.
You may want to change the way you are connecting to the db though. Allowing the db to be specified with a get var opens you up to potential problems from less than honest people.
JAG
3. I really just use an image directory so all my images are /images/myimage.whatever.
My problem with this is that I have my
[localhost...]
[localhost...]
nameofsite=domain so in the live version, that path is the equivalent of
[nameofsite1.com...]
So if on the domain, I have images in
/images
Then the absolute path is /images/image1.jpg
Locally, however, the path is /nameofsite1/images/image1.jpg
By using the constants to override the default path, it works both locally and live without changing anything. The only thing that matters is that the path to the file that overrides the live constants is only valid on the local version.
So that way, if I have
if file exists, load local.php and set constants
define('IMAGE_DIR', "/nameofsite/images/");
load live.php and try to set constants, but can't if already set
define('IMAGE_DIR', "/images/");
now in page.php
echo '<img src="' . IMAGE_DIR . 'image1.jpg" />';
works everytime without having to change "1000++ pages" just because you upload to the server. The method works with absolute or relative paths, which I see was not the way it sounded from my last post.
Tom
Although it took me some time to look it up, I already got the immediate redirect that you suggested.
ergophobe,
The site-specific-settings.php worked like a charm. And you are also right about the page weight.
Thanks to both of you (as well as all the people behind Webmaster World). I couldn't have done it without your help and patience.
Yet, seeking another advise...
The site we are creating actually has a layout (header, footer, and left menu). Initially, what we are doing was just reference from the browser (for example: lib?db=main&id=1) with a simple script that would go inside the database, look for the same id and then output the content via include() command. The lib.php contains the layout.
So the structure of the db contains the ff: fields
id
title
location
where: location is the URL of the file to be included.
This output technically retains the layout, then the output would just be inserted in an alloted space for the content.
This solution actually was the easiest, not to mention, the lowest file sizes, and is actually working. However, the problem is on the Search part. We have a third-party CGI script that does the searches. However, when the file that we include is found, the output does not contain the layout, just the raw file. What should happen is that the layout should be included.
Now going back to my main concern, how can I do a site search with an output similar to Yahoo! using PHP? I want to display the Title of the page, some texts from the meta description, the percentage and of course, the link? Any sample scripts? Although I'm currently reading on the $HTTP_GET_VARS.
Although Atomz would be okay, I don't think it would be suitable for the site we are creating. Plus the fact that I want to learn how to go about with creating my own search script. Any other suggestions?
Thanks again.
You said: "You may want to change the way you are connecting to the db though. Allowing the db to be specified with a get var opens you up to potential problems from less than honest people. "
The db_to_use actually refers to the tables inside the database because I have multiple tables.
Are you referring to this? If that is so, how can I make sure that no problems would arise from the opening of the db?
Thanks also.