Make Array Dynamic

Ask about a PHP problem here.
Post Reply
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Make Array Dynamic

Post by unemployment »

I need to make the [0] dynamic so that it displays all of the companies in the array.
$own_company = fetch_owned_companies($user_info['uid']);

if ($news['companyid'] == $own_company[0]['companyid'])
{
	$viewer_user_lvl = 7;
}
else
{
	$viewer_user_lvl = user_lvl($news['id']);
}

Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Make Array Dynamic

Post by Tino »

Use a while loop to fetch the data instead.

As a general example:
$query = mysql_query("SELECT `title` FROM `posts`");

while ( $row = mysql_fetch_assoc($query) ) {
    if ( 'condition here' ) {
        $var = 1;
    }
}
Is that what you mean?
Please check out my CodeCanyon items.
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Make Array Dynamic

Post by unemployment »

Tino wrote:Use a while loop to fetch the data instead.

As a general example:
$query = mysql_query("SELECT `title` FROM `posts`");

while ( $row = mysql_fetch_assoc($query) ) {
    if ( 'condition here' ) {
        $var = 1;
    }
}
Is that what you mean?
Tino,

I am using a while loop in my function to fetch the data.

The <pre> is displayed as...

Output
Array
(
   

    => Array


        (
            [companyid] => 1
            [companyname] => s
            [companytag] =>
            [companywebsite] => http://s.com
            [country] => 1
            [state] => 0
            [city] =>
            [industry] => 4
            [stage] => 2
            [capitalrequested] =>
            [guestviews] => 0
            [iviews] => 3
            [eviews] => 3
        )

)
I suppose what I should have said is that the array is dynamic, but I am not performing a dynamic check.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make Array Dynamic

Post by jacek »

This code looks familiar :lol:

You would need to us ea foreach loop to loop over all of the returned companies
foreach ($own_company as $company){
    // do something
}
But it may be better to create a new function that checks the database to see if the given company id belongs to the logged in use, which you could do with a single query.
SELECT COUNT(`companid`) FROM `companies` WHERE `companyid` = {$cid} AND `user_id` = {$GLOBALS['user_info']['uid']}
I can't really remember the field names that are used, but that should give you the idea.
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Make Array Dynamic

Post by unemployment »

jacek wrote:This code looks familiar :lol:

You would need to us ea foreach loop to loop over all of the returned companies
foreach ($own_company as $company){
    // do something
}
But it may be better to create a new function that checks the database to see if the given company id belongs to the logged in use, which you could do with a single query.
SELECT COUNT(`companid`) FROM `companies` WHERE `companyid` = {$cid} AND `user_id` = {$GLOBALS['user_info']['uid']}
I can't really remember the field names that are used, but that should give you the idea.
Yes, that is exactly what I am going to do. I'm just going to make an is_admin function. That should do what I need to in a much nicer way.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make Array Dynamic

Post by jacek »

unemployment wrote:Yes, that is exactly what I am going to do. I'm just going to make an is_admin function. That should do what I need to in a much nicer way.
was there not already an is_company_owner() type function ?
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Make Array Dynamic

Post by unemployment »

jacek wrote:
unemployment wrote:Yes, that is exactly what I am going to do. I'm just going to make an is_admin function. That should do what I need to in a much nicer way.
was there not already an is_company_owner() type function ?
No, I checked and there wasn't.

I'm getting this error on my new function. Warning: mysql_result(): Unable to jump to row 0 on MySQL result index.
function is_admin($uid, $cid)
{
	$uid = (int)$uid;
	$cid = (int)$cid;
	
	$sql =	"SELECT
				`users`.`id` AS `uid`,
				`companies`.`companyid` AS `cid`,
				`companies`.`adminid` AS `aid`
			FROM `companies`
			LEFT JOIN `users`
			ON `users`.`id` = companies.adminid
			WHERE `users`.`id` = {$uid}
			AND `companies`.`companyid` = {$cid}";
	
	$user = mysql_query($sql);
	
	return (mysql_result($user, 0) == '1') ? true : false;	
}
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make Array Dynamic

Post by jacek »

Can't you just do
        $sql =  "SELECT
                                COUNT(`companies`.`companyid`)
                        FROM `companies`
                        WHERE `companies`.`adminid` = {$uid}
                        AND `companies`.`companyid` = {$cid}";
??
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Make Array Dynamic

Post by unemployment »

jacek wrote:Can't you just do
        $sql =  "SELECT
                                COUNT(`companies`.`companyid`)
                        FROM `companies`
                        WHERE `companies`.`adminid` = {$uid}
                        AND `companies`.`companyid` = {$cid}";
??
Yup :)
Post Reply