Stian Olsen
Stian Olsen

Reputation: 62

Let users visit other users (profiles) in my own written social community?

The thing I'm asking for is how the users can visit other users profile.

The past week I have been scripting a social network I'm doing, and I'm kind of stuck right now.

I don't know where to start and read or anything.

So I'm asking you guys to kindly help me =)

Im thinking of the url to be like user.php?id=123 looking and get user you are visiting to show up instead of "you" wich is at user.php!

Live demo: http://social.swipper.org

EDIT #2:

Here's the current code i got inside user.php :

<?php
session_start();
if($_SESSION['username']) {
    include "config.php";

    $username = $_SESSION['username'];
    $fetcher = mysql_query("SELECT * FROM userinfo WHERE username='$username'");

    while ($data = mysql_fetch_assoc($fetcher)){
        $firstname = $data['firstname'];
        $lastname = $data['lastname'];
        $gender = $data['gender'];
    }

    // get user's profile image
    if (file_exists("users/$username/profile.jpg")) {
            $userPhoto = "<img class='pic' src='users/$username/profile.jpg' width='90%' height='30%' />";
    }
    elseif (!file_exists("users/$username/profile.jpg")) {
            $userPhoto = "<img class='pic' src='http://www.uavmedia.com/portal/images/no-user.jpg' width='90%' height='30%' />";
    }
    // the user's profile image is fetched

    // henter profil text
    $file = "users/$username/bio.txt"; 
    $contents = file($file);
    $profile_text = implode($contents);
    // ferdig å hente profil text, vil nå echo ut profil siden.
    // henter profil stilsett
    $file2 = "users/$username/style.css"; 
    $contents2 = file($file2);
    $profile_style = implode($contents2);
    // ferdig å hente profil stilsett.

    echo
    "
    <!doctype html>
    <html lang='en'>
    <head>
        <title>Social FW</title>
        <meta name='Description' content='A Social network for everyone. Come join us!' />
        <meta name='Keywords' content='Social, Network, Framewerk, Framework, FW, Open-Source, Free' />
        <link rel='stylesheet' type='text/css' href='user_files/style.css' media='screen' />
        <link rel='stylesheet' type='text/css' href='SFW_files/style2.css' media='screen' />
<style type='text/css'>
$profile_style
</style>
        <link rel='icon' href='SFW_files/favicon.ico' media='screen' />
        <script type='text/javascript' src='http://code.jquery.com/jquery-latest.js'></script>
        <script type='text/javascript' src='SFW_files/scripts/login.js'></script>
    </head>
    <body>

    <div id='top'>
        <h1>Swipper.org</h1>
    </div>
    <div id='side-menu'>
        <ul>
            <li><a href='user.php'><b>".$username."</b></a></li><br/>
            <li><a href=''>Inbox</a></li>
            <li><a href=''>Guestbook</a></li>
            <li><a href=''>Friends</a></li>
            <li><a href=''>Pictures</a></li><br />

            <div id='showOptions'><li><a href='#'>Edit Profile</a></li></div>
                <div id='editOptions' style='display:none;'>
<pre class='user_info'><b>></b><a href='changeText.php'>Edit Profile Text</a>
<b>></b><a href='changeCss.php'>Edit Stylesheet(CSS)</a>
<b>></b><a href='changeProfilePic.php'>Change Profile Image</a></pre>
                </div><br />
            <a href='logout.php'><b>Logout</b></a>
        </ul>
    </div>
    <div id='side-left'>
    <!-- START USER PIC --!>
            <center>
                $userPhoto<br />
            </center>
    <!-- END USER PIC --!>
    <!-- START USER INFO --!>
            <br />
            <h3><pre class='user_info'>User Info</pre></h3>
                <pre class='user_info'>Name: ".$firstname." ".$lastname."<br />Sex : $gender</pre>
    <!-- END USER INFO --!>
    </div>
    <div id='box_center'>
        <h2 style='font-size:30px;' align='center'>$username</h2>
            <hr />
        <br />
        $profile_text
    </div>
    <div id='footer'>
        <p align='center'>Swipper.org &copy; All rights reserved. 2010-2012</p>
    </div>

    </body>
    </html>
    ";
}
else {
    die("You need to login first or register. You can register/login <a href='index.php'>here</a>!");
}
?>

and when the url is like: user.php?id=3 i want to get the user with userid 3 to show up, and get user #3's information instead of the "user" who wants to visit other people.

Upvotes: 0

Views: 1634

Answers (3)

dhazelett
dhazelett

Reputation: 335

Are you listing all the users some where? if you are the href needs to be something like

<?php
    $sql = mysql_query("SLECET * FROM userinfo");
    while ($row = mysql_fetch_assoc($sql)){
        echo "<a href='users.php?id=" . $row['id'] . "'>"
    }

then in users.php

    <?php
        if(isset($_REQUEST['id'])) {
            if(is_numeric($_REQUEST['id'])){
                $uid = $_REQUEST['id'];
                $sql = mysql_query("SELECT * FROM userinfo WHERE id='" . $uid . "' LIMIT 1");
            }
        } else {
            $sql = mysql_query("SELECT * FROM userinfo WHERE username='" . $_SESSION['username'] . "' LIMIT 1");
        }

then later your html can use that with a fetch_assoc($sql)

<table>
    <tr>
        <td><?php echo $row['username'] ?>'s Profile</td>
    </tr>
</table>

simple example. but i think you get the picture, message me on FB for more on this Stian

Upvotes: 1

Astha
Astha

Reputation: 1734

Make your query like

$userID = $_GET['id'];
$fetcher = mysql_query("SELECT * FROM userinfo WHERE userID='$userID'");

and you can accordingly show data for any user with the valid user ID as given in url.

if profile can be viewed without login too then there is no need of session and if its compulsory only check if isset or not. if you are passing username in url as

user.php?user=test1

then your query will be like

$username = $_GET['user'];
$fetcher = mysql_query("SELECT * FROM userinfo WHERE username='$username'");

Hope now its more clear to u.

Upvotes: 0

Rene Pot
Rene Pot

Reputation: 24815

This should be your answer:

if (isset($_GET['username']){
    $username = mysql_escape_string($_GET['username']);
}else{
    $username = $_SESSION['username'];
}
$fetcher = mysql_query("SELECT * FROM userinfo WHERE username='$username'");
if (mysql_num_rows() == 0){
    $username = '';
    echo 'Username not found'; die();
}

Upvotes: 0

Related Questions