0

I have run into a tough one here, I hope someone would help out. I want the public user to define the order of the display of information on the webpage. The user will know the data fields in the mysql database and make a custom output of the fields in the order he or she wants. Here is my php generating xml:

<?php 
    header("Content-type: text/xml"); 
    include_once("config.php");

    parse_str($_POST['data'], $order);
    $order = array(firstname,surname,title,booktitle);

    $neworder = explode("&", $order);
    echo $neworder[1];      

    $query = "SELECT `author`.`surname`,`author`.`firstname`,`publication`.`title`,`book`.`book_title` FROM author, book, publication "; 
    $resultID = mysql_query($query, $conn) or die("Data not found."); 

     $xml_output = "<?xml version=\"1.0\"?>\n"; 
    $xml_output .= "<entries>\n"; 

    for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 
        $row = mysql_fetch_assoc($resultID); 
        $xml_output .= "\t<entry>\n"; 
        foreach($order as $col){
            $xml_output = "\t\t<$col>" . $row[$col] . "</$col\n"; 
        }    
        $xml_output .= "\t</entry>\n"; 
    } 
    $xml_output .= "</entries>"; 
    echo $xml_output;  
?> 

And the xml output

<entries>
<entry>
<firstname>Kimtai</firstname>
<surname>Evans</surname>
<title>
Operational Decision Support: Context-Based Approach and Technological Framework
</title>
<book_title>Operational Support Decision</book_title>
</entry>
</entries>

What i need is this, the user can change the order of output from a html form by putting the order of the fields he wants, so that the final output on the webpage can start with surname and not firstname for example. I hope i have explained my question well. Any help will be appreciated.

jquery ajax code

<script>
  $(function() {
        $( "#sortable" ).sortable({
        placeholder: "ui-state-highlight",
        opacity: 0.6,
       update: function(event, ui) {
        var info = $(this).sortable("serialize");
        alert(info);
        $.ajax({
            type: "POST",
            url: "home.php",
            data: info,
            context: document.body,
            success: function(){
            }
      });
    }
    });
    $( "#sortable" ).disableSelection();
  });

1
  • You could use htmlspecialchars() instead of the series of str_replaces Commented May 30, 2013 at 14:19

1 Answer 1

2

Have the user order the fields using something like jQuery UI Sortable. Add those fields to an array in the same order that the user chose. Example:

$order = array('firstname', 'title', 'etc');

Then, when you're printing out the XML, do something similar to:

<?php

//Example array
$order = array('firstname', 'title', 'etc');

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 
    $row = mysql_fetch_assoc($resultID); 
    $xml_output .= "\t<entry>\n"; 
    foreach($order as $col){
        $xml_output = "\t\t<$col>" . $row[$col] . "</$col\n"; 
    }    
    $xml_output .= "\t</entry>\n"; 
} 

Caveat: Column names will have to have the same names as the XML elements that you're creating. If that's not do-able, you'll need to create some sort of lookup array. I suggest doing this anyway, simply because you'll want to whitelist what fields that user can choose.

From the example you've given in the comments:

<?php
$string = 'post[]=firstname&post[]=title&post[]=surname&post[]=booktitle';
$order = array();

$stringExploded = explode("&", $string);
foreach($stringExploded as $val){
    $keyVal = explode("=", $val);
    array_push($order, $keyVal[1]);
}

var_dump($order);
Sign up to request clarification or add additional context in comments.

4 Comments

i used the jquery sortable and iam able to serialize the data and oarse it using ajax, but am stuck here, parse_str($_POST['data'], $order); echo $order; How do i use this data from post and make an array that ill use to make the output
You should separate the order via a common delimiter such as "," and then use the function php.net/manual/en/function.explode.php
still not working, please check i have updated the php xml code, the jquery ui portable outputs this when i alert post[]=firstname&post[]=title&post[]=surname&post[]=booktitle, this is what ajax sends, i also updated the jquery ajax code above.
Now if i echo the $order array i get the elements in it, but i the order will always be different, according to what the user determines in the jquery ui sortable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.