Jump to content

Capture value via querystring


simflex

Recommended Posts

Greetings experts,

 

Please forgive me for this cheap question.

 

I have got a calendar of events and displays events for  7 departments.

 

For each department, we would like the calendar to display events specific to that department.

 

The way I am thinking about doing this is to grab the department from the url and compare it with the department on the db for which events is inserted into the db with.

 

If there is a match, then display the events for that department.

 

Something like:

 

    $tquery = "select * from ".$EVENTS_TB." where ".$EVENTS_TB.".month='".$smmonth."' and ".$EVENTS_TB.".year='".$smyear."' and ".$EVENTS_TB.".deptid='deptFromQueryString' " ;

 

The problem that I have is that I don't in php, how to capture the querystring from url.

 

For instance, assume this  is the url:

 

http://www.domain.com/departName.

 

DeparmentName represents the department value.

 

How do I capture the query above?

 

Thanks so much.

 

Link to comment
Share on other sites

Assuming that you are NOT using mod_rewrite.  This is put in the simplest of forms.  It is basic only, and may need some tweaking for your use.

 

preg_match('~/(.*)/*~',$_SERVER['REQUEST_URI'],$match);
$event = $match[1];

echo $event;

Link to comment
Share on other sites

Assuming that you are NOT using mod_rewrite.  This is put in the simplest of forms.  It is basic only, and may need some tweaking for your use.

 

preg_match('~/(.*)/*~',$_SERVER['REQUEST_URI'],$match);
$event = $match[1];

echo $event;

 

You'd need to use mod_rewrite, otherwise your server is going to look for a folder called departname and an index file inside of it.

Link to comment
Share on other sites

jcbones,

 

Given the code I posted initially, how do I use your code?

 

Sorry, still getting my feet wet on php.

 

About the mod_rewrite bit, is it a builtin feature of Apache which means that I don't have to do anything?

 

Thanks a lot

Link to comment
Share on other sites

Lets get down to everything, so that you can get this running.

 

3 different ways.

 

1. Make subfolders for each department, and place an index page inside each folder.

 

http://domain.com/departName/index.php

preg_match('~http://domain.com/(.*)/*~',$_SERVER['REQUEST_URI'],$match);
$EVENT_TB = $match[1];

$tquery = "select * from ".$EVENTS_TB." where ".$EVENTS_TB.".month='".$smmonth."' and ".$EVENTS_TB.".year='".$smyear."' and ".$EVENTS_TB.".deptid='deptFromQueryString' " ;

echo $tquery;

 

2. Put the department name into the queryString, and point the string to ONE page.

index.php

<p>Select a department</p>
<a href="?dept=deptA">Department A</a><br />
<a href="?dept=deptB">Department B</a><br />
<?php
$EVENT_TB = (isset($_GET['dept'])) ? $_GET['dept'] : 'deptA';
$tquery = "select * from ".$EVENTS_TB." where ".$EVENTS_TB.".month='".$smmonth."' and ".$EVENTS_TB.".year='".$smyear."' and ".$EVENTS_TB.".deptid='deptFromQueryString' " ;
echo $tquery;
?>

 

3. Make a .htaccess file that will point to the correct department.

.htaccess

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^/Departments/(.*)$ index.php?dept=$1

 

Then make your page

index.php

<p>Select a department</p>
<a href="http://domain.com/Departments/deptA">Department A</a><br />
<a href="http://domain.com/Departments/deptB">Department B</a><br />
<?php
$EVENT_TB = (isset($_GET['dept'])) ? $_GET['dept'] : 'deptA';
$tquery = "select * from ".$EVENTS_TB." where ".$EVENTS_TB.".month='".$smmonth."' and ".$EVENTS_TB.".year='".$smyear."' and ".$EVENTS_TB.".deptid='deptFromQueryString' " ;
echo $tquery;
?>

Link to comment
Share on other sites

so, this query

 

$tquery = "select * from ".$EVENTS_TB." where ".$EVENTS_TB.".month='".$smmonth."' and ".$EVENTS_TB.".year='".$smyear."' and ".$EVENTS_TB.".deptid='deptFromQueryString' " ;

 

then becomes:

 

$tquery = "select * from ".$EVENTS_TB." where ".$EVENTS_TB.".month='".$smmonth."' and ".$EVENTS_TB.".year='".$smyear."' and ".$EVENTS_TB.".deptid='.$EVENT_TB' " ;

??

 

 

Again, thanks a lot for your help.

 

 

Link to comment
Share on other sites

Also, please one more thing I wanted to repeat is that I am already inserting departments name into the db.

 

What I am struggling to do is put a calendar on each department's page and let it show ONLY that department's events.

 

So for instance, if a calendar is on deptA, and there are events specific to deptA on the db, that's what we would like to show on that department's page.

 

I am just reviewing your 3 different methods (Thanks a lot again) but I am still trying to see how they help me. I know it is me, not your code.

 

That's precisely what I am trying to accomplish here.

Link to comment
Share on other sites

Lets start simple.  Since you have a page for each department, make the calendar a separate script, and include it into each page.

 

Then query the database for events on each department page, and set your variables for the calendar.  In this way, you don't have to capture the department from the URL at all.

 

Get me a sandwich department:)

$deptName = 'Get me a sandwich';
$smmonth = date('m');
$smyear = date('Y');
$tquery = "select * from events where month='$smmonth' and year='$smyear' and deptid='$deptName'" ;
echo $tquery;

include_once('mycalendar.php');

Link to comment
Share on other sites

hi jcbones,

 

The great news is the last solution you provided is working beautifully in terms of displaying calendar based on deptid.

 

The caveat here is that we will have to create 7 of those small calendars and place each calendar on eah dept page.

 

While this is a huge relief, is there way to make it in such that the code can grab the deptid from browser - back to original querystring question?

 

At least, I can ask them to use this solution temporarily if I can get it worked out here but it would be great if that would be possible.

 

Many thanks for your patience and huge assistance.

Link to comment
Share on other sites

Sure, pass the deptName in the URL as such.

 

<a hef="http://domain.com/mypage.php?dept=deptName">deptName</a>

 

Then replace this line.

$deptName = 'get me a sandwich';
//with this one.
$deptName = (isset($_GET['dept'])) ? mysql_real_escape_string($_GET['dept']) : 'defaultName';

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.