URL Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Thizzle
Forum Newbie
Posts: 12
Joined: Fri Jul 23, 2010 12:29 pm

URL Question

Post by Thizzle »

READING THIS WON'T BE AS USEFUL AS READING MY LAST POST

Website: http://gift-for-men.com/
PHP Version: 5.0

Alright, what my website does is automatically generate a "Gift" suggestion whenever someone visits "Gift-For-Men.Com." That's exactly what I wanted and exactly what I built. Anyways, the problem I ran into is I wanted to implement social features like the Facebook like button. After looking through books, google, and more I finally decided I can't figure it out on my own. So if you could please help me out by answering the question.

Question: What I want is when the person visits the page it automatically generates a "suggestion" as it's doing. But when it generates that suggestion it adds the primary id to the end of the URL. For Examample I want to URL to be "http://gift-for-men.com/ "id?=1" The "id" is just text I added to make it look prettier instead of being http://gift-for-men.com/1. The code I used to build the suggestions is below.

Explanation: The QUERY is stored in the variable $row. The primary key in the database is named id. Any ideas please?

Code: Select all

<?php
     $query  = "SELECT * FROM suggestions ORDER BY Rand() LIMIT 1";
     $result = mysql_query($query);
	 $row = mysql_fetch_array($result);
     ?> 
Last edited by Thizzle on Fri Jul 23, 2010 7:49 pm, edited 1 time in total.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: URL Question

Post by Jade »

You're going to have to do a check to see if there is an ID already and if not re-direct with an ID attached.

Code: Select all

<?php
//put this at the very top of the index page
if (!$_GET['id'])
{

     //there was no ID found, randomly select one and redirect back to the index page
     $result = mysql_query("SELECT id FROM suggestions ORDER BY Rand() LIMIT 1") or die (mysql_error());
     $row = mysql_fetch_array($result);
     
     header("Location: http://gift-for-men.com/?id=" . $row['id']);
     exit;
}

//then wherever you're displaying the gift information you can do this
if ($_GET['id'])
{
    $result = mysql_query("SELECT * FROM suggestions WHERE id='" . mysql_real_escape_string($_GET['id']) . "'") or die (mysql_error());
    $row = mysql_fetch_array($result);

    if (!$row['id']) //you know this id doesn't exist in your database (maybe it was a typo) so redirect back to the index page and try again
    {
       header("Location: http://gift-for-men.com/");
       exit;
     }
   
    //otherwise you can display the gift information
    print_r($row);
}
?>
Thizzle
Forum Newbie
Posts: 12
Joined: Fri Jul 23, 2010 12:29 pm

Re: URL Question

Post by Thizzle »

I'm getting this error:
Warning: Cannot modify header information - headers already sent by (output started at /home/content/00/6453700/html/index.php:4) in /home/content/00/6453700/html/index.php on line 13


Thanks by the way.
Thizzle
Forum Newbie
Posts: 12
Joined: Fri Jul 23, 2010 12:29 pm

Re: URL Question

Post by Thizzle »

Alright I'm getting off so I thought I'd update you on where I'm at... I actually just started to learn PHP within the past few days when I had the idea for this website. Anyways, I went over your code and I couldn't exactly follow everything making the last few hours a waste trying to fix it. I think the route I was trying to go won't work so I need help getting started with the new program.

Basically as far as I can tell here is what I'm going to need to do: When someone visits the index.php (http://gift-for-men.com/) a query is sent to the database. That data is then stored in an array like ($row). One of the data items is going to be the "id" which determines the page they are looking at. Thats just code, what I want is the user to be redirected to a URL (based on the id in query ) For example, gift-for-men.com/page?=1 (2, 500, 1000). (Note: the "page?" is just their to look pretty).

Now, let's say someone visits the URL of a page with an id... For example gift-for-men.com/page?=1. I want the pages to be static and not just randomly grab a row from the database as if they were visiting the index file.

Any help while I'm gone is useful.
Thizzle
Forum Newbie
Posts: 12
Joined: Fri Jul 23, 2010 12:29 pm

Re: URL Question

Post by Thizzle »

Based on what i want above I'm mapping out everything...

Let's see there are three possible scenarios.

Those scenario's are

1. Someone typed: http://gift-for-men.com/ into the URL
2. Someone typed: http://gift-for-men.com/page?=1 into the Url
3. Someone typed the wrong thing into the URL ( No page exist )

