Page 1 of 1
Make Array Dynamic
Posted: Mon May 16, 2011 5:08 pm
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']);
}
Re: Make Array Dynamic
Posted: Mon May 16, 2011 5:16 pm
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?
Re: Make Array Dynamic
Posted: Mon May 16, 2011 5:20 pm
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.
Re: Make Array Dynamic
Posted: Mon May 16, 2011 5:30 pm
by jacek
This code looks familiar
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.
Re: Make Array Dynamic
Posted: Mon May 16, 2011 5:49 pm
by unemployment
jacek wrote:This code looks familiar
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.
Re: Make Array Dynamic
Posted: Mon May 16, 2011 5:52 pm
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 ?
Re: Make Array Dynamic
Posted: Mon May 16, 2011 5:53 pm
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;
}
Re: Make Array Dynamic
Posted: Mon May 16, 2011 6:02 pm
by jacek
Can't you just do
$sql = "SELECT
COUNT(`companies`.`companyid`)
FROM `companies`
WHERE `companies`.`adminid` = {$uid}
AND `companies`.`companyid` = {$cid}";
??
Re: Make Array Dynamic
Posted: Mon May 16, 2011 6:03 pm
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