[Pro] How to create Google-like moveable sections

Hi Walter

I was able to recreate the page of above

http://gerlesborg21.se/temp_index.php/site/draggable_div2 but it does not keep the order.
I created a field in member table called widget_order, uploaded a modified php page save_sort_order.php in the same directory as my Templates, but now I’m lost here.

Could you spare some time to show me the way?

Thanks…


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

You’ve got the sortable effect, but no callback to send the updated order back to the server. In order to do that, take another look at my example now (now that I’ve made it persist with the database).

Is this page being drawn with EE? Or is it just static HTML from Freeway?

You’re going to need to have the server (EE) create this page based on the desired order of elements.

Walter

On Nov 8, 2011, at 6:00 PM, atelier wrote:

Hi Walter

I was able to recreate the page of above

http://gerlesborg21.se/temp_index.php/site/draggable_div2 but it does not keep the order.
I created a field in member table called widget_order, uploaded a modified php page save_sort_order.php in the same directory as my Templates, but now I’m lost here.

Could you spare some time to show me the way?

Thanks…


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

index.php:

and here’s the matching save_sort_order.php:

Note how the page is drawn from the server whether the user is logged in or not. If they are logged in, then the order of the person’s “layout” field is used to grab all of the elements in order. If not, then we fall back to the default database order (by id).

The session is also used to communicate back and forth between the main page and the Ajax callback page that saves the order each time the page is sorted.

Walter

On Nov 8, 2011, at 9:18 PM, Walter Lee Davis wrote:

You’ve got the sortable effect, but no callback to send the updated order back to the server. In order to do that, take another look at my example now (now that I’ve made it persist with the database).

Is this page being drawn with EE? Or is it just static HTML from Freeway?

You’re going to need to have the server (EE) create this page based on the desired order of elements.

Walter

On Nov 8, 2011, at 6:00 PM, atelier wrote:

Hi Walter

I was able to recreate the page of above

http://gerlesborg21.se/temp_index.php/site/draggable_div2 but it does not keep the order.
I created a field in member table called widget_order, uploaded a modified php page save_sort_order.php in the same directory as my Templates, but now I’m lost here.

Could you spare some time to show me the way?

Thanks…


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I just saw this article which may prove to be useful as well;
http://www.yourinspirationweb.com/en/how-to-order-the-elements-with-jquery-22/
Regards,
Tim.

FreewayActions.com - Freeware and commercial actions for Freeway Express & Pro.

Protect your mailto links from being harvested by spambots with Anti Spam.
Only available at FreewayActions.com

http://www.freewayactions.com


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Hi Walter

Just for my understanding, I see in your index and in the save_sort_order.php file this code

require_once('lib/MyActiveRecord.php');

Question: is this another PHP file, and if yes, what is the content, and where does it live on the server?

What does lib mean, is it the serverpath?

Where does the file save_sort_order.php live, is it on root level? Or in the directory of the Freeway templates?

Also I see in the top part of the index template person and people, like here:

