Jump to content

Archived

This topic is now archived and is closed to further replies.

Mutley

Using multiple ?id=1 methods

Recommended Posts

I'm wanting to have id?=1 for a page but also one for a user, so a user with the id?=1 wants to view page number 2.

Obviouslly in the URL id?=1?page=2 wouldn't work, if it did how do I call it twice in my PHP?

Share this post


Link to post
Share on other sites
[quote]
Obviouslly in the URL id?=1?page=2 wouldn't work, if it did how do I call it twice in my PHP?
[/quote]

Use '&' to pass additional parameters

[code]
mypage.php?id=1&page=2
[/code]

Share this post


Link to post
Share on other sites
That's great, so what would I do here:

[code]
$ewid = $_GET['id'];

if($ewid) {

$sql  = "SELECT * ";
$sql .= "FROM user ";
$sql .= "WHERE id=".$ewid." ";[/code]

Would it look like this:
[code]
$ewid = $_GET['id'];
$page = $_GET['page'];
if($ewid, $page) {

$sql  = "SELECT * ";
$sql .= "FROM user, page ";
$sql .= "WHERE id=".$ewid. && .$page." ";[/code]

That's confusing me^^. The user id and page id, are on different tables.

Share this post


Link to post
Share on other sites
Theres 2 tables in the database, 1 to store the users and 1 to store the pages for that user.

The first ?ID=1 will be the user, then to get the page it will be ?PAGE=2 (for page 2 for example), so ?ID=1&?PAGE=2

I want 1 php file so when you goto the ?ID=1&?PAGE=2 it finds the user and their page, then displays them. So there can be multiple users with their own pages.

Share this post


Link to post
Share on other sites
so what you need to do is, pass the page the user ID and in the page table have a user_id colum and query data database for the page ID where the user_id = the ID in the url

I.E.

page.php?id=1

[quote]$sql = mysql_query("select * from pages where user_id = '$id'");[/quote]

also, is the page a file or data from the page table?

Share this post


Link to post
Share on other sites
Can you explain in more detail? That's confused me alot.

The database will have data in.

So will I have to include the pages.php file or something?

Share this post


Link to post
Share on other sites
[quote author=zawadi link=topic=106927.msg428302#msg428302 date=1157455943]
so what you need to do is, pass the page the user ID and in the page table have a user_id colum and query data database for the page ID where the user_id = the ID in the url

I.E.

page.php?id=1

[quote]$sql = mysql_query("select * from pages where user_id = '$id'");[/quote]

also, is the page a file or data from the page table?
[/quote]

The problem with that, if the user id=1 it would only show page 1, I want it so users can have lots of pages.

Share this post


Link to post
Share on other sites
When you're using more than one $_GET variable, it should always be like this:

www.myurl.com?user_id=1&page=x

After the initial question mark, you have to use ampersands.  And if you're coding the links on the page, always use the HTML equivalent &

Share this post


Link to post
Share on other sites
I understand that, thank-you Ober but my problem is how to code it into the page?

I know how to find the user, like this:
[code]$ewid = $_GET['id'];
if($id) {

$sql  = "SELECT * ";
$sql .= "FROM users ";
$sql .= "WHERE id=".$ewid." ";

$result = mysql_query($sql) or die (mysql_error());

if(mysql_num_rows($result) == 1) {

$row = mysql_fetch_array($result);
[/code]

But then how do I also do it, so it loads a page from the database with the URL? So ?id=1 would bring up that user but how would I make it so ?id=1&page=2 bring up page 2 data from the database table named "pages"?

Share this post


Link to post
Share on other sites
You would have to run the same sort of SQL query for the page contents.  Your actual content display function can be kind of generic, but you just plug the ID from the URL into the SQL statement to get the different content.  Now, where it can get somewhat tricky is if the content is actually specific to one user.  In that case, you just store the userid AND the page id in the contents table and use an AND in your WHERE clause to grab the right data.

Does that make sense?

Share this post


Link to post
Share on other sites
How do I make it so if no &page=1 etc was put in, it would display it the first page anyway?

For example you put in ?id=1&page=1

It goes to page 1 but if you put in just ?id=1 it still goes to page 1?

Share this post


Link to post
Share on other sites
You should be using a setup like this:
[code]
<?php
if(isset($_GET['page']))
{
  switch($_GET['page'])
  {
      case "1":
        require("somepage.php");
      break;
      case "2":
        require("someotherpage.php")
      break;
      default:
        require("index.php");
  }
}
else
    require("index.php");
?>
[/code]

Share this post


Link to post
Share on other sites
Not like that. It's all in the database, heres what I've got:

[code]<?
if($page) {

$sql  = "SELECT * ";
$sql .= "FROM pages ";
$sql .= "WHERE page_id=".$page." ";

$result = mysql_query($sql) or die (mysql_error());

if(mysql_num_rows($result) == 1) {

$row = mysql_fetch_array($result);

$content = $row['content'];
?>
<?=$content?>
<?
} else {
echo "No Page selected"
}
}
?>
<!--^ Content of the page ^-->
[/code]

That would work with ?id=1&page=1 but if someone just put ?id=1    I want it to show &page=1 automaticly.

Share this post


Link to post
Share on other sites

// if this gets the information from a url .
if($page) {

do this.

if($_GET['page']=="what_ever_condition"){

do some think

}

good luck.

Share this post


Link to post
Share on other sites
Sorry, could you explain what to do with that and how it works? Would that make it so if I put ?id=1 it would goto ?id=1&page=1 automaticly.

Share this post


Link to post
Share on other sites
Tried so many ways, I just can't get it so if it is

?id=1 it displays page 1 and if I then put ?id=1&page=2 it comes up blank, I can only get one of the other to work. I've tried this:

[code]if($_GET['page']) {

$sql  = "SELECT * ";
$sql .= "FROM pages ";
$sql .= "WHERE page_id=".$page." ";

$result = mysql_query($sql) or die (mysql_error());

if(mysql_num_rows($result) == 1) {

$row = mysql_fetch_array($result);

$content = $row['content'];
?>
<?=$content?>
<?
} else {
$sql  = "SELECT * ";
$sql .= "FROM pages ";
$sql .= "WHERE page_id=1 ";

$result = mysql_query($sql) or die (mysql_error());

if(mysql_num_rows($result) == 1) {

$row = mysql_fetch_array($result);

$content_empty = $row['content'];
?>
<?=$content_empty?>
<!--^ Content of the page ^-->
<?
}
}
}
[/code]

Also tried:

if(!empty($page)) {

and:

if($page == "")) {

...but they all make ?id=1 find page 1 but if I then did ?id=1&page=2 it just comes up blank.

Share this post


Link to post
Share on other sites
I fail to see why you have <? and ?> all over the place.

[code]
<?php
// What happens if the url contains page=0?
// 0 could be a valid page number, but won't pass the test (I think)
// I recommend testing:  if(isset($_GET['page'])){
if($_GET['page']){
  // In your query you refer to $page, but nowhere did you give it a value
  // change $page in your query to $_GET['page']
  $sql  = "SELECT * ";
  $sql .= "FROM pages ";
  //$sql .= "WHERE page_id=".$page." ";
  $sql .= "WHERE page_id={$_GET['page']}"; // $page changed to use $_GET
  $result = mysql_query($sql) or die (mysql_error());
  if(mysql_num_rows($result) == 1) {
    $row = mysql_fetch_array($result);
    $content = $row['content'];
    $content // This should probably read: echo $content;
  } else {
    $sql  = "SELECT * ";
    $sql .= "FROM pages ";
    $sql .= "WHERE page_id=1";
    $result = mysql_query($sql) or die (mysql_error());
    if(mysql_num_rows($result) == 1) {
      $row = mysql_fetch_array($result);
      $content_empty = $row['content'];
      $content_empty // should probably say: echo $content_empty;
    }
  }
}
?>
[/code]

If you have questions about the following line of code:
$page = isset($_GET['page']) ? $_GET['page'] : 1;
See [b]The Ternary Operator[/b] at [url=http://www.php.net/operators.comparison]http://www.php.net/operators.comparison[/url]
[code]
<?php
// Here is a shorter block of code that should do what you want

// Set the page, using a default if $_GET['page'] is not set
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$sql = "SELECT * FROM pages WHERE page_id={$page} LIMIT 1";
$q = mysql_query($sql);
if(mysql_num_rows($q) == 1){
  while($row = mysql_fetch_array($result)){
    $content = $row['content'];
  }
}else{
  $content = "You're trying to find random pages in my website!";
}
echo $content;
?>
[/code]

Share this post


Link to post
Share on other sites
Thanks but it isn't doing what I mean.

If someone went to:

www.mysite.com/index.php?id=1

It would return a website with no content because all the content is in the "pages" part of the database, so if I went to:

www.mysite.com/index.php?id=1&page=2

I would see content on Page 2 for the User 1 and if I went to ?id=1&page=1 it would show page 1 of the user 1.

Now what I want it to do, is if I goto www.mysite.com/index.php?id=1 for it to show what would be on www.mysite.com/index.php?id=1&page=1

See what I mean?

Share this post


Link to post
Share on other sites
Makes sense now....

[code]
<?php
  $page = isset($_GET['page']) ? $_GET['page'] : '1'; // if $_GET['page'] is set then assign it to the variable $page, else assign '1'
?>
[/code]

Try that at the top of your code.

Rich

Share this post


Link to post
Share on other sites
Unless I totally misunderstood you, I'm not sure you even looked at what I wrote.

Share this post


Link to post
Share on other sites
At the top of my page I have this:
[code]$page = isset($_GET['page']) ? $_GET['page'] : '1';
[/code]

Then further down this:

[code]
<?php
if($page) {

$sql = "SELECT * FROM pages WHERE page_id={$page} LIMIT 1";
$q = mysql_query($sql);
if(mysql_num_rows($q) == 1){
  while($row = mysql_fetch_array($result)){
    $content = $row['content'];
  }
}else{
  $content = "You're trying to find random pages in my website!";
}
echo $content;
}
?>

[/code]

It won't work, I feel like I'm doing something obviously wrong, as Roopurt18 suggests.

Share this post


Link to post
Share on other sites

×

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.