Based on those three things I need to wrote code for each of those possible scenario's in their respectable order.
1. Someone visits the index part of the website, they are then redirected to a random page.
2. Someone visits a page in the website and they are shown that page ( Meaning PHP Would need to check URL for the "id" and then pull the id's row and insert data from database into page. ( No <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> clue )
3. Someone typed wrong thing, they are then redirected to the index page and then is redirect to a random page.

I don't know, if anyone has any suggestions I would appreciate them.
Thizzle
Forum Newbie
Posts: 12
Joined: Fri Jul 23, 2010 12:29 pm

Re: URL Question

Post by Thizzle »

Back to work.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: URL Question

Post by califdon »

This will sound cruel, but it has to be said: for a beginner, just learning PHP, an attempt to create a relatively complex web page like you describe is utterly pointless. There are a thousand things you have to learn first! Start out with something FAR simpler!
Thizzle
Forum Newbie
Posts: 12
Joined: Fri Jul 23, 2010 12:29 pm

Re: URL Question

Post by Thizzle »

Wow that's negative man but it's alright.

You know I think I understand the concept it's pretty simple I just need to learn the language. For example, I don't know any of the functions that would help me do this. Which is why I'm here, learning. Still, this is my goal with this website.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: URL Question

Post by Jade »

The error means you're trying to display header content and the re-direct to another page. You have to make sure that if you're going to redirect to another page that you don't have any blank spaces at the top of the page, that you've echoed something to the screen or displayed an HTML. The redirect must be the first thing that's output to the page because once the header goes out PHP cannot complete a redirect because the header has already gone out and it has to use that header to complete the redirect. The code I gave you above should work fine.
califdon wrote:This will sound cruel, but it has to be said: for a beginner, just learning PHP, an attempt to create a relatively complex web page like you describe is utterly pointless. There are a thousand things you have to learn first! Start out with something FAR simpler!
I have to disagree with you there. I think if you start off with something more challenging it may take you longer to complete it (and be a bit more frustrating) but you'll learn a helluva lot more in the long run.
Last edited by Jade on Wed Jul 28, 2010 1:40 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: URL Question

Post by Benjamin »

Actually, you need to buy this book, and read it, and study it.

http://www.amazon.com/Programming-Colle ... 671&sr=8-1
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: URL Question

Post by califdon »

Jade wrote:I have to disagree with you there. I think if you start off with something more challenging it may take you longer to complete it (and be a bit more frustrating) but you'll learn a helluva lot more in the long run.
I suppose that I was pretty broad in my comment, and I grant you that there is something to be said for stretching to learn something complex, but I am biased by my years of teaching in college. In my experience, the greatest learning takes place when one learns in a progressive way, beginning with the basics and steadily expanding one's knowledge. I have seen all too many students think that they can tackle an advanced project without first understanding the fundamentals. They fail to really learn much useful skills, because there are always gaps in their understanding, so they constantly struggle to do anything productive. We may disagree and I respect your point of view, but I will always recommend beginning with simple projects and working upward to more advanced ones, because I've seen it work hundreds of times.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: URL Question

Post by Jade »

califdon wrote:In my experience, the greatest learning takes place when one learns in a progressive way, beginning with the basics and steadily expanding one's knowledge. I have seen all too many students think that they can tackle an advanced project without first understanding the fundamentals. They fail to really learn much useful skills, because there are always gaps in their understanding, so they constantly struggle to do anything productive.
I'm not discounting starting with the basics. Having a strong foundation is important and I'm happy to admit I learn something new at a basic level all the time even though I've been programming for over 10 years. I agree using a progressive learning style has it's benefits. However everyone learns differently and a progressive style isn't always feasible or practical for the situation at hand. Case in point, Thizzle may decide to hire an outside company or contract programmer to make the modifications for him instead of taking the time to change it himself. Either way that doesn't mean someone trying to accomplish something beyond their capabilities should be told
califdon wrote:an attempt to create a relatively complex web page like you describe is utterly pointless.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: URL Question

Post by califdon »

@Jade: I accept your criticism. Sometimes I can be too rigid in my views. You are certainly correct in saying that everyone learns differently. I just hate to see someone waste their time trying to do a complex task without the framework of knowledge that makes it possible to understand what they are doing and why. Anyway, 'nuff said. Thanks for your honest criticism.
Post Reply