Forum Moderators: coopster
if ($goto)
{
$goto = true;
$Query = "SELECT * from $TableName where (pgname='$goto')";
}
else
{
$Query = "SELECT * from $TableName where (pgname='r_home')";
}
ENDS
I am making a dynamic site, and I want it so that if $goto (eg 'navi.php?goto=A_PAGE') is true, as in pgname (eg where pgname=$goto) in the DB exists, it will call up that row within the DB.
If not, or if the value is false, as in pgname doesnt exist, it will go to the home page (navi.php?goto=index)
Any help?
if ($goto)
{
#temp query tests for existence of $goto in db
$QueryA="select * from $TableName where pgname='$goto'";
#use your database's syntax to check for db entry
#run the temp query to get "db_entry_ok"
if ($db_entry_ok) {
$topage=$goto;
} else {
$topage=$r_home;
}
}
else {
#no $goto, so default to $r_home
$topage=$r_home;
}
#write the "real" query using the result
$Query="select * from $TableName where pgname='$topage'";
The result of the final $Query will be for either the value of $goto, if it was a valid value in the db, or the value of $r_home (which I assume is your home page URI)
I used a new variable named "$topage" (to page) to hold the end result of the investigation into whether (a) there is a $goto variable being passed, (b) and if there is, whether it is a valid db entry, or (c) if not a valid db entry or no $goto being passed.
Like this:
#initialize $topage with $home_page value
$topage=$home_page;
#there IS a $goto variable in URI
if ($goto) {
#check for valid $goto value in db
$queryA="select * from $table where pgname='$goto'";
#set $topage to a new value, depending on the result
#valid $goto = set $topage=$goto
if ($goto_is_in_db) { $topage = $goto; }
#invalid $goto = set $topage=$home_page
if ($goto_is_not_in_db) { $topage = $home_page; }
}
#no $goto in URI = set $topage=$home_page
else { $topage = $home_page; }
#write final query using whatever value $topage is
$query="select * from $table where pgname='$topage'";
By using a variable name independent of the two potential "real" variable names, we can write a simple query that plugs in either the $goto passed and validated or the default home page with minimal code.
The final $query does not need to ever change, regardless of how many variables go into figuring it out, because the "correct" variable is always sent to it, after figuring out which is the "correct" variable.
Using this method, we can write one temp query to test whether the $goto is actually in the db, and one final query to get the "correct" page. Instead of writing four queries (is $goto a valid page, $goto is a valid page, $goto is not a valid page, no $goto) we write two (is $goto a valid page, get "correct" page).
This approach separates the figuring out of the "correct" page value from the query that gets the db data for the "correct" page, and cuts down on the amount of typing you need to do by cutting four queries down to two.
#initialize "to page" variable
$topage=$home_page;
takes care of setting the "to page" variable to the default in case all else fails, so setting $topage to $home_page after initializing it with that value during the previous script is not required.
I wrote the script above as I did because I wanted to illustrate the entire logic cycle.
Here is the abbreviated version:
=== START SCRIPT ===
#set the "to page" variable to the default
$topage=$home_page;
#check for (and find) the $goto parameter (in the URI, cookie, etc.)
if ($goto)
{
#check for $goto in db
$query="select * from $tablename where pgname='$goto'";
#run the query and set $goto_in_db to 1 if found
#the syntax depends on the db format
#if $goto_in_db=1 (value was found in the db)
if ($goto_in_db) {
#reset the "to page" variable to the $goto value
$topage=$goto;
}
}
=== END SCRIPT ===
At the end of this script, the "to page" variable ($topage) will be one of two things: It will still be the default ($home_page) OR it will have been reset to the value of $goto, because (1) $goto was a URI parameter AND (2) the $goto value was found in the database.
That's it.
It is valuable to have 3 variables for this. These are the reasons for using the new $topage variable instead of just resetting the existing $goto and $r_home variables to new values:
$goto
was (or was not) passed by a previous page, and should probably be maintained. If it was important enough to gather in the first place, it may be useful in some following sequence of events, to dump into a db, add to a cookie, modify future pages' content, etc.
$r_home
may be set by a global configuration script, and should remain stable regardless of the twists and turns a visitor's path might take.
$topage
collects the current, dynamic variable value set by whatever procedure you desire without impacting the other two variables. If a condition is not met, the default remains the same (home). If something in the visit triggers a page-change, this variable is ready to accept that value to alter the visitor's experience without disturbing your tracking, root path information, etc.
That REALLY is it.
I tried copying the 2nd script straight into my php file and i got this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/firestar/public_html/navi.php on line 128
So, lets go over it again:
// so this $home_page value should = my home page yea?
// therefore...
$home_page="index";
$topage=$home_page;
if ($goto)
{
// so now its checking to see whether $goto is true or not and if it exists in the DB rite?
$query="select * from $tablename where pgname='$goto'";
// so now, if $goto is found and =1, this query will run
if ($goto_in_db) {
$topage=$goto;
}
}
-- ends
ok, where are the else statments? should i inculde any?
what about the final query?
eg
$query="select * from $tablename where pgname='$goto'";
sorry, i just learnt php about a week ago!
First, become familiar with: [php.net...] and the PHP Manual you will find there. Also, bookmark [dev.mysql.com...] and download the MySQL Manual.
You don't want to be stuck copying-and-pasting like some "script-kiddie". You want to understand the languages so you can fix stuff and write new scripts.
Okay, on with the tutorial:
Here is what I am calling stuff for this example:
home page = "index"
database = "sampledb"
table name = "pagetable"
table columns = id, pgname
So here is the (fake) URI for the example:
/navi.php?goto=PlasticToys
You (a) are wondering if $goto is in the URI (it is, in this case), (b) are wondering if the value of $goto is in your database and then (c) wish to pull the relevant data from the database either for the validated $goto page or from the default "index" page.
Here is the script to find out if "PlasticToys" is a valid page, and if it is, query the db for its elements and pull them out for use in the page:
======SNIP======
<?php
#connect to the database (use real information)
$getDB=mysql_connect("mysql_host", "mysql_user", "mysql_password")
mysql_select_db("sampledb");
#initialize "to page" variable
$topage="index";
#test for $goto
if ($goto)
{
#YES $goto, so query db for valid page name
$checkPage=mysql_query("select id from pagetable where pgname='$goto'");
#result of test stored in $goto_in_db
$goto_in_db=mysql_num_rows($checkPage);
#if any rows are returned by the query, $goto_in_db is more than 0 (true)
#if no rows are returned, $goto_in_db is 0 (false)
#if $goto_in_db is true, reset "to page" value
if ($goto_in_db)
{
$topage=$goto;
}
}
#at this point, either the "to page" variable is still "index"
#or it has been reset because of a $goto match in the db
#now set up the query to get the page data
$query="select * from pagetable where pgname='$topage'";
#run the query
#this is a sample, just to show you it is working
$getPageData=mysql_query($query);
while($row=mysql_fetch_array($getPageData)) {
$id=$row["id"];
$pgname=$row["pgname"];
echo "ID = ".$id."<br>\n";
echo "PGNAME = ".$pgname."<br>\n";
}
#clear the query result out of your computer's memory
mysql_free_result($getPageData);
#disconnect from the database
mysql_close($getDB);
?>
====== SNIP ======
You will need to make changes to the above, using a real mysql server address, a real mysql user and password, a real database name with real tables, and the data you seek needs to be in that database. After all of that, you should be on your way.