Page 1 of 1

failing to get database result

Posted: Wed Dec 19, 2007 11:32 pm
by ewel
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!

Re: failing to get database result

Posted: Thu Dec 20, 2007 7:27 am
by ewel
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);

Re: failing to get database result

Posted: Thu Dec 20, 2007 12:16 pm
by ewel
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!