Skip to main content
Inspiring
April 2, 2011
Question

Need Help Converting a small bit of ColdFusion to PHP

  • April 2, 2011
  • 3 replies
  • 1239 views

I don't know much about PHP, but am hoping someone can help me convert this to PHP.

<cfquery datasource="mydb">

SELECT brand,type,model_name,url

FROM features

ORDER BY type,brand

</cfquery>

<cfoutput group="type">

    <h2>#type#</h2>

  <cfoutput group="brand">

    <strong>#brand#</strong><br />

    <cfoutput>

        #model_name#<br />

    </cfoutput>

  <br />

  </cfoutput>

</cfoutput>

Thanks.

    This topic has been closed for replies.

    3 replies

    Known Participant
    April 7, 2011

    I'm no expert in PHP, but there's no real way of doing it (easy).  As the poster above said, you will need to create some variables (you don't need sessions) and test each data row to see if they match, if they don't, then output the data.

    A very cut down version - something like this (haven't tested it or anything);

    $getData = "SELECT brand,type,model_name,url FROM features ORDER BY type,brand";

    ... database connection/querying stuff here etc etc...

    $type = ''; // empty var here

    $brand = ''; // empty var here

    foreach($getData as $getDataRow) {

         if ($type != $getDataRow->type) {

              echo '<h2>' . $getDataRow->type . '</h2>';

         }

         if ($brand != $getDataRow->brand) {

              echo '<strong>' . $getDataRow->brand . '</strong><br />';

         }

         echo $getDataRow->model_name . '<br />';

    }

    Got to love Coldfusion don't you!

    Inspiring
    April 7, 2011

    iSaid, thanks so much! I had to add two lines to make the variable set $type & $brand at the end of the loop to the current row value, but now it works like a charm.

    $getData = "SELECT brand,type,model_name,url FROM features ORDER BY type,brand";

    ... database connection/querying stuff here etc etc...

    $type = ''; // empty var here

    $brand = ''; // empty var here

    foreach($result as $getDataRow) {

         if ($type != $getDataRow->type) {

              echo '<h2>' . $getDataRow->type . '</h2>';

         }

         if ($brand != $getDataRow->brand) {

              echo '<strong>' . $getDataRow->brand . '</strong><br />';

         }

         echo $getDataRow->model_name . '<br />';

    $type = $getDataRow->type;

    $brand = $getDataRow->brand;

    }

    Known Participant
    April 8, 2011

    I had to add two lines to make the variable set $type & $brand at the end of the loop to the current row value, but now it works like a charm.

    Oops... that might help - I forgot to add that - sorry.

    Inspiring
    April 5, 2011

    I left the query name out by accident, but I think you can get the idea.

    Thanks.

    WolfShade
    Legend
    April 6, 2011

    It's basically along the lines of creating a session variable and setting it to "" (blank), then starting your outer loop, compare a value of the recordset to that - if it doesn't match, make it match and start the next level of loop; and keep going.  Or something like that.  I'm not thinking clearly, right now.

    ^_^

    WolfShade
    Legend
    April 5, 2011

    I don't think PHP has any kind of built-in functionality that compares with CFOUTPUT group="".  You have to build the loops manually.

    It's been YEARS since I've had to do something like that (Classic ASP).  Unfortunately, I do not have any sample code, for you.

    Inspiring
    April 5, 2011

    Since I've never worked with anything besides ColdFusion, I just assumed they all had something like <cfoutput group=...