Jump to content

The $_GET action


Twister1004

Recommended Posts

i have a blog, what i use get for is when i need to pass a variable (like an id number) to a link, so when somebody clicks one of my entries, it takes them to a new page where they can login and add comments, but only displays that entry

 

like so

 

print '<a href="/something.php?id=' . $entry_id . '">this goes to one specific entry</a>

 

the question mark tells php that the next thing in the url is a key to the GET array that you are defining at that moment, then there will be an equals sign and then the value of the get key

 

so it links to a page that has all the php on it to display 1 entry except the db query is incomplete, it needs an id number, so near the top of the page you use $id = $_GET['id'], that pulls the variable from the url and then you can stick it in your query (SELECT * FROM entries WHERE id=$id)

 

also if you want to put more than one variable in your url use & between them

 

<a href="/something.php?id=' . $entry_id . '&var=' . $var .'">

 

and that is what i use GET for

Link to comment
Share on other sites

@Crayon

---------

Honestly, I had no idea what you were trying to tell me. I didn't understand how to use it. Thanks though.

 

@prexep

--------

That kinda helped in a way. Thanks

 

@lodious

--------

This made a WHOLE lot more since. I'm going to try and see if I understand it now. Be back in like 5 minutes =)

Link to comment
Share on other sites

@Crayon

---------

Honestly, I had no idea what you were trying to tell me. I didn't understand how to use it. Thanks though.

 

@prexep

--------

That kinda helped in a way. Thanks

 

@lodious

--------

This made a WHOLE lot more since. I'm going to try and see if I understand it now. Be back in like 5 minutes =)

 

Best way to understand this is to make 2 scripts and test it out yourself..  Just use CV's example.  This is a basic PHP method of passing data to another page.

Link to comment
Share on other sites

I'm getting it now! However, When i try to link it to another page, it isnt executing correctly.

 

How would i be able to use it inside a link? could i have to do this?

<div style = "position:absolute; left:501px; top:-4px;">
<a href = <?php echo " "cp.php?user='.$username.'" "; ?> >profile</a>
</div>

 

OR some other way?

Link to comment
Share on other sites

For example, say I had three phrases. If Bob comes along, I want to say "Hi, Bob!" If Frank comes along, I want to say "Hi, Frank!" Perhaps a stranger comes along. I might want to just nod and give a quick hi.

 

On my page I would have three links: one for Bob, one for Frank, and one for the stranger.

 

<a href="say.php?person=bob">Bob</a>
<a href="say.php?person=frank">Frank</a>
<a href="say.php?person=stranger">Stranger</a>

 

Notice how I am "passing" the person to the script "say.php" throught the URL. Let's start say.php:

 

<?php

$person = $_GET['person'];

?>

 

That says "I want to define variable Person to the value of the Person variable passed in the URL." Now we just need to use either A) An IFTHEN, ELSEIF, ELSE structure or B) A switch function. Both these options will decide which phrase to say depending on the person.

 

<?php

$person = $_GET['person'];

$phrases = array(
    'bob'       => 'Hi, Bob!',
    'frank'     => 'Hi, Frank!',
    'stranger'  => '*nods* Hi.',
);

// IFTHEN, ELSEIF, ELSE

if($person == $phrases['bob']) {
    echo $phrases['bob']; // Bob walks by
}
elseif($person == $phrases['frank']) {
    echo $phrases['frank']; // Frank walks by
}
else {
    // Must be a stranger
    echo $phrases['stranger'];
}

// Switch function
switch($person) {

    case 'bob':
        echo $phrases['bob']; // Bob walks by
    break;
    
    case 'frank':
        echo $phrases['frank']; // Frank walks by
    break;
    
    case 'stranger':
        echo $phrases['stranger']; // A stranger walks by
    break;
    
    default:
        echo 'No one has walked by.';
    break;

}

?>

Link to comment
Share on other sites

@Maq

------

When I go to cp.php (Clicking the link) I am getting a undefined Index in the URL. So that means I did it 50%, In a way.

 

@Altec

-------

I have no idea what what you used in your code. I could guess, but Id rather figure out the $_Get first for the URL =P.

 

@lodius

-------

I used, it and it just came out as an Error. But I believe Maq fixed it.

 

 

Cerious, do any of you have TeamViewer to make this easier?

Link to comment
Share on other sites

@Maq

------

When I go to cp.php (Clicking the link) I am getting a undefined Index in the URL. So that means I did it 50%, In a way.

 

Sorry could you elaborate, what's the exact error?  Are you in the same directory you're calling this page from?

Link to comment
Share on other sites

OK, when I'm on the front page, of the site (The link to get to cp.php) I click the profile link (cp.php) when i got there, this is listed in my URL

