failing to get database result

Discussion and education for beginner / novice programmers interested in embarking on the development process to take advantage of the extensible nature of the Joomla! CMS.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Forum rules
Post Reply
ewel
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Mon Oct 01, 2007 11:35 am

failing to get database result

Post by ewel » Wed Dec 19, 2007 11:32 pm

Hopefully someone will be able to help me with this because I am quite stuck...

I am trying to make some code that checks the database to see which pages are part of a category with a given id, and which categories and pages are part of a section with a given id (and then the code checks if any of that matches the id of the current page). I spent a good amount of time researching how this could be done and then wrote my code, but unfortunately it fails to produce a result.

I really have no idea why. I first thought I might have failed to call the database or make a connection but I see other modules working without that, so I have no clue how I can improve my code.

Here is the code:

Code: Select all

$moap_catpageids = 0;
$database->setQuery("SELECT id FROM jos_content WHERE catid=$moap_catids");
$catpageids = $database->loadResultArray();
$catpageidn = explode(",",$catpageids);
foreach ($catpageidn as $catpageid) {
   if ($catpageid == $id) {
      $moap_catpageids = 1;
   }
}
$moap_sectpageids = 0;
$database->setQuery("SELECT id FROM jos_content WHERE sectionid=$moap_sectids");
$sectpageids = $database->loadResultArray();
$sectpageidn = explode(",",$sectpageids);
foreach ($sectpageidn as $sectpageid) {
   if ($sectpageid == $id) {
      $moap_sectpageids = 1;
   }
}
$moap_sectcatids = 0;
$database->setQuery("SELECT id FROM jos_categories WHERE section=$moap_sectids");
$sectcatids = $database->loadResultArray();
$sectcatidn = explode(",",$sectcatids);
foreach ($sectcatidn as $sectcatid) {
   if ($sectcatid == $id) {
      $moap_sectcatids = 1;
   }
}

Apart from this I do not have any code in my module's php document which refers to the database. The $id variable is defined elsehwere in the code and is not the issue. By echoing the variable that should be filled by the loadResultArray I know that no value is produced.

Please help!

ewel
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Mon Oct 01, 2007 11:35 am

Re: failing to get database result

Post by ewel » Thu Dec 20, 2007 7:27 am

Anyone please? With this I would be near completion of a module which I plan to share with the community, but I really need the community first to make it work!

Meanwhile I also tried without the 'explode' bit, but clearly the fault is in the second and third line. I also unsuccessfully tried to replace the third line by this:

Code: Select all

$query = "SELECT id FROM jos_content WHERE catid=$moap_catids";
$database->setQuery($query);
Last edited by ewel on Thu Dec 20, 2007 10:06 am, edited 1 time in total.

ewel
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Mon Oct 01, 2007 11:35 am

Re: failing to get database result

Post by ewel » Thu Dec 20, 2007 12:16 pm

Thankfully I managed to solve it... I discovered by chance that $moap_catids and $moap_sectids are arrays, so I had to put what I had into a foreach loop after exploding the arrays. Well I suppose this is how one learns - pity though that it is so incredibly time consuming!


Post Reply