Page 1 of 1

User Profile List Catergories

Posted: Wed Aug 29, 2012 8:52 am
by rabatos
Hi I've set up the user profile system and I needed one feature which I'm not sure about.

I want a user list which instead of displaying all users I want to show a list of categories.
Then by selecting a cat you will see a list of users corresponding to that category.

A simple example would be genders.
So a page with male and female.
Then under each it shows all male members and all female members.

The cat male or female is obviously in the user table for each user.

If I could get an example of how to do this it would be great.

Re: User Profile List Catergories

Posted: Fri Aug 31, 2012 12:07 pm
by Temor
I'm guessing you're using a boolean to determine the gender? ( 0 for male 1 for female )
If so, then it's pretty much just like any other time you fetch data from a table.
$sql = "SELECT `user_name` FROM `user_system` WHERE `gender` = 0";
This will select all the usernames of all males.

You would then need to run the query as you would any other.

You're doing several of these queries already. A wonderful thing with PHP is that you can recycle pretty much all of your code. If you have a query with a WHERE clause, copy and paste it and just modify it to fit its new purpose.

If there is anything more specific you need me to explain, please do say so. This is something you should be able to figure out yourself though, if you give it some time.

Re: User Profile List Catergories

Posted: Sun Sep 02, 2012 4:12 am
by rabatos
Actually it may be a little more complicated.

My website is for trades companies (electricians, plumbers etc)

And I have a table, "trades_cats" which holds each trade catergory, so electrician, plumber, carpenter, each with a id

And then in the users table each user has a trades catergory, which is the id from the "trades_cat" table.

eg.

John, Banks, john@mail.com, 2, location, info.

where 2 is the trade cat id from the other table. where 2=plumber.

So I want to display all the trade categories on one page and then under each it should select all users with the trade_cat id.

Any ideas? or should I change the system?

Re: User Profile List Catergories

Posted: Sun Sep 02, 2012 4:46 am
by rabatos
What I've got at the moment is this.

function in user.inc.php
//fetches cats.
function fetch_cats(){
    $result = mysql_query("SELECT `id` AS `id`, `name` AS `name` FROM `trade_cats`");
		
	$users = array();
	
	while (($row = mysql_fetch_assoc($result)) !== false){
	    $users[] = $row;
	}
	
	return $users;
}

trades_list.php the page the lists the trades cats
<?php

include('core/init.inc.php');

$pageTitle = "Trades";

include('../includes/header.php')
?>
<body class="body">
	<br>
	<div class="gradientBoxesWithOuterShadows">
<div>
		<h4 class="font5">NZ Trades</h4>
		<br>
	<div align="center">	
	<?php 
	include('../includes/menu.php');
 
	?>
	</div>
	    <div>
		    <?php
			
			foreach (fetch_cats() as $user){
			    ?>
				<p class="font3">
				    <a href="//this is where I need to had a page for users of each cat.; ?>"><?php echo $user['name']; ?></a>
				</p>
			<?php
			}
			
			?>
		</div>
<br>
</div>
<?php

include('includes/footer.php');
?>

	</div>
<div class="footer">
<h4 class="footfont">© 2012</h4>
</div>
</body>

Re: User Profile List Catergories

Posted: Sun Sep 02, 2012 10:42 am
by rabatos
I've added a couple things.

this function in user.inc.php
//fetches trades category info.
function fetch_cats_info($id){
    $id = (int)$id;
	
	$sql = "SELECT
				`id` AS `id`,
				`name` AS `name`
			FROM `trade_cats`
			WHERE `id` = {$id}";
			
	$result = mysql_query($sql);
	
	return mysql_fetch_assoc($result);
	
}
and this page "trade.php" which displays the trade cat and should display all users with this trade cat id in the users table.
<?php

include('core/init.inc.php');

$cats = fetch_cats_info($_GET['id']);

$email = $_SESSION['email'];

include('../includes/header.php')
?>
<html>
	<title><?php echo $cats['name']; ?>'s</title>
    <body class="body">
	<br>
	<div class="gradientBoxesWithOuterShadows">