http://5.3.17.155/dreamweaver%20sites/abysmal-essence/cp.php?user=%3Cbr%20/%3E%3Cb%3ENotice%3C/b%3E:%20%20Undefined%20variable:%20username%20in%20%3Cb%3EC:\Documents%20and%20Settings\Compaq_Owner\Desktop\Server%20Files\Web%20Server\xampp\htdocs\dreamweaver%20sites\Abysmal-Essence\header.php%3C/b%3E%20on%20line%20%3Cb%3E51%3C/b%3E%3Cbr%20/%3E

 

and this is on the page.

Notice: Undefined index: username in C:\Documents and Settings\Compaq_Owner\Desktop\Server Files\Web Server\xampp\htdocs\dreamweaver sites\Abysmal-Essence\cp.php on line 4

 

If you would like to view the whole file, i will gladly share it.

(The files are 2 separate things. header.php which has all the login stuff and all that stuff. and cp.php which loads the whole profile.)

 

If you are cerious, about team viewer, it is kind of like a remote assistance kind of thing. Except it is less stressing for the computer. It stresses the net and relies on the person's RAM.

Link to comment
Share on other sites

I've tried and I've tried, I just can't seem to fix what ever the problem is -_- *Sigh* any other suggestions?

 

Also, what ever it is saying "Undefined Index" is really making me mad. I can't figure out what it is trying to say.

 

Anyone with an idea what that is trying to tell me and how it could be fixed? It is driving me insaine. That is about all the errors i get now, :P

Link to comment
Share on other sites

Wow, the edit thing should be enables for the last message the person wrote. o.o

 

 

Anyways, I've played around all day with codes, and I'm not getting any errors now, however the code isnt executing for the URL. any ideas? ._.

 

Edit: Nvm, I'm now getting a error X_X

Notice: Undefined index: username in C:\Documents and Settings\Compaq_Owner\Desktop\Server Files\Web Server\xampp\htdocs\dreamweaver sites\Abysmal-Essence\cp.php on line 3

 

If you would like to view the page, here ya go,

<?php session_start(); ?>
<?php error_reporting(E_ALL);?>
<?php $username = $_GET['username']; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>Abysmal Essence | Control Pannel</title>
<style>
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #FFFFFF;
}
body {
background-color: #000000;
background-image: url(images/bg-soft.PNG);
background-repeat: repeat;
}
#loggedin_info {
position:absolute;
left:159px;
top:100px;
width:218px;
height:400px;
z-index:1;
margin:auto;
padding:0px;
}
#header{
position:absolute;
background:#000000;
top:2px;
left:100px;
}
#notloggedin{
position:absolute;
left: 405px;
top: 198px;
width: 444px;
height: 32px;
}
#content_loggedin{
position:absolute;
top:99px;
left:381px;
width: 528px;
height: 399px;
}
#side_rename{
position:absolute;
top:99px;
left:920px;
width: 189px;
height: 401px;
}
#nav{
position:absolute;
left:4px;
top:102px;
width:145px;
height:388px;
color:white;
background:#4682B4;
}
#button_account{
position:absolute;
top:473px;
left:209px;
width: 150px;
}
#content_account{
position:absolute;
top:472px;
left:586px;
}
#unknown_account{
position:absolute;
top:474px;
left:955px;
}
</style>
</head>

<body>
<div id = login>
<?php
include('header.php');
$result = mysql_query("SELECT about,content,unknown FROM `profile` WHERE `accountid` = {$_SESSION['login_id']}");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit();
}
$row = mysql_fetch_row($result);
$getuser = mysql_query("SELECT * FROM `accounts` WHERE `username` = '$username'"); 
/*
//Get users progile id
$profileid = $_REQUEST['username'];

// you'll need to change the values here to suit your db
$query1 = "SELECT * FROM `accounts` WHERE `username` = '$profileid'";

$result1 = mysql_query($query1) or die ("There was an error");
$row1 = mysql_fetch_row($result1);*/
?>
</div>
<?php if (isset($_SESSION['login_user'])){ ?>
<div id="loggedin_info">
<p align = center>
About me, <br/><?php echo htmlentities($row[0]); ?></p>
</div>
<div id = content_loggedin>
  <p align = center>About your self,<br/> <?php echo htmlentities($row[1]); ?></p>
</div>
<div id = side_rename>
  <p align = center>Personal Use <br/><?php echo htmlentities($row[2]); ?></p>
</div>
<?php
}
else{
?>
  <div id = notloggedin>
    <center>
<strong>You must be loggedin to view your profile!</strong><br/> The profiles is currently, not done, or functional. Please wait while we get this online, ASAP!<br/> Thank you!</center></div>
<?php } ?>
<div id = nav>
<center>
<hr />
<a href = "./index.php">Home</a><br/><hr />
<a href = "./faq.php">FAQ</a><br/><hr />
<a href = "./about.php">About Us</a><br/><hr />
<a href = "./index.php">Projects</a><br/><hr />
</center>
</div>

</body>

</html>

 

I'm so sorry I sound like a nub, however, Learning PHP isn't easy just learning it online. ._.

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.