Jump to content

PHP Page Index.php?=variable Question


rwachowiak

Recommended Posts

Ok, so ive got my page:

 

<!--- INCLUDE THE HEADER -->
<?php include("incs/inc_header.php"); ?>
<!--- INCLUDE THE HEADER -->

	<?php include 'libs/dbopen.php';

	$query  = "SELECT * FROM files";
	$result = mysql_query($query);

	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
echo 	"File Name: <span class='titlebar2'><a href='indextest.php?id={$row['id']}'>{$row['Filename']}</a></span> <br>" .
	"Keywords: <span class='description'>{$row['Keyword1']}, {$row['Keyword2']}</span> <br>" .
	"Description: <span class='description'>{$row['Description']}</span> <br>" .
	"Date Added: <span class='description'>{$row['DateAdded']}</span> <br><br>";
	}
	include 'libs/dbclose.php';
	?>


<!--- INCLUDE THE RECENT DOCS -->
<?php include("incs/inc_recent.php"); ?>
<!--- INCLUDE THE RECENT DOCS -->

<!--- INCLUDE THE NAVIGATION -->
<?php include("incs/inc_nav.php"); ?>
<!--- INCLUDE THE NAVIGATION -->

<!--- INCLUDE THE WIDGETS -->
<?php include("incs/inc_widgets.php"); ?>
<!--- INCLUDE THE WIDGETS -->

</body>
</html>

 

And what i want to do is, when someone clicks on certain links on the page, say, "Tax" "Audit" "Engagement" etc, that the page changes to: Index.php?=Tax, or the corresponding link etc. And when that happens, the query changes to reflect that variable, so it only pulls certain documents from the query that only pertain to the link that was clicked.

 

Can someone point me in the direction of a good tutorial on how to teach me to do such things? Or give me some start off code?

 

Thanks!

Link to comment
Share on other sites

an added note to neptunus maris ' example,

 

when using any $_GET/$_POST or even ANY data directly from a user, always use "mysql_escape_string()" before inserting it into the query (you must be connected to mysql for this function to work).

 

eg:

 

<?php

// connect

$Query = "SELECT * FROM `tablename` WHERE `username`='".mysql_escape_string($_GET['user'])."';";
$Results = mysql_query($Query);
print_r($Results);

?>

 

this code would grab user details depending on which user the client has specified;

 

http://localhost/test.php?user=rwachowiak or

http://localhost/test.php?user=uniflare

 

 

hope this helps,

Link to comment
Share on other sites

this is html, whereever your menu is just change the links inside.

 

<!--- INCLUDE THE NAVIGATION -->
<?php include("incs/inc_nav.php"); ?>
<!--- INCLUDE THE NAVIGATION -->

 

this is your menu/navigation, give us this file: incs/inc_nav.php

 

it will be a lot harder to customize a script to do what you are wanting than to create a switch script just for this purpose.

Link to comment
Share on other sites

this is html, whereever your menu is just change the links inside.

 

<!--- INCLUDE THE NAVIGATION -->
<?php include("incs/inc_nav.php"); ?>
<!--- INCLUDE THE NAVIGATION -->

 

this is your menu/navigation, give us this file: incs/inc_nav.php

 

it will be a lot harder to customize a script to do what you are wanting than to create a switch script just for this purpose.

 

yep ok here you go:

 

	<div class="titlebar">Browse by Category</div>
<!-- Keep all menus within masterdiv-->
<div id="masterdiv">

<div class="menutitle" onclick="SwitchMenu('sub1')">
<img src="media/navigation/tax.jpg" align="absmiddle" />  Tax</div>
<span class="submenu" id="sub1">
	- <a href="new.htm">Popular Tax Wesbites</a><br>
	- <a href="hot.htm">What's hot</a><br>
	- <a href="revised.htm">Revised Scripts</a><br>
	- <a href="morezone/">More Zone</a>	</span>

<div class="menutitle" onclick="SwitchMenu('sub2')">
<img src="media/navigation/audit.jpg" align="absmiddle" />  Audit</div>
<span class="submenu" id="sub2">
	- <a href="notice.htm">Usage Terms</a><br>
	- <a href="faqs.htm">DHTML FAQs</a><br>
	- <a href="help.htm">Scripts FAQs</a>	</span>

<div class="menutitle" onclick="SwitchMenu('sub3')">
<img src="media/navigation/humanresources.jpg" align="absmiddle" />  Human Resources</div>
<span class="submenu" id="sub3">
	- <a href="http://www.codingforums.com">Coding Forums</a><br>
</span>

<div class="menutitle" onclick="SwitchMenu('sub4')">
<img src="media/navigation/computer.jpg" align="absmiddle" />  Computer</div>
<span class="submenu" id="sub4">
	- <a href="http://www.javascriptkit.com">JavaScript Kit</a><br>
	- <a href="http://www.freewarejava.com">Freewarejava</a><br>
	- <a href="http://www.cooltext.com">Cool Text</a><br>
	- <a href="http://www.google.com">Google.com</a>	</span>

<div class="menutitle" onclick="SwitchMenu('sub5')">
<img src="media/navigation/engagement.jpg" align="absmiddle" />  Engagement</div>
<span class="submenu" id="sub5">
	- <a href="#">Known Engagement Issues</a><br>
</span>

<div class="menutitle" onclick="SwitchMenu('sub6')">
<img src="media/navigation/support.jpg" align="absmiddle" />  Support Numbers</div>
<span class="submenu" id="sub6">
	- <a href="http://www.codingforums.com">Coding Forums</a><br>
</span>

<div class="menutitle" onclick="SwitchMenu('sub7')">
<img src="media/navigation/paperless.jpg" align="absmiddle" />  Paperless Documents</div>
<span class="submenu" id="sub7">
	- <a href="http://www.codingforums.com">Coding Forums</a><br>
</span>

<div class="menutitle" onclick="SwitchMenu('sub8')">
<img src="media/navigation/misc.jpg" align="absmiddle" />  Miscellaneous</div>
<span class="submenu" id="sub8">
	- <a href="http://www.codingforums.com">Coding Forums</a><br>
</span></div>	</td>
  </tr>
  <tr>

 

 

Now, i got the WHERE statement working,

 

		<?php include 'libs/dbopen.php';

	$query  = "SELECT * FROM files WHERE Keyword1 = 'Tax'";
	$result = mysql_query($query);

	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
echo 	"File Name: <span class='titlebar2'><a href='indextest.php?id={$row['id']}'>{$row['Filename']}</a></span> <br>" .
	"Keywords: <span class='description'>{$row['Keyword1']}, {$row['Keyword2']}</span> <br>" .
	"Description: <span class='description'>{$row['Description']}</span> <br>" .
	"Date Added: <span class='description'>{$row['DateAdded']}</span> <br><br>";
	}
	include 'libs/dbclose.php';
	?>

 

Its pulling the correct document, now what i want to do is change the

WHERE Keyword1 = 'Tax'"

to like, WHERE keyword1 = "index.php?=THISHERE" type thing if you know what i mean!?

Link to comment
Share on other sites

ahhh ok very simple:

 

		<?php include 'libs/dbopen.php';

	// this variable has an if statement built in. (If Key is not found, set keyword to tax, otherwise set to keyword given.
	$Keyword1 = (isset($_GET['Key']))? mysql_escape_string($_GET['Key']) : "Tax";
	$query  = "SELECT * FROM files WHERE Keyword1 = '$Keyword1'";
	$result = mysql_query($query);

	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
echo 	"File Name: <span class='titlebar2'><a href='indextest.php?id={$row['id']}'>{$row['Filename']}</a></span> <br>" .
	"Keywords: <span class='description'>{$row['Keyword1']}, {$row['Keyword2']}</span> <br>" .
	"Description: <span class='description'>{$row['Description']}</span> <br>" .
	"Date Added: <span class='description'>{$row['DateAdded']}</span> <br><br>";
	}
	include 'libs/dbclose.php';
	?>

 

you would then use something like: http://localhost/index.php?Key=Tax

 

 

hope this helps,

Link to comment
Share on other sites

Ok! So i changed the links to:

 

		- <a href="index.php?Key=Audit">All Audit Documents</a><br>

 

and when i click the link it changes it perfectly! Ok, so now, its working just the way i want, BUT! for each File, it can have 2 keywords, how can i change the query to be an OR, and say if Keyword1 or Keyword2 = Key?=THISHERE  ?!?!?

 

Thanks again you guys are great!

Link to comment
Share on other sites

		<?php include 'libs/dbopen.php';

	// this variable has an if statement built in. (If Key is not found, set keyword to tax, otherwise set to keyword given.
	$Keyword1 = (isset($_GET['Key']))? mysql_escape_string($_GET['Key']) : "Tax";
	$query  = "SELECT * FROM files WHERE Keyword1 = '$Keyword1' OR Keyword2 = '$Keyword'1";
	$result = mysql_query($query);

	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