if($person = MyActiveRecord::FindFirst('people', 

and

$person = MyActiveRecord::Create('people');

For my understanding, do I have to replace people with Member like in my MSQL setup?

My problem is that I have to transform your script so it works for me. One small error in writing, and this does not work, and I have no idea where to look for debugging.

I am sorry about this, maybe it takes too long after all…


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

On Nov 11, 2011, at 4:57 AM, atelier wrote:

Hi Walter

Just for my understanding, I see in your index and in the save_sort_order.php file this code

require_once('lib/MyActiveRecord.php');

Question: is this another PHP file, and if yes, what is the content, and where does it live on the server?

Yes. This is the Object/Relational wrapper that I use. You can see a screencast about it on Freewaycast, and you can also read the source code here: https://github.com/walterdavis/activerecord

What does lib mean, is it the serverpath?

That’s just the name of the folder that contains the file, no special importance or significance to that filename.

Where does the file save_sort_order.php live, is it on root level? Or in the directory of the Freeway templates?

It lives wherever you put it and call to it. In my example, it is on the same file level as index.php. If you move it somewhere else, just remember to update the Ajax call to reflect its relative location from the page that’s calling it (index.php, again).

Also I see in the top part of the index template person and people, like here:

if($person = MyActiveRecord::FindFirst('people', 

and

$person = MyActiveRecord::Create('people');

For my understanding, do I have to replace people with Member like in my MSQL setup?

These are MyActiveRecord calls, so if you are translating, you would want to find the nearest equivalent to these, conceptually:

FindFirst finds the first ‘people’ record that matches the second argument when that’s used as the WHERE clause in a SQL query. This method call: MyActiveRecord::FindFirst('people', 'name = "' . $name . '"') would translate into this: SELECT * FROM people WHERE name = "$name";.

The result is returned as either null (no results found) or a single instance of the People class. In addition, MyActiveRecord also retrieves the schema from that table, and knows what each of the columns are named and what sort of data they like to receive. It then creates “getter” and “setter” functions, so all I have to do to change the resulting People object and call save(), and the database is back in step with my in-memory copy of that object.

Create first loads the schema, so it knows what all of the columns are called and what type of data they default to. Then it creates a new instance of the People class in memory, and if any of the setters are called, sets the value of that attribute to the value passed. When save() is called, the object is persisted in the database as a new row.

My problem is that I have to transform your script so it works for me. One small error in writing, and this does not work, and I have no idea where to look for debugging.

I am sorry about this, maybe it takes too long after all…

That depends on how abstracted your code is. If EE offers you a nice object-oriented view of the database, this could be very easy. The latest EE is based on CodeIgniter, which is a very high-quality attempt to clone Ruby on Rails into PHP. While PHP (the language) does not include all the magic that Ruby does, and therefore any clone of Rails that doesn’t run in Ruby will be inherently less capable than the original, CodeIgniter is a very complete toolkit and provides a very similar data model to MyActiveRecord. MAR is a deliberately limited port of only the ActiveRecord library from Rails, and it does not even try to go as far as CodeIgniter does toward feature parity with Rails.

Walter


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Hi Walter

I am afraid I do not follow you anymore, so I decided to do the trial and error way.

My first attempt resulted in a PHP error which I cannot relate to anything I can solve.
EE generated this:

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  require_once(lib/MyActiveRecord.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory</p>
<p>Filename: libraries/Functions.php(656) : eval()'d code</p>
<p>Line Number: 12</p>

</div><br />
<b>Fatal error</b>:  require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'lib/MyActiveRecord.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in <b>/home/gerlesbo/12reg/expressionengine/libraries/Functions.php(656) : eval()'d code</b> on line <b>12</b><br />

freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Please don’t try to mix EE and MyActiveRecord. They are not going to work well within one another. Each one has an independent way of looking at the database and mapping that into an object you can use in your application.

If you have a way to extract a member from the database, and to write new values back into the database when you’re done modifying that member, then that’s where you should focus your efforts.

Walter

On Nov 11, 2011, at 9:43 AM, atelier wrote:

Hi Walter

I am afraid I do not follow you anymore, so I decided to do the trial and error way.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I am sorry, I was not aware of the fact that there are two different systems in that file. Let alone that I knew I was mixing them.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Your last post said that you loaded the MyActiveRecord code into EE, and your error message certainly did not look like a bare-metal PHP error.

If you want to try out the example I posted, use the sql file to create and populate the tables in the test database, edit the username and password in the config file, and have at it! Do this in a new folder on your server.

http://scripty.walterdavisstudio.com/sortable/sortable.zip

http://scripty.walterdavisstudio.com/sortable/test.sql

This should just be a drop-in Just Works™ thing on nearly any PHP server of even remotely recent vintage.

Walter

On Nov 11, 2011, at 11:01 AM, atelier wrote:

I am sorry, I was not aware of the fact that there are two different systems in that file. Let alone that I knew I was mixing them.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

All of my templates must run completely through EE, that is how this system works. I cannot call pages, html or php from outside EE.

I made in my member_fields table one extra field, widget_order. I can make more, if I have to.

I can populate that field on a logged-in member basis with information. But I do not know how to implement that into sortable-widgets that keep the sorting order for the next time the logged-in member visits this page.

This is because I cannot produce or read PHP code.
I am sorry, Walter.



-- phpMyAdmin SQL Dump
-- version 3.4.5
-- http://www.phpmyadmin.net
--
-- Värd: localhost
-- Skapad: 11 nov 2011 kl 19:12
-- Serverversion: 5.0.92
-- PHP-version: 5.2.6

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Databas: `mydatabase`
--

-- --------------------------------------------------------

--
-- Tabellstruktur `exp_member_fields`
--

CREATE TABLE IF NOT EXISTS `exp_member_fields` (
  `m_field_id` int(4) unsigned NOT NULL auto_increment,
  `m_field_name` varchar(32) NOT NULL,
  `m_field_label` varchar(50) NOT NULL,
  `m_field_description` text NOT NULL,
  `m_field_type` varchar(12) NOT NULL default 'text',
  `m_field_list_items` text NOT NULL,
  `m_field_ta_rows` tinyint(2) default '8',
  `m_field_maxl` smallint(3) NOT NULL,
  `m_field_width` varchar(6) NOT NULL,
  `m_field_search` char(1) NOT NULL default 'y',
  `m_field_required` char(1) NOT NULL default 'n',
  `m_field_public` char(1) NOT NULL default 'y',
  `m_field_reg` char(1) NOT NULL default 'n',
  `m_field_cp_reg` char(1) NOT NULL default 'n',
  `m_field_fmt` char(5) NOT NULL default 'none',
  `m_field_order` int(3) unsigned NOT NULL,
  PRIMARY KEY  (`m_field_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

--
-- Dumpning av Data i tabell `exp_member_fields`
--

INSERT INTO `exp_member_fields` (`m_field_id`, `m_field_name`, `m_field_label`, `m_field_description`, `m_field_type`, `m_field_list_items`, `m_field_ta_rows`, `m_field_maxl`, `m_field_width`, `m_field_search`, `m_field_required`, `m_field_public`, `m_field_reg`, `m_field_cp_reg`, `m_field_fmt`, `m_field_order`) VALUES
(1, 'widget_order', 'widget_order', 'widget_order', 'text', '', 10, 100, '100%', 'y', 'n', 'n', 'n', 'y', 'none', 1);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

OK, one last attempt.

I read in the manual of EE something about inserting data in the database, and calling them when needed.
I guess this is what the top part of your script does, Walther. If so, I could use this in my index template since it must be processed by EE, am I right?

http://expressionengine.com/user_guide/development/usage/database.html

This is still very abstract for me, but I have the feeling that I should look for the solution here.

I appreciate your answer, thanks.

Calling the DB Class

ExpressionEngine has an abstract database layer that allows developers to easily access the MySQL database and also provide many features like automatic escaping of characters and query caching.


Performing a Query

$this->EE->db->query() sends a query to the database and will also return back the results, if it is a SELECT query. If you are doing an INSERT or UPDATE, then you do not need to set a variable since there are no results being returned.

// Simple select query
$query = $this->EE->db->query("SELECT * FROM exp_channels");

// Update, with no variable being set
$this->EE->db->query("UPDATE exp_channels SET channel_name = 'dog' WHERE channel_name = 'cat'");
*Note: When doing any sort of query using user submitted data make sure to use the $this->EE->db->escape_str() function (details below) to prevent any problems between MySQL and the data.*

Retrieving Results from SELECT query

Upon performing a SELECT query an object containing the results of query will be returned by the function. To check for the number of results returned by the query, use the num_rows value in the returned object.

$results = $this->EE->db->query("SELECT * FROM exp_channels");

if ($results->num_rows() == 0)
{
    exit('No channels exist');
}
In many instances, you will know that only a single row of data will be returned from a query. Instead of looping through an array, you can simply use the row array in the object returned.

$results = $this->EE->db->query("SELECT * FROM exp_channels ORDER BY channel_id LIMIT 0,1");

$first_channel = $results->row('channel_name');

freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

This does sound like the right way to go in your case. If you look at the data that I am setting and getting (try to ignore how I am doing that precisely) you will see that it’s a very small tidbit of data. Anything that looks like this:

$person->layout = implode(',', $_POST['sort_me']);

interpret like this: Anything to the left of the equals sign is the setter function, and everything to the right is the value you are assigning. Therefore, if you are substituting the EE code for mine, you will need to write a SQL UPDATE statement and issue it according to the example you posted. It’s going to be something like

'UPDATE table_name SET layout = "' . implode(',', $_POST['sort_me']) . '"

Likewise for the reading of the value. Recall in my example where I built an array of widgets by getting them out of the database in the same order as the layout string:

if (!empty($person->layout)){
	$widgets = array();
	foreach(explode(',',$person->layout) as $id)
		$widgets[] = MyActiveRecord::FindById('widgets',$id);
}

In this case, substitute whatever variable you’ve assigned in EE for $person->layout and then find all of your widgets like this:

'SELECT * FROM widgets WHERE id = ' . $id

and assign that to the $widgets array just like in my example.

Walter

On Nov 13, 2011, at 1:12 PM, atelier wrote:

OK, one last attempt.

I read in the manual of EE something about inserting data in the database, and calling them when needed.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Fantastic, Walter. I am aware of the fact that this could be tricky, just because accessing the DB directly from a Template has a potential securety risk. I shall only be trying this from a password protected Template. And I’ll make backups before I start testing. I presume this takes some time, but I do hope to come back here with results. Thanks again for the support.


freewaytalk mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options