Solved

YUI, PHP, MySQL inline editing data

Posted on 2007-03-28
10
7,517 Views
Last Modified: 2013-12-13
Ok, I'm feeling extremely dense here.  
I'm using YUI.  I downloaded the library and examples.
Their example for inline editing  is pulling it's data from a flat file.  I spent about 4 hours reading and trying things but I can't figure out how the query to get the full table data and then post back single fields works.
I think if I could see a code sample using YUI and php and a table in MySQL I could go from there.  I think I'm understanding the principle but I haven't seen the actually queries and how they are called.

for instance how could I duplicate http://developer.yahoo.com/yui/examples/datatable/inlineediting.html

assuming the data came from "tablea"
with coloms:sku,quantity,items,description

assuming the query to get the table data is
"SELECT sku,quantity,item,description FROM tablea WHERE blah=blah"

and the update db code snip is

$fieldname = $_GET(THEFIELDBEINGUPDATED)
$content = $_GET(THEDATATOUPDATE)
$sku = $GET_(THEPRIMARYKEYROWID)
UPDATE tablea SET $fieldname = $content WHERE sku = $sku

And I'm only guessing at it being a $_GET.  Is that correct?  



thanks
0
Comment
Question by:livegirllove
  • 6
  • 4
10 Comments
 
LVL 5

Expert Comment

by:Gitcho
ID: 18817696
hey livegirllove : understanding yui isn't all that easy - don't feel dense ...  from what I gather, you want to use PHP/MySQL as your data source, correct ?  If so, you need to define your source this way (from : http://developer.yahoo.com/yui/datatable/) :

--------------------------------------------------------------------
// Point to a local or proxy URL
var myDataSource = new YAHOO.util.DataSource("http://url/datafeed.php");

// Set the responseType as JSON
myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;

// Define the data schema
myDataSource.responseSchema = {
    resultsList: "SalesDatabas.items", // Dot notation to results array
    fields: ["Company","Title","Name","Phone","Email"] // Field names
};
--------------------------------------------------------------------

Your http://url/datafeed.php needs to output xml like so :

--------------------------------------------------------------------
<SalesDatabase totalItems="40" itemsFound="2">
    <Item>
        <Company>A1 Services</Company>
        <Title>CEO</Title>
        <Name>Jane Jones</Name>
        <Phone>800-555-2121</Phone>
        <Email>janejones@a1services.com</Email>
    </Item>

    <Item>
        <Company>Acme</Company>
        <Title>President</Title>
        <Name>John Smith</Name>
        <Phone>800-555-1212</Phone>
        <Email>johnsmith@acme.com</Email>
    </Item>
</SalesDatabase>
--------------------------------------------------------------------

If you'd rather send/receive data in plain text, rather than XML, the you just need to define your data source differenly, and output differently. The code examples are also on that page ...
0
 
LVL 5

Expert Comment

by:Gitcho
ID: 18817807
The above code will only pull the data via XHR.  In the YUI inline editor example, it does not update a database each time a cell is modified - it simply outputs the change to the log window.

If you are also looking to update a DB each time a cell value changes, that's a different question.  The code above will help you duplicate the example (http://developer.yahoo.com/yui/examples/datatable/inlineediting.html) as per your request.  To do what I think you're tryin to do, you'll need to [1] add code for an ajax request (to your PHP page) in the onCellEdit event, and [2] PHP code to receive the reqeust, update the DB and return the result.  Those should probably be asked in seperate questions.   Am i on the right track with what you're looking to do ?
0
 
LVL 1

Author Comment

by:livegirllove
ID: 18818763
"If you are also looking to update a DB each time a cell value changes, that's a different question."
What would be different about the question?  How can I re-ask it differently?  My goal as stated in the question is to retrieve data from a database and then change the data (in the database) inline.  I'm not sure what good doing an inline edit that doesnt update on the backend would be?

so http://url/datafeed.php should look something like this:

<? $sqlvendor44 = "SELECT sku,quantity,item,description FROM tablea WHERE blah=blah";
     $res_cat44 = mysql_query($sqlvendor44); // handle to category set      ?>
<?php while ($row44 = mysql_fetch_assoc($res_cat44)) {
$count+;

?>
        echo "
<SalesDatabase totalItems=\"$rowsindatabase\" itemsFound=\"$count\">
<Item>
        <sku>$row44['sku']</sku>
        <quantity>$row44['quantity']</quantity>
        <item>$row44['item']</item>
        <description >$row44['description ']</description >
 </Item>
"
?>

so after I have then data where do I send it?
Or are you saying I should open 2 questions.  1 being how do I get data into a YUI table
and another one of how to post it back to the DB?

thanks for your help.  Im a little suprised that in all my searchs I havent found a simple example/tutorial that shows A-Z how to do this.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 5

Accepted Solution

by:
Gitcho earned 500 total points
ID: 18819238
hey livegirllove : sorry if i wasn't clear on that one ... what I meant to say, is that you should probably split this question and post another question in the PHP zone on how to receive the XHR request, grab the DB info, and return the results. The reason you don't see many examples on this is likely because it's far easier to use standard editing, and anyone who has gone to the work of putting a full ajax inline editor together doesn't want to give away his/her code for free =)

As far as getting this question answered, you're not that far off with the code you have in yui.  In the yui example, when a change is made to the cell, the onCellEdit function is called.  You need to add code to that function to send the change to your PHP page, and wait for the response.  The yahoo connection manager (http://developer.yahoo.com/yui/connection/) is what you need.  It is yui's framework for ajax requests.

There's a connection manager video tutorial on dustindiaz.com that was super helpful to me. I think he's got 5 video tutorials (screecasts) on YUI stuff.  http://www.dustindiaz.com/screencast-episode-02/

The other screencasts are here :
http://www.google.ca/search?num=100&hl=en&as_qdr=all&q=yui+screencast+site%3Adustindiaz.com&btnG=Search
0
 
LVL 1

Author Comment

by:livegirllove
ID: 18820637
thanks for the help.  ill check those links and see what I see.
0
 
LVL 1

Author Comment

by:livegirllove
ID: 18820649
because it's far easier to use standard editing

I assume you mean.  click edit, open page with editable fields.  click submit and reload the whole page with the new data?  Then well yes it's easy.... but I want to AJAX it because PHP by itself seems very Web 1.0.  The idea of AJAX and inline editing is about the most user friendly thing I've seen and I can't wait to get one example that works so I can build on it.  I'm the type that learns by seeing a working model and butchering it to make it do what I want. ;-)
0
 
LVL 5

Expert Comment

by:Gitcho
ID: 18825197
heh heh ... that's how I learn most of my stuff too =)

 PHP doesn't really have anything to do with Web 1.0 or 2.0 ... its the client side code that defines the "web 2.0" experience, with AJAX being the most commonly used framework in "web 2.0" ...

The YUI data table + YUI connection manager will allow you do do inline editing with AJAX updating on a PHP backend.

This might be an example you're looking for, but its not based on the yui framework  : http://www.yvoschaap.com/index.php/weblog/ajax_inline_instant_update_text_20/
0
 
LVL 1

Author Comment

by:livegirllove
ID: 18826138
i tried that.  It is for single items.  ie it doesnt do so well with a table of data.  it would work if it was just one line item.  Ill poke around some more.  thatnks for the tips.
0
 
LVL 1

Author Comment

by:livegirllove
ID: 18827176
hmm.  actually i think i can get it to work in a table.  however ill definately be lost when trying to use a select list..  Im going to award points and then open  new questions one at a time...  
0
 
LVL 1

Author Comment

by:livegirllove
ID: 18827836
Ah Ha.  I got it.  This doesnt use YUI...
 
Ok so using the js from the page below:
http://www.yvoschaap.com/index.php/weblog/ajax_inline_instant_update_text_20/

assuming you have a table called tblInLineEdit with fields id, name,descr

**ajaxtest.php**
<?
$db_user = "";
$db_pass = ""; // password here
$db = "";
$host = "";
$link = mysql_connect($host,$db_user,$db_pass) or die("Database connection broken");
mysql_select_db($db,$link) or die("Database connection unavailable - ".mysql_error() );
$result = mysql_query("SELECT * FROM tblInLineEdit");
//$row = mysql_fetch_assoc($result);
?>
<script type="text/javascript" src="instantedit.js"></script>
<table>
<? while ($row = mysql_fetch_assoc($result)) { ?>
<tr>
<td><span id="name-|||-<?php echo $row['id']; ?>" class="editText"><? echo $row['name']; ?></span></td>
<td><span id="descr-|||-<?php echo $row['id']; ?>" class="editText"><? echo $row['descr']; ?></span></td>
</tr>
<?php } ?>
</table>



**update.php**
<?
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0

$db_user = "";
$db_pass = "";
$db = "";
$host = "";
$link = mysql_connect($host,$db_user,$db_pass) or die("Database connection broken");
mysql_select_db($db,$link) or die("Database connection unavailable - ".mysql_error() );
$content = $_GET['content'];
list($fieldname, $id) = explode("-|||-",$_GET['fieldname']);
mysql_query("UPDATE tblInLineEdit SET $fieldname = '$content' WHERE id = $id ") or die("blah failure - ".mysql_error() );
$result = mysql_query("SELECT * FROM tblInLineEdit WHERE id = $id");
$row = mysql_fetch_assoc($result);
echo $row["{$fieldname}"];
?>


0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question