Associative array can't fetch the last data.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thenndral
Forum Newbie
Posts: 20
Joined: Wed Sep 29, 2010 8:36 pm

Associative array can't fetch the last data.

Post by thenndral »

Hello,

Getting data from mysql and assign to associative array
Last data not assign to associative array.

for ex:

mysql query:

$qry = "
SELECT ci.itemType, SUM(ci.itemValue) AS itemValue
FROM count_info AS ci, s_info as si
WHERE si.`site_cd`=ci.`site_info`
AND ci.info_datetime BETWEEN '$date_start' AND '$date_end'
AND ci.adfile_uid='$ad_uid'
AND si.areacode = '$ph_code'
GROUP BY ci.itemType
ORDER BY ci.`itemtype`";

Result set:
itemType itemValue
MVC 942943
MTC 106503
SVC 213587
STC 1411050

php:
//define associative array
$as_arr = array('MVC'=>0, 'MTC'=>0,'SVC'=>0, 'STC'=>0);

All data assign perfectly, except last one[STC].

I'm getting the data by this way,

echo "MVC = " . $as_arr ['MVC']; // get data perferctly
echo "MTC = " . $as_arr ['MTC']; // get data perferctly
echo "SVC = " . $as_arr ['SVC']; // get data perferctly

echo "SRC = " . $as_arr ['SRC']; // NOT GETTING DATA

Code help please.
Where i did mistake? Please correct the code.

Thanks & Best Regards,
Thendral.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Associative array can't fetch the last data.

Post by social_experiment »

Code: Select all

<?php echo "SRC = " . $as_arr ['SRC']; // NOT GETTING DATA ?>
Maybe this is a typo but in your code the last element of the array is 'STC' and you are calling $as_arr['SRC']?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
thenndral
Forum Newbie
Posts: 20
Joined: Wed Sep 29, 2010 8:36 pm

Re: Associative array can't fetch the last data.

Post by thenndral »

hi,

Sorry for inconvenience. I mistyped.

This is my correct sentence.

echo "STC = " . $as_arr ['STC']; // NOT GETTING DATA
same thing is happening, not getting data.

Thanks,
thendral
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Associative array can't fetch the last data.

Post by VladSun »

Post the missing code parts - ie.e fetching the data from the DB server, assigning data to you array, etc.

Please use [syntax=php ][/syntax] for your code blocks.
There are 10 types of people in this world, those who understand binary and those who don't
thenndral
Forum Newbie
Posts: 20
Joined: Wed Sep 29, 2010 8:36 pm

Re: Associative array can't fetch the last data.

Post by thenndral »

hi,

please refer to my previous post.
hope it helps.

$qry = "
SELECT ci.itemType, SUM(ci.itemValue) AS itemValue
FROM count_info AS ci, s_info as si
WHERE si.`site_cd`=ci.`site_info`
AND ci.info_datetime BETWEEN '$date_start' AND '$date_end'
AND ci.adfile_uid='$ad_uid'
AND si.areacode = '$ph_code'
GROUP BY ci.itemType
ORDER BY ci.`itemtype`";


$this->res = $this->query($qry);
if ( $this->getCount() > 0 ) {


//define associative array
$as_arr = array('MVC'=>0, 'MTC'=>0,'SVC'=>0, 'STC'=>0);

echo "MVC = " . $as_arr ['MVC']; // get data perferctly
echo "MTC = " . $as_arr ['MTC']; // get data perferctly
echo "SVC = " . $as_arr ['SVC']; // get data perferctly

echo "SRC = " . $as_arr ['SRC']; // NOT GETTING DATA

Thanks,
thenral
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Associative array can't fetch the last data.

Post by social_experiment »

Code: Select all

<?php
//define associative array
$as_arr = array('MVC'=>0, 'MTC'=>0,'SVC'=>0, 'STC'=>0);
?>
This code tells me each of the values are equal to 0. How did you create the array?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
thenndral
Forum Newbie
Posts: 20
Joined: Wed Sep 29, 2010 8:36 pm

Re: Associative array can't fetch the last data.

Post by thenndral »

Hello,

I fix it.

I assign data through while loop, before last data while loop stop.
after change the count value, it works charm.

Thanks for giving the idea.

Thanks again,
Thendral
Post Reply