Forum Moderators: coopster

Message Too Old, No Replies

Refactoring Code

How can I minimize it?

         

abuzurah

7:29 pm on May 13, 2009 (gmt 0)

10+ Year Member



I have this code that work for me, but how can i minimize it. So I can make it unlimited and small size.


$pcat = 1;
$query = "SELECT scat.catid FROM cats_relations AS srel LEFT JOIN categories AS scat ON srel.catid = scat.catid WHERE srel.parentid = {$pcat} AND scat.published = 1 ORDER BY ordering ASC";
$database->setQuery( $query );
$catrel = $database->loadResultArray();

$catrelimp = implode(",", $catrel);
if (!empty($pcat)){

if (!empty($catrelimp)) {
$catids1 = explode( ',', $catrelimp );
JArrayHelper::toInteger($catids1);
$whereCatid1 = "parentid=" . implode( " OR parentid=", $catids1 ) . "";
$query = "SELECT catid FROM `cats_relations` WHERE " . $whereCatid1 . " ORDER BY catid ASC LIMIT {$limit}";
$database->setQuery( $query );
$s_results1 = $database->loadResultArray();
$catrelimp1 = implode(",", $s_results1);
}
else {$catrelimp1 = "";}

if (!empty($catrelimp1)) {
$catids2 = explode( ',', $catrelimp1 );
JArrayHelper::toInteger($catids2);
$whereCatid2 = "parentid=" . implode( " OR parentid=", $catids2 ) . "";
$query = "SELECT catid FROM `#cats_relations` WHERE " . $whereCatid2 . " ORDER BY catid ASC LIMIT {$limit}";
$database->setQuery( $query );
$s_results2 = $database->loadResultArray();
$catrelimp2 = implode(",", $s_results2);
}
else {$catrelimp2 = "";}

if (!empty($catrelimp2)) {
$catids3 = explode( ',', $catrelimp2 );
JArrayHelper::toInteger($catids3);
$whereCatid3 = "parentid=" . implode( " OR parentid=", $catids3 ) . "";
$query = "SELECT catid FROM `cats_relations` WHERE " . $whereCatid3 . " ORDER BY catid ASC LIMIT {$limit}";
$database->setQuery( $query );
$s_results3 = $database->loadResultArray();
$catrelimp3 = implode(",", $s_results3);
}
else {$catrelimp3 = "";}

if (!empty($catrelimp3)) {
$catids4 = explode( ',', $catrelimp3 );
JArrayHelper::toInteger($catids4);
$whereCatid4 = "parentid=" . implode( " OR parentid=", $catids4 ) . "";
$query = "SELECT catid FROM `cats_relations` WHERE " . $whereCatid4 . " ORDER BY catid ASC LIMIT {$limit}";
$database->setQuery( $query );
$s_results4 = $database->loadResultArray();
$catrelimp4 = implode(",", $s_results4);
}
else {$catrelimp4 = "";}

if (!empty($catrelimp4)) {
$catids5 = explode( ',', $catrelimp4 );
JArrayHelper::toInteger($catids5);
$whereCatid5 = "parentid=" . implode( " OR parentid=", $catids5 ) . "";
$query = "SELECT catid FROM `cats_relations` WHERE " . $whereCatid5 . " ORDER BY catid ASC LIMIT {$limit}";
$database->setQuery( $query );
$s_results5 = $database->loadResultArray();
$catrelimp5 = implode(",", $s_results5);
}
else {$catrelimp5 = "";}

}

$catparent = ("$catrelimp,$catrelimp1,$catrelimp2,$catrelimp3,$catrelimp4,$catrelimp5");
$catparentexp = explode(',', $catparent);
JArrayHelper::toInteger($catparentexp);
$catparentimp = "\n AND (relation.catid=" . implode( " OR relation.catid=", $catparentexp ) . " )";

eelixduppy

8:12 am on May 14, 2009 (gmt 0)



This is what I came up with real quick. This is of course untested so we'll see how well this works...



$results = array();
$i = 0;
#
$query = "SELECT scat.catid FROM cats_relations AS srel LEFT JOIN categories AS scat ON srel.catid = scat.catid WHERE srel.parentid = {$pcat} AND scat.published = 1 ORDER BY ordering ASC";
$database->setQuery( $query );
$catrel = $database->loadResultArray();
#
if (!empty(catrel)){
#
for($i; $i < 5; $i++) {
if($i != 0 && empty($results[$i - 1])) break;
if($i == 0)
$query = 'SELECT `catid` FROM `cats_relations` WHERE `parentid`=' . implode(' OR `parentid`=', $catrel) . ' ORDER BY `catid` ASC LIMIT ' . $limit;
else
$query = 'SELECT `catid` FROM `cats_relations` WHERE `parentid`=' . implode(' OR `parentid`=', $results[$i - 1]) . ' ORDER BY `catid` ASC LIMIT ' . $limit;
#
$database->setQuery($query);
$results[] = $database->loadResultArray();
}
#
$catparentimp = "\n AND (relation.catid=" . implode(' OR relation.catid=', array_values($results)) . " )";
}

Should get you started at least. ;)

abuzurah

5:44 pm on May 14, 2009 (gmt 0)

10+ Year Member



Thanks for your replay eelixduppy.

I'm using what you give but not work for me, but anyway I have do it like this and that work for me.


$pcat = 1;
$i = 0;
$query = "SELECT scat.catid FROM cats_relations AS srel LEFT JOIN categories AS scat ON srel.catid = scat.catid WHERE srel.parentid = {$pcat} AND scat.published = 1 ORDER BY ordering ASC";
$database->setQuery( $query );
$catrel = $database->loadResultArray();
$catrelimp[$i] = implode(",", $catrel);

if (!empty($pcat)){
$ii = 1;

while (!empty($catrelimp[$i])) {
if (!empty($catrelimp[$i])) {
$catids[$ii] = explode( ',', $catrelimp[$i] );
JArrayHelper::toInteger($catids[$ii]);
$whereCatid[$ii] = "parentid=" . implode( " OR parentid=", $catids[$ii] ) . "";
$query = "SELECT catid FROM `cats_relations` WHERE " . $whereCatid[$ii] . " ORDER BY catid ASC LIMIT {$limit}";
$database->setQuery( $query );
$s_results[$ii] = $database->loadResultArray();
$catrelimp[$ii] = implode(",", $s_results[$ii]);
$i++;
$ii++;

}
else {$catrelimp[$ii] = "";}
}

}

$catparent = implode(",", $catrelimp);
$catparentexp = explode(',', $catparent);
JArrayHelper::toInteger($catparentexp);
$catparentimp = "\n AND (relation.catid=" . implode( " OR relation.catid=", $catparentexp ) . " )";