Similar Classes

I’m working on a website and Im trying to find people who are in the same classes as the logged in user. Here is a pic of the users table I’m working out of.

It should only show people that are in the same classes as the logged in user and not including the logged in user. I believe that that part works fine, but Im trying to show the classes underneath their name they have in common with the logged user. Could someone point me in a direction to go upon doing this? The way I am doing it now, the $peopleInClass array values always equal 0 for some reason.


<?php //Connect to database: location, username, password $con = mysql_connect("localhost","root",""); //If not connected, throw an error and say why if (!$con) { die('Could not connect: ' . mysql_error()); } //Select the database to use mysql_select_db("maindb", $con); ?> The Thought Cloud
Users Online: <?php //Check how many users online $queryNumOnline = mysql_query("SELECT * FROM Users WHERE Online='1'"); echo mysql_num_rows($queryNumOnline); ?>
<?php //error_reporting(0); //FInd classmates //Start by getting the classes the user has $user = strip_tags($_COOKIE[userEmailCookieStored]); $query = "SELECT * FROM users WHERE Email = '$user'"; $doquery = mysql_query($query); $row = mysql_fetch_array( $doquery ); //Heres the classes $classString = $row['enrolledClasses']; $classStringArray = explode("|",$classString); //People related array. holds related people ID's $peopleRelated = array(); //Class to hold info about what classes the person in in with the user //Same format as enrolledClasses column //So if the user is in Algebra and Critical Analysis and a person is in it as well, //[0] = "Algebra|Critical Analysis" $peopleInClass = array(); foreach($classStringArray as $item) { //This is a query to get the current user's ID $query2 = "SELECT * FROM users WHERE Email = '$user'"; $doquery2 = mysql_query($query2); $resultCurUser = mysql_fetch_array($doquery2); //This finds all rows that have similar content in the enrolledClasses relating to the current item //So if $item is Algebra and Algebra is found in a row in the column enrolledClasses, //it's added to the returned array if ($item != "") { $query = "SELECT * FROM users WHERE enrolledClasses LIKE '%".$item."%'"; $doquery = mysql_query($query); //We want to add these peoples ID's to the $peopleRelated array as long as they arent the current user //and they arent already in the array while($result = mysql_fetch_array($doquery)) { //The current related users ID to use as the $peopleInClass array index $index = $result["ID"]; //print_r($result); if (!(in_array($result["ID"],$peopleRelated)) && ($result["ID"] != $resultCurUser["ID"])) { //Add them to the array because they arent in the array and they arent the current user $peopleRelated[] = $result["ID"]; //Find what classes they actually are in with the users //Somehow keep the indexes in $peopleRelated[] and $peopleInClass[] the same //THIS WORKS BUT SINCE THE ABOVE FOR EACH GOES BY THE USERS CLASS ONE //BY ONE, IT DOESNT GET ANY PAST THE FIRST CLASS! if (preg_match("/".$item."/",$result["enrolledClasses"],$matches)) { foreach($matches as $match) { //Use the related persons id as the index $peopleInClass[$index] += ($match . "|"); } } } } } } ?>
Search for Classmates!


//Loop through the people related
//That array stores related peoples ID’s
//So $peopleRelatedp[$i] is the current iterations ID
for ( $i = 0; $i < count($peopleRelated); $i++)
if ($peopleRelated[$i] != “”)
//This is a query to get the current user’s ID
$query3 = “SELECT * FROM users WHERE ID = ‘$peopleRelated[$i]’”;
$doquery3 = mysql_query($query3);
$result3 = mysql_fetch_array($doquery3);

	echo "<tr>";
	echo "<td style='height:60px;border-bottom:solid;border-color:green;width:15%;'>";
	echo "<img src='' style='float:left;width:60px;height:60px;margin-right:5px;'>" . $result3["FirstName"];
	echo " " . $result3["LastName"] . "<br />";
	echo "<span style='font-size:12px;'>Is in ";
	//Explode the related classes array
	$classStringClass = $peopleInClass[$peopleRelated[$i]];
	$classStringArray2 = explode("|",$classStringClass);
	//Print them out
	foreach($classStringArray2 as $key => $curClass)
		if ($curClass != "")
			echo $curClass;
		}else if($curClass != "" && $key[$i + 1] != "")
			echo $curClass . ",";
	echo "<br /><a href=''>Add as Friend</a></span>";
	echo "</td>";
	echo "</tr>";


© 2010-<?php echo date('Y'); ?> The Thought Cloud


Is a user in this table more then once… for example is Dave Kramer in the table for multiple classes, or are they only enrolled in one class.

Also it would make it easier if you have two (or more) tables then have them linked, for example:

student_ID (primary key)

enrolledClass_ID (primary key)
student_ID (foreign key)

this will avoid issues with redundant data and students with the same name. If you make these changes then I can help you further.

Sponsor our Newsletter | Privacy Policy | Terms of Service