Jump to content


Photo

Using multiple ?id=1 methods


  • Please log in to reply
31 replies to this topic

#21 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 07 September 2006 - 05:54 PM

I fail to see why you have <? and ?> all over the place.

<?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;
    }
  }
}
?>

If you have questions about the following line of code:
$page = isset($_GET['page']) ? $_GET['page'] : 1;
See The Ternary Operator at http://www.php.net/o...tors.comparison
<?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;
?>

PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#22 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 07 September 2006 - 06:36 PM

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?

~ Mutley.

#23 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 07 September 2006 - 06:44 PM

Makes sense now....

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

Try that at the top of your code.

Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#24 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 07 September 2006 - 06:50 PM

Unless I totally misunderstood you, I'm not sure you even looked at what I wrote.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#25 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 07 September 2006 - 07:00 PM

At the top of my page I have this:
$page = isset($_GET['page']) ? $_GET['page'] : '1';

Then further down this:

<?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;
}
?>		


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

#26 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 07 September 2006 - 07:04 PM

Remove the curley braces { } from around $page in your SQL statement.

Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#27 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 07 September 2006 - 07:13 PM

By placing:

$page = isset($_GET['page']) ? $_GET['page'] : '1';

at the top of the page you garauntee that $page is set, so you can remove the opening if statement.  I.E., if($page) is not necessary unless you specifically want nothing to be shown when the url contains page=0.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#28 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 07 September 2006 - 07:18 PM

I was going to say that, but wanted to go one step at a time  ;D

Cheers
Roopert
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#29 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 07 September 2006 - 08:07 PM

So I've done this:

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;
}

Which still doesn't work but I havn't removed the IF statement, what would it look like as I would need to remove curley brackets to? Could you do a mock up for me please?
~ Mutley.

#30 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 07 September 2006 - 08:46 PM

I'm determined to get this sorted for you...

Forget all the previous stuff, lets go from scratch as it's all very confusing.

<?php
   // Get the parameters from the URL
   $id = $_GET['id']
   $page = isset($_GET['page']) ? $_GET['page'] : '1'; // if '&page=n' (n is a number) is in the url use it, if not, default to page=1

   // The following assumes that you have a user_id column in your pages table, and the columns are called 'content', 'page_id' and 'user_id'
   $sql = "SELECT content FROM pages WHERE page_id = '$page' AND user_id = '$id'";
   $result = mysql_query($sql);

   // We should only have one row returned, so mysql_fetch_row should be fine here
   $content = mysql_fetch_row($result);

   // Check we found a page with the page_id that was given to us and output accordingly
   if ($content){
      echo $content[0];
   else {
      echo "No record was found for Page ID $page";
   }
?>

Now this is assuming quite a lot, but this simple bit of code should work fine and you should be able to build whatever you need around it.

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#31 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 07 September 2006 - 09:02 PM

Hurah! Finally, I've tested it and it works. Brilliant.

I would really like to thank everyone that has got me through this problem. Thanks Rich.  :)
~ Mutley.

#32 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 07 September 2006 - 09:11 PM

No problem my friend, no problem at all.

Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users