News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Array Filtering

Started by mickjav, July 19, 2022, 04:07:44 PM

Previous topic - Next topic

mickjav

I have a recordset that I want to get two counts from The Total Records Returned plus a count on the number of records marked as is_fan 0 or 1 rather than creating a second recordset for the second count I wanted to see if I could use an array but having trouble understanding the filter thing.

hopefully somebody can point me in the right direction.

mick

p.s. I could create a loop as a thought but feel that wouldn't be the correct way.

mickjav


live627

Quote from: mickjav on July 19, 2022, 04:07:44 PMp.s. I could create a loop as a thought but feel that wouldn't be the correct way.
nothing wrong with a good ol' loop if it gets you what you want.

The functional coding purists are gonna hate me now.

mickjav

#3
Finally Put my brain on  :o

Did This in the end

FUNCTION GetLikesFans($art)
{
global $smcFunc, $scripturl, $txt;

//Check Member hasen't been added for this artist
$dbquery = $smcFunc['db_query']('', '
    SELECT COUNT(a.art_id) AS likes, SUM(a.is_fan) AS fans
    FROM {db_prefix}artists_fans AS a
WHERE art_id = {int:this_art}',
array(
'this_art' => $art,
));

$row = $smcFunc['db_fetch_assoc']($dbquery);
$smcFunc['db_free_result']($dbquery);
if ($row['fans'] == '')
$nofans = 0;
else
$nofans = $row['fans'];

$likes = str_replace("##", $row['likes'], $txt['Music_artist_likes']);
$fans = str_replace("##", $nofans, $txt['Music_artist_fans']);

return $fans . ' '. $likes;
}

End Result here: https://www.databasedreams.co.uk/testing/index.php?action=music;area=artists;sa=home;art=-477275235

SpacePhoenix

Give this a try:

FUNCTION GetLikesFans($art)
{
global $smcFunc, $scripturl, $txt;

//Check Member hasen't been added for this artist
$dbquery = $smcFunc['db_query']('', '
    SELECT COUNT(a.art_id) AS likes, COALESCE(SUM(a.is_fan),0) AS nofans
    FROM {db_prefix}artists_fans AS a
WHERE art_id = {int:this_art}',
array(
'this_art' => $art,
));

$row = $smcFunc['db_fetch_assoc']($dbquery);
$smcFunc['db_free_result']($dbquery);

$likes = str_replace("##", $row['likes'], $txt['Music_artist_likes']);
$fans = str_replace("##", $nofans, $txt['Music_artist_fans']);

return $fans . ' '. $likes;
}

mickjav

Quote from: SpacePhoenix on July 20, 2022, 03:13:20 PMGive this a try:

FUNCTION GetLikesFans($art)
{
global $smcFunc, $scripturl, $txt;

//Check Member hasen't been added for this artist
$dbquery = $smcFunc['db_query']('', '
    SELECT COUNT(a.art_id) AS likes, COALESCE(SUM(a.is_fan),0) AS nofans
    FROM {db_prefix}artists_fans AS a
WHERE art_id = {int:this_art}',
array(
'this_art' => $art,
));

$row = $smcFunc['db_fetch_assoc']($dbquery);
$smcFunc['db_free_result']($dbquery);

$likes = str_replace("##", $row['likes'], $txt['Music_artist_likes']);
$fans = str_replace("##", $nofans, $txt['Music_artist_fans']);

return $fans . ' '. $likes;
}

Thanks That's much better

Advertisement: