User Profile List Catergories

Post here is you are having problems with any of the tutorials.
Post Reply
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

User Profile List Catergories

Post 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.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Profile List Catergories

Post 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.
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile List Catergories

Post 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?
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile List Catergories

Post 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>
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile List Catergories

Post 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.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Profile List Catergories

Post 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 :)
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile List Catergories

Post 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.
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile List Catergories

Post by rabatos »

There is also a extra closing bracket on line 15 which I removed, but that didn't affect anything
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Profile List Catergories

Post 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.
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile List Catergories

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: User Profile List Catergories

Post by jacek »

Once you have done
$users = fetch_users_by_category($_GET['id']);
you can get the total using count()
echo count($users);
Image
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile List Catergories

Post 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.
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile List Catergories

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: User Profile List Catergories

Post by jacek »

Post the code ...
Image
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile List Catergories

Post 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;
}

User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Profile List Catergories

Post 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);
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile List Catergories

Post 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+
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Profile List Catergories

Post by Temor »

That would be done easiest with an if statement.
$countAmount = count($users);
if($countAmount => 1){
// Show row.
}else{
// Don't show.
}
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile List Catergories

Post by rabatos »

Thanks
Post Reply