<div>
		<h4 class="font5">NZ Trades</h4>
		<br>
	<div align="center">	
	<?php 
	include('../includes/menu.php');
 
	?>
	</div>
	<br><br>
	    <div>
		<div class="profile">
		    <?php
			
			if ($cats === false){
			    echo 'That user does not exist.';
			}else{
			    ?>
				<h2> <?php echo $cats['name']; ?> </h2>
				
				
					<div>
						<?php
						
						foreach (fetch_users() as $user){
							?>
							<p class="font3">
								<a href="profile.php?id=<?php echo $user['id']; ?>"><?php echo $user['company_name']; ?></a>
							</p>
						<?php
						}
						
						?>
					</div>
				
		</div>		
			
				<?php
			}
			?>
						
	
		</div>
<br>
</div>
</div>
<?php

include('includes/footer.php');
?>

	</div>
<div class="footer">
<h4 class="footfont">© 2012</h4>
</div>
</body>
			
So this so far displays all cats, then currently under each cat it just display all users.

I need it do display only the users who has in the trade_cat column on the users table, a id number that is equal to the id number of the current pages trade cat.

if you understand.

Re: User Profile List Catergories

Posted: Sun Sep 02, 2012 12:38 pm
by Temor
I think I do understand.

I would sort this into two pages. Categories.php and Users.php ( For simplicity, you can name them whatever you want ).
Categories.php will fetch all categories, and print them to the screen.
// Put this function in a backend file.
function fetch_categories(){
$sql = "SELECT `id`, `name` FROM `trade_cats`";
$result = mysql_query($sql);

$categories = array();

while ($row = mysql_fetch_assoc($result)) {
$categories[] = array(
'id' => $row['id'],
'name' => $row['name']
);
}
return $categories;
}

$categories = fetch_categories();

foreach($categories as $category){
echo "<a href='Users.php?catID={$category['id']}'> {$category['name']} </a><br />";
}
Now to Users.php
we will need to fetch the $_GET variable and base our query on that.



function fetch_users_by_category($category_id){
$category_id = (int)$category_id;
$sql = "SELECT `id`, `name` FROM `users` WHERE `category` = {$category_id}";
$result = mysql_query($sql);

$users = array();

while ($row = mysql_fetch_assoc($result)) {
$users[] = array(
'id' => $row['id'],
'name' => $row['name']
);
}
return $users;
}
}

$users = fetch_users_by_category($_GET['id']);

foreach( $users as $user ){
 echo $user;
}
 

Summary:
This will fetch a list of ALL categories and create a list of links. Once a category is clicked, it will take you to another page which shows ALL users in that category.


This is what you wanted, right? If not, please correct me :)

Re: User Profile List Catergories

Posted: Sun Sep 02, 2012 1:48 pm
by rabatos
Hi Temor, thanks for your help so far.

Yeah what you have said is pretty much what I was trying to explain. I already have the categories page and all that.

The Users.php file is really what I needed help with.

I've gone through what you posted and modified it to fit for me.
function fetch_users_by_category($category_id){
$category_id = (int)$category_id;
$sql = "SELECT `company_name` FROM `trades` WHERE `trade_cat` = '{$category_id}'";
$result = mysql_query($sql);
									 
$users = array();
									 
while ($row = mysql_fetch_assoc($result)) {
$users[] = array(
'company_name' => $row['company_name']
);
}
return $users;
}
}
									 
$users = fetch_users_by_category($_GET['id']);
									 
foreach( $users as $user ){
 echo $user;
}
But I have a problem.
All that is displayed on the page is "Array" for every user.

So for a categories with 4 users all that is displayed is "ArrayArrayArrayArray"

I can't work out why though as the functions should work correctly.

Re: User Profile List Catergories

Posted: Sun Sep 02, 2012 1:51 pm
by rabatos
There is also a extra closing bracket on line 15 which I removed, but that didn't affect anything

Re: User Profile List Catergories

Posted: Sun Sep 02, 2012 5:41 pm
by Temor
$user will always be an array. You need to tell php what you want to show from that array.
echo $user['company_name'];
Doing this should suffice.

Re: User Profile List Catergories

Posted: Mon Sep 03, 2012 4:59 am
by rabatos
Hey thanks alot, just a small thing I missed, but its all working now.

Just one more thing I would like to ask.

On the page that displays all the categories. How can I show a count of how many users there are for each category.

So:

