Hello,

I am really really stuck on a small project im working on and have been at it now for the past 12 hours. lol

I am trying to set up a restrcited page for my admins and have the following:

$find_access = fetch ("SELECT * FROM job_roles WHERE user_id = '$username'");  
$find_job = fetch ("SELECT * FROM job_positions WHERE name = '$find_access[job_title]'");

print "$openHTML";

if ($find_job) { 



               
                 if ( $find_job[lobby]== 1) { 

       echo "show page content gere for access level 1"; 
                 } 
                  if ( $find_job[lobby]== 2) { 
        echo "show page content gere for access level 2";
                 } 
                 if ( $find_job[lobby]== 3) { 
        echo "show page content gere for access level 3 "; 
                 } 
                 if ( $find_job[lobby]== 4) { 
        echo "show page content gere for access level 4"; 
                 }

                  if ( $find_job[lobby]== 0) { 
        echo "<p><br><p><p><center>Sorry but your Access Level is not permitted for this page!"; 
                 }
} 




if (!$find_job) { 
   echo "<p><br><p><p><center>This page is a restrcited area. You do not have access!"; 
       } 


print "$closeHTML";

?>

The problem is that for some reason it will only return 1 result of jobs for the user. I want to be able to allocate more than one position to my admins and therefore have the code find all job positions allocated to that member and then allow the highest level.

.... so say for example ... one admin has the job position of technical support and moderator! Tech support is level 1 (highest) and moderator is level 4! By using this method the code only seems to pick up the first job allocated to that member in the database and wont check all job positions!

Does this make sense?

I know that the while statement selects all rows where the command is positive but I gave it a quick go and it didnt work.

Can anyone tell me why it will only return and check against one result and not compare these variables against all rows that are true???? And if there is a way around this?

Thanks

Justin

huh? lol

It seems to work for my website? What do you mean post that function? :>/ Sorry im still only learning PHP and MySQL.

I got a script that was very badly coded and have been trying to improve it so this is a typical case. lol

Ah ... I do have this is a file that is included :

function fetch($query)
{
//	echo $query;
	return mysql_fetch_array(mysql_query($query));

From what I understand this is used for fecth querys to the database!
:>/

Ah ... I do have this is a file that is included :

function fetch($query)
{
//	echo $query;
	return mysql_fetch_array(mysql_query($query));

From what I understand this is used for fecth querys to the database!
:>/

The way this function is being used, it will only return one row. What you should do for any query you want more than just the first row from is the following:

$find_access = mysql_query("SELECT * FROM job_roles WHERE user_id = '$username'");
while($row = mysqsl_fetch_array($find_access))
{
    //loop through each row here with $row['column_name']
}

Hello,

Thanks for your help.

I have tried the following code but get an error!

Parse error: syntax error, unexpected T_IF on line 24

Obviously this means that IF statements are not allowed within the WHILE statement. Am I correct in saying that?

$find_access = fetch ("SELECT * FROM job_roles WHERE user_id = '$username'");  
$find_job = fetch ("SELECT * FROM job_positions WHERE name = '$find_access[job_id]'");

print "$openHTML";

if ($find_job) { 
$find_access = mysql_query("SELECT * FROM job_roles WHERE user_id = '$username'");
while($row = mysqsl_fetch_array($find_job))
{
    //loop through each row here with $row['column_name']
    $row[id]

}

                 if ( $row[lobby]== 1) { 
       echo "show content gere for access level 1"; 
                 } 
                  if ( $row[lobby]== 2) {   
        echo "show content gere for access level 2";
                 } 
                 if ( $row[lobby]== 3) { 
        echo "show content gere for access level 3 "; 
                 } 
                 if ( $row[lobby]== 4) { 
        echo "show content gere for access level 4"; 
                 }

                  if ( $row[lobby]== 0) { 
        echo "<p><br><p><p><center>Sorry but your Access Level is not permitted for this page!"; 
                 }


if (!$find_job) { 
   echo "<p><br><p><p><center>This page is a restrcited area. You do not have access!"; 
       } 

}
print "$closeHTML";

try this:

<?php
$find_access = fetch ("SELECT * FROM job_roles WHERE user_id = '$username'");

print "$openHTML";
 
$find_job = mysql_query("SELECT * FROM job_positions WHERE name = '$find_access[job_id]'");
$counter = 0;
while($row = mysqsl_fetch_array($find_job))
{
	if ( $row[lobby]== 1) 
	{ 
		echo "show content gere for access level 1"; 
		$counter++;
	} 
	if ( $row[lobby]== 2) 
	{   
		echo "show content gere for access level 2";
		$counter++;
	} 
	if ( $row[lobby]== 3) 
	{ 
		echo "show content gere for access level 3 "; 
		$counter++;
	} 
	if ( $row[lobby]== 4) 
	{ 
		echo "show content gere for access level 4"; 
		$counter++;
	}
	
	if ( $row[lobby]== 0) 
	{ 
		echo "<p><br><p><p><center>Sorry but your Access Level is not permitted for this page!"; 
	}
}

echo $counter == 0?"<p><br><p><p><center>This page is a restrcited area. You do not have access!":"";
print "$closeHTML";

?>

[just edited] to iterate the counter in all cases except "0"

Hi again,

I see what youve done and what I didnt do. lol I forgot the ++.

Now it seems to have included the config files and shows the following error which ive seen before and is related to that fetch command I was using and the while statement. :>/

Fatal error: Call to undefined function mysqsl_fetch_array() in /access_rights.php on line 15

Line 15 is :

while($row = mysqsl_fetch_array($find_job))

Im not so familiar with while statements so am a little stuck. I tried changing the fetch to mysql_query for the $find_access but that didnt work.

oops, misspelled it, its not suppose to be mysqsl_fetch_array but mysql_fetch_array

try this:

<?php
$find_access = fetch ("SELECT * FROM job_roles WHERE user_id = '$username'");

print "$openHTML";
 
$find_job = mysql_query("SELECT * FROM job_positions WHERE name = '$find_access[job_id]'");
$counter = 0;
while($row = mysql_fetch_array($find_job))
{
	if ( $row[lobby]== 1) 
	{ 
		echo "show content gere for access level 1"; 
		$counter++;
	} 
	if ( $row[lobby]== 2) 
	{   
		echo "show content gere for access level 2";
		$counter++;
	} 
	if ( $row[lobby]== 3) 
	{ 
		echo "show content gere for access level 3 "; 
		$counter++;
	} 
	if ( $row[lobby]== 4) 
	{ 
		echo "show content gere for access level 4"; 
		$counter++;
	}
	
	if ( $row[lobby]== 0) 
	{ 
		echo "<p><br><p><p><center>Sorry but your Access Level is not permitted for this page!"; 
	}
}

echo $counter == 0?"<p><br><p><p><center>This page is a restrcited area. You do not have access!":"";
print "$closeHTML";

?>

Wow blimey! My eyes totally passed that! Amazing how one little typo can cause the script not to work. lol

Now its working although its still only picking up the access set for one job position.

My two tables are as follows:

job_positions
id
name
daily_exp
lobby


and the job_roles are
id
job_id
user_id
job_title


I have made 2 job positions Founder and Test

I have allocated myself to these 2 positions and when I set test to level 1 and founder to level 2 it will only show the level 2 as founder must be the first one it comes to!!

I want it to look at all the job roles allocated to the member and if one of them is say level 1 then it will show level 1 access.

I just cant see why it wont work. LOL

I did notice a slight error in my variables but have fixed that!

My varaibles are
So the following:

$find_access = fetch ("SELECT * FROM job_roles WHERE user_id = '$username'");

print "$openHTML";
 
$find_job = mysql_query("SELECT * FROM job_positions WHERE name = '$find_access[job_id]'");

So it should make the following commands:

$find_access selects all field info from job_roles where the user_id equals the username of the member. (for me this is founder and test)

The the $find_job should take all jobs that have the id number equal to the job_id of the job_roles table. (for me this would again be founder and test)

The the IF statement should say collect all rows (job_positions) that has a number equal to ==1,2,3 or 4 and then allow access to the page content within that particular IF statement.

Hmm ... I cant see anything wrong with that! I guess its just making it so it checks against all job posiitons for that member. Maybe I will have to reorganise the tables or database and set it up another way! :>/

ah ha ... ive had an idea. Instead of doing it this way ill have to separate it up and make the variable check for each lobby number and then print the info accordingly...

Will go have a play and see if it works. lol

Try this, the only thing that should make a difference is I converted the database record to an integer with (int) rather than a string, The rest should work pretty much the same was except it's just easier to manage.

<?php
$find_access = fetch ("SELECT * FROM job_roles WHERE user_id = '$username'");

print "$openHTML";
 
$find_job = mysql_query("SELECT * FROM job_positions WHERE name = '$find_access[job_id]'");
$counter = 0;
while($row = mysql_fetch_array($find_job))
{
	if((int)$row[lobby] > 0)
	{
		$counter++;
	}
	
	switch((int)$row['lobby'])
	{
		case 1:
			echo "show content gere for access level 1"; 
			break;
		case 2:
			echo "show content gere for access level 2";
			break;
		case 3:
			echo "show content gere for access level 3"; 
			break;
		case 4:
			echo "show content gere for access level 4";
			break;
		case 0:
			echo "<p><br><p><p><center>Sorry but your Access Level is not permitted for this page!"; 
			break;
		default:
			echo "<p><br><p><p><center>Sorry but your Access Level is not permitted for this page!"; 
			break;
	}
}

echo $counter == 0?"<p><br><p><p><center>This page is a restrcited area. You do not have access!":"";
print "$closeHTML";

?>

hmm it didnt work. Ive even tried using if .. elseif ... elseif ... but that just comes up with an error. :(

oh ...let me try your way. I didnt refresh so had no idea you replied. lol

Hmm it seems to not be recognising it and shows up =
This page is a restrcited area. You do not have access!

My access is still set to 0 and 1 so it should show level 1 access for one of my jobs.

How do you mean you have cnahged it to int?

The field in the table for lobby is already an int. Then obviously has either 1,2,3 or 0

:>/ Im just trying to work out how to maybe change it and define it slightly more.


Here is what I just tried and it didnt do anything different. lol

$find_access = fetch ("SELECT job_id FROM job_roles WHERE user_id = '$username'");

print "$openHTML";
 
  //checking to see if member has a level 1 position for lobby
 
 
$find_job = mysql_query("SELECT * FROM job_positions WHERE id = '$find_access[job_id]' AND lobby = 1");
$counter = 0;
while($row = mysql_fetch_array($find_job))
{
	if ( $row[lobby]== 1) 
	{ 
		echo "show content gere for access level 1"; 
	} 
	
	$counter++;
}


elseif {



 //checking to see if member has a level 2 position for lobby


$find_access2 = fetch ("SELECT job_id FROM job_roles WHERE user_id = '$username'");

 
$find_job2 = mysql_query("SELECT * FROM job_positions WHERE id = '$find_access2[job_id]' AND lobby = 2");
$counter2 = 0;
while($row2 = mysql_fetch_array($find_job2))
{
	if ( $row2[lobby]== 2) 
	{ 
		echo "show content gere for access level 2"; 
	} 
	
	$counter2++;
}

}

elseif
{

 //checking to see if member has a level 3 position for lobby

$find_access3 = fetch ("SELECT job_id FROM job_roles WHERE user_id = '$username'");


 
$find_job3 = mysql_query("SELECT * FROM job_positions WHERE id = '$find_access3[job_id]' AND lobby = 3");
$counter3 = 0;
while($row3 = mysql_fetch_array($find_job3))
{
	if ( $row3[lobby]== 3) 
	{ 
		echo "show content gere for access level 3"; 
	} 
	
	$counter3++;
}

}

else
{
 //checking to see if member has a level 3 position for lobby

$find_access0 = fetch ("SELECT job_id FROM job_roles WHERE user_id = '$username'");


 
$find_job0 = mysql_query("SELECT * FROM job_positions WHERE id = '$find_access0[job_id]' AND lobby = 0");
$counter0 = 0;
while($row0 = mysql_fetch_array($find_job0))
{
	if ( $row0[lobby]== 0) 
	{ 
		echo "show content gere for access level 0"; 
	} 
	
	$counter0++;
}
}
print "$closeHTML";

oh actually it come up with an error saying unexpected t_if else error thing. :O( God I wish there was a solution. lol

The fact is that no matter how you have it set up in the database, it all comes out as type string. So to be most accurate, we are converting it to type integer.

Anyway, it is time to get dirty :)
BTW: I love errors

Run this script, it will dump the queries and the array data that it is looping through

<?php
$debug = true; //set to false if not debugging script

$query = "SELECT * FROM job_roles WHERE user_id = '$username'";
$find_access = fetch ($query);
echo $debug?$query . "<br />":""; //if in debug, display query

print "$openHTML";
 
$query = "SELECT * FROM job_positions WHERE name = '$find_access[job_id]'";
$find_job = mysql_query($query);
echo $debug?$query . "<br />":""; //if in debug, display query

$counter = 0;
while($row = mysql_fetch_array($find_job))
{
	if($debug) //if in debug, dump array content to browser
	{
		var_dump($row);
		echo "<br />";
	}
	
	if((int)$row[lobby] > 0)
	{
		$counter++;
	}
	
	switch((int)$row['lobby'])
	{
		case 1:
			echo "show content gere for access level 1"; 
			break;
		case 2:
			echo "show content gere for access level 2";
			break;
		case 3:
			echo "show content gere for access level 3"; 
			break;
		case 4:
			echo "show content gere for access level 4";
			break;
		case 0:
			echo "<p><br><p><p><center>Sorry but your Access Level is not permitted for this page!"; 
			break;
		default:
			echo "<p><br><p><p><center>Sorry but your Access Level is not permitted for this page!"; 
			break;
	}
}

echo $counter == 0?"<p><br><p><p><center>This page is a restrcited area. You do not have access!":"";
print "$closeHTML";

?>

The first thing you are going to look at is the data that the arrays are dumping, is it what you expect? It probably won't be, I also set it up to echo your query statements so you can paste them into your database manager query window to see exactly what data they are pulling.

Ah ok thanks.

Ive never done that query database manager thing and gave it a go. It showed me the job_roles table with all rows.

:>/

Ah ok thanks.

Ive never done that query database manager thing and gave it a go. It showed me the job_roles table with all rows.

:>/

So are you looking to loop through all the rows from the only the second query and the first as well? And did the script actually loop through all the data as you were expecting, probably not huh?

nope!
lol it didnt.

I dunno what im doing now. Got myself all confused.

I thought using the IF statement with else if for each level.

So it would check IF job position equals to 1 in the lobby field then show this ....

elseif the job position equals to 2 in the lobby field then show this .... and so on. That way it would have first checked to see if that member has a level one job then if not to see if they have a level 2 job and so on.

Arrghh ... lol

The problem is that for some reason it will only return 1 result of jobs for the user. I want to be able to allocate more than one position to my admins and therefore have the code find all job positions allocated to that member and then allow the highest level.

.... so say for example ... one admin has the job position of technical support and moderator! Tech support is level 1 (highest) and moderator is level 4! By using this method the code only seems to pick up the first job allocated to that member in the database and wont check all job positions!

I think what you are saying is you want to loop through all database results from both queries, try this:

<?php
$debug = false; //set to false if not debugging script

echo $debug?$query . "<br />":""; //if in debug, display query

print "$openHTML";
 
$query = "SELECT * FROM job_roles WHERE user_id = '$username'";
$findresult = mysql_query($query);
while($find_access = mysql_fetch_assoc($findresult))
{
	$query = "SELECT * FROM job_positions WHERE name = '".$find_access['job_id']."'";
	$find_job = mysql_query($query);
	echo $debug?$query . "<br />":""; //if in debug, display query
	
	$counter = 0;
	while($row = mysql_fetch_array($find_job))
	{
		if($debug) //if in debug, dump array content to browser
		{
			var_dump($row);
			echo "<br />";
		}
		
		if((int)$row[lobby] > 0)
		{
			$counter++;
		}
		
		switch((int)$row['lobby'])
		{
			case 1:
				echo "show content gere for access level 1"; 
				break;
			case 2:
				echo "show content gere for access level 2";
				break;
			case 3:
				echo "show content gere for access level 3"; 
				break;
			case 4:
				echo "show content gere for access level 4";
				break;
			case 0:
				echo "<p><br><p><p><center>Sorry but your Access Level is not permitted for this page!"; 
				break;
			default:
				echo "<p><br><p><p><center>Sorry but your Access Level is not permitted for this page!"; 
				break;
		}
	}
}

echo $counter == 0?"<p><br><p><p><center>This page is a restrcited area. You do not have access!":"";
print "$closeHTML";

?>

Heya .... sorry for the dealy. I have got somewhere but still have a slight issue!

The code is now working but is of course showing results for all access levels when all I want is one result!

I had a think about it all and worked out the problem was with the initial variables. Because I have the 2 tables that I need to retrieve info from and need to create a variable for that...it was still only getting one result.

So I have searched and found out you can create a JOIN to select the information from 2 tables and basically make it one variable.

So now the code works but returns all results for each access level. This would obviusly look a mess and so I need to work out how to make it so it only returns the results for level 1 access first ... then if no level 1 access check and return results for level 2....and if none then to continue!

:>/ Any Ideas? im just going to have a play and see what i can come up with! :O)

Thanks for all your help. :O)

$find_access =  mysql_query("SELECT user_id,lobby,shops FROM job_roles,job_positions WHERE job_roles.job_id = job_positions.id");

print "$openHTML";
 
$find_job = fetch("SELECT * FROM job_positions WHERE name = '$find_access[job_id]'");
$counter = 0;
while($row = mysql_fetch_array($find_access))
{
	if ( ($row[lobby]== 1) AND ($row[user_id] == $username))
	{ 
		echo "show content gere for access level 1"; 
		$counter++;
	} 
	if ( ($row[lobby]== 2) AND ($row[user_id] == $username))
	{   
		echo "show content gere for access level 2";
		$counter++;
	} 
	if ( ($row[lobby]== 3) AND ($row[user_id] == $username))
	{ 
		echo "show content gere for access level 3 "; 
		$counter++;
	} 
	if ( ($row[lobby]== 4) AND ($row[user_id] == $username))
	{ 
		echo "show content gere for access level 4"; 
		$counter++;
	}
	
	if ( ($row[lobby]== 0) AND ($row[user_id] == $username))
	{ 
		echo "<p><br><p><p><center>Sorry but your Access Level is not permitted for this page!"; $counter++;
	}
}

echo $counter == 0?"<p><br><p><p><center>This page is a restrcited area. You do not have access!":"";
print "$closeHTML";
This article has been dead for over six months. Start a new discussion instead.