SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Online Dating Guru rawnet's Avatar
    Join Date
    Jan 2002
    Location
    Windsor, UK
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Converting Cold Fusion code to PHP - I'm a CF Muppet - can u help?

    Hi,

    I'm a Cold Fusion developer. Please be gentle.

    Basically, I'm moving my site over to vbPortal/vBulletin and hence am integrating the vBulletin database (started another thread about that in a different forum).

    The main problem is my inaptitude at PHP - I'm rather terrible, and can't learn it properly before moving my site to PHP!! Which is a scary situation to be in. I was wondering if anybody could help me translate this Cold Fusion code into PHP? Basically, it queries the database based on contents of submitted form fields and outputs the results :

    PHP Code:
    <cfif IsDefined("serviceid")>
                    
    <
    cfif serviceid IS 0>
    <
    p><b>Error</b> - you have not selected a Military ServicePlease use the back button on your browser to return to the <a href="index.cfm">form</a>.</p>

    <
    cfelseif firstname IS "" AND surname IS "" AND Bases_Served IS "" AND Branch_Trades IS "" AND Squadrons_Served IS "" >
    <
    p><b>Error</b> - you have not defined any search termsYou must select a service and at least one search termPlease use the back button on your browser to return to the <a href="index.cfm">form</a>.</p>

    <
    cfelse>

    <
    cfparam name="form.firstname" default="">
    <
    cfparam name="form.surname" default="">
    <
    cfparam name="form.Bases_Served" default="">
    <
    cfparam name="form.Branch_Trades" default="">
    <
    cfparam name="form.Squadrons_Served" default="">


        <
    CFQUERY NAME="qGetResults" DATASOURCE="#application.dsn#" blockfactor="100">
        
    SELECT ContactIDServiceFirstnameNicknameMiddlenameSurnameOther_SurnameBases_ServedBranch_TradesSquadrons_Served
        FROM Pals
        WHERE Service 
    #form.serviceid#
        
    <cfif firstname NEQ "">
        AND (
    Firstname '#form.firstname#') OR (Nickname '#form.firstname#') OR (Middlename '#form.firstname#')
        </
    cfif>
        <
    cfif surname NEQ "">
        AND (
    Surname '#form.surname#') OR (Other_Surname '#form.surname#')
        </
    cfif>
        <
    cfif Bases_Served NEQ "">
        AND 
    Bases_Served LIKE '%#form.Bases_Served#%'
        
    </cfif>
        <
    cfif Branch_Trades NEQ "">
        AND 
    Branch_Trades LIKE '%#form.Branch_Trades#%'
        
    </cfif>
        <
    cfif Squadrons_Served NEQ "">
        AND 
    Squadrons_Served LIKE '%#form.Squadrons_Served#%'
        
    </cfif>
        </
    CFQUERY>
        
        <
    cfif #qGetResults.RecordCount#>
        
        
    <p><b>Here are the results of your search :</b></p>
        
        <
    cfoutput>
        <
    p>There were #qGetResults.recordcount# results found which matched your search criteria :</p>
        
    </cfoutput>
        
        <
    p>Click on a name for more information</p>
        
        <
    p>
        <
    cfoutput query="qGetResults">
        <
    a href="detail.cfm?id=#contactID#"><img src="../images/small_arrow.gif" width="5" height="8" border="0" alt="">#firstname# #surname#</a><br>
        
    </cfoutput>
        </
    p>
        
        <
    cfelse>
        <
    p><b>No records found.</b></p>
        
        <
    p>Sorrythere are no records matching your search criteriaYou could try <a href="index.cfm">searching again</ausing a modified search criteria. Try lowering the number of variables that you search under. For examplejust use a first or last name.</p>
        
        </
    cfif>

    </
    cfif>

    <
    cfelse>

    <
    p><b>Error</b> - please <a href="index.cfm">fill out the search form</aproperly to find old friends and colleagues.</p>

    </
    cfif

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here ya go, just change the params in the database setup. This script assumes you are using a MySQL database. If not then you'll need to adjust the db specific functions. It also assumes that you used a POST method in your form. If you used a GET, please change all instances of HTTP_POST_VARS to HTTP_GET_VARS

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
        <title>Untitled</title>
    </head>

    <body>
    <?php
    if(isset($HTTP_POST_VARS['serviceid'])) {
        if(
    $HTTP_POST_VARS['serviceid'] == 0) {
    ?>                
            <p><b>Error</b> - you have not selected a Military Service. 
            Please use the back button on your browser to return to the 
            <a href="index.cfm">form</a>.</p>
    <?php
            
    }
        elseif(
    $HTTP_POST_VARS['firstname'] == '' && 
               
    $HTTP_POST_VARS['surname'] == '' && 
               
    $HTTP_POST_VARS['Bases_Served'] == '' && 
               
    $HTTP_POST_VARS['Branch_Trades'] == '' && 
               
    $HTTP_POST_VARS['Squadrons_Served'] == '') {
    ?>
            <p><b>Error</b> - you have not defined any search terms. 
            You must select a service and at least one search term. 
            Please use the back button on your browser to return to the 
            <a href="index.cfm">form</a>.</p>
    <?php
            
    }
        else {
            
    //Setup db connection
            
    $db mysql_connect('hostname''user''password') or DIE(mysql_error());

            
    //Select database
            
    mysql_select_db('dbname') or DIE(mysql_error());

            
    $serviceid $HTTP_POST_VARS['serviceid'];
            
    $firstname $HTTP_POST_VARS['firstname'];
            
    $surname $HTTP_POST_VARS['surname'];
            
    $Bases_Served $HTTP_POST_VARS['Bases_Served'];
            
    $Branch_Trades $HTTP_POST_VARS['Branch_Trades'];
            
    $Squadrons_Served $HTTP_POST_VARS['Squadrons_Served'];
        
            
    $q "SELECT ContactID, 
                         Service, 
                         Firstname, 
                         Nickname, 
                         Middlename, 
                         Surname, 
                         Other_Surname, 
                         Bases_Served, 
                         Branch_Trades, 
                         Squadrons_Served
            FROM Pals
            WHERE Service = '
    $serviceid' ";
            
            if(
    $firstname != '') {
                
    $q .= "AND (Firstname = '$firstname') OR 
                           (Nickname = '
    $firstname') OR 
                           (Middlename = '
    $firstname') ";
                }
            if(
    $surname != '') {
                
    $q .= "AND (Surname = '$surname') OR 
                           (Other_Surname = '
    $surname') ";
            
                }
            if(
    $Bases_Served != '') {
                
    $q .= "AND Bases_Served LIKE '%$Bases_Served%' ";
            
                }
            if(
    $Branch_Trades != '') {
                
    $q .= "AND Branch_Trades LIKE '%$Branch_Trades%' ";
            
                }
            if(
    $Squadrons_Served != '') {
                
    $q .= "AND Squadrons_Served LIKE '%$Squadrons_Served%' ";
            
                }
            
    $result mysql_query($q) or DIE(mysql_error());
            
            
    //There were results
            
    if(mysql_num_rows($result)) {
        
    ?>
                <p><b>Here are the results of your search :</b></p>
                
                <p>There were <?php echo mysql_num_rows($result); ?> results 
                found which matched your search criteria :</p>
        
                <p>Click on a name for more information</p>
            
                <p>
        <?php
                
    while($row mysql_fetch_array($result)) {
        
    ?>
                <a href="detail.cfm?id=<?php print $row['ContactID']; ?>">
                <img src="../images/small_arrow.gif" width="5" height="8" border="0" alt="">
                <?php print $row['firstname'].' '.$row['surname'] ; ?></a><br>
        <?php
                    
    }
        
    ?>

                </p>
            <?php
                
    }
            
    //There were no results
            
    else {
            
    ?>
                <p><b>No records found.</b></p>
                
                <p>Sorry, there are no records matching your search criteria. 
                You could try <a href="index.cfm">searching again</a> using a 
                modified search criteria. Try lowering the number of variables 
                that you search under. For example, just use a first or last name.</p>
        
        <?php
                
    }
            }
        }
    else {
    ?>
    <p><b>Error</b> - please <a href="index.cfm">fill out the search form</a> 
    properly to find old friends and colleagues.</p>
    <?php
        
    }
    ?>
    </body>
    </html>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    Your daddy. WALoeIII's Avatar
    Join Date
    Apr 2001
    Location
    USA
    Posts
    526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doh! I wanted to do it, but freddie beat me

    LOL, you're in good hands with freddie. (Sounds like All State Commercial)

  4. #4
    Online Dating Guru rawnet's Avatar
    Join Date
    Jan 2002
    Location
    Windsor, UK
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    WOW!!

    You guys are absoloutely fantastic!!

    That is incredible!! Thankyou!!

    Will get to it now - am using a MySQL database now - am a recent convert to MySQL/PHP after my experiences with vBulletin etc - just wish I'd learnt this 6 months (and 3000) earlier!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •