Skip to main content
added 1009 characters in body
Source Link
Josh K
  • 23k
  • 10
  • 68
  • 100

Sure.

You are using json_encode right? So start with the same data structure. For simplicity here I will use an array.

$stories = array( '[id]' => [details] );

Now you have a quick break. If the request is an AJAX request, then simply echo json_encode($stories);. Otherwise standard requests will process the view with that same data structure.

I'm going to expand on this a bit. Assuming you aren't using some MVC paradigm and doing "straight" PHP work.

For generating the page you are going to want to encapsulate the logic for this table in a separate PHP file called with an include(). Let's call this file storyList.php and it's in the same web-accessible folder as everything else.

// File: storyList.php
// First connect to the database and pull the story list
// based on any parameters passed.
$story = new StoryList($_REQUEST);
$list = $story->fetch_list();

// Now do a quick conditional based on the headers.
// See http://php.net/manual/en/function.get-headers.php and
// define your own function for testing if a request is made
// with AJAX or not. Several libraries include their own.
if (is_xhr())
{
    echo json_encode($list);
}
else
{
    foreach ($list as $title => $story)
    {
        // Format as a table
    }
}

Sure.

You are using json_encode right? So start with the same data structure. For simplicity here I will use an array.

$stories = array( '[id]' => [details] );

Now you have a quick break. If the request is an AJAX request, then simply echo json_encode($stories);. Otherwise standard requests will process the view with that same data structure.

Sure.

You are using json_encode right? So start with the same data structure. For simplicity here I will use an array.

$stories = array( '[id]' => [details] );

Now you have a quick break. If the request is an AJAX request, then simply echo json_encode($stories);. Otherwise standard requests will process the view with that same data structure.

I'm going to expand on this a bit. Assuming you aren't using some MVC paradigm and doing "straight" PHP work.

For generating the page you are going to want to encapsulate the logic for this table in a separate PHP file called with an include(). Let's call this file storyList.php and it's in the same web-accessible folder as everything else.

// File: storyList.php
// First connect to the database and pull the story list
// based on any parameters passed.
$story = new StoryList($_REQUEST);
$list = $story->fetch_list();

// Now do a quick conditional based on the headers.
// See http://php.net/manual/en/function.get-headers.php and
// define your own function for testing if a request is made
// with AJAX or not. Several libraries include their own.
if (is_xhr())
{
    echo json_encode($list);
}
else
{
    foreach ($list as $title => $story)
    {
        // Format as a table
    }
}
Source Link
Josh K
  • 23k
  • 10
  • 68
  • 100

Sure.

You are using json_encode right? So start with the same data structure. For simplicity here I will use an array.

$stories = array( '[id]' => [details] );

Now you have a quick break. If the request is an AJAX request, then simply echo json_encode($stories);. Otherwise standard requests will process the view with that same data structure.