Welcome Guest, Not a member yet? Register   Sign In
Need help converting old SQL to CI's Active Record
#1

[eluser]daparky[/eluser]
I need a little help converting the following old SQL's

Code:
function getFeature($id) {

$sql    = "    SELECT    f.feature_id        AS    id,
f.feature_category    AS    catId,
f.feature_template    AS    tempId,
f.feature_added        AS    added,
f.feature_addedby        AS    addedBy,
f.feature_edited        AS    edited,
f.feature_editedby    AS    editedBy,
f.feature_parent        AS    parent,
f.feature_approved    AS    approved,
f.feature_order        AS    forder,
f.feature_on        AS    fon,
f.feature_off        AS    foff,
c.category_name        AS     catName,
t.template_name        AS    tempName,
t.template_page        AS    tempPage
FROM    db_features            f,
db_templates        t,
db_categories        c
WHERE    f.feature_id        = '".$id."'
AND    t.template_id        = f.feature_template
AND    c.category_id        = f.feature_category";

$result    = mysql_query($sql);
$det        = array();

$rs = mysql_fetch_array($result);

$det['id']            = $rs['id'];
$det['tempId']        = $rs['tempId'];
$det['added']        = $rs['added'];
$det['addedBy']        = $rs['addedBy'];
$det['edited']        = $rs['edited'];
$det['editedBy']        = $rs['editedBy'];
$det['on']            = $rs['fon'];
$det['off']            = $rs['foff'];
$det['parent']        = $rs['parent'];
$det['catId']        = $rs['catId'];
$det['approved']        = $rs['approved'];
$det['order']        = $rs['forder'];
$det['catName']        = stripslashes($rs['catName']);
$det['tempName']        = stripslashes($rs['tempName']);
$det['tempPage']        = $rs['tempPage'];

$attributes    = $this->featureAttributes($id, $rs['tempId']);

$z=0;
foreach($attributes as $key => $value) {
$det[$key]            = $value;
$det[$z]            = $value;
$z++;
}

return($det);

}


function featureAttributes($feature, $template) {

$sql    = "    SELECT        a.attribute_id,
at.type_name            as type,
ta.attribute_form_name        as form
FROM        db_templates            t,
db_template_attributes        ta,
db_attributes            a,
db_attribute_types        at
WHERE        t.template_id    = '".$template."'
AND            ta.template_id    = t.template_id
AND            a.attribute_id    = ta.attribute_id
AND            at.type_id        = a.attribute_type
ORDER BY    ta.attribute_order ASC";

$result    = mysql_query($sql) or die (mysql_error());

while($rs = mysql_fetch_array($result)) {

$attSql        = "    SELECT    fa.fa_".$rs['type']."
FROM        db_feature_attributes    fa
WHERE        fa.fa_feature_id        = '".$feature."'
AND        fa.fa_attribute_id    = '".$rs['attribute_id']."'";

$attResult    = mysql_query($attSql) or die (mysql_error());
$attRs        = mysql_fetch_array($attResult);

$att[$rs['form']]    = $attRs['fa_'.$rs['type']];

}

return($att);

}

to Codeigniters Active Record. Please note that i want the results to output as objects, the above queries are for arrays.

The output result looks like this:

Code:
<?php
$form        = $cCms->tempAttributes(1);
$feature    = $cCms->getFeature(1);            
for($i=0; $i<count($form); $i++) { ?&gt;
&lt;input name="&lt;?= $form[$i]['form'] ?&gt;" type="text" style="width:100%;" value="&lt;?= $feature[$form[$i]['form']]; ?&gt;" /&gt;
&lt;?php
} ?&gt;

I've been trying all night and cannot achieve anything from it. Basically i'm converting a CMS to Codeigniter.

Any help would be much appreciated.
#2

[eluser]Damien K.[/eluser]
My first impression of this looks trivial... you may want to try to attempt it now.
#3

[eluser]bretticus[/eluser]
What specifically is tripping you up?

For example your first function could be the get function (method is more appropriate here) of your db_features model.

Converting is easy. I suspect you just need to set the 2nd param of select() to FALSE to preserve the column aliases. ie.
Code:
$this->db_select('f.feature_id AS id, f.feature_category AS catId,..'. FALSE)

Really though, you will have to be more specific than just stating you can't get it to work to get more help in the forum here. In other words, at least show your CI code.




Theme © iAndrew 2016 - Forum software by © MyBB