echo 	"File Name: <span class='titlebar2'><a href='indextest.php?id={$row['id']}'>{$row['Filename']}</a></span> <br>" .
	"Keywords: <span class='description'>{$row['Keyword1']}, {$row['Keyword2']}</span> <br>" .
	"Description: <span class='description'>{$row['Description']}</span> <br>" .
	"Date Added: <span class='description'>{$row['DateAdded']}</span> <br><br>";
	}
	include 'libs/dbclose.php';
	?>

 

or to have mulitple keywords in the url eg index.php?Key=blah&Key2=Blah then:

		<?php include 'libs/dbopen.php';

	// this variable has an if statement built in. (If Key is not found, set keyword to tax, otherwise set to keyword given.
	$Keyword1 = (isset($_GET['Key']))? mysql_escape_string($_GET['Key']) : "Tax";
	$Keyword2 = (isset($_GET['Key2']))? mysql_escape_string($_GET['Key2']) : "Tax";
	$query  = "SELECT * FROM files WHERE Keyword1 = '$Keyword1' OR Keyword2 = '$Keyword'2";
	$result = mysql_query($query);

	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
echo 	"File Name: <span class='titlebar2'><a href='indextest.php?id={$row['id']}'>{$row['Filename']}</a></span> <br>" .
	"Keywords: <span class='description'>{$row['Keyword1']}, {$row['Keyword2']}</span> <br>" .
	"Description: <span class='description'>{$row['Description']}</span> <br>" .
	"Date Added: <span class='description'>{$row['DateAdded']}</span> <br><br>";
	}
	include 'libs/dbclose.php';
	?>

 

hope this helps,

Link to comment
Share on other sites

ok i tried the first one, and it doesnt pull any documents at all. i tried a OR like you did before and it didnt work, is that the correct syntax?

 

PS. even when i dont have  Key?=something it doesnt pull the Tax documents like it did before

 

Ok nevermind, you had a mini-typo, Keyword2 = '$Keyword'1";

 

misplaced the '

 

its working great now, thanks a lot!

 

Now, my next project is getting a search engine going, theres gotta be some tuts on that right?

Link to comment
Share on other sites

try this:

 

<?php include 'libs/dbopen.php';

 

// this variable has an if statement built in. (If Key is not found, set keyword to tax, otherwise set to keyword given.

$Keyword1 = (isset($_GET['Key']))? mysql_escape_string($_GET['Key']) : "Tax";

$query  = "SELECT * FROM files WHERE Keyword1 = '$Keyword1' OR Keyword2 = '$Keyword'1";

$result = mysql_query($query) or die("Mysql Query: ".$query."<br />".mysql_error();

 

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

echo "File Name: <span class='titlebar2'><a href='indextest.php?id={$row['id']}'>{$row['Filename']}</a></span> <br>" .

"Keywords: <span class='description'>{$row['Keyword1']}, {$row['Keyword2']}</span> <br>" .

"Description: <span class='description'>{$row['Description']}</span> <br>" .

"Date Added: <span class='description'>{$row['DateAdded']}</span> <br><br>";

}

include 'libs/dbclose.php';

?>

Link to comment
Share on other sites

ok simple one,

 

$query = "select * FROM files WHERE Filename OR Description LIKE \"%$trimmed%\"  
  order by Filename"; // EDIT HERE and specify your table and field names for the SQL query

 

I have 3 other fields i want to search, so i thought this would work:

 

 

$query = "select * FROM files WHERE Filename OR Description OR Keyword1 OR Keyword2 OR DateAdded LIKE \"%$trimmed%\"  
  order by Filename"; // EDIT HERE and specify your table and field names for the SQL query

 

but it does not, whats the correct way to do that?

Link to comment
Share on other sites

lol sry for the typo:P, glad you caught it :)

 

im not very adept with sql but try this: (almost definetely an easier way but i dont know it... yet:P)

 

$query = "select * FROM `files` WHERE `Filename` LIKE \"%$trimmed%\" OR `Description` LIKE \"%$trimmed%\" OR `Keyword1` LIKE \"%$trimmed%\" OR `Keyword2` LIKE \"%$trimmed%\" OR `DateAdded` LIKE \"%$trimmed%\"  
  order by `Filename`"; // EDIT HERE and specify your table and field names for the SQL query

 

i just added the value to each where key. very long query but should work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.