Jump to content

Using multiple ?id=1 methods


Mutley

Recommended Posts

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.

Link to comment
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.
Link to comment
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?
Link to comment
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.
Link to comment
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 &
Link to comment
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"?
Link to comment
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?
Link to comment
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]
Link to comment
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.
Link to comment
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.
Link to comment
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]
Link to comment
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?
Link to comment
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.
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.