Page 1 of 1

Regarding User Profile!

Posted: Fri Jan 06, 2012 11:56 pm
by fukurou
Hey,
Thank you so much for the user profile tutorial but i would like to ask about the function user_info.

Now there is,
function fetch_user_info($uid){
$uid = (int)$uid;

If lets say i would want to put more than one array in it? Is it possible? Such as,
function fetch_user_info($uid, $bid){
$uid = (int)$uid;
$bid = (int)$bid;

The reason why i'm trying to do that its because im trying to run two queries. For example there is profile=1, profile=2, profile=3 as a link and there is item=1,item=2,item=3. So profile and item will be running two different queries. So UID is for profile and BID is for item.
I'm sorry for my bad explanation. I hope you guys could help me out. Appreciate it a lot.

Thank you.

Regards,
fukurou.

Re: Regarding User Profile!

Posted: Sat Jan 07, 2012 12:13 am
by Temor
wouldn't it be easier to just make a separate function?
function fetch_item_info($bid){
query
}
is the profile and the item linked in any way?

Re: Regarding User Profile!

Posted: Sat Jan 07, 2012 10:01 am
by fukurou
Temor,

Thank you for you response.
Ya they are link together. Example for music there is "User - Item" displayed. Once the "user" being clicked then it will link you to the page that shown all the user post and once "item" being clicked then that particular item description being shown.


Thank you.

Regards,
fukurou.

Re: Regarding User Profile!

Posted: Mon Jan 09, 2012 9:51 pm
by jacek
Yes you can define a function with as many arguments as you like.

Re: Regarding User Profile!

Posted: Fri Jan 13, 2012 7:47 pm
by fukurou
Guys, I need help in this. Please.

index.php
<?php
include ('core/init.inc.php');
?>
 
<head>
        <body>
        <?php
                foreach (fetch_users() AS $user)
                {
                        ?>
               
                        <p>
                                <a href="profile.php?uid=<?php echo $user['id']; ?>"><?php echo $user['username']; ?></a>
                                <a href="position.php?pid=<?php echo $user['position_id']; ?>"><?php echo $user['position']; ?></a>
                        </p>
                        <?php
                }
 
        ?>
        </body>
</head>

core/init.inc.php
<?php
 
session_start();
 
mysql_connect('localhost', 'admin', '');
mysql_select_db('users_profile');
 
$path = dirname(__FILE__);
 
include("{$path}/inc/user.inc.php");
 
$_SESSION['uid'] = 1;
 
?>
core/inc/user.inc.php
<?php
//fetches ALL OF the users FROM the TABLE.
FUNCTION fetch_users(){
        $result = mysql_query('SELECT `user_id` AS `id` , `user_username` AS `username`, `position_id` AS posID`, `position_name` AS `position` FROM `users`');
       
        $users = array();
       
        while (($row = mysql_fetch_assoc($result)) !== FALSE){
                $users[] = $row;
        }
       
        RETURN $users;
}
//fetches profile information FOR the given USER.
 
FUNCTION fetch_users_info( $uid)
{
        $uid = (INT)$uid;
        $sql = "SELECT `user_id` AS `id` , `user_username` AS `username`, `position_id` AS posID`, `position_name` AS `position` FROM `users`
                                WHERE   (user_id = {$uid})
                                ";
                               
                                                $result = mysql_query($sql);
                                                        RETURN mysql_fetch_assoc($result);                     
}
 
 
FUNCTION fetch_position_info( $pid)
{
        $pid = (INT)$uid;
        $sql = "SELECT `user_id` AS `id` , `user_username` AS `username`, `position_id` AS posID`, `position_name` AS `position` FROM `users`
                                WHERE   (position_id = {$pid})
                                ";
                               
                                                $result = mysql_query($sql);
                                                        RETURN mysql_fetch_assoc($result);                     
}
 
?>

profile.php
<?php
include('core/init.inc.php');
$user_info = fetch_user_info($_GET['uid']);
?>
<html>
<head>
<title><?php echo $user_info['username']; ?>'s Profile</title>
</head>
<body>
                <?php
                if ($user_info === false){
                                echo 'That USER does NOT exist.' ;
                }else{
                        ?>
                        <h1> <?php echo $user_info['firstname']; ?>  <?php echo $user_info['lastname']; ?> </h1>
                        <p>Username: <?php echo $user_info['username']; ?></p>
                        <p>Gender: <?php echo ($user_info['gender'] == 1) ? 'Male' : 'Female'; ?></p>
                        <p>Email:<?php echo $user_info['email']; ?></p>
                        <p>Locaition: <?php echo $user_info['location']; ?></p>
                        <p>About me: <?php echo $user_info['about']; ?></p>
                <?php
        }
        ?>
 
</body>
</html>

position.php
<?php
include('core/init.inc.php');
$position_info = fetch_position_info($_GET['pid']);
?>
<html>
<head>
<title><?php echo $position_info['position']; ?>'s Profile</title>
</head>
<body>
                <?php
                if ($position_info === false){
                                echo 'That user does not exist.' ;
                }else{
                        ?>
                        <h1> <?php echo $position_info['firstname']; ?>  <?php echo $position_info['lastname']; ?> </h1>
                        <p>Username: <?php echo $position_info['username']; ?></p>
                        <p>Gender: <?php echo ($position_info['gender'] == 1) ? 'Male' : 'Female'; ?></p>
                        <p>Email:<?php echo $position_info['email']; ?></p>
                        <p>Locaition: <?php echo $position_info['location']; ?></p>
                        <p>About me: <?php echo $position_info['about']; ?></p>
                <?php
        }
        ?>
 
</body>
</html>
Once i run it, profile.php query doesn't run but whenever i remove one function from user.inc.php to this,

<?php
//fetches all of the users from the table.
function fetch_users(){
        $result = mysql_query('SELECT `user_id` AS `id` , `user_username` AS `username`, `position_id` AS posID`, `position_name` AS `position` FROM `users`');
       
        $users = array();
       
        while (($row = mysql_fetch_assoc($result)) !== false){
                $users[] = $row;
        }
       
        return $users;
}
//fetches profile information for the given user.
 
function fetch_users_info( $uid)
{
        $uid = (int)$uid;
        $sql = "SELECT `user_id` AS `id` , `user_username` AS `username`, `position_id` AS posID`, `position_name` AS `position` FROM `users`
                                WHERE   (user_id = {$uid})
                                ";
                               
                                                $result = mysql_query($sql);
                                                        return mysql_fetch_assoc($result);                     
}
 



Then it able to run. Why is that so?

Re: Regarding User Profile!

Posted: Fri Jan 13, 2012 10:16 pm
by bowersbros
In the position function i can see an error.
$pid = (INT)$uid;
the $uid isnt defined anywhere, i think it should be $pid

Give that a try?

Re: Regarding User Profile!

Posted: Fri Jan 13, 2012 10:22 pm
by Temor
bowersbros wrote:In the position function i can see an error.
$pid = (INT)$uid;
the $uid isnt defined anywhere, i think it should be $pid

Give that a try?
Shouldn't (int) be lowercase also? I'm not sure if it makes any difference, but considering the syntax highlighter does not highlight it, it might be wrong :P

Re: Regarding User Profile!

Posted: Fri Jan 13, 2012 11:15 pm
by bowersbros
Temor wrote:
bowersbros wrote:In the position function i can see an error.
$pid = (INT)$uid;
the $uid isnt defined anywhere, i think it should be $pid

Give that a try?
Shouldn't (int) be lowercase also? I'm not sure if it makes any difference, but considering the syntax highlighter does not highlight it, it might be wrong :P
I have a feeling that it doesnt matter, but that would cause an actual PHP syntax error as it would have something like UNEXPECTED_T_VARIABLE or something

Re: Regarding User Profile!

Posted: Fri Jan 13, 2012 11:35 pm
by Temor
bowersbros wrote:
Temor wrote:
bowersbros wrote:In the position function i can see an error.
$pid = (INT)$uid;
the $uid isnt defined anywhere, i think it should be $pid

Give that a try?
Shouldn't (int) be lowercase also? I'm not sure if it makes any difference, but considering the syntax highlighter does not highlight it, it might be wrong :P
I have a feeling that it doesnt matter, but that would cause an actual PHP syntax error as it would have something like UNEXPECTED_T_VARIABLE or something
I guess you're right.
I just tried it out and it does not matter if it's upper or lowercase.

Re: Regarding User Profile!

Posted: Sat Jan 14, 2012 2:00 pm
by jacek
Temor wrote:I guess you're right.
I just tried it out and it does not matter if it's upper or lowercase.
Well I didn't know that. I think function names are the same too.

Anyway, try adding
echo mysql_error();
under which every query is not running and it should tell you what the problem is.