0

I have an open ended question for PHP View-Controller-Class model.

Say, we have to perform logic loop in controller to use method in class to retrieve data row from database.

However, at anyone time, I can set 1 PHP variable in view to display the content. I need help to understand how to display data in nicely tabulated HTML format from controller.

I know one way is to use Javascript

$(#div_id).html(variable_embedded_html_coding);

All advise would be very much appreciated.

3
  • if you want a table use <table></table> in your view i think you are thinking about grid control??? Commented May 9, 2013 at 11:58
  • I don't understand why you can set only 1 variable? Controller assigns variables to views so it can be more that one. Do you use your own framework? Also check this link phpgrid.com You can pass many variables with JSON or XML Commented May 9, 2013 at 11:58
  • That's just an example. It suppose to output values via Ajax. Commented May 10, 2013 at 0:28

2 Answers 2

1

To have nicely looked table you can use phpgrid.com

Php grid

To send message from php to js having more than 1 variable. You can use for example JSON or xml. To encode array in php to json format use json_encode(); In Ajax you should change your datatype to JSON.

so from JS side you should do something like this

var request = $.ajax({
 url: "script.php",
 type: "POST",
 data: {id : menuId}, //it'll be in $_POST['id']
 dataType: "json"
}).done(function(msg){
  $("#your_div").html(msg.html);
  //msg.othervariable is also possible here
});

from php(script.php)

$arr = array('html'=> "<p>asdasd</p>", othervariable=>"it works");
echo json_encode($arr);exit;
Sign up to request clarification or add additional context in comments.

Comments

0

If you know the structure of your variable (probably an array) you can create a helper function which you can call from your view layer. The function will take your original variable and output it as a HTML table.

e.g. something like:

tabHelper.php

function print_tab($var)
{
    $tab = '<table>'."\n";
    foreach ($var as $row) {
        $tab .= '<tr><td>'.$row.'</td></tr>'."\n";
    }

    $tab .= '</table>';
    echo $tab;
}

and then in your view you would call:

<?php include_once ('tabHelper.php'); ?>

...

<?php print_tab($my_variable); ?>

You could even extend the helper to be able to pass additional attributes, such as class, id, etc.

2 Comments

is tabHelper.php a standalone file? Can I put that print_tab function in my controller? How can I use it with my controller file?
That is just an example code. You can put the print_tab function anywhere you want in fact ;) If you want to stick to MVC though you should keep the controller logic and view logic separated, so you shouldn't use the printing function inside or together with your controller. Inside the controller you can prepare the array with data and in the view print it out to the browser.

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.