Jump to content


Photo

Now that I know what to ask, here goes


  • This topic is locked This topic is locked
13 replies to this topic

#1 alwaysinit

alwaysinit
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 04 October 2006 - 12:53 AM

I have a musician community portal site. I'm trying to create 'one' PHP page that is used for all musician's profiles.

Does anyone know what I should name this page to where it will function as everyone's profile page?(Can it be just "profiles.php"? Or do I need something more in the name to do that?

Thanks again for the help guys

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 04 October 2006 - 12:59 AM

You can name it whatever you like but in order to display each memebers profile you will need to pass a reference to that user. eg;

profiles.php?member=thorpe

would display my profile.

#3 alwaysinit

alwaysinit
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 04 October 2006 - 01:12 AM

Ok thanks, Now say I have "profiles.php?member=thorpe". How did your name get there? And how do I put "SallyAnne" Or "JoeSmith" in place of "thorpe"?



#4 alwaysinit

alwaysinit
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 04 October 2006 - 02:39 AM

How would the "profile.php" page come to be renamed "profiles.php?member=thorpe". How did it get that way?


#5 tippy_102

tippy_102
  • Members
  • PipPipPip
  • Advanced Member
  • 446 posts

Posted 04 October 2006 - 04:13 AM

Here's the basics....
<a href='profiles.php?member=memberName'>memberName</a>




Fur is more actively protested than leather 'cause its easier to harrass rich women than biker gangs.

#6 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 04 October 2006 - 04:37 AM

How would the "profile.php" page come to be renamed "profiles.php?member=thorpe". How did it get that way?

Either you worded this oddly or you're fairly new.  Its the same page with a different GET value...
Simply by changing memebe= in your address bar, or by using the more sought after <a href..., you can gain the same effect.
Why doesn't anyone ever say hi, hey, or whad up world?

#7 alwaysinit

alwaysinit
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 04 October 2006 - 07:39 AM

Yes I'm new to coding(two months). Sorry if my questions seem odd and presented incorrectly.

From your responses(tanx to all), this is what I now think:

(1) I create the profiles.php page.
(2) Use "<a href='profiles.php?member=memberName'>memberName</a>" in my site's search results?(I'm not sure where to use the (<a href))
(3) In the "profiles.php" place "$_GET['member']" to show the page as that member's.
(4) then start echoing result rows

Do I have all that right? If incorrect could someone show the correct sequence of events for showing 1000's of different profiles off the same page?

Here's the basics....

Code:
<a href='profiles.php?member=memberName'>memberName</a>

  Where does this link go?
Thanks again


#8 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 04 October 2006 - 10:55 AM

The whole process is database driven. You'll need a database table.

CREATE TABLE users (
  id INT PRIMARY KEY,
  uname VARCHAR(80),
  hobby TEXT
)

This is a make believe example designed to store a users id, name and there hobbies. Lets pretend we had 3 users...

id name hobby
--------------
1  bob   internet
2  foo    cooking
3  mary  music

Ok.... now you'll need 2 pages. One (index.php) to display the links to your users profile, and Two (profiles.php) to display each users hobby in detail.

index.php

<?php
  // connect to database
  $sql = "SELECT uname FROM users";
  $result = mysql_query($sql);
  if ($result) {
    if (mysql_num_rows($result) > 0) {
      // here we loop through each member in the database
      while ($row = mysql_fetch_assoc($result)) {
        /// creating a link out of there uname, and passing there uname through the url.
        echo "<a href=\"profiles.php?member={$row['uname']}\">{$row['uname']}</a><br />";
      }
    } else {
      echo "No users found";
  } else {
    echo "Query failed ". mysql_error();
  }
?>

profiles.php

<?php
  if (isset($_GET['member'])) {
    // connect to database.
    $sql = "SELECT uname, hobby FROM users WHERE uname = '{$_GET['member']}'";
    $result = mysql_query($sql);
    if ($result) {
      if (mysql_num_rows($result) > 0) {
        $row = mysql_fetch_assoc($result));
        echo "The user {$row['uname']} enjoys the hobby {$row['hobby']}";
      } else {
        echo "No user found by that name";
    } else {
      echo "Query failed ".mysql_error();
    }
  } else {
    echo "No member selected";
  }
?>

Ive tried to keep these examples pretty simple. There isn't allot of html mixed in just to make things a little clearer. Hope it helps.

ps; Take a look at mysql_connect() in the manual for examples on how to connect to the database.

#9 alwaysinit

alwaysinit
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 04 October 2006 - 03:08 PM

Wow thorpe, that is phreakin' awesome. I could'nt have asked for more detail and example.
You rock.

I'll get to coding now and post back if I run into trouble.
Thanks for your time and know-how.

#10 Hydrian

Hydrian
  • Members
  • PipPipPip
  • Advanced Member
  • 57 posts

Posted 02 September 2012 - 06:41 AM

I get  Parse error: syntax error, unexpected T_ELSE in /home/localhost/public_html/members/index.php on line 14

when i open the index.php
"A person who never made a mistake never tried anything new."
Albert Einstein

#11 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,429 posts
  • LocationBelgium

Posted 02 September 2012 - 06:57 AM

It's a made-up example to communicate a point. The code is untested and it's not advised to just copy-paste it and put it on your live website due to the gaping security holes in the code.

If you take a look at the code for index.php you'll notice a missing } after "No users found!"; The same for profiles.php after "No user found by that name";

#12 Christian F.

Christian F.
  • Staff Alumni
  • Advanced Member
  • 3,106 posts
  • LocationNorway

Posted 02 September 2012 - 08:07 PM

Nitpicking a bit, but I'd written the code a bit differently for ease of readability. I'd check for error conditions first, and if found add a warning/error message and exit early. Now, since this isn't a function or included code, I can't really exit early from this, but that's easily fixable by separating logic and presentation. (Read more about it in the "HEADER ERRORS" thread stickied at the top of this section.)

Now, as to how I'd written the code:
function getMemberDetails ($username) {
	if (empty ($username)) {
		return "No member selected";
	}
	
	// Create the query, and escape input to avoid SQL injections.
	$sql = "SELECT uname, hobby FROM users WHERE uname = '%s'";
	$sql = sprintf ($sql, mysql_real_escape_string ($username));
	$result = mysql_query ($sql);
	
	if (!$result) {
		// TODO: mysql_error () should/must be removed before putting the code into production!
		return "Query failed " . htmlspecialchars (mysql_error ());
	}
	
	if (mysql_num_rows ($result) <= 0) {
		return "No user found by that name";
	}
	
	return mysql_fetch_assoc ($result);
}

// Retrieve the user details, and check if it returned an array or string (error message).
if ($userDetails = getMemberDetails ($_GET['member'])) {
	// Query succeeded, add completed message to output. Adding escaping to prevent HTML injections.
	$temp = 'The user %1$s enjoys the hobby %2$s';
	$output = sprintf ($temp, htmlspecialchars ($userDetails['uname']), htmlspecialchars ($userDetails['hobby']));
} else {
	// Something failed, show error message.
	$output = $userDetails;
}

Note that I haven't validated $_GET['member'] in the code above, something which you definitely should be doing.
Keeping it simple.

#13 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 02 September 2012 - 09:46 PM

Your seriously commenting on a six year old post?

#14 Christian F.

Christian F.
  • Staff Alumni
  • Advanced Member
  • 3,106 posts
  • LocationNorway

Posted 02 September 2012 - 09:52 PM

Damn. Only checked the date on the last two replies. Sorry about that.
Keeping it simple.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users