I have the following site:

  • content type house
  • content type booking
  • content type weeks (available weeks for renting the house)

I've made a block to show all the available weeks by means of a special SQL query.
There is some input needed in the query.

So the user must enter a form submitting the number of weeks to stay in the house.
Then that number must be part of the query.

My question is how to show the form (only 1 field with a submit form) and after that show the output of the sql content.

So there is interaction between the events. The code so far is as follows

<?php
<?php
/**
* Implementation of hook_views_api().
*/
function weekcalendar_views_api() {
return array(
'api' => 3.0,
);
}

function
weekcalendar_block_info() {
// This hook returns an array, each component of which is an array of block
// information. The array keys are the 'delta' values used in other block
// hooks.

// The required block information is a block description, which is shown
// to the site administrator in the list of possible blocks. You can also
// provide initial settings for block weight, status, etc.

// Many options are defined in hook_block_info():

// This sample shows how to provide default settings. In this case we'll
// enable the block in the first sidebar and make it visible only on
// 'node/*' pages. See the hook_block_info() documentation for these.
$blocks['weekcalendar_freeweeks'] = array(
'info' => t('Showing weeks available'),
'status' => TRUE,
'cache' => DRUPAL_NO_CACHE,
'region' => 'content', // Not usually provided.
'visibility' => BLOCK_VISIBILITY_LISTED, // Not usually provided.
'pages' => 'node/*', // Not usually provided here.
);

return
$blocks;
}

// The block with the query must only be triggered after the form submit
function weekcalendar_block_view ($delta = '') {
$nweeks = 2; // an example of nweeks = 2 (user must submit it).
switch($delta){
case
'weekcalendar_freeweeks': { // weekfilter is my module name
$ret = "";
$query = 'select alle.title,
alle.saturday as startdatum,
DATE_ADD(alle.saturday,INTERVAL (:nweeks * 7) DAY), // ?? is this the proper way to insert $nweeks?
max(res.reserved) as reserved,
sum(res.price) as price
from ((vweek as alle
left outer join vweek as occ
on (occ.saturday >= alle.saturday
and occ.saturday < DATE_ADD(alle.saturday,INTERVAL (:nweeks * 7) DAY)
and occ.free = 0)
) left outer join vweek as res
on (res.saturday >= alle.saturday
and res.saturday < DATE_ADD(alle.saturday,INTERVAL (:nweeks * 7) DAY)));
where occ.datum is null
group by alle.title, alle.datum'
;
$result = db_query($query, array(':nweeks' => 5));
foreach (
$result as $record) {
$ret .= "

The title is "
. $record->title . "

"
;
print_r($record->title);
}
}
}
return
$ret;
}

function
weekcalendar_node_view ($node) {
//print l(t('Booking'), '/node/add/booking', array('query' => array('field_rental_house' => $node->nid)));
}
?>

Any help appreciated.

John

Comments

.

Can't you use an exposed filter? They're made for this sort of things

Exposed filters would indeed

Exposed filters would indeed be worth looking into.

Otherwise, https://www.google.com/search?q=drupal+multistep+form may tell you something useful.

Oh, and PLEASE surround your code with php tags. It's much more readable.

--
www.ztwistbooks.com. Math books that are actually fun.

Exposed filter

Thanks. The exposed filter is made for this that I know. But the query is to complex to use in a normal filter. So I must use my own query. Or are you trying to say that you can use a dummy view for that and not using the output but direct it to your own code?
The multi step link I have to dig in to this it looks helpful.

John

Thanks,
John

A couple of suggestions

Hello,
I would recommend referring to the API documentation page here:http://api.drupal.org/api/drupal/modules!block!block.api.php/function/hook_block_view/7 :) The documentation page includes a working example.

Your hook_block_view should return a render array (data plus a #theme element). The #theme element directs Drupal to perform a "theme" function (a method that adds html to your data).

Next, I would avoid embedding business logic in a "report writer". Technically of course, you could embed your sql query in a report writer (E.G. views module). However, later on, you may wish to use that business logic outside of the report writer (E.G. you set up a rest service for mobile clients).

One option you have is, create a "sql view table" (in your database). An "sql view table" is not related to the Drupal module "views". An "sql view table" is a often a saved complex query. The "sql view table" can be used just like a "read only" table (you can query it further). Since Drupal uses PDO to access the database (at least the default does), the new "sql view table" is exposed the Drupal database layer automatically. It looks like you have some database engine dependencies built in to your query. I would double check to see if you really need or want to implement the query that way. Drupal uses a long integer to store date times (it does not use a Database engine date time object).

I would start by just getting the "report" portion of you task done first. Then working on using a form to change query parameters. Getting familiar with how the Drupal "theme" functions and render arrays work, will help you on the next step, adding form processing.

Finally, I recommend using xdebug to diagnose your code. I noticed you have extraneous logging calls. Logging should only be used to indicate where you should debug (logging is not a substitute for a debugger). You should also be developing on a local installation. A local installation gives you access to all the system logs, database logs etc.

Hope that helps:)

Thinking is the best way to travel.

Some reflections

Thanks. I am now on 8 weeks drupal now and new to PHP. So a double learning curve.
I already have a MySQL view to make things easier. I need it because some non Drupal SQL specialist made my query. I did change my table to hold date fields.
I was also thinking of using a view and not use the output but I could use the expose filter for the number of weeks but there are other ways possible also.
Disadvantage of local development my Mac is not portable and issues are harder to share with others
xdebug is new to me but it should be a good suggestion to use this.
Drupal is very strong with its modules but to use them the right way is a real challenge.

John

Thanks,
John

A question and suggestion

Hello,
Thank you for your reply.

I'm not sure if you still have a question. You have several steps to perform:

  • Generate form.
  • Determine form method is being called to 1) display form to user, or 2) process the user's submit
  • If form method is called to display form to user:
    • Select Records from database table.
    • Populate form elements with data selected from database table.
    • If form method is being called to process submit:
      • Let code fall through the form submit handler.

    Thus, I'm not sure if you need help with the form processing or the database access?

    Here are a couple resources that should help you:

    Good Luck :)

    Thinking is the best way to travel.