Plumber(1)
Electrician(5)
Carpenter(2)

How can I get a function to count the number of users and display the number next to each category.

Re: User Profile List Catergories

Posted: Tue Sep 04, 2012 12:49 pm
by jacek
Once you have done
$users = fetch_users_by_category($_GET['id']);
you can get the total using count()
echo count($users);

Re: User Profile List Catergories

Posted: Tue Sep 04, 2012 2:11 pm
by rabatos
Well I wanted the count on the previous page, in Catergories.php.

So something like this.
<a href="trade.php?id=<?php echo $user['id']; ?>"><?php echo $user['name']; 
					echo "(" . count($users) . ")";?></a>
But this is before
$users = fetch_users_by_category($_GET['id']);
And I put that function in a separate file not user.inc.php.

Re: User Profile List Catergories

Posted: Fri Sep 07, 2012 4:52 am
by rabatos
Oh yup I got the count sort of working.

I get the correct count of users, but it displays as zeros.

like this:

category 000000

It should show a number.

Re: User Profile List Catergories

Posted: Sun Sep 09, 2012 7:49 pm
by jacek
Post the code ...

Re: User Profile List Catergories

Posted: Mon Sep 10, 2012 4:37 am
by rabatos
This is the category list page
<?php

include('core/init.inc.php');

$pageTitle = "Trades";

include('../includes/header.php')
?>
<body class="body">
	<br>
	<div class="gradientBoxesWithOuterShadows">
<div>
		<h4 class="font5">NZ Trades</h4>
		<br>
	<div align="center">	
	<?php 
	include('../includes/menu.php');
 
	?>
	</div>
	<br>
	    <div>
		    <?php
			
			foreach (fetch_cats() as $user){
			    ?>
				<p class="font3">
				    <a href="trade.php?id=<?php echo $user['id']; ?>"><?php echo $user['name']; ?></a>
					
																		<?php
																		
																		$count = fetch_users_by_category($user['id']);
									 
																		foreach( $count as $user ){
																		echo count($users);
																		 
																		}
																		?>
																		
				</p>
			<?php
			}
			
			?>
		</div>
<br>
</div>
<?php

include('includes/footer.php');
?>

	</div>
<div class="footer">
<h4 class="footfont">© 2012</h4>
</div>
</body>
These are the two functions in the user.inc.php
//fetches cats.
function fetch_cats(){
    $result = mysql_query("SELECT `id` AS `id`, `name` AS `name` FROM `trade_cats`");
		
		$sql = "SELECT `user_name` FROM `user_system` WHERE `gender` = 0";
		
	$users = array();
	
	while (($row = mysql_fetch_assoc($result)) !== false){
	    $users[] = $row;
	}
	
	return $users;
}

//users 
function fetch_users_by_category($category_id){
	$category_id = (int)$category_id;
		
		$sql = "SELECT `id`, `company_name`, `location` FROM `trades` WHERE `trade_cat` = '{$category_id}'";
			$result = mysql_query($sql);
									 
			$users = array();
									 
		while ($row = mysql_fetch_assoc($result)) {
				$users[] = array(
						'id' => $row['id'],
						'company_name' => $row['company_name'],
						'location' => $row['location']
				);
		}
	return $users;
}


Re: User Profile List Catergories

Posted: Tue Sep 11, 2012 1:59 pm
by Temor
This is all wrong:
 $count = fetch_users_by_category($user['id']);                                            
foreach( $count as $user ){
echo count($users);                                                                                                                                                 
}
You should not be looping count();
You have not defined the variable that you put into count();

This will do the trick:
 
$count = fetch_users_by_category($user['id']);      
echo count($count);

Re: User Profile List Catergories

Posted: Wed Sep 12, 2012 1:58 am
by rabatos
Oh okay I see my problem. Thank you for that.
Also is there a way, that if the number of users in 0 that I can hide that.
So it only shows 1+

Re: User Profile List Catergories

Posted: Wed Sep 12, 2012 2:18 pm
by Temor
That would be done easiest with an if statement.
$countAmount = count($users);
if($countAmount => 1){
// Show row.
}else{
// Don't show.
}

Re: User Profile List Catergories

Posted: Wed Sep 12, 2012 10:21 pm
by rabatos
Thanks