Page 1 of 1

Associative array can't fetch the last data.

Posted: Wed Dec 15, 2010 3:57 am
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.

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

Posted: Wed Dec 15, 2010 4:41 am
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']?

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

Posted: Wed Dec 15, 2010 4:48 am
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

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

Posted: Wed Dec 15, 2010 5:19 am
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.

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

Posted: Wed Dec 15, 2010 5:40 am
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

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

Posted: Wed Dec 15, 2010 5:52 am
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?

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

Posted: Thu Dec 16, 2010 2:25 am
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