Jump to:

327 Posts in 212 Topics by 159 members

Migrating a Site to Silverstripe

SilverStripe Forums » Migrating a Site to Silverstripe » integrate external PHP code

What you need to know when migrating your existing site to SilverStripe.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 5884 Views
  • Megabyte
    Avatar
    Community Member
    13 Posts

    integrate external PHP code Link to this post

    Hello,
    I am migrating my website to Silverstripe. But I have a question: On my old page I used a special registration form that generates productkeys. I want to integrate this form in my new page with silverstripe.

    How can I integrate the external PHP-code in the CMS??

  • UncleCheese
    Avatar
    4085 Posts

    Re: integrate external PHP code Link to this post

    Of course you can do that. That's what's so great about Silverstripe -- you're never stuck with just the tools they give you.

    I'd start with the recipe on custom forms here:
    http://doc.silverstripe.com/doku.php?id=recipes:forms&s=recipes%20forms

    If you can't get it working, post your code, and I'd be happy to help you out.

  • Megabyte
    Avatar
    Community Member
    13 Posts

    Re: integrate external PHP code Link to this post

    Is there any other possibility to integrate PHP code? For example a wrapper or something like this. I am looking for an opportunity, when I use the code does not adapt.

    Thank you for helping!

  • UncleCheese
    Avatar
    4085 Posts

    Re: integrate external PHP code Link to this post

    There's no reason something like that shouldn't work if you follow the tutorial. If you want, post what you're working on and one of us can help you out.

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: integrate external PHP code Link to this post

    I tried to ask the similar question on another SilverStripe forum

    http://www.silverstripe.org/form-questions/show/256267#post256267

    While tutorial describes form with action to write into DB, I am more interested in reading from DB

    Victor

  • UncleCheese
    Avatar
    4085 Posts

    Re: integrate external PHP code Link to this post

    You don't need custom PHP to do this. Use the wonderful MVC framework that Silverstripe provides.

    Here's your code:

    // database info

    $query ="SELECT * FROM StaffPage WHERE `lastname` LIKE '$_POST[last_name]%'";

    $result = mysql_query($query) or die("Could not execute query");
    if (mysql_num_rows($result)==0 ) {print("Not found "); return;
    }

    while($row = mysql_fetch_array($result)) {
    echo $row[lastname].", ".$row[lfirstname];
    }
    ?>

    Now I want to put on some page form which is

    <form action='./dosearch.php'>
    <input type='text' name='last_name'>
    <input type='submit'>
    </form>

    Here's how to do it in Silverstripe:

    function StaffSearchForm()
    {
    return new Form(
    $this,
    "StaffSearchForm",
    new FieldSet(new TextField('StaffSearch','')),
    new FieldSet(new FormAction('doStaffSearch','Search'))
    );
    }

    function doStaffSearch($data, $form)
    {
    $results = DataObject::get("StaffPage","lastname LIKE '%".$data['StaffSearch']."%'");
    return $this->customise(array('Results' => $results))->renderWith(array('StaffSearch_results','Page'));
    }


    StaffPage.ss

    $StaffSearchForm

    StaffSearch_results.ss

    $StaffSearchForm
    <% if Results %>
    <ul>
    <% control Results %>
    <li><a href="$Link">$firstname $lastname</a></li>
    <% end_control %>
    </ul>
    <% else %>
    There were no results that matched your query.
    <% end_if %>

    I would also look at the recipe on forms if I were you. Also, the LIKE operator is not the best way to do search in MySQL. It's case sensitive and far too exact to return reliable results. I would use the MATCH and AGAINST functions and order by relevance.

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: integrate external PHP code Link to this post

    Thanks! But as no good deed goes unpunished I will need to ask few more questions:

    We are trying to build a Department site and People are divided into Faculty, Staff, Students. Currently we are in very early stage and decided (but decision could be changed) to have separate FacultyPage, StaffPage, StudentPage with the same fields, because we want to have the neither the same "fill forms" nor outputs

    Right now I am trying to search just Staff by lastname. But the general idea was to search all, so external (working) php script has UNION ALL in querry, also we matched against

    `lastname` LIKE '$_POST[lastname]%' AND `firstname` LIKE '$_POST[firstname]%'

    (we want lastname starting from ... and firstname starting from ... (those may be empty); here lastname and firstname are fields name in DB

    [quota]
    I would also look at the recipe on forms if I were you. Also, the LIKE operator is not the best way to do search in MySQL. It's case sensitive and far too exact to return reliable results. I would use the MATCH and AGAINST functions and order by relevance.
    [/quota]

    No, we really want to order by lastname and we are looking for 'lastname' starts from not contains the string. I am not sure that you are right about case sensitivity: always worked as not case sensitive. Further

    http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html says

    "The following two statements illustrate that string comparisons are not case sensitive unless one of the operands is a binary string:"

    mysql> SELECT 'abc' LIKE 'ABC';
    mysql> SELECT 'abc' LIKE BINARY 'ABC';

    Thanks in advance. Victor

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: integrate external PHP code Link to this post

    I ran into little problem:

    If search ends in no results it does not display "Your search ..." message but a complete blank

    PS Search form is on StaffHolder while people are StaffPage (as in tutorial-2 but much more fields to fill)

    Victor

    5